How to combine multiple columns as one single string in MySQL?

I have a user_details table with user details like house no, street, city, and country. So, how can I combine them and make a complete address in MySQL? All the columns must be comma separated. Is this possible in MySQL, or do I have to query the details and then build the address with PHP?

SELECT id, address FROM user_details;

We have two available functions in MySql.

  1. CONCAT()
  2. CONCAT_WS()

1. CONCAT()

This function joins everything you pass as a parameter. So you have to set “,” after each column.

SELECT id, CONCAT(house_no, ', ', street, ', ', city, ', ', country ) AS address FROM user_details;

2. CONCAT_WS()

With this function, you can set the separator as the first parameter. So you don’t have to use “,” every time.

SELECT id, CONCAT_WS(',', house_no, street, city, country ) AS address FROM user_details;