Current location: Hot Scripts Forums » Programming Languages » PHP » joins????


joins????

Reply
  #1 (permalink)  
Old 04-21-05, 06:20 AM
godonholiday godonholiday is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
joins????

hi i have a little join problem i think:

i have a table: cars [carno,teamname,factory,engine]

another table called: Drivers[driverno,fullname,carno,age,nationality]


I want to list all team names whose drivers have a specific nationality.


i currently use a search method were i use a form(uses GET) and a seachstring. I use a URL varialbe called SearchString and then Do a record set for the results.

can anyone help me with this?
thanks in advance
g
Reply With Quote
  #2 (permalink)  
Old 04-21-05, 06:45 AM
godonholiday godonholiday is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
the code im trying

this is the code im trying but it dont seem to work??

PHP Code:

SELECT *

FROM cars INNER JOIN drivers ON car_no=carno
WHERE nationality like 
'DriverSearchString' 
any help would be great thanks
g
Reply With Quote
  #3 (permalink)  
Old 04-21-05, 06:46 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
you dont have the same field in both tables such as a primary key so you cant do it!
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #4 (permalink)  
Old 04-21-05, 06:48 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Code:
SELECT
  cars.teamname
FROM
   cars LEFT OUTER JOIN Drivers 
      ON cars.carno=Drivers.carno
WHERE
   Drivers.nationality='$nationality'
try this one..

or you could use a normal join..
Code:
SELECT
  cars.teamname
FROM 
  cars, Drivers
WHERE 
  cars.carno=Drivers.carno
  AND Drivers.nationality='$nationality'
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-21-05 at 06:54 AM.
Reply With Quote
  #5 (permalink)  
Old 04-21-05, 07:08 AM
godonholiday godonholiday is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for that

cheers mate that worked a treat.

two more thing, if you can help

1) i want to allow the user to search for a car were the factory date is within a user specified range?

will i have to have two text boxes, and ask them to enter two date ie, 03/03/05 and 04/04/06?

how do i do a search with these two strings?

or am i going about it in the wrong way?

2)the average thing i was trying to do, i need to join three tables i think, i need the average age of the drivers and mechanics for a specified team. so thats the driver, mechanic and car tables. any ideas?

thanks again.

g
Reply With Quote
  #6 (permalink)  
Old 04-21-05, 07:17 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
1) i want to allow the user to search for a car were the factory date is within a user specified range?

will i have to have two text boxes, and ask them to enter two date ie, 03/03/05 and 04/04/06?

how do i do a search with these two strings?
what is the type of the date field you have in your database?

Quote:
2)the average thing i was trying to do, i need to join three tables i think, i need the average age of the drivers and mechanics for a specified team. so thats the driver, mechanic and car tables. any ideas?
you mean you want to get the average of car drivers age by nationality?
this could be done with the same query you used from above, just add the following after SELECT:
Code:
AVG(Drivers.age) AS averageAge
and at the end put:
Code:
GROUP BY Drivers.nationality
so it would be:
Code:
SELECT
  cars.teamname,
  AVG(Drivers.age) AS averageAge
FROM 
  cars, Drivers
WHERE 
  cars.carno=Drivers.carno
  AND Drivers.nationality='$nationality'
GROUP BY
  Drivers.nationality
I didn't quit understand the mechanics part! what does the third table has in it?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #7 (permalink)  
Old 04-21-05, 07:25 AM
godonholiday godonholiday is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
hi ill try and be a little clearer

what is the type of the date field you have in your database?
= its in an sql database as type 'date'

you mean you want to get the average of car drivers age by nationality?
= no, sorry i got that wrong, i want to list the average age of all personal who work for a user specified teamname.

table are: cars[carno,teamname,factorydate,make,sponsor]
drivers[fullname,carno,age,nationality,points]
mechanics[fullname,carno,age,nationality,skill,position]

hope that clears up what im trying to do

thanks for the help
g
Reply With Quote
  #8 (permalink)  
Old 04-21-05, 07:41 AM
godonholiday godonholiday is offline
Newbie Coder
 
Join Date: Feb 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
iv come up with something like this

this is what i have at the min:

[
PHP Code:

SELECT cars.teamnameAVG(drivers.age) AS averageAge

FROM cars
driversmechanics
WHERE cars
.car_no=drivers.carno AND cars.car_no=mechanics.carno AND  teamname like 'DriverSearchString'
GROUP BY cars.teamname

i dont think this is right 
and idont know were do i put the searchstring
thanks agian

Last edited by godonholiday; 04-21-05 at 08:07 AM.
Reply With Quote
  #9 (permalink)  
Old 04-22-05, 05:49 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Code:
SELECT
  cars.teamname,
  AVG(Drivers.age) AS DriverAverageAge,
  AVG(mechanics.age) AS mechanicsAverageAge
FROM 
  cars, Drivers
WHERE 
  cars.carno=Drivers.carno
  AND cars.teamname='$teamname'
GROUP BY
  cars.teamname
don't use LIKE when you know the exact value of the teamname (or whatever field)

as for date comparision..
you could use:
Code:
SELECT * FROM cars WHERE factorydate > '2005-04-22' AND factorydate < '2005-04-25'
this will fetch everything from "cars" table between the dates 2005-04-22 and 2005-04-25
I assumed the field date's type as DATE (format YYYY-MM-DD)
the user input should be in format YYYY-MM-DD
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
tables joins - 3 tables lordmerlin PHP 2 03-19-04 02:00 PM
tables joins - 3 tables lordmerlin ASP 1 03-18-04 08:20 AM
Still confused about joins Primitive_screw PHP 1 02-19-04 01:25 AM
im confused about joins Primitive_screw PHP 1 01-31-04 10:32 PM
Problem With Multiple Joins cebuy PHP 5 12-11-03 12:14 PM


All times are GMT -5. The time now is 06:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.