![](/img/trans.png)
[英]How to select multiple rows of the same user that satisfies some condition in SQL?
[英]to write a select query which strictly satisfies a multiple condition on same column
我想通过连接两个表来编写一个选择查询。 像这样..
select entityid from companytagrel left join taginfo on companytagrel.tagid=taginfo.tagid where taginfo.tag = "own" and taginfo.tag ="rocking";
在表companytagrel和taginfo之间的关系为N-> 1的情况下,我想选择严格满足其taginfo.tag拥有且摇摆的条件的objectid。
注意:公司可以具有任意数量的标签。
companytagrel's column->(uniqueid,entityid,tagid)
taginfo's column -> (tagid,tagname)
您可以使用group by
并having
:
select ct.entityid
from companytagrel ct left join
taginfo t
on ct.tagid = t.tagid
where t.tag in ('own', 'rocking')
group by ct.entityid
having count(distinct t.tag) = 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.