gpt4 book ai didi

mysql - 如何为MySQL调试复杂的嵌套SQL语句?

转载 作者:行者123 更新时间:2023-11-30 22:03:47 25 4
gpt4 key购买 nike

下面有3个表,

table a ( eid, COLUMNA, COLUMNB, COLUMNC, COLUMND)

1001,1,1,0,1
1002,1,0,0,0




table b (id, description)
1, ABC
2, BCD
3,CDE
4,DEF

注意:描述通常与table1的列相关;这些是 ui 上的复选框,因此 desciption = 来自 ui 的复选框名称。

table c(eid, groupid)

我的问题是,我需要将数据从表 1 迁移到表 3,如下所示

1001,1
1001,2
1001,4
1002,1

下面是我的查询,但没有得到我预期的结果。

SELECT DISTINCT eid, id
FROM (SELECT eid,
CASE
WHEN cola = 1
THEN
(SELECT id
FROM tableb
WHERE description = 'ABC')
WHEN cola = 0
THEN
-1
END
AS coln_a,
CASE
WHEN colb = 1
THEN
(SELECT id
FROM tableb
WHERE description = 'BCD')
WHEN colb = 0
THEN
-1
END
AS coln_b,
CASE
WHEN colc = 1
THEN
(SELECT id
FROM tableb
WHERE description = 'CDE')
WHEN colc = 0
THEN
-1
END
AS coln_c,
CASE
WHEN cold = 1
THEN
(SELECT id
FROM tableb
WHERE description = 'DEF')
WHEN cold = 0
THEN
-1
END
AS coln_d
FROM tablea
WHERE cola = 1 OR colb = 1 OR colc = 1 OR cold= 1) temp_t,
tableb
WHERE coln_a = id OR coln_b = id OR coln_c = id OR coln_d = id
ORDER BY eid, id;

错在哪里?

最佳答案

您通过执行一系列联合来完成此操作:

(SELECT eid, 1 AS groupid FROM tablea WHERE COLUMNA = 1)
UNION ALL
(SELECT eid, 2 FROM tablea WHERE COLUMNB = 1)
UNION ALL
(SELECT eid, 3 FROM tablea WHERE COLUMNC = 1)
UNION ALL
(SELECT eid, 4 FROM tablea WHERE COLUMND = 1)

我没有看到B表与你想要的立即输出有什么关系。

关于mysql - 如何为MySQL调试复杂的嵌套SQL语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com