SQL 服务器查询与 intersect except or union 关系代数
SQL Server query with intersect except or union relational Algebra
我正在尝试解决一个问题。好像是脑筋急转弯。
给定两个 table,return 只有第一个 table 的值,而第二个 table 中的每条记录都匹配。所以 table 1 中的记录必须与 table 2 中的每条记录匹配。如果 table 2 少于我想从最终结果中排除的每一行。
这必须在不使用 count、having、group by 的情况下完成。我必须用 union, intersect, except, exists 来解决它。
我正在使用 SQL 服务器顺便说一句。
CREATE TABLE table1 (id int, lid int)
INSERT INTO table1 VALUES (1, 1),(1, 2),(1,3),(1,4),(2,1),(3,3),(4,4)
CREATE TABLE table2 (lid int)
INSERT INTO table2 VALUES (1),(2),(3),(4)
Table 1:
id lid
--------
1 1
1 2
1 3
1 4
2 1
3 3
4 4
Table2:
lid
-----
1
2
3
4
这里的方法是"not the way I am supposed to solve it"。令人沮丧,因为这个解决方案非常简单并且完全按照它应该做的去做。我不能使用 count、group by 和 having。
SELECT id
FROM dbo.table1, dbo.table2
WHERE table1.lid = table2.lid
GROUP BY id
HAVING COUNT(*) = (SELECT COUNT(*) FROM dbo.table2)
所以基本上,当 table 中没有完整的匹配集时,我需要找到一种方法从第一个 table 中排除结果 2. 在这个例子中,唯一的值 table 1 与 table 中的每条记录都匹配 2 是 1。需要排除 2,3,4。
你要找的东西有一个名字。它被称为relational division. It has no equivalent in SQL, although it can be emulated in a variety of ways. Joe Celko has written one of the most complete blog posts about the topic。
由于您必须在 SQL 中使用一些更基本的关系运算符,这可能是您的一个解决方案:
SELECT DISTINCT id
FROM table1 t1a
WHERE NOT EXISTS (
SELECT *
FROM table2
WHERE NOT EXISTS (
SELECT *
FROM table1 t1b
WHERE t1a.id = t1b.id
AND t2.lid = t1b.lid
)
)
它是英文的,通俗地说:
Get me all the elements in table1
for which there is no element in table2
, which doesn't match such an element from table1
或者:
Get me the elements from table1
, which match all the elements in table2
这是解决方案之一:
select distinct id from table1 AS T1
where not exists(
select lid from table2
except
select lid from table1 where id = T1.id
)
我正在尝试解决一个问题。好像是脑筋急转弯。
给定两个 table,return 只有第一个 table 的值,而第二个 table 中的每条记录都匹配。所以 table 1 中的记录必须与 table 2 中的每条记录匹配。如果 table 2 少于我想从最终结果中排除的每一行。
这必须在不使用 count、having、group by 的情况下完成。我必须用 union, intersect, except, exists 来解决它。
我正在使用 SQL 服务器顺便说一句。
CREATE TABLE table1 (id int, lid int)
INSERT INTO table1 VALUES (1, 1),(1, 2),(1,3),(1,4),(2,1),(3,3),(4,4)
CREATE TABLE table2 (lid int)
INSERT INTO table2 VALUES (1),(2),(3),(4)
Table 1:
id lid
--------
1 1
1 2
1 3
1 4
2 1
3 3
4 4
Table2:
lid
-----
1
2
3
4
这里的方法是"not the way I am supposed to solve it"。令人沮丧,因为这个解决方案非常简单并且完全按照它应该做的去做。我不能使用 count、group by 和 having。
SELECT id
FROM dbo.table1, dbo.table2
WHERE table1.lid = table2.lid
GROUP BY id
HAVING COUNT(*) = (SELECT COUNT(*) FROM dbo.table2)
所以基本上,当 table 中没有完整的匹配集时,我需要找到一种方法从第一个 table 中排除结果 2. 在这个例子中,唯一的值 table 1 与 table 中的每条记录都匹配 2 是 1。需要排除 2,3,4。
你要找的东西有一个名字。它被称为relational division. It has no equivalent in SQL, although it can be emulated in a variety of ways. Joe Celko has written one of the most complete blog posts about the topic。
由于您必须在 SQL 中使用一些更基本的关系运算符,这可能是您的一个解决方案:
SELECT DISTINCT id
FROM table1 t1a
WHERE NOT EXISTS (
SELECT *
FROM table2
WHERE NOT EXISTS (
SELECT *
FROM table1 t1b
WHERE t1a.id = t1b.id
AND t2.lid = t1b.lid
)
)
它是英文的,通俗地说:
Get me all the elements in
table1
for which there is no element intable2
, which doesn't match such an element fromtable1
或者:
Get me the elements from
table1
, which match all the elements intable2
这是解决方案之一:
select distinct id from table1 AS T1
where not exists(
select lid from table2
except
select lid from table1 where id = T1.id
)