现在有一张表,假设有 A B C D 几个字段假设这张表里边有 100条数据1, 3, 3, 42, 3,3,43, 3,3,44, 3,3,45, 3,3,4.....有个需求:如果 A>B的情况下 有个自定义变量 X 设为1否则X设为0大概就是 select * ,X from Table where A>B ....这个sql 能写出来么? 不需要存储过程,就是用sql实现.最后的结果大概是这样的A,B,C,D,      X1, 3,3,4 ,    02, 3,3,4 ,    03, 3,3,4 ,    04, 3,3,4,     15, 3,3,4,     1

解决方案 »

  1.   

    select *,case when A>B then 1 else 0 end
    from tb
      

  2.   


    select *,A>B as X from Table
      

  3.   

    select * , case when A>B then 1 else 0 end as X from table
      

  4.   

    select *,if(A>B,1,0) X from table
      

  5.   

    你本来是只有ABCD4个字段的吧,后来的X字段是判断A>B后才加进去的。alter table table_name
    add column X boolean;
    update table_name set X=true where A>B;
    update table_name set X=false where A<=B;