从同一 table 中检索丢失的数据
Retrieving missing data from within the same table
我正在将缺少数据的 CSV 文件导入 MariDB table。我需要找到所有没有对应 place = 2
.
的代码
Table cityX
| id | code | place | value | description | subcode |
| 1 | 001x | 1 | 6.00 | unique str | A |
| 2 | 002x | 1 | 2.23 | diff string | B |
| 3 | 003x | 1 | 2.23 | another str | B |
table 中的每个 code
必须有一个包含 place = 1
和 place = 2
的重复行
| id | code | place | value | description | subcode |
| 1 | 001x | 1 | 6.00 | unique str | A |
| 2 | 001x | 2 | 6.00 | unique str | A |
我使用了 select ... except
语句的变体来隔离具有不同数量错误字段的代码。
使用 SELECT [code] FROM cityX WHERE place = '1' EXCEPT SELECT [code] FROM cityX where place = '2'
,创建临时 table 并将剩余的 place
、value
、description
和 subcode
字段连接到检索丢失的代码。我正在检索 大部分 的缺失记录,但也引入了重复记录。
如何正确 select 并插入缺少 place = 2
的行
此解决方案避免了在每个 RDBMS 中都不起作用的 EXCEPT(不确定 mysql)
SELECT CODES.code,
CODE_W_1.place AS place_1,
CODE_W_2.place AS place_2
FROM (SELECT code
FROM cityx
GROUP BY code) AS CODES
LEFT OUTER JOIN (SELECT code,
place
FROM cityx
WHERE place = 1
GROUP BY code) AS CODE_W_1
ON CODES.code = CODE_W_1.code
LEFT OUTER JOIN (SELECT code,
place
FROM cityx
WHERE place = 2
GROUP BY code) AS CODE_W_2
ON CODES.code = CODE_W_2.code
WHERE code_w_1 IS NULL
OR CODE_W_2.code IS NULL
我无权访问 mysql 来测试它,但我从 Rasgo 获得了它,它会自动写入 SQL.
我们可以使用selects which来测试其他值是否存在。我们可以单独使用查询来检查不匹配的值,或者在插入中添加缺失值。
create table cityX (
id int primary key not null auto_increment,
code char(5),
place int );
insert into cityX (code, place) values
('001x',1),('001x',2),('002x',1),('003x',2);
select * from cityX
order by code, place;
id | code | place
-: | :--- | ----:
1 | 001x | 1
2 | 001x | 2
3 | 002x | 1
4 | 003x | 2
insert into cityX (code, place)
select x.code,1 from cityX x
where place = 2
and not exists
(select id from cityX c
where c.code = x.code
and place = 1);
insert into cityX (code, place)
select x.code,2 from cityX x
where place = 1
and not exists
(select id from cityX c
where c.code = x.code
and place = 2);
select * from cityX
order by code, place;
id | code | place
-: | :--- | ----:
1 | 001x | 1
2 | 001x | 2
3 | 002x | 1
6 | 002x | 2
5 | 003x | 1
4 | 003x | 2
db<>fiddle here
我正在将缺少数据的 CSV 文件导入 MariDB table。我需要找到所有没有对应 place = 2
.
Table cityX
| id | code | place | value | description | subcode |
| 1 | 001x | 1 | 6.00 | unique str | A |
| 2 | 002x | 1 | 2.23 | diff string | B |
| 3 | 003x | 1 | 2.23 | another str | B |
table 中的每个 code
必须有一个包含 place = 1
和 place = 2
| id | code | place | value | description | subcode |
| 1 | 001x | 1 | 6.00 | unique str | A |
| 2 | 001x | 2 | 6.00 | unique str | A |
我使用了 select ... except
语句的变体来隔离具有不同数量错误字段的代码。
使用 SELECT [code] FROM cityX WHERE place = '1' EXCEPT SELECT [code] FROM cityX where place = '2'
,创建临时 table 并将剩余的 place
、value
、description
和 subcode
字段连接到检索丢失的代码。我正在检索 大部分 的缺失记录,但也引入了重复记录。
如何正确 select 并插入缺少 place = 2
此解决方案避免了在每个 RDBMS 中都不起作用的 EXCEPT(不确定 mysql)
SELECT CODES.code,
CODE_W_1.place AS place_1,
CODE_W_2.place AS place_2
FROM (SELECT code
FROM cityx
GROUP BY code) AS CODES
LEFT OUTER JOIN (SELECT code,
place
FROM cityx
WHERE place = 1
GROUP BY code) AS CODE_W_1
ON CODES.code = CODE_W_1.code
LEFT OUTER JOIN (SELECT code,
place
FROM cityx
WHERE place = 2
GROUP BY code) AS CODE_W_2
ON CODES.code = CODE_W_2.code
WHERE code_w_1 IS NULL
OR CODE_W_2.code IS NULL
我无权访问 mysql 来测试它,但我从 Rasgo 获得了它,它会自动写入 SQL.
我们可以使用selects which来测试其他值是否存在。我们可以单独使用查询来检查不匹配的值,或者在插入中添加缺失值。
create table cityX ( id int primary key not null auto_increment, code char(5), place int ); insert into cityX (code, place) values
('001x',1),('001x',2),('002x',1),('003x',2);
select * from cityX order by code, place;
id | code | place -: | :--- | ----: 1 | 001x | 1 2 | 001x | 2 3 | 002x | 1 4 | 003x | 2
insert into cityX (code, place) select x.code,1 from cityX x where place = 2 and not exists (select id from cityX c where c.code = x.code and place = 1);
insert into cityX (code, place) select x.code,2 from cityX x where place = 1 and not exists (select id from cityX c where c.code = x.code and place = 2);
select * from cityX order by code, place;
id | code | place -: | :--- | ----: 1 | 001x | 1 2 | 001x | 2 3 | 002x | 1 6 | 002x | 2 5 | 003x | 1 4 | 003x | 2
db<>fiddle here