繁体   English   中英

在同一列上编写严格满足多个条件的选择查询

[英]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 byhaving

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.

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