我的TABLE-A具有以下结构:
id step1error step2error step3error
1 null {json:"string"} null
2 {json:"error1"} {json:"error2"} {json:"error3"}
和具有以下结构的TABLE-B:
id ref(of TABLE-A) error
1 1 null
2 2 null
3 2 null
3 2 null
我尝试了以下查询,但是它始终使用来自TABLE-A的step1error更新TABLE-B。 查询:
SET @exist = (select count(TABLE-B.id) from TABLE-B left join TABLE-A on TABLE-B.ref = TABLE-A.id where
TABLE-B.error = TABLE-A.step2error);
UPDATE `TABLE-B` inner join VIEW-AB on TABLE-B.ref=VIEW-AB.ref
SET TABLE-B.error =
CASE
WHEN VIEW-AB.step1error != 'null' THEN VIEW-AB.step1error
WHEN VIEW-AB.step2error != 'null' THEN VIEW-AB.step2error
ELSE VIEW-AB.step3error END
WHERE
TABLE-B.error = 'null' and
TABLE-B.id = VIEW-AB.B_ref and
@exist = 0;
VIEW-AB
create view VIEW-AB as (SELECT TABLE-B.id "B_ref", TABLE-A.step1error , TABLE-A.step2error, TABLE-A.step3error FROM `TABLE-B` left join TABLE-A on TABLE-B.ref= TABLE-A.id WHERE TABLE-B.error = 'null');
VIEW-AB如下:
B_ref ref(TABLE-A) step1error step2error step3error
1 1 null {json:"string"} null
2 2 {json:"error1"} {json:"error2"} {json:"error3"}
3 2 {json:"error1"} {json:"error2"} {json:"error3"}
3 2 {json:"error1"} {json:"error2"} {json:"error3"}
我的结果:
id ref(of TABLE-A) error
1 1 {json:"string"}
2 2 {json:"error1"}
3 2 {json:"error1"}
3 2 {json:"error1"}
我需要编写一个MySQL查询来更新TABLE-B(错误为null),就像下面这样:
预期结果:
id ref(of TABLE-A) error
1 1 {json:"string"}
2 2 {json:"error1"}
3 2 {json:"error2"}
3 2 {json:"error3"}
答案 0 :(得分:0)
更新case语句中的空检查。代替'null'
,直接使用null
。
因此mysql将'null'
视为字符串。
使用以下查询
SET @exist = (select count(TABLE-B.id) from TABLE-B left join TABLE-A on TABLE-B.ref = TABLE-A.id where
TABLE-B.error = TABLE-A.step2error);
UPDATE `TABLE-B` inner join VIEW-AB on TABLE-B.ref=VIEW-AB.ref
SET TABLE-B.error =
CASE
WHEN VIEW-AB.step1error is not null THEN VIEW-AB.step1error
WHEN VIEW-AB.step2error is not null THEN VIEW-AB.step2error
ELSE VIEW-AB.step3error END
WHERE
TABLE-B.error = 'null' and
TABLE-B.id = VIEW-AB.B_ref and
@exist = 0;