Sunday, 15 April 2012

sql - How can I 'Horizontalize' a second query into my first query? -



sql - How can I 'Horizontalize' a second query into my first query? -

i have problem can simulated scheme much so's: posts , tags. posts, might this:

select title, body, author posts id = ?

and tags simply

select tag_id tags_on_posts post_id = ?

let's have limit can have 5 tags per post. there anyway can in single query?

select title, body, author, tag1, tag2, tag3, tag4, tag5 posts bring together /*help*/ id = ?

you can aggregate tags string , split them @ application side

select title, body, author, string_agg(tag_id, ',') tag_ids posts p inner bring together tags_on_posts top on p.id = top.post_id p.id = ? grouping 1, 2, 3

if tag names in 3rd table

select title, body, author, string_agg(tag_name, ',') tag_names posts p inner bring together tags_on_posts top on p.id = top.post_id inner bring together tags t on t.id = top.tag_id p.id = ? grouping 1, 2, 3

sql postgresql

No comments:

Post a Comment