Hi,
I have a table that has username and users name (as well as other columns).
I want to sort the columns alphabetically which is very simple; however a wrinkle is that sometimes the users name can be blank while a username is unique and cannot be blank. I don’t want to order by username as my name could be Steve but my username could be Ictus, so sorting by users names is what is needed and the blank user names should be put at the end of the alphabetisation. To get this I have done the following:
SELECT
username, users_name
FROM users
ORDER
BY case when length(users_name) > 0
then 0
else 1 end
, users_name";
This almost works but then the blank user_names are left unsorted at the end of the result set. like:
| --Name-- | Username |
|Alfred Alphons | aalphons |
|Bill Bailey | wizard |
|----------------| Pictures |
|--------------- | empty_one |
(sorry about the crappy formating… don’t know how to make this look more tablature)
You can see that the empty names with the usernames ‘Pictures’ and ‘empty_one’ are not alphabetized. Do you know how I could order by to get:
| --Name-- | Username |
|Alfred Alphons | aalphons |
|Bill Bailey | wizard |
|--------------- | empty_one |
|----------------| Pictures |
I tried:
SELECT
username,raw_password,users_name
FROM users
ORDER
BY
CASE
when length(users_name) > 0
then 0
else 1 END
,
CASE
when length(users_name) > 0
then 'users_name'
else 'username' END
but this does not work.
Any ideas?
Regards,
Steve