I have this table in mysql enter image description here 1. Is it possible to select a count of - ALL same entity_id where field_tags_tid=2 and field_tags_tid=7

in this example the result would be 1 because only entity_id=6 matches field_tags_tid=2 and field_tags_tid=7

有帮助吗?

解决方案

This problem is often called Relational Division

SELECT  entity_ID
FROM    tableName
WHERE   field_tags_ID IN (2,7)
GROUP   BY entity_ID
HAVING  COUNT(*) = 2

if uniqueness was not enforce on field_tags_ID for every entity_ID then a DISTINCT keyword is needed. otherwise, leave it as is,

SELECT  entity_ID
FROM    tableName
WHERE   field_tags_ID IN (2,7)
GROUP   BY entity_ID
HAVING  COUNT(DISTINCT field_tags_ID) = 2

UPDATE 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top