database - 按两个标记系统过滤
问题描述
我有一个post
关系tag_system_one
和tag_system_two
。
post
和tag_system_one
through之间存在多对多的关系tag_system_one_post
。
post
和tag_system_two
through之间存在多对多的关系tag_system_two_post
。
我想要一个用于多标签搜索的 SQL 查询。
当只有一个标签系统时,查询非常简单:
SELECT post.*
FROM post
JOIN tag_system_one_post ON post.id = tag_system_one_post.fk_post
JOIN tag_system_one ON tag_system_one_post.fk_tag_system_one = tag_system_one.id
WHERE tag_system_one.id IN (500, 533)
GROUP BY post.id
HAVING COUNT(*) = 2;
在此示例中,它检索具有两个标签500
和533
(intersect) 的所有帖子。
Posts that only have tag `500` but not `533` will not be shown.
我能够执行上述声明,因为我使用的是COUNT(*) = 2
. tag_system_two
但是当我引入另一个标记系统( )时,这将不起作用。
有没有办法在没有子查询的情况下做到这一点?
解决方案
想了一会儿,我认为解决方案是:
HAVING COUNT(*) =
no. of items selected in tagging_system_one
MULTIPLIED BY
no. of items selected in tagging_system_two
推荐阅读
- docker - 如何在 docker build(Dockerfile)上显示 curl 进度条?
- c# - 是否有可能有重叠的正则表达式匹配?
- visual-studio - 通过 Visual Studio 调试时无法导出或导入文件
- annotations - AnyChart:适合以价格包含注释线
- java - 无法使用 Java SDK 通过 MS Graph API 在 Azure AD 中添加企业应用程序的 SAML 签名选项
- ansible - 如果在 Inventory 中找不到,Ansible 会自动添加主机
- python - Keras模型未编译
- node.js - 为什么我在 NodeJS express 应用中的上传不完整
- java - 如何在 JPQL Hibernate 中使用 SUM(CASE WHEN....)?
- javascript - 反应useState变量未设置