gpt4 book ai didi

mysql - 将 IF 放入选择查询中

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

这段代码如何转换为正确的查询?可能吗?

首先我想检查id IN (55,1454,232,444,10999,223)是否存在行。其次,如果不存在,则随机获取行。

 SELECT
id
name
title
FROM
table t
WHERE
id IN (55,1454,232,444,10999,223)

IF count_row(t) == 0 // <-- if for WHERE no result row
SELECT
id
name
title
FROM
table
ORDER BY RAND()
LIMIT 20

最佳答案

尝试类似:

   SELECT id,
name,
title
FROM
table t
WHERE
id IN (55,1454,232,444,10999,223)

UNION

SELECT *
FROM
table t
WHERE NOT EXISTS (SELECT id,
name,
title
FROM
table t
WHERE
id IN (55,1454,232,444,10999,223));

关于mysql - 将 IF 放入选择查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595496/

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