if if条件在sql中

lol*_*lol -8 c# sql sql-server

我想编写一个sql查询:

if此查询(select X from table1 where x = 'value1')有结果返回1 else if (select x from table2 where x = 'value2')有任何结果返回2否则返回0.

谢谢

Gor*_*off 7

一种方法是selectcase:

select (case when exists (select X from table1 where x = 'value1')
             then 1
             when exists (select x from table2 where x = 'value2')
             then 2
             else 0
        end) as flag
Run Code Online (Sandbox Code Playgroud)

  • 假设您想从C#执行查询`select 1;`.你知道怎么做吗?如果你这样做,那么你也知道如何从C#运行Gordon的查询.它使用`case`这一事实无关紧要; 它们都只是返回单个整数的查询.如果不这样做,那么您应该在C#中查找有关数据库访问的教程. (2认同)