GROUP_CONCAT:搜索子集并检索所有值
GROUP_CONCAT: search for subset and retrieve all values
我有这两个表:
posts
+--------------------------------------------------------------------+
|id ¦status¦type ¦url |
+------------+------+-----+------------------------------------------+
|25949664 ¦80 ¦link ¦http://example.com/25949664 |
|25777570 ¦80 ¦photo¦http://example.com/25777570 |
+--------------------------------------------------------------------+
属性
╔════════════╦════════════╦══════════════════════════════════════════╗
║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 ║
╚════════════╩════════════╩══════════════════════════════════════════╝
执行此查询:
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
AND ( attributes.value IN ('red','brown') )
GROUP BY posts.id
HAVING COUNT(DISTINCT attributes.value) = 2
我有这个结果:
╔════════════╦════════════╗
║id ║tags ║
╠════════════╬════════════╣
║25949664 ║red,brown ║
║25777570 ║red,brown ║
╚════════════╩════════════╝
我宁愿有这个:
╔════════════╦════════════════════════════════╗
║id ║tags ║
╠════════════╬════════════════════════════════╣
║25949664 ║red,yellow,brown ║
║25777570 ║red,yellow,brown,black,orange ║
╚════════════╩════════════════════════════════╝
实际上,有 n 个标签,我会检索 all post 的标签只执行一次查询。
谁有什么建议,或者这是完全不可能的?
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
AND ( attributes.value IN ('red','brown') )
GROUP BY posts.id
使用 and 条件,您仅获得红色和棕色属性。所以你需要删除这个和条件并重写那个条件。
我认为这可以帮助你:
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
GROUP BY posts.id
HAVING Find_In_Set('red',tags)>0 AND Find_In_Set('brown',tags)>0
我有这两个表:
posts
+--------------------------------------------------------------------+
|id ¦status¦type ¦url |
+------------+------+-----+------------------------------------------+
|25949664 ¦80 ¦link ¦http://example.com/25949664 |
|25777570 ¦80 ¦photo¦http://example.com/25777570 |
+--------------------------------------------------------------------+
属性
╔════════════╦════════════╦══════════════════════════════════════════╗
║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 ║
╚════════════╩════════════╩══════════════════════════════════════════╝
执行此查询:
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
AND ( attributes.value IN ('red','brown') )
GROUP BY posts.id
HAVING COUNT(DISTINCT attributes.value) = 2
我有这个结果:
╔════════════╦════════════╗
║id ║tags ║
╠════════════╬════════════╣
║25949664 ║red,brown ║
║25777570 ║red,brown ║
╚════════════╩════════════╝
我宁愿有这个:
╔════════════╦════════════════════════════════╗
║id ║tags ║
╠════════════╬════════════════════════════════╣
║25949664 ║red,yellow,brown ║
║25777570 ║red,yellow,brown,black,orange ║
╚════════════╩════════════════════════════════╝
实际上,有 n 个标签,我会检索 all post 的标签只执行一次查询。
谁有什么建议,或者这是完全不可能的?
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
AND ( attributes.value IN ('red','brown') )
GROUP BY posts.id
使用 and 条件,您仅获得红色和棕色属性。所以你需要删除这个和条件并重写那个条件。
我认为这可以帮助你:
SELECT posts.id, GROUP_CONCAT(attributes.value) as tags
FROM posts
JOIN attributes ON attributes.id = posts.id
WHERE ( attributes.attr = 'tag' )
GROUP BY posts.id
HAVING Find_In_Set('red',tags)>0 AND Find_In_Set('brown',tags)>0