-2

I have a table with related data across multiple rows that I need to query as one row IF the meta_key value is in ('A', 'B', 'C').

meta_id | post_id | meta_key | meta_value
--------+---------+----------+-----------
   1    |  1234   |    A     |    Foo
   2    |  1234   |    B     |    Bar
   3    |  1234   |    C     |    Baz
   4    |  1234   |    D     |    junk

What I am looking to end up with is a query that gives me results

Output

 A  |  B  |  C
----+-----+----
Foo | Bar | Baz
veilig
  • 5,085
  • 10
  • 48
  • 86

2 Answers2

1

You can use conditional aggregation to get such a result. This assumes there is one meta_value per meta_key for a given post_id.

select 
max(case when meta_key='A' then meta_value end) as A,
max(case when meta_key='B' then meta_value end) as B,
max(case when meta_key='C' then meta_value end) as C 
from yourtable
group by post_id
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
-2

You can simply run this SQL conditional query command:

SELECT * FROM table_name WHERE meta_key='A' AND meta_key='B' AND meta_key='C'

To limit the query results, do:

SELECT * FROM table_name WHERE meta_key='A' AND meta_key='B' AND meta_key='C' LIMIT 10

Note: the LIMIT keyword accept any valid range of integers.

John Zenith
  • 472
  • 5
  • 10