Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 82
Trades: 0
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 11:13 AM..
SpottyDog is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-24-2007, 11:22 AM Re: Select data from 2 tables
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-24-2007, 11:41 AM Re: Select data from 2 tables
SpottyDog's Avatar
Skilled Talker

Posts: 82
Trades: 0
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 11:43 AM..
SpottyDog is offline
Reply With Quote
View Public Profile
 
Old 04-24-2007, 12:26 PM Re: Select data from 2 tables
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Select data from 2 tables
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 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.19526 seconds with 12 queries