1个查询中的多个条件 [英] Multiple criteria in 1 query

查看:102
本文介绍了1个查询中的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人的名字表,我有另一个人的誓言表.

I have one table of peoples names and I have another table of pledges these people have made.

他们可能会针对项目1,2,3或4进行抵押.

They might pledge money against item 1,2,3 or 4.

每次做出质押时,都会在质押表中输入一个人的ID和其所作的质押编号.

Each time a pledge is made an entry is made in the pledge table with the id of the person and the pledge number they made.

我想要一个查询,该查询可以得到不同的人的数量,例如对1和2都做出了承诺.

I would like a query that gets a count of distinct people that made pledges for both 1 and 2 for example.

如何实现?

推荐答案

您可以使用 编辑

关于查询的第二部分,您可以尝试

SELECT  p.PersonID,
        pp.PersonName,
        SUM(p.Pledge) TotalPledged
FROM    pledges p INNER JOIN
        people pp ON p.PersonID = pp.PersonID
WHERE   EXISTS(SELECT * FROM pledges WHERE PersonID = p.PersonID AND ItemID = 1)
AND     EXISTS(SELECT * FROM pledges WHERE PersonID = p.PersonID AND ItemID = 2)
GROUP BY p.PersonID,pp.PersonName

这篇关于1个查询中的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆