SQL if select statement returns no rows then perfo

2020-06-01 06:58发布

Basically, what syntex would allow me to achieve the title statement?

If (select statement 1) returns 0 rows THEN (select statement 2) else (select statement 3)

So that the sql returns results from either statement 2 or 3 I've looked for a way to do this but nothing I've found so far seems to exactly address the if requirements.

标签: sql
3条回答
不美不萌又怎样
2楼-- · 2020-06-01 07:22
IF EXISTS (SELECT field FROM table)
BEGIN
SELECT field FROM table2
END
ELSE
BEGIN
SELECT field FROM table3
END
查看更多
相关推荐>>
3楼-- · 2020-06-01 07:40

Here you go...

IF ((select count(*) from table1)= 0)
BEGIN
Select * from table2
END
ELSE
BEGIN
SELECT * from table3
END
查看更多
叛逆
4楼-- · 2020-06-01 07:42

Sorry for the lack of feedback. Someone else in the office took an interest and came up with this:

select * from (
        select *
              , (SELECT Count(*) 
                   FROM users 
                  WHERE version_replace = 59 AND moderated = 1) AS Counter 
          FROM users WHERE version_replace = 59 AND moderated in (0,1)
     ) AS y
where Counter = 0 and Moderated = 0
   or Counter > 0 and Moderated = 1
ORDER By ID DESC

Which does what I need.

查看更多
登录 后发表回答