I have two tables that relates 1:n
content
---------
- id
- title
- text
content_meta
-------------
- id
- content_id
- meta_key
- meta_value
A content can have multiple content_meta registers associated to it. Typically content_meta will contain the category, tags, descriptions and all that stuff, so I really don’t know the number of registers a content will have.
What I want to accomplish is to take the content register and also all the related registers in content_meta in a single query.
I’ve tried the subselect approachment but seems that I can only get one register/column (¿?)
SELECT content.*, (
SELECT *
FROM content_meta
WHERE content_id = content.id
)
FROM content
This query complains that “Operand should contain 1 column(s)”, so changing the ‘*’ by for example meta_key clears the error, but returns a NULL for this subselect…
SELECT content.*, (
SELECT meta_key
FROM content_meta
WHERE content_id = content.id
)
FROM content
Can anybody show me where to go from here please?
Use:
That will only return CONTENT and related when there is a supporting record in the CONTENT_META table. If it’s possible for a CONTENT record to not have any CONTENT_META data, use a LEFT JOIN instead:
Followup Question –
MySQL doesn’t have PIVOT syntax – you have to use CASE statements:
You’ll have to specify the
meta_keyfor each one you want to appear in the resultset.