Mysql – Select Multiple Values from Same Column; one sql statment

MySQLselect

I am looking to return a list of users from a database. There are hundreds of users in the db. Each user has it's own unique user_id. All of the values that I am trying to return reside in the same column (meta_value).

The database structure is as follows:

id | user_id | meta_key | meta_value

sample data is as follows:

1 | 3434 | first_name | Brandon
2 | 3434 | last_name  | Johnson
3 | 3434 | street_add | 123 main
4 | 3434 | city       | ocean beach
5 | 3434 | state      | Texas

I am trying to return the first name, last name, street_add, city, and state in one sql statement.

I'd like the output to look like:

Brandon, Johnson, 123 Main, Ocean Beach, Texas 

Best Answer

Just use GROUP_CONCAT with a WHERE condition

SELECT user_id,GROUP_CONCAT(meta_value ORDER BY id) 
FROM t 
WHERE meta_key  IN('first_name','last_name','street_add','city','state') 
GROUP BY user_id