如何解决这个查询?
IF EXISTS (SELECT * FROM links WHERE age='10')
SELECT * FROM links WHERE age = '10' ORDER BY
别的
SELECT * FROM 链接 ORDER BY
因此,如果存在恰好 10 天的链接,那么它将被显示,否则它将是随机的。
您有两个不同的查询 - 使用联合:
SELECT *
FROM links
WHERE age = '10'
UNION ALL
SELECT *
FROM links
WHERE NOT EXISTS (SELECT * FROM links WHERE age='10')
ORDER BT RAND()
我想你可以写:
SELECT *
FROM links
WHERE age = '10'
OR NOT EXISTS
( SELECT 1
FROM links
WHERE age = '10'
)
ORDER
BY RAND()
LIMIT 1
;
. . . 但我不确定它的性能如何。仅使用 运行查询可能更有意义,WHERE age = '10'
然后仅当第一个查询没有返回结果时才运行另一个查询。