$sql1 = "select CODE,max(YR_SC),SUM_N,SUM_U from bse_prc_p where CDE_L='".substr($code,0,11)."' and SC_L='YN'  and SUM_N is not null and SUM_U is not null ";   我想做个查询,比如 当CODE=‘%TW%’时,执行  SC_L=‘YW’,这个条件查询
                   当CODE=‘%HK%’时,执行  SC_L=‘YN’,这个条件查询  
                   当CODE=‘其他’时,执行  SC_L=‘CN’or 'BH',这个条件查询这个怎么做?

解决方案 »

  1.   

    mysql 有case语句  自己去试一下
      

  2.   

    本帖最后由 xuzuning 于 2010-09-09 09:08:34 编辑
      

  3.   

    $sql1 = "select max(YR_SC),SUM_N,SUM_U from bse_prc_p where CDE_L='".substr($code,0,11)."' and (select SC_L=
       case 
       when CODE like '%TW%' then 'YW'
       when CODE like '%HK%' then 'YN'
       else 'CN' or 'BH'
       end
       )
        and SUM_N is not null and SUM_U is not null "; 
    这样写可以吗?
    好像最后一个条件没有执行
      

  4.   

    最后一个条件让他 不like '%TW%' and  '%HK%'  应该怎么写?
      

  5.   

    else 后好像只能加一个条件把,不能加 OR
    怎么做啊?
      

  6.   

    三楼的方法我试了,貌似不行啊!
    $sql1 = "select max(YR_SC),SUM_N,SUM_U from bse_prc_p where CDE_L='".substr($code,0,11)."' and (select SC_L=
    case  
    when CODE like '%TW%' then 'YW'
    when CODE like '%HK%' then 'YN'
    else 'CN' or 'BH'
    end
    )
    and SUM_N is not null and SUM_U is not null ";  这种方式没法改吗?  else 'CN' 后就是不能加 OR 
    有啥办法没有?
      

  7.   

    $sql1 = "select max(YR_SC),SUM_N,SUM_U from bse_prc_p where CDE_L='".substr($code,0,11)."' and (select SC_L=
    case  
    when CODE like '%TW%' then 'YW'
    when CODE like '%HK%' then 'YN'
    else 'CN' or 'BH'
    end
    )
    and SUM_N is not null and SUM_U is not null ";  
    这种方式没法改吗? else 'CN' 后就是不能加 OR  
    有啥办法没有?
      

  8.   

    可以直接在组合SQL语句的时候判断呀~例如...
    if($a=="XXXXXX")
    {
          $stnt="XXXXXX";
    }
    else
    {
          $stnt="BBBBBBB";}
    $sqlstr="select*from `product` where ".$stnt;然后在执行..不必要直接生成呀..
      

  9.   

    用法:select case when 字段 >、<、>=、<=、!=、like 条件 then 结果 when 字段 >、<、>=、<=、!=、like 条件 then 结果 ... else 结果 end from 表名例子:select id,case when name = 'zhangsan' then 'z' when name = 'lisi' then 'l' else 'no' end from abc(abc表名;id字段;name字段名)
      

  10.   

    楼上的意思我明白,我的select case写的不太规范!
    我想说的是 最后一句 
    else 后是不是只能加一个条件?不能加OR
    比如
    or 'BH'
      

  11.   

    楼主的写法感觉没意义啊,你到底让他等于'CN' 还是 'BH',给个明确点的意思,模棱两可的估计程序不通过的原因
      

  12.   

    唉,我最后一个条件是想让他等于‘BH’或者‘CN’没看懂我的意思!