這個問題從MYSQL join results set wiped results during IN() in where clause?分割一個MYSQL串從GROUP_CONCAT成(陣列一樣,表達,列表),該IN()可以理解
如下上因此,問題的短版本。你怎麼又被GROUP_CONCAT返回到一個逗號分隔的表達式列表IN()將當作多個項目的遍歷列表中的字符串?
N.B. MySQL的文檔出現來表示爲「表達式列表」,和有趣上IN()的頁面中使用由IN()「(逗號,分隔,列表)」似乎或多或少在MySQL文檔的唯一的頁面永遠提及表達式列表。所以我不知道,如果用於製作數組或臨時表會在這裏任何使用功能。
的問題爲基礎的例子龍版:從2臺DB這樣的:
SELECT id, name, GROUP_CONCAT(tag_id) FROM person INNER JOIN tag ON person.id = tag.person_id GROUP BY person.id;
+----+------+----------------------+
| id | name | GROUP_CONCAT(tag_id) |
+----+------+----------------------+
| 1 | Bob | 1,2 |
| 2 | Jill | 2,3 |
+----+------+----------------------+
我怎麼能拒絕這一點,這是它可以使用字符串被視爲邏輯等效(1 = X)和(2 = X)...
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person LEFT JOIN tag ON person.id = tag.person_id
GROUP BY person.id HAVING ((1 IN (GROUP_CONCAT(tag.tag_id))) AND (2 IN (GROUP_CONCAT(tag.tag_id))));
Empty set (0.01 sec)
...弄成其中GROUP_CONCAT結果被視爲一個列表,使得對於鮑勃,這將是等同於:
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person INNER JOIN tag ON person.id = tag.person_id AND person.id = 1
GROUP BY person.id HAVING ((1 IN (1,2)) AND (2 IN (1,2)));
+------+--------------------------+
| name | GROUP_CONCAT(tag.tag_id) |
+------+--------------------------+
| Bob | 1,2 |
+------+--------------------------+
1 row in set (0.00 sec)
...和吉爾,這將相當於:
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person INNER JOIN tag ON person.id = tag.person_id AND person.id = 2
GROUP BY person.id HAVING ((1 IN (2,3)) AND (2 IN (2,3)));
Empty set (0.00 sec)
...所以整體結果將是要求不使用具有COUNT所有上市標籤的獨家搜索條款(DISTINCT ...)?
(注:此邏輯作品,未經AND,適用於字符串的第一個字符如
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person LEFT JOIN tag ON person.id = tag.person_id
GROUP BY person.id HAVING ((2 IN (GROUP_CONCAT(tag.tag_id))));
+------+--------------------------+
| name | GROUP_CONCAT(tag.tag_id) |
+------+--------------------------+
| Jill | 2,3 |
+------+--------------------------+
1 row in set (0.00 sec)
這是好的,但值得注意的是,你不需要做`GROUP_CONCAT()`所有`FIND_IN_SET()`。你可以'SELECT GROUP_CONCAT(tag.tag_id)AS tags_list`然後`HAVING FIND_IN_SET(20,tags_list)` – Treffynnon 2011-03-03 16:12:12