草庐IT

mysql - SQL IF SELECT query is null then do another query

coder 2023-10-19 原文

我有一个定期返回“无”的查询,如果是这种情况,我想运行一个不同的查询,但我不知道这样做的方法。如果有人可以提供帮助,请。

这是我当前使用的代码...

SELECT * FROM cfg_users JOIN cfg_ash ON cfg_users.iUserId = cfg_ash.iUserid WHERE iTeamId='0' AND sDisabled 为 NULL AND iStatusId > 0 AND sDate = '2014-08-01 ' GROUP BY cfg_users.iUserId ORDER BY iStatusId, sName

我主要想说

IF <my code> IS NULL THEN <do other code>, IF <my code> IS NOT NULL THEN return the result.

谢谢

最佳答案

有一些简单的方法只用sql。

将您的第一个查询定义为临时表,使用 union all,使用临时表的计数过滤第二个查询。

with temp as (select * from t1 where 1=0)
select * from temp
union all
select * from t2 where (select count(*) from  temp) =0

此查询将返回第二个表的记录。

with temp as (select * from t1 )
select * from temp
union all
select * from t2 where (select count(*) from  temp) =0

如果临时查询有结果,只返回临时查询。

您可以使用 sql fiddle here 进行测试.

关于mysql - SQL IF SELECT query is null then do another query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121213/

有关mysql - SQL IF SELECT query is null then do another query的更多相关文章

随机推荐