获取SQL中记录的所有值

时间:2015-10-21 21:50:29

标签: sql

我需要使用SQL获取所有记录中所有值相同的列表。

SELECT
    Record,
    Value
FROM Record r
INNER JOIN Value v
    ON v.RecordId = r.RecordId

Record | Value
1      | a
1      | b
1      | c
2      | a
2      | b
3      | a
3      | b
3      | c
3      | d

我需要结果:

Value
a
b

1 个答案:

答案 0 :(得分:1)

您的查询不需要JOIN。我认为这是等价的:

SELECT v.RecordId, v.Value
FROM Value v;

从这里,您想要查找所有记录中的值:

select v.value
from value v
group by v.value
having count(recordid) = (select count(distinct recordid) from value);

这会查找包含值的所有记录的值。如果你想要所有记录,那么:

select v.value
from value v
group by v.value
having count(recordid) = (select count(*) from record r);