How can I optimize this query for more meta rows?
SELECT wp_users.ID,
(
SELECT GROUP_CONCAT(IF(wp_usermeta.meta_value like '%main%',
'paid', '') SEPARATOR '')
FROM wp_usermeta
WHERE user_id = wp_users.ID
) AS metas
FROM wp_users
limit 1;
I have a users table wp_users
and a user metadata table wp_usermeta
. The wp_usermeta
has the schema.
+----------+---------+-----------------------+------------------------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+-----------------------+------------------------------+
| 23367468 | 4 | ac_id | 659497 |
| 23367473 | 4 | ac_tag_id | 676135 |
| 23367461 | 4 | admin_color | fresh |
| 23367469 | 4 | cf_key | xxxxxxxxxxxxxxxx |
| 23367460 | 4 | comment_shortcuts | false |
| 23367470 | 4 | credits | 1500 |
| 23367457 | 4 | description | |
| 23367479 | 4 | dismissed_wp_pointers | |
| 23367480 | 4 | expire_on | 2023-04-13 |
| 23367455 | 4 | first_name | |
| 23367456 | 4 | last_name | |
| 23367464 | 4 | locale | |
| 23367454 | 4 | nickname | 12918905 |
| 23367471 | 4 | plagiarism | 1500 |
| 23367481 | 4 | products | ["demo_product"] |
| 23367458 | 4 | rich_editing | true |
| 23367463 | 4 | show_admin_bar_front | true |
| 23367459 | 4 | syntax_highlighting | true |
| 23367482 | 4 | total_credits | 1500 |
| 23367462 | 4 | use_ssl | 0 |
| 23367466 | 4 | wp_capabilities | a:1:{s:10:"subscriber";b:1;} |
| 23367465 | 4 | wp_iufsr:subscriber | NULL |
| 23367467 | 4 | wp_user_level | 0 |
+----------+---------+-----------------------+------------------------------+
My query will become a real burden on the database if I add a new subquery for each meta_key. Is there a way to dynamically create the columns based on the meta key of the metadata table?
reading the docs, google