Forum Moderators: coopster
Let's say I have a table "user":
userId: int
userNick: varchar 255
userNickSpecial: varchar 255
Sometimes, I'd like to be able to sort the users using BOTH the "userNick" and "userNickSpecial"!
This:
SELECT * FROM user ORDER BY userNick, userNickSpecial;
Ex:
-----
userId: 1
userNick: 'bbb'
userNickSpecial: ''
-----
userId: 2
userNick: ''
userNickSpecial: 'ccc'
-----
userId: 3
userNick: ''
userNickSpecial: 'aaa'
-----
userId: 4
userNick: 'ddd'
userNickSpecial: ''
-----
Would sort the users like this:
userId: 3
userId: 1
userId: 2
userId: 4
Is it possible?
In that case, it might be worthwhile to create a new field with the desired data, which you can index on.
ORDER BY CONCAT is really what I was looking for! Thanks again.
It works for the simplified "user" example but it doesn't work in my real code.
Problem is, I think, my LEFT JOIN returns NULL for some fields and I guess CONCAT(NULL, 'something else') doesn't equal CONCAT('', 'something else')!
My real example, using ORDER BY CONCAT, returns that:
id ¦ nick ¦ nickSpecial
834 NULL test123
836 NULL test1234
837 NULL aaaaa
835 julian ''
832 roger ''
833 roger ''
Any idea to make that CONCAT works even with NULL values?
ORDER BY CONCAT( IFNULL( nick, '' ) , nickSpecial )
IFNULL doc: [dev.mysql.com...]