gpt4 book ai didi

mysql - 选择查询不起作用时的情况

转载 作者:行者123 更新时间:2023-11-29 06:15:45 25 4
gpt4 key购买 nike

我不确定情况如何与 mysql 一起使用,任何人都可以解释这个查询有什么问题吗?

CASE WHEN SELECT COUNT(*) from websites where website_id = 171 and master = 2 > 0
SELECT timestamp from websites where website_id = 171 and master = 2
ELSE SELECT NOW() as timestamp

最佳答案

查看 MySQL Control Flow Functions

CASE...WHEN 需要有 THEN 关键字才能工作。

您可能想要:

CASE 
WHEN (SELECT COUNT(*) from websites where website_id = 171 and master = 2) > 0
THEN (SELECT timestamp from websites where website_id = 171 and master = 2)
ELSE NOW()
END as timestamp

如果只有两种可能性,那么使用 IF 会更好:

IF((SELECT count(*) from websites where website_id = 171 and master = 2) > 0,  
(SELECT timestamp from websites where website_id = 171 and master = 2),
NOW()) AS TIMESTAMP

或者,您可以使用 IFNULL 并跳过计数(*)

IFNULL((SELECT timestamp from websites where website_id = 171 and master = 2),
NOW()) AS TIMESTAMP

关于mysql - 选择查询不起作用时的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6674274/

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