gpt4 book ai didi

mysql - 您可以在 MySQLi 查询中使用超过 1 个案例吗?

转载 作者:行者123 更新时间:2023-11-29 07:25:33 24 4
gpt4 key购买 nike

这个查询对我来说很复杂。第一个案例有效。我尝试添加第二种情况;但是,无法让它工作。一个案例是否需要超过 1 个 when 语句?我完全做错了第二种情况吗?

我需要第二种情况的唯一原因是因为当table1.data_lock = 0时,不存在等于0的table2.id。我只想要它包含 table2 IF table1.data_lock != 0

可能有更好的方法来做到这一点。

SELECT table1.id, table1.draft_sectionid, table1.page_columnid, table1.sortid, 
(case
when table1.data_lock = 0 then table1.data
when table1.data_lock != 0 then table2.data
end) as data,
table1.style
FROM table1

(case
when table1.data_lock != 0 then JOIN table2 ON table1.data_lock=table2.id
end)

WHERE table1.draft_sectionid = " . $section['id'] . " ORDER BY table1.sortid ASC LIMIT " . $section['columns']) or die ('Unable to execute query. '. mysqli_error($connection));

最佳答案

不可能使用 case-when 来定义选择的结构。

但是,在这种情况下,您可以使用子选择来生成值:

SELECT table1.id, table1.draft_sectionid, table1.page_columnid, table1.sortid, 
(case
when table1.data_lock = 0 then table1.data
when table1.data_lock != 0 then (select table2.data from table2 where table2.id = table1.data_lock)
end) as data,
table1.style
FROM table1
WHERE ...

关于mysql - 您可以在 MySQLi 查询中使用超过 1 个案例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34698319/

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