如果select返回null,则选择不同的select

时间:2017-11-28 10:06:51

标签: sql

我们需要这样做:

select * 
from table1 as t
where t.num = 111
orderby t.createon

如果没有返回的记录 - 做不同的选择:

select * 
from table1 as t
orderby t.createon

有什么方法可以加入这些选择吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

SELECT
    *
FROM
    table1 as t
WHERE
    EXISTS((SELECT * FROM table1 WHERE num = 111) AND t.num = 111)
        OR NOT EXISTS (SELECT * FROM table1 where num = 111)
ORDER BY
    t.createon