Friday, 27 July 2012

Combning firstname and lastname as name in Mysql Query

I had the following mysql statement that I thought I could simpify.

$query = "SELECT #__users.id,#__users.name FROM #__users,#__comprofiler where #__users.id=#__comprofiler.user_id order by name ";
 

this has become

$query = "SELECT #__comprofiler.user_id as id, CONCAT(#__comprofiler.firstname,' ',#__comprofiler.lastname)AS name FROM #__comprofiler ORDER BY name ";


which uses CONCAT and AS to use two colums and return as one; with a space in the middle also

No comments: