Re: MySQL and GROUP BY

From: Date: Tue, 20 Nov 2001 17:27:48 +0000
Subject: Re: MySQL and GROUP BY
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-75270@lists.php.net to get a copy of this message
"David Freeman" <dfreeman@outbackqld.net.au> wrote: > SELECT * FROM My_Table ORDER BY Business_Name, Last_Name, First_Name; > > What I would _like_ to have happen is that the select list will end up > sorted by business name as well as last name and then first name. At > the moment this query is resulting in all business names (sorted) and > then, following all business names, all last/first names (also sorted). > > I want them all integrated into a single alphabetical list. This is really an SQL question so the MySQL mailing list would have been a better place to ask, but... You need to do an IF() to check whether to use Business_Name or a combo of Last_Name/First_Name, then you need to join Last_Name/First_Name if Business_Name is empty and name the column something and order by that column. SELECT IF( Business_Name<>'', 'Business_Name', CONCAT( Last_Name, ', ', First_Name ) ) AS mylist FROM My_Table ORDER BY mylist -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/

« previous php.general (#75270) next »