For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > July 2007 > Subject: sql statement - complex order by









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Subject: sql statement - complex order by
Neil Smith [MVP, Digital media]

2007-07-04, 6:58 pm

At 20:36 02/07/2007, you wrote:
>Message-ID: <F4.81.24153.5C359864@pb1.pair.com>
>To: php-db@lists.php.net
>Date: Mon, 02 Jul 2007 14:34:35 -0500
>From: Bryan <bcotter@wordsearchbible.com>
>
>SELECT * FROM productgroup WHERE groupid = $productid
>AND label = 'Cats' ORDER BY title
>
>SELECT * FROM productgroup WHERE groupid = $productid
>AND label != 'Cats' ORDER BY label,title
>
>I'd like to find a way to combine these 2 statements. I want to list
>out all the products, ordered by title but listing out all the Cats
>products first. Any way to do that without having separate statements?


See http://dev.mysql.com/doc/refman/5.0/en/union.html

(SELECT * FROM productgroup WHERE groupid = $productid
AND label = 'Cats' ORDER BY title)
UNION
(SELECT * FROM productgroup WHERE groupid = $productid
AND label != 'Cats' ORDER BY label,title)


HTH
Cheers - Neil
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com