SELECT c.id,c.email,cs.Source
FROM contact c INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE cs.Source IN (SELECT cs.Source FROM contact c
INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE cs.Source = 'DOGS')
AND cs.Source IN (SELECT cs.Source FROM contact c
INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE cs.Source = 'CATS')
我想要一个包含'DOGS'和'CATS'作为来源的电子邮件列表。
Source将位于表中的单独行:contactsource,但具有相同的contact_id(表中的PK id:contact)
我运行时没有得到任何结果(单独运行子查询会返回结果,并且有些实例的源是CATS和DOGS,在不同的行上注明)
答案 0 :(得分:1)
我想要一个包含'DOGS'和'CATS'作为来源的电子邮件列表。
因此,仅从contact
中选择,并将标准放在WHERE子句中:
select id, email
from contact
where id in
(
select contactid__c
from contactsource__C
where source in ('DOGS', 'CATS')
group by contactid__c
having count(distinct source) = 2
);
答案 1 :(得分:0)
将所有的代码列设置为较低或较高可能有效。
SELECT c.id,c.email,cs.Source
FROM contact c INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE lower(cs.Source) IN (SELECT lower(cs.Source) FROM contact c
INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE cs.Source = 'DOGS')
AND lower(cs.Source) IN (SELECT lower(cs.Source) FROM contact c
INNER JOIN contactsource__C cs ON cs.contactid__c = c.id
WHERE cs.Source = 'CATS')
答案 2 :(得分:0)
首先你有两个相同的子查询,所以最好只需更改子查询中的谓词就可以将它们连接在一起,后者变为:
SELECT
cs.Source
FROM contact c
INNER JOIN contactsource__C cs
ON cs.contactid__c = c.id
WHERE cs.Source = 'DOGS' or cs.Source = 'CATS';
但我不知道你期望子查询产生什么,除了结果' CATS'或者' DOGS',因为您正在尝试选择您在条件子句中放入的相同列:
select
u.username
from dba_users u
inner join dba_profiles p
on u.profile = p.profile
where u.username = 'SYS';
USERNAME
-----------------------------------------------
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
因此,子查询的结果将是' CATS'或者' DOGS'。
所以你的查询应该是这样的:
SELECT
c.id,
c.email,
cs.Source
FROM contact c
INNER JOIN contactsource__C cs
ON cs.contactid__c = c.id
WHERE cs.Source = 'DOGS' OR cs.Source='CATS';