SQL DELETE From 子查询语法错误
SQL DELETE From with sub query syntax error
我在 MS Access 2010 中运行 SQL 代码时遇到困难。我想寻求帮助来审查和更正它。
数据:
- 两个 table 的名字:Tbl_001_WholeBase
和 Tbl_002_NewKVG
- 他们通过名为 Key
的列进行了连接
问题:
我想删除 Tbl_001_WholeBase
中具有 Key
且在 table Tbl_002_NewKVG
中不可用的所有行
示例:
Tbl_001_WholeBase
ID Key
1 Hronic1
2 Hronic2
3 Hronic3
Tbl_002_NewKVG
ID Key
1 Hronic1
2 Hronic2
因此,我想在 Tbl_001_WholeBase
中只留下第 3 条记录,基础看起来像这样:
ID Key
3 Hronic3
我想在 Access 中使用的是:
DELETE
FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.KEY IN
(SELECT *
FROM Tbl_001_WholeBase
LEFT JOIN Tbl_002_NewKVG
ON Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
WHERE (((Tbl_002_NewKVG.Key) Is Null)));
子查询工作正常,但我无法将它与 Delete 语句连接起来。
当 运行 此代码为:
时出现错误
You have written a subquery that can return more than one field without using the Exists reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field.
Select 子查询中的键而不是 (*)
DELETE
FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.KEY IN
(SELECT keyId
FROM Tbl_001_WholeBase
LEFT JOIN Tbl_002_NewKVG
ON Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
WHERE (((Tbl_002_NewKVG.Key) Is Null)));
此处 keyId
将是您要删除行的列名或唯一键。
使用 LEFT JOIN
连接两个 table。
这将 return 联接左侧 table 中的所有记录,以及右侧 table 中的所有匹配记录。 NULL
用于右侧记录不可用的地方。
SELECT *
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
| Tbl_001_WholeBase.ID | Tbl_001_WholeBase.Key | Tbl_001_NewKVG.ID | Tbl_001_NewKVG.Key |
|----------------------|-----------------------|-------------------|--------------------|
| 1 | Hronic1 | 1 | Hronic1 |
| 2 | Hronic2 | 2 | Hronic2 |
| 3 | Hronic3 | NULL | NULL |
可以看到最后的NewKVG.Key
为NULL,所以可以从结果中省略:
SELECT *
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
WHERE NOT Tbl_001_NewKVG.Key IS NULL
| Tbl_001_WholeBase.ID | Tbl_001_WholeBase.Key | Tbl_001_NewKVG.ID | Tbl_001_NewKVG.Key |
|----------------------|-----------------------|-------------------|--------------------|
| 1 | Hronic1 | 1 | Hronic1 |
| 2 | Hronic2 | 2 | Hronic2 |
或者您可以从 table 中删除它:
DELETE DISTINCTROW Tbl_001_WholeBase.*
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
WHERE NOT Tbl_001_NewKVG.Key IS NULL
| ID | Key |
|----------|----------|
| #Deleted | #Deleted |
| #Deleted | #Deleted |
| 3 | Hronic3 |
使用EXISTS
:
DELETE FROM Tbl_001_WholeBase
WHERE EXISTS (SELECT 1
FROM Tbl_002_NewKVG
WHERE Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
);
或者使用 IN
没有 JOIN
:
DELETE FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.Key IN (SELECT Tbl_002_NewKVG.Key
FROM Tbl_002_NewKVG
);
我在 MS Access 2010 中运行 SQL 代码时遇到困难。我想寻求帮助来审查和更正它。
数据:
- 两个 table 的名字:Tbl_001_WholeBase
和 Tbl_002_NewKVG
- 他们通过名为 Key
问题:
我想删除 Tbl_001_WholeBase
中具有 Key
且在 table Tbl_002_NewKVG
示例:
Tbl_001_WholeBase
ID Key
1 Hronic1
2 Hronic2
3 Hronic3
Tbl_002_NewKVG
ID Key
1 Hronic1
2 Hronic2
因此,我想在 Tbl_001_WholeBase
中只留下第 3 条记录,基础看起来像这样:
ID Key
3 Hronic3
我想在 Access 中使用的是:
DELETE
FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.KEY IN
(SELECT *
FROM Tbl_001_WholeBase
LEFT JOIN Tbl_002_NewKVG
ON Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
WHERE (((Tbl_002_NewKVG.Key) Is Null)));
子查询工作正常,但我无法将它与 Delete 语句连接起来。
当 运行 此代码为:
You have written a subquery that can return more than one field without using the Exists reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field.
Select 子查询中的键而不是 (*)
DELETE
FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.KEY IN
(SELECT keyId
FROM Tbl_001_WholeBase
LEFT JOIN Tbl_002_NewKVG
ON Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
WHERE (((Tbl_002_NewKVG.Key) Is Null)));
此处 keyId
将是您要删除行的列名或唯一键。
使用 LEFT JOIN
连接两个 table。
这将 return 联接左侧 table 中的所有记录,以及右侧 table 中的所有匹配记录。 NULL
用于右侧记录不可用的地方。
SELECT *
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
| Tbl_001_WholeBase.ID | Tbl_001_WholeBase.Key | Tbl_001_NewKVG.ID | Tbl_001_NewKVG.Key |
|----------------------|-----------------------|-------------------|--------------------|
| 1 | Hronic1 | 1 | Hronic1 |
| 2 | Hronic2 | 2 | Hronic2 |
| 3 | Hronic3 | NULL | NULL |
可以看到最后的NewKVG.Key
为NULL,所以可以从结果中省略:
SELECT *
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
WHERE NOT Tbl_001_NewKVG.Key IS NULL
| Tbl_001_WholeBase.ID | Tbl_001_WholeBase.Key | Tbl_001_NewKVG.ID | Tbl_001_NewKVG.Key |
|----------------------|-----------------------|-------------------|--------------------|
| 1 | Hronic1 | 1 | Hronic1 |
| 2 | Hronic2 | 2 | Hronic2 |
或者您可以从 table 中删除它:
DELETE DISTINCTROW Tbl_001_WholeBase.*
FROM Tbl_001_WholeBase LEFT JOIN Tbl_001_NewKVG ON Tbl_001_WholeBase.Key = Tbl_001_NewKVG.Key
WHERE NOT Tbl_001_NewKVG.Key IS NULL
| ID | Key |
|----------|----------|
| #Deleted | #Deleted |
| #Deleted | #Deleted |
| 3 | Hronic3 |
使用EXISTS
:
DELETE FROM Tbl_001_WholeBase
WHERE EXISTS (SELECT 1
FROM Tbl_002_NewKVG
WHERE Tbl_001_WholeBase.Key = Tbl_002_NewKVG.Key
);
或者使用 IN
没有 JOIN
:
DELETE FROM Tbl_001_WholeBase
WHERE Tbl_001_WholeBase.Key IN (SELECT Tbl_002_NewKVG.Key
FROM Tbl_002_NewKVG
);