如何从 SQL 服务器的查询中排除另一个 table?
How do you exclude another table from a query in SQL Server?
假设您有以下两个 SQL 服务器表:
t1:
|----------------------------------------------|
|name | day_planed | day_canceled | discription|
|----------------------------------------------|
|alpha| 2015-11-02 | 2015-11-01 | some |
|alpha| 2015-11-02 | 2015-10-30 | text |
|beta | 2015-11-02 | 2015-11-01 | here |
|----------------------------------------------|
t2:
|----------------------------------------------|
|name | day_planed | day_canceled | discription|
|----------------------------------------------|
|alpha| 2015-11-02 | 2015-10-30 | text |
|----------------------------------------------|
在 t1 的查询中,我现在想要除 t2 之外的所有条目。我已经尝试过类似于
的东西
SELECT *
FROM t1 AS A
LEFT JOIN t2 as B ON (A.name = B.name
AND A.day_planed = B.day_planed
AND A.day_canceled != B.day_canceled)
不幸的是,我不明白为什么它不在 t1 的查询中排除来自 t2 的行。
第二个问题是,是否有一种简单的方法可以通过仅返回具有最大描述的行来查询没有 t2 的 t1。我尝试在 SQL 服务器中查看它,但只能找到第一个标识符,这不适用于 sql...
的 "wonderful" 实现
在此 "wonderful" 实施中有几种方法可以做到这一点。
SELECT * FROM t1
EXCEPT
SELECT * FROM t2
是一个。另一个是:
SELECT *
FROM t1
WHERE NOT EXISTS
( SELECT *
FROM t2
WHERE t2.name = t1.name
AND t2.day_planed = t1.day_planed
AND t2.day_canceled = t1.day_canceled
)
或者您可以使用 LEFT JOIN
并在您的 ON 子句之后检查与 WHERE t2.name IS NULL
不匹配的行。就像你拥有的一样,但用 = 而不是 !=
SELECT t1.*
FROM t1
LEFT JOIN t2
ON t2.name = t1.name
AND t2.day_planed = t1.day_planed
AND t2.day_canceled = t1.day_canceled
WHERE t2.name IS NULL;
如果要检查每一列(包括 discription
),请使用 EXCEPT。
您只是缺少 where 子句和描述条件。您还需要将 != 更改为 =.
SELECT *
FROM t1 AS A
LEFT JOIN t2 as B ON (A.name = B.name
AND A.day_planed = B.day_planed
AND A.day_canceled = B.day_canceled
AND A.discription = B.discription)
WHERE B.Name IS NULL
假设您有以下两个 SQL 服务器表:
t1:
|----------------------------------------------|
|name | day_planed | day_canceled | discription|
|----------------------------------------------|
|alpha| 2015-11-02 | 2015-11-01 | some |
|alpha| 2015-11-02 | 2015-10-30 | text |
|beta | 2015-11-02 | 2015-11-01 | here |
|----------------------------------------------|
t2:
|----------------------------------------------|
|name | day_planed | day_canceled | discription|
|----------------------------------------------|
|alpha| 2015-11-02 | 2015-10-30 | text |
|----------------------------------------------|
在 t1 的查询中,我现在想要除 t2 之外的所有条目。我已经尝试过类似于
的东西SELECT *
FROM t1 AS A
LEFT JOIN t2 as B ON (A.name = B.name
AND A.day_planed = B.day_planed
AND A.day_canceled != B.day_canceled)
不幸的是,我不明白为什么它不在 t1 的查询中排除来自 t2 的行。
第二个问题是,是否有一种简单的方法可以通过仅返回具有最大描述的行来查询没有 t2 的 t1。我尝试在 SQL 服务器中查看它,但只能找到第一个标识符,这不适用于 sql...
的 "wonderful" 实现在此 "wonderful" 实施中有几种方法可以做到这一点。
SELECT * FROM t1
EXCEPT
SELECT * FROM t2
是一个。另一个是:
SELECT *
FROM t1
WHERE NOT EXISTS
( SELECT *
FROM t2
WHERE t2.name = t1.name
AND t2.day_planed = t1.day_planed
AND t2.day_canceled = t1.day_canceled
)
或者您可以使用 LEFT JOIN
并在您的 ON 子句之后检查与 WHERE t2.name IS NULL
不匹配的行。就像你拥有的一样,但用 = 而不是 !=
SELECT t1.*
FROM t1
LEFT JOIN t2
ON t2.name = t1.name
AND t2.day_planed = t1.day_planed
AND t2.day_canceled = t1.day_canceled
WHERE t2.name IS NULL;
如果要检查每一列(包括 discription
),请使用 EXCEPT。
您只是缺少 where 子句和描述条件。您还需要将 != 更改为 =.
SELECT *
FROM t1 AS A
LEFT JOIN t2 as B ON (A.name = B.name
AND A.day_planed = B.day_planed
AND A.day_canceled = B.day_canceled
AND A.discription = B.discription)
WHERE B.Name IS NULL