简体   繁体   English

多对多sql select查询包含多个值,所有值都考虑在内

[英]Many-to-many sql select query containing multiple values, all values are taken into account

There is the following database structure:有以下数据库结构:

在此处输入图片说明

How do I write a query that returns only those Questions that have both CSS and JS tags?我如何编写一个只返回那些同时具有 CSS 和 JS 标签的问题的查询?

You can use aggregation and having :您可以使用聚合并having

select tq.question_id
from tagsquestions tq join
     tags t
     on tq.tag_id = t.id
where t.name in ('JS', 'CSS')
group by tq.question_id
having count(*) = 2;

This assumes that the tags assigned to a single question are distinct.这假设分配给单个问题的标签是不同的。 That seems like a reasonable assumption.这似乎是一个合理的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM