我的问题是,如何在单个表中查找记录不相同的记录。好吧,这听起来很难理解,我将在图表中向您展示我的解释。
示例您有此表:
tblsample
accountid|accountname|budgetforyear|
-----------------------------------
30 | rent | 2018 |
40 | lights | 2018 |
50 | water | 2018 | <---- How can I only query this?
30 | rent | 2017 |
40 | lights | 2017 |
你怎么能找到那条记录?
答案 0 :(得分:1)
我想你想要这样的东西:
select *
from sample t1
where not exists (select *
from sample t2
where (t2.accountname, t2.accountid) = (t1.accountname, t1.accountid)
and t2.budgetforyear <> t1.budgetforyear);
以上内容返回表格中的行,其中没有相应的行存在相同的帐户名和accountid以及不同的年份。
答案 1 :(得分:0)
这是我认为的解决方案
SELECT *
FROM ( SELECT accountid, accountname, count(budgetforyear) AS years
FROM tblsample
GROUP BY accountid, accountname ) AS CountYears
WHERE CountYears.years = 1