Reply
Select data from 2 tables
Old 04-24-2007, 09:28 AM Select data from 2 tables
SpottyDog's Avatar
Skilled Talker

Posts: 82
basically i have 2 tables. 1 named vehicles and another called images.
I have several vehicels listed on the 1st and in the images tablke i have images which relate to each vehicle by the imageid field.

Also in the images table i have a prival column which can be 1 or 0. if 1 then it is the primary photo fo rthis vehicle.


I have a script which repeats all the vehicles into a list but i need to know how to call the priamary image (selected in the privalfield) realting to the vehicle

PHP Code:
$query  "SELECT vehicles.*,images.* ORDER BY `ID` ASC";
//$query  = "SELECT * FROM vehicles ORDER BY `ID` ASC";
$result mysql_query($query) or die(mysql_error());
while (
$row mysql_fetch_object($result)) {

echo
" <table width=\"200\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
  
"  <tr>"
  
"    <td><div align=\"center\" class=\"style2\"><img src=\"../phpThumb.php?src=vehicles/$row->images.imageid/$row->images.imagename&w=200\">
</div></td>"
  
"  </tr>"
  
"  <tr>"
  
"    <td><div align=\"center\" class=\"style2\"><b>$row->make</b> $row->model</div></td>"
  
"  </tr>"
  
"  <tr>"
  
"    <td><div align=\"center\" class=\"style3\">£$row->price</div></td>"
  
"  </tr>"
  
" </table><br><br>"
 
."";


at the moment i getr the error
Code:
Unknown table 'vehicles'
i hope you understand my question and can help

Last edited by SpottyDog : 04-24-2007 at 10:13 AM.
SpottyDog is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-24-2007, 10:22 AM Re: Select data from 2 tables
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
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
  
}

__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 04-24-2007, 10:41 AM Re: Select data from 2 tables
SpottyDog's Avatar
Skilled Talker

Posts: 82
thanks for the help mate this is helping alot.

Also i dont think using 2 querys would be any good for me i think as i need it all showing in one while loop so they can list

Last edited by SpottyDog : 04-24-2007 at 10:43 AM.
SpottyDog is offline
Reply With Quote
View Public Profile
 
Old 04-24-2007, 11:26 AM Re: Select data from 2 tables
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Ok, just I really tell you, outer join can be REALLY heavy on the server.
I already had brought a double p4 3Ghz on his knees because I was using a query as simple as the one I showed you before, that was ran on every page accesses.

If you can avoid an outer oin, then do it, as much as you can if it's gonna be used on a page without a limited number of views.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Select data from 2 tables
 

Thread Tools

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

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




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14425 seconds with 12 queries