Thursday, February 23, 2012

MySQL - Order users by address

Today I encountered a little SQL problem I had to here is the task:

Order the users by their address. Basically this isn't a big thing, but I had all the address parts separatedly in the database. There is a coloumn for the city,

Here is the solution:

SELECT *, CONCAT_WS(' ', `users`.`zipcode`, `users`.`city`, `users`.`street`, `users`.`housenumber)` AS address FROM (`users`) ORDER BY `address` asc, `name` ASC

This is not such a great achievement, but it's good for reminding myself about this cool trick.