mysql - GROUP_CONCAT: search for subset and retrieve all values -


i have 2 tables:

posts

+--------------------------------------------------------------------+ |id          ¦status¦type ¦url                                       | +------------+------+-----+------------------------------------------+ |25949664    ¦80    ¦link ¦http://example.com/25949664               | |25777570    ¦80    ¦photo¦http://example.com/25777570               | +--------------------------------------------------------------------+ 

attributes

╔════════════╦════════════╦══════════════════════════════════════════╗ ║id          ║attr        ║value                                     ║ ╠════════════╬════════════╬══════════════════════════════════════════╣ ║25949664    ║timestamp   ║1430836105                                ║ ║25949664    ║tag         ║red                                       ║ ║25949664    ║tag         ║yellow                                    ║ ║25949664    ║tag         ║brown                                     ║ ║25949664    ║source      ║http://example.com/wallin/                ║ ║25949664    ║source_title║wallin                                    ║ ║25949664    ║state       ║published                                 ║ ║25949664    ║format      ║html                                      ║ ║25777570    ║timestamp   ║1430836105                                ║ ║25777570    ║tag         ║red                                       ║ ║25777570    ║tag         ║yellow                                    ║ ║25777570    ║tag         ║brown                                     ║ ║25777570    ║tag         ║black                                     ║ ║25777570    ║tag         ║orange                                    ║ ╚════════════╩════════════╩══════════════════════════════════════════╝ 

performing query:

  select posts.id, group_concat(attributes.value) tags     posts     join attributes on attributes.id = posts.id    ( attributes.attr = 'tag' )      , ( attributes.value in ('red','brown') ) group posts.id   having count(distinct attributes.value) = 2 

i have result:

╔════════════╦════════════╗ ║id          ║tags        ║ ╠════════════╬════════════╣ ║25949664    ║red,brown   ║ ║25777570    ║red,brown   ║ ╚════════════╩════════════╝ 

i rather have this:

╔════════════╦════════════════════════════════╗ ║id          ║tags                            ║ ╠════════════╬════════════════════════════════╣ ║25949664    ║red,yellow,brown                ║ ║25777570    ║red,yellow,brown,black,orange   ║ ╚════════════╩════════════════════════════════╝ 

practically, having n tags, retrieve all post's tags performing 1 query.

anyone have suggestion, or totally impossible?

  select posts.id, group_concat(attributes.value) tags     posts     join attributes on attributes.id = posts.id    ( attributes.attr = 'tag' )      , ( attributes.value in ('red','brown') ) group posts.id 

with , condition, only attributes red , brown. need remove , condition , rewrite condition.

i think can you:

 select posts.id, group_concat(attributes.value) tags     posts     join attributes on attributes.id = posts.id    ( attributes.attr = 'tag' )      group posts.id   having find_in_set('red',tags)>0 , find_in_set('brown',tags)>0 

online demo


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -