I've got a table like this:
I want to select like the result is something like this:
611766 | 20912 | gruppo-1_codice_articolo | 112509892 | field_5db85a4e1decf
Where the last 2 column are the meta_value column from gruppo-1_codice_articolo and meta_value from _gruppo-1_codice_articolo
I can't figure out how. Is it possible?
EDIT:
I ended up with this, and it seems it's working:
select distinct m1.post_id, m1.meta_key, m1.meta_value, m2.meta_key, m2.meta_value
from fiamgroup_postmeta m1
left join fiamgroup_postmeta m2 on m2.meta_key = CONCAT('_', m1.meta_key)
where m1.post_id = 20912 and m1.meta_key like 'gruppo_%' and m1.meta_value > ''
But it's quite slow. How could I speed up it? Is it slow because I'm using CONCAT?
EDIT 2:
I've found that if I join also on m1.post_id = m2.post_id it's blazing fast, probably because post_id is indexed. Also, I don't need distinct anymore. Also, I added another join (because I needed it) So the resulting query is:
select m1.post_id, m1.meta_key, m1.meta_value, m2.meta_key, m2.meta_value, p.post_title
from fiamgroup_postmeta m1
left join fiamgroup_postmeta m2 on m2.meta_key = CONCAT('_', m1.meta_key) and m1.post_id = m2.post_id
left join fiamgroup_posts p on p.post_name = m2.meta_value
where m1.post_id = 20912 and m1.meta_key like 'gruppo_%' and m1.meta_value > ''
Still, as you can guess, I'm not an sql expert, therefore if you've got other suggestions to better write this query, I'll be happy to read and learn.
