Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Code:
SELECT *
FROM vehicles v, images i
WHERE v.vehicleId = i.vehicleId
Both the table need to share a common ID (I named it vehicleId there), to relate the pictures and the vehicles.
With that query, you will get 1 line per picture and vehicle, but if a vehicle have no pictures, it won't be shown.
To have all the vehicle shown, even without picture, you can use an aouter join:
Code:
SELECT *
FROM vehicle v
LEFT OUTER JOIN pictures p ON v.vehicleId = p.vehicleId
Beware than an outer join can by way more CPU intesive than a usual "inner join".
What you can do is use 2 queries.
The first to get all your vehicles, and the second to get all your images
PHP Code:
$q1="SELECT * FROM vehicles"; $r1=mysql_query($q1); while($o1=mysql_fetch_object($rq)){ //You have now the vehicle record in the $o1 object
$q2="SELECT * FROM PICTURES WHERE vehicleId = {$o1->vehicleId}"; $r2 = mysql_query($q2); while($o2=mysql_fetch_object($r2)){ //you have one image related to the $o1->vehicleId vehicle in the $o2 object } }
__________________
Only a biker knows why a dog sticks his head out the window.
|