我有一张包含以下数据的表格:
licence_number
date_of_birth
organisation
我想在以下地址进行查询:
在organisation1中获取licence_numbers和dobs 许可证号码和数据包都在组织中。
我知道这很难,但是我很难挣扎。
答案 0 :(得分:3)
您可以按照license_number和date_of_birth分组,其中组织设置为两个有趣的组织之一,并计算组中有多少个不同的组织。
如果一个组中有两个可能,则有一个命中。
SELECT license_number, date_of_birth
FROM mytable
WHERE organisation IN ('organisation1', 'organisation2')
GROUP BY license_number, date_of_birth
HAVING COUNT(DISTINCT organisation) = 2;
...或者您可以使用INTERSECT
;
SELECT license_number, date_of_birth
FROM mytable WHERE organisation = 'organisation1'
INTERSECT
SELECT license_number, date_of_birth
FROM mytable WHERE organisation = 'organisation2'
答案 1 :(得分:0)
select
from t t0
where
organization = 'organization1'
and
exists (
select 1
from t
where
organization = 'organization2'
and
licence_number = t0.licence_number
and
date_of_birth = t0.date_of_birth
)
答案 2 :(得分:0)
您可以自行加入许可证编号和日期相同的表格,但组织不是:
SELECT DISTINCT p1.licence_number, p1.date_of_birth
FROM people p1
INNER JOIN people p2
ON p1.licence_number = p2.licence_number AND
p1.date_of_birth = p2.date_of_birth AND
p1.organisation <> p2.organisation
答案 3 :(得分:-1)
是JOIN还是1张桌子?
Select
[licence_number],
[date_of_birth],
[organisation]
From YourTable
Where organisation1 = organisation2
--OR
Select
[licence_number],
[date_of_birth],
[organisation]
From YourTable
Where organisation1 IN ('organisation2','organisation3','organisation3')
Order By [licence_number]