我不确定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

案例...何时需要一个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并跳过count(*)

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

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

10-10 13:36
查看更多