ORDER BY in subquery not working

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

ORDER BY in subquery not working

Post by rhecker »

Table A contains columns photo and id
Table B contains columns id, other_id and rank (primary index on id and other_id)

The following query correctly returns the result I want, except that it ignores my request to order by rank, and I need it to do that.

SELECT photo FROM A WHERE id= ANY(SELECT id FROM B WHERE other_id='$other_id')

Any suggestions?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: ORDER BY in subquery not working

Post by Darhazer »

join the tables :)

Code: Select all

SELECT photo FROM A INNER JOIN B USING (id) WHERE other_id='$other_id' ORDER BY rank
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: ORDER BY in subquery not working

Post by rhecker »

Yes, I had suspected an inner join was the key, but those I wrote didn't work. Yours worked perfectly! Thanks!
Post Reply