如何选择满足条件的行(如果存在),否则选择所有行?

时间:2018-08-03 13:18:18

标签: mysql

我只想选择name='David'存在的行,如果没有这样的行

我要select * rows。以下是我的无效声明:

SELECT * FROM table A
Where name='David'
else
SELECT * from table A

1 个答案:

答案 0 :(得分:1)

在下面的查询中使用它将有效

  select m1.name from
   (
    select m1.*, case when m1.name ='David' then 1  else 0  end  as cnt from tableA m1 ) m1
     inner join 
    (
    select max(Cnt) as Cnt from
    (
     select t1.*, case when name ='David' then 1  else 0  end  as Cnt 
      from tableA t1
     ) as t2

     )as n 
     on m1.cnt=n.Cnt

http://sqlfiddle.com/#!9/88ed0c/2