Subject: sql statement - complex order by

Subject: sql statement - complex order by

am 05.07.2007 00:28:20 von Neil Smth

At 20:36 02/07/2007, you wrote:
>Message-ID:
>To: php-db@lists.php.net
>Date: Mon, 02 Jul 2007 14:34:35 -0500
>From: Bryan
>
>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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php