Re: PHP array or SQL problem
| From: | Daniel Grace | Date: | Tue, 15 Jan 2002 09:58:27 +0000 |
| Subject: | Re: PHP array or SQL problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-80537@lists.php.net to get a copy of this message | ||
"Dean Householder" <dean@adoption.com> wrote in message
news:007301c19d97$21968660$d5246541@daylightcreations...>
>I've got a database with lname (lastname), fname (firstname), and nickname.
>
>What I want to do is search by either first name or last name. Either way,
>I display the nickname instead of the firstname if it exists. I have no
>problems on the last name, but when I sort by firstname, the people with
>nicknames that are very different come up at the place where their first
>name (not the displayed name) is.
>
>For example:
>
>Adam Smith
>Joe Schmoe
>Bill Somebody
>
>When Bill Somebody is really William Somebody in the database, his nickname
>is bill and that's what's displayed but he 'bill' shows up in
>'williams'
>spot. Here is my SQL query:
>
>select * from employees order by fname;
>
>Should I create an array and sort it there or can I somehow query MySQL to
>sort by nickname only if it exists and then by fname?
>
>Dean
>
>
SELECT IF(nickname IS NOT NULL AND nickname != "", nickname, fname) AS
fname, lname FROM table ORDER BY fname
if it doesn't sort right, copy the IF(...) to the oRDER BY part
-- Daniel Grace