SQL if select statement returns no rows then perform alternative select statement

前端 未结 3 558
忘掉有多难
忘掉有多难 2021-02-07 07:27

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)
         


        
3条回答
  •  野的像风
    2021-02-07 08:10

    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.

提交回复
热议问题