Short Version:
I need to customize the result of a query, using one value as the column name, and another value as the column’s value. It also needs to be automatic (not hard coded, hundreds of possible “meta keys”).
Long Version:
I’m using WordPress’s “user meta” structure, where a single user has a user ID, and another table contains “meta-data” which is related to the user.
This meta data is arbitrary, and only has four columns:
meta_id
, post_id
, meta_key
, meta_value
.
I would like to do an SQL query for a certain user, and format the results so that all of the metadata is formatted as if they were rows, where the meta_key
is a column name, and meta_value
is the value for the column.
Example database:
-- wp_users
* ID | username | email
35 | radgh | radgh@example.org
-- wp_usermeta
* meta_id | user_id | meta_key | meta_value
1 | 35 | first-name | Radley
2 | 35 | last-name | Sustaire
3 | 35 | newsletter | on
My Query (I need the (???) portion figured out, which would generate the meta key/value pairs as fake columns)
SELECT
`users`.ID as 'user_id',
`users`.username as 'username',
`users`.email as 'email',
(???)
FROM `wp_users` `users`
INNER JOIN `wp_usermeta` `meta`
ON `users`.ID = `meta`.user_id
WHERE `wp_users`.ID = 35
Desired Outcome:
* user_id | username | email | first-name | last-name | newsletter
35 | radgh | radgh@example.org | Radley | Sustaire | on
In this similar question the selected answer uses hard coded fields. I would like to do this automatically. 🙂
MySQL has no pivot function. If you know already all of the possible values of meta_key, you could use a hard-coded query like this:
Or you could create your SQL query dynamically, using a prepared statement, like this:
Please see fiddle here.
It is called Entity–attribute–value model. Solution for dynamic columns (meta data) in MySQL via simple/complex query is AFAIK impossible to achive (like other great function like
PIVOT
MySQL is missing). The only way I think is static definition or MySQL function, which fetch from meta table possible attributes and build proper query.Please look at What is best performance for Retrieving MySQL EAV results as Relational Table
Also read about EAV design (pros/cons) on stack Entity-Attribute-Value Table Design