I have 2 tables:
table1: |id|create_date|title |type | ------------------------------- | 1|2015-07-20 |bla-bla|TICKET| table2_meta: |id|table1_id|meta_name|meta_value| ----------------------------------- | 1| 1 | status | new | | 2| 1 | priority| low | | 3| 1 | owner | alex |
Now, i wish to select it like this:
|id|create_date|title |status|priority|owner| |1 |2015-07-20 |bla-bla|new |low |alex |
My solution is:
SELECT
t1.id,
t1.event_date,
case when m.meta_name="status" then m.meta_value end as status,
case when m.meta_name="priority" then m.meta_value end as priority,
case when m.meta_name="owner" then m.meta_value end as owner
FROM table1 as t1
LEFT JOIN table2_meta as m
on m.table1_id=t1.id
WHERE t1.type='TICKET'
GROUP BY t1.id
So, it works. But seems a litle bit ugly.
My question is:
Are there any other solutions to this select and how to make it more productive?