Go Back   SitePoint Forums > Forum Index > Program Your Site > Databases > MySQL
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 30, 2006, 12:49   #1
PotatoFro
SitePoint Enthusiast
 
Join Date: May 2005
Posts: 51
Question Nested Query Problem? :(

basic question: i'm trying to grab info from one table and sort by a value in the nested query -- below is the background and code:

i'm developing a photo upload site. I give the user the ability to upload a bunch of photos and post upload decide which albums each photo is connected to (ie. an individual photo may be under the album/label "camping trip" and "summer shots"). To accomodate i've created one table that holds all photo info (title,description, tags,id,filepath etc etc) that may change and then a separate table that links each photo to an album/label (images id, albums id).

i also allow the user to sort their images in a user defined order. that information is saved in the later table as it will be different for each album/label that the image is in.

here is the query i'm using to access all the images in the album with an id of 1:

PHP Code:

$sql="SELECT SQL_CALC_FOUND_ROWS photos.title,photos.filename,photos.image_id,albums.order FROM photos,albums WHERE photos.image_id IN(SELECT albums.image_id FROM albums WHERE albums.album_id=1) ORDER BY albums.order LIMIT 0,20"; 

i was hoping to get a result row with the photo information (first table) as well as the order field (second table), sorted by the order field. instead (even though there are only 9 images) i get the full 20 of my limit (half of them duplicated) and i get an inacurate order result.

Am i doing this nested query incorrectly? am i even able to get the result i want?
PotatoFro is offline   Reply With Quote
Old Nov 30, 2006, 13:17   #2
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
Code:
select sql_calc_found_rows
       photos.title
     , photos.filename
     , photos.image_id
     , albums.order
  from albums
inner
  join photos
    on photos.image_id = albums.image_id
 where albums.album_id = 1
order
    by albums.order limit 0,20
r937 is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

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 On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 19:37.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved