我在 mysql
1 中有这个表。是否可以选择一个计数 - ALL 相同 entity_id where field_tags_tid=2 and field_tags_tid=7
在此示例中,结果将为 1,因为只有 entity_id=6 匹配 field_tags_tid=2 和 field_tags_tid=7
我在 mysql
1 中有这个表。是否可以选择一个计数 - ALL 相同 entity_id where field_tags_tid=2 and field_tags_tid=7
在此示例中,结果将为 1,因为只有 entity_id=6 匹配 field_tags_tid=2 和 field_tags_tid=7
这个问题通常被称为Relational Division
SELECT entity_ID
FROM tableName
WHERE field_tags_ID IN (2,7)
GROUP BY entity_ID
HAVING COUNT(*) = 2
如果没有对field_tags_ID
每个强制执行唯一性,则需要entity_ID
一个DISTINCT
关键字。否则,保持原样,
SELECT entity_ID
FROM tableName
WHERE field_tags_ID IN (2,7)
GROUP BY entity_ID
HAVING COUNT(DISTINCT field_tags_ID) = 2
更新 1
SELECT COUNT(*) totalCOunt
FROM
(
SELECT entity_ID
FROM tableName
WHERE field_tags_tid IN (2,7)
GROUP BY entity_ID
HAVING COUNT(DISTINCT field_tags_tid) = 2
) s