我有三个复选框和一个只显示交叉口的附加选项。
复选框
还有另一个复选框,上面写着:选中以显示交叉。
所以当有人选择绿色的时候,他们需要看到所有的绿色产品,如果他们选择绿色和社会--他们会看到所有带有绿色标签或社会标签的产品,这可以简单地使用其中的product_category_id IN (1,11)等等。
现在,当有人检查Green和Social和附加复选框以只显示交集时,我只想显示产品,这两个标签都是平等分配的。也就是那些既有绿色标签,又有社会标签的人,而忽略了其他人。
在下面的表格中,绿色和社会应该返回
id organisation_name
-----------------------------------------------
3221 Nederlandse Watershapsbank NV如果有人选择绿色的、可持续的和社会化的,加上互联网络选项,他们应该只看到:
id organisation_name
--------------------------------
21 Uppsalahem AB产品表
id issuer
-------------------------
1 1
11 1741
21 21
31 31
41 3221
51 51 组织表
id organisation_name
----------------------------------------
1 Nordic Investment Bank
21 Uppsalahem AB
31 Stangastaden
51 European Investment Bank
1741 Global Infrastructure Partners
3221 Nederlandse Watershapsbank NV项目类别表
id product_id product_category_id
--------------------------------------------------
1 1 1
2 11 1
3 21 1
4 31 1
5 41 1
5 41 61
6 51 1
6 51 11
6 21 61
6 21 11项目类别表
id product_category_name
--------------------------------------
1 Green
11 Sustainable
61 SocialSQL:
SELECT Products.id,
Organisation.organisation_name,
ProductCategory.product_category_id,
ProductCategory.product_id
FROM `Products`
LEFT JOIN `product_category` `ProductCategory` ON ProductCategory.product_id=Products.id
LEFT JOIN `organisation` `Organisation` ON Organisation.id=Products.issuer
LEFT JOIN `categories` `Categories` ON Categories.id=ProductCategory.product_category_id
WHERE `ProductCategory`.`product_category_id` IN (1, 11)
GROUP BY Products.id HAVING COUNT(*) = 2
ORDER BY Products.id ASC;发布于 2018-06-06 10:56:27
虽然我可能会在表示层中这样做,但您要寻找的逻辑如下:
...GROUP BY ... HAVING COUNT(DISTINCT ...) = n
其中:
区分是可选的,但不会造成任何伤害。
和
N等于IN()中的参数数。
https://stackoverflow.com/questions/50718310
复制相似问题