Combine/format two fields in query?

I have two fields - author_name_first and author_name_last in my table.

What I’d like to do is combine the two in this format: author_name_last, author_name_first (to yield something like Hunt, Mike or Baker, Sheldon) - I would do this for sorting purposes (ORDER BY…etc).

Anyone point me in the right direction on this?

Thanks.


SELECT author_name_first, author_name_last FROM authors ORDER BY author_name_first DESC, author_name_last DESC

Ah - that works just fine.

Is there a way to combine those fields into a formatted string like I’ve described above anyways?

um, not sure. try UNION.

Try this:

Select CONCAT(author_name_last,’, ',author_name_first) AS full_name FROM my_table ORDER BY author_name_first ASC, author_name_last ASC

And after you do your query, the full name would be available in $row[‘full_name’].

Perfect - thanks!

Can you not just use a string expression in MySQL? I know you can do it in MSSQL - you can pretty much do any expression you want in a T-SQL statement as long as the syntax is valid

For instance - this works in SQL2000/2005


SELECT author_name_first + ', ' + author_name_last as full_name FROM authors ORDER BY author_name_first + ', ' + author_name_last