1 Apr 2009 01:15
Re: formulate nested select
PJ <af.gourmet <at> videotron.ca>
2009-03-31 23:15:43 GMT
2009-03-31 23:15:43 GMT
haliphax wrote: > On Tue, Mar 31, 2009 at 5:15 PM, PJ <af.gourmet <at> videotron.ca> wrote: >> kyle.smith wrote: >>> What about using "IN", something like: >>> >>> SELECT * FROM book WHERE id IN (SELECT bookID FROM book_authors WHERE >>> authID IN (SELECT author.id FROM authors WHERE last_name LIKE >>> "$Auth%")); >>> >>> You could use LEFT instead of LIKE, too. >> Well, I learned something here... but that also revealed another, >> hidden, problem which I had not considered - the order by clause which I >> had stupidly not included in my example: >> >> $SQL = "SELECT * FROM book >> Â Â Â Â WHERE id IN (SELECT bookID >> Â Â Â Â FROM book_author WHERE authID IN (SELECT author.id >> Â Â Â Â FROM author WHERE LEFT(last_name, 1 ) = '$Auth')) >> Â Â Â Â ORDER BY $sort $dir >> Â Â Â Â LIMIT $offset, $records_per_page "; >> >> I now realize that to keep things as simple as possible in the rest of >> the code, I must join 1 column to the query result and that is last_name >> from the author table. >> the spelling, column and table names are spelled correctly. Without the >> ORDER BY I get the right results. I'll try to JOIN the author table ??? >> But I see that I may be trying to do too much - I thought of showing how >> many books were listed under each letter of the alphabet but I don't see >> how it can be done in any simiple way as it would mean that I would have >> to do the select once with the ORDER BY and a second time without it(Continue reading)
RSS Feed