SELECT merging several columns from one table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

SELECT merging several columns from one table

Post by andym01480 »

I have a query

Code: Select all

SELECT surname,addressname,children, add1,add2,town,county,postcode,phone,mobile,email,email2 FROM address  ORDER BY surname
what I really like is for the fields "add1, add2, town, county, postcode" to be formatted with comma and space and in the result as one field say "address".

How do I do that?
Last edited by andym01480 on Wed Mar 07, 2007 10:44 am, edited 1 time in total.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Code: Select all

SELECT CONCAT(add1,', ',add2,', ',postcode) AS address.....
something along those lines should do it
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Better yet...

Code: Select all

select CONCAT_WS(", ", add1, add2, town, county, postcode) AS address FROM table
Post Reply