1. 程式人生 > 實用技巧 >記錄一個SQL語句 case select 1

記錄一個SQL語句 case select 1

select Code, CodeName, CodeAlias, ComCode, OtherSign
  from ldcode
 where codetype = 'edorapptype'
   and code <> (case
         when (select 1
                 from lccont b
                where b.contno = 'W86190001290019'
                  and b.trdingchannel like 'ZFB%') = 1 then
          '-9'
         else
          '9'
       end)
   and othersign = '1'
 order by length(code), code asc

  去掉(case when)

 select Code, CodeName, CodeAlias, ComCode, OtherSign
  from ldcode
 where codetype = 'edorapptype'
   and code <> '-9' and othersign = '1'

  分析case when 1= 1 then '-9' else '9' end

(case
         when (select 1
                 from lccont b
                where b.contno = 'W86190001290019'
                  and b.trdingchannel like 'ZFB%') = 1 then
          '-9'
         else
          '9'
       end)

  分析 select 1

select 1  from lccont b
                where b.contno = 'W86190001290019'
                  and b.trdingchannel like 'ZFB%'

  select * ,select 某欄位,

可以用來查詢表中是否有符合條件的記錄(比如select 1 from seckill where id = 1001;),select 1一般用來當作條件使用,比如exists( select 1 from 表名)等。select 1的效率比select 列名select*快,因為不用查字典表。