Mysql Select 1:n

Posted by clinisbut on Stack Overflow See other posts from Stack Overflow or by clinisbut
Published on 2010-05-23T21:28:35Z Indexed on 2010/05/23 21:30 UTC
Read the original article Hit count: 285

Filed under:

Hello, 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?

© Stack Overflow or respective owner

Related posts about mysql