gpt4 book ai didi

MYSQL 案例

转载 作者:行者123 更新时间:2023-11-29 05:48:08 26 4
gpt4 key购买 nike

我进退两难,以下 SQL 查询的结果是什么以及它是如何工作的:

SELECT ...
CASE
WHEN (a.FIELD=1 AND b.FIELD=2) THEN 1
WHEN (a.FIELD=1 AND b.FIELD=3) THEN 3
WHEN (b.FIELD=2 AND c.FIELD=1) THEN 0
END)

..

FROM a
LEFT JOIN b ON a.ID = b.FOREIGN_KEY
LEFT JOIN c ON b.ID = c.FOREIGN_KEY

WHERE <conditions>

所以基本上有更多条件适用于 CASE 语句。MySQL 如何评估它们?结果是第一个 TRUE 条件还是最后一个?

谢谢

PS:我正在查看 MySQL 文档,但没有找到相关提示。

最佳答案

第一个匹配条件获胜。

CASE

CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE

each WHEN clause search_condition expression is evaluated until one is true, at which point its corresponding THEN clause statement_list executes. If no search_condition is equal, the ELSE clause statement_list executes, if there is one.

关于MYSQL 案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57506238/

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