sql - Oracle SQL - 加入和拥有
问题描述
我试图获取在合同表中出现多次的帐户,无论它在 c_map 表中加入的次数如何。
CREATE TABLE cntrct (
cntrct_id VARCHAR(10),
account_id varchar(10)
);
CREATE TABLE c_map (
cntrct_id VARCHAR(10)
);
INSERT INTO cntrct VALUES (1,'a');
insert into cntrct values (2,'b');
insert into cntrct values (3,'c');
insert into cntrct values (4,'b');
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (3);
insert into c_map values (3);
insert into c_map values (3);
commit;
select ct.account_id
from cntrct ct, c_map cm
where ct.cntrct_id = cm.cntrct_id
group by ct.account_id
having count(ct.account_id)> 1
小提琴: http ://sqlfiddle.com/#!4/cec1b7/4
我期待输出:
b
但相反,我得到了所有这些。如何限制它,以便在运行 count()>1 时不考虑 c_map 表?
谢谢,
解决方案
获取在合同表中出现多次的帐户,无论它在 c_map 表中加入的次数。
为什么要加入?您想要的信息在合同表中:
select account_id
from cntrct
group by account_id
having count(*) > 1
也许您想根据映射表中存在的合同 ID 过滤数据集。如果是这样,我会推荐 exists
:
select account_id
from cntrct c
where exists (select 1 from c_map m where m.cntrct_id = c.cntrct_id)
group by account_id
having count(*) > 1
推荐阅读
- compare - 如何在 Notepad++ 中从两个不同的文档中查找重复值?
- android - Cordova/Phonegap AJAX iOS 似乎没有得到响应
- java - 如何正确解析此 JSON?
- javascript - Moment.js 从现在开始计算错误
- javascript - 使用 Jquery Query Builder 时如何防止 SQL 注入
- c# - 如何在不重置 TCP 客户端的情况下使用按钮从客户端发送消息?
- java - 如何在 IntelliJ 运行控制台中使用颜色?
- android-studio - 文件提供者提出问题
- jpql - REST 调用中的 Xwiki XWQL 查询不起作用
- stata - 错误 ”
: 3499 mm_radial() 未找到”