SQlite - 修改 SQL 查询以删除在单独的 table 中找到的记录
SQlite - Modify SQL query to remove records found in separate table
我有一个 SQlite 数据库,其中有两个 table 具有相同的模式。它们包含来自两个不同来源的学生考试数据。
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
| rowid | student_id | level | name | start | grade |
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
| 0 | 10001 | Diploma | Business | 2022-04-01 | Pass |
| 1 | 10001 | Standard Grade | Mathematics | 2013-08-06 | Pass |
| 2 | 10002 | Intermediate 1 | Hospitality: Practical Cookery | 2013-08-06 | Pass |
| 3 | 10003 | Unlevelled | Elementary Food Hygiene Plus | 2016-04-28 | Pass |
| 4 | 10004 | SCQF Level 4 | Employability Award | 2015-06-18 | Pass |
| 5 | 10004 | Level 1 | SVQ in Food Preparation and Cooking | 2015-06-18 | Pass |
| 6 | 10004 | Standard Grade | Geography | 2013-08-06 | Fail |
| 7 | 10005 | Standard Grade | Social & Vocational Skills | 2013-08-06 | Pass |
| 8 | 10006 | Standard Grade | Art and Design | 2013-08-06 | Pass |
| 9 | 10006 | Standard Grade | English | 2013-08-06 | Pass |
| 10 | 10007 | Intermediate 1 | Chemistry | 2013-08-06 | Pass |
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
Table A 已经使用下面的 SQL 查询进行过滤,以过滤掉学生不及格然后重新参加并通过的考试。它使用 EXCEPT 删除先前失败的尝试,留下新的剩余通行证。
query_A = """
SELECT * FROM Table_A
EXCEPT
SELECT t1.*
FROM Table_A t1
WHERE t1.grade = 'Fail'
AND EXISTS (
SELECT *
FROM Table_A t2
WHERE (t2.student_id, t2.level, t2.name) = (t1.student_id, t1.level, t1.name)
AND t1.grade <> t2.grade
)
"""
Table B(虽然架构相同)包含不同的记录,一些但不是全部也存在于 Table A 中。
基本上,我想通过修改 query_A 语句从 Table_B 中删除 Table_B 中存在的任何记录,同时保留由 Table_A 创建的过滤器EXCEPT 命令。
I would like to remove any records that are present in Table_B from
Table_A by modifying the query_A statement while retaining the filter
on Table_A created by the EXCEPT command
再添加一个EXCEPT
:
SELECT * FROM Table_A
EXCEPT
SELECT t1.*
FROM Table_A t1
WHERE t1.grade = 'Fail'
AND EXISTS (
SELECT *
FROM Table_A t2
WHERE (t2.student_id, t2.level, t2.name) = (t1.student_id, t1.level, t1.name)
AND t1.grade <> t2.grade
)
EXCEPT
SELECT * FROM Table_b;
我有一个 SQlite 数据库,其中有两个 table 具有相同的模式。它们包含来自两个不同来源的学生考试数据。
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
| rowid | student_id | level | name | start | grade |
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
| 0 | 10001 | Diploma | Business | 2022-04-01 | Pass |
| 1 | 10001 | Standard Grade | Mathematics | 2013-08-06 | Pass |
| 2 | 10002 | Intermediate 1 | Hospitality: Practical Cookery | 2013-08-06 | Pass |
| 3 | 10003 | Unlevelled | Elementary Food Hygiene Plus | 2016-04-28 | Pass |
| 4 | 10004 | SCQF Level 4 | Employability Award | 2015-06-18 | Pass |
| 5 | 10004 | Level 1 | SVQ in Food Preparation and Cooking | 2015-06-18 | Pass |
| 6 | 10004 | Standard Grade | Geography | 2013-08-06 | Fail |
| 7 | 10005 | Standard Grade | Social & Vocational Skills | 2013-08-06 | Pass |
| 8 | 10006 | Standard Grade | Art and Design | 2013-08-06 | Pass |
| 9 | 10006 | Standard Grade | English | 2013-08-06 | Pass |
| 10 | 10007 | Intermediate 1 | Chemistry | 2013-08-06 | Pass |
+────────+─────────────+─────────────────+──────────────────────────────────────+─────────────+────────+
Table A 已经使用下面的 SQL 查询进行过滤,以过滤掉学生不及格然后重新参加并通过的考试。它使用 EXCEPT 删除先前失败的尝试,留下新的剩余通行证。
query_A = """
SELECT * FROM Table_A
EXCEPT
SELECT t1.*
FROM Table_A t1
WHERE t1.grade = 'Fail'
AND EXISTS (
SELECT *
FROM Table_A t2
WHERE (t2.student_id, t2.level, t2.name) = (t1.student_id, t1.level, t1.name)
AND t1.grade <> t2.grade
)
"""
Table B(虽然架构相同)包含不同的记录,一些但不是全部也存在于 Table A 中。
基本上,我想通过修改 query_A 语句从 Table_B 中删除 Table_B 中存在的任何记录,同时保留由 Table_A 创建的过滤器EXCEPT 命令。
I would like to remove any records that are present in Table_B from Table_A by modifying the query_A statement while retaining the filter on Table_A created by the EXCEPT command
再添加一个EXCEPT
:
SELECT * FROM Table_A
EXCEPT
SELECT t1.*
FROM Table_A t1
WHERE t1.grade = 'Fail'
AND EXISTS (
SELECT *
FROM Table_A t2
WHERE (t2.student_id, t2.level, t2.name) = (t1.student_id, t1.level, t1.name)
AND t1.grade <> t2.grade
)
EXCEPT
SELECT * FROM Table_b;