表结构如下 
create table my_tj
(
 name varchar(30),
 my_type decimal(5),--判断这个
 app_id varchar(50)
)假如我表中有以下数据 
name  my_type    app_id
abc      1         abc
aaa      2         abcde
tyu      2         qqqqqqq
我想查询
name like '%a%' and 
如果my_type='1'时,app_id数据的长度大于2的数据
如果my_type='2'时,app_id数据的长度大于10的数据这个条件判定语句应该是怎么写啊

解决方案 »

  1.   

    上边写的有点问题
    应该是 
    我想查询 
    name like '%a%' and 
    如果my_type=1时,app_id数据的长度大于2的数据 
    如果my_type=2时,app_id数据的长度大于10的数据 
      

  2.   

    select * from tb where name like '%a%' and ((my_type='1' and len(app_id) > 2) or (my_type='2' and len(app_id) > 10))
      

  3.   

    create table tb(name varchar(30),my_type decimal(5),app_id varchar(50))
    insert into tb values('abc' ,     1 ,       'abc') 
    insert into tb values('aaa' ,     2 ,       'abcde') 
    insert into tb values('tyu' ,     2 ,       'qqqqqqq') 
    goselect * from tb where name like '%a%' and ((my_type='1' and len(app_id) > 2) or (my_type='2' and len(app_id) > 10))drop table tb/*
    name                           my_type app_id                                             
    ------------------------------ ------- -------------------------------------------------- 
    abc                            1       abc(所影响的行数为 1 行)
    */
      

  4.   

    [code=SQL]
    ...
    where name like '%a%' 
    and case 
        when my_type=1 and len(app_id)>2 then 1 -- 如果my_type=1时,app_id数据的长度大于2的数据 
        when my_type=2 and len(app_id)>10 then 1 -- 如果my_type=2时,app_id数据的长度大于10的数据 
        else 0 end
    [/code[
      

  5.   

    回答的不是我想要的,这个我也会。。
    我刚才问的不清楚,
    应该是name like '%a%' and 
    如果my_type=1时,app_id数据的长度大于2的数据 
    如果my_type=2时,app_id数据的长度大于10的数据
    如果my_type=3时,则不做app_id数据的判定了这个怎么写呢?
    我主要就是想知道my_type改变时,
    判定的条件可能会有多有少,
    例如等于1时,可能就做1个判断
    2时可能3,4个判断
    3时可能6,7个判断
    等于4时可能就什么判断就不做
    这样的情况怎么写
      

  6.   


    恩,这个就是我想问的
    when my_type=1 and len(app_id)>2 then 1   最后的  then 1  ,这个 then 1  是固定的 1 ?有什么含义?
      

  7.   

    你的条件不定啊?那就没法搞了.把条件写到某个表里,然后取出来,用动态SQL完成了.
      

  8.   

    比如说是个人员登陆的程序,
    我除了判定账号密码对不对
    还判定,这个账号是不是临时账号,(比如linshi=y就说明是临时)
    如果是临时账号,就判定现在超时没,
    如不是临时账号,就只判断账号密码对不对
      

  9.   

    判定账号密码对不对 
    还判定,这个账号是不是临时账号,(比如linshi=y就说明是临时) 
    如果是临时账号,就判定现在超时没, 
    如不是临时账号,就只判断账号密码对不对case
      when linshi='y' and overtime='Y' then 0 -- 临时账号, 超时
      when linshi='y' then 1 -- 临时账号, 没超时
      else dbo.fn_passwordcorrect(password) -- 判定账号密码
      end 
      

  10.   

    select * from tb where name like '%a%' and 
    len(app_id)>(case when my_type=1 then 2 when my_type=2 then 10 else -1 end)
      

  11.   

    确切地说,上面的语句不完整。
    完整的表达应该是:
    ...
    where name like '%a%' 
    and case 
        when my_type=1 and len(app_id)>2 then 1 -- 如果my_type=1时,app_id数据的长度大于2的数据 
        when my_type=2 and len(app_id)>10 then 1 -- 如果my_type=2时,app_id数据的长度大于10的数据 
        else 0 end = 1
    ..即:当表达式中的逻辑成立时, 1=1 -- 记录符合条件 
    最后 else 0 end -- 当所有逻辑均不成立时,0=1 -- 记录不符合条件
      

  12.   

    AND和OR就可以完成了啊
    select * from tb where name like '%a%' and ((my_type='1' and len(app_id) > 2) or (my_type='2' and len(app_id) > 10))
      

  13.   

    select * from tb where name like '%a%' and ((my_type='1' and len(app_id) > 2) or (my_type='2' and len(app_id) > 10))
      

  14.   

    create table my_tj
    (
     name varchar(30),
     my_type decimal(5),--判断这个
     app_id varchar(50)
    )insert into my_tj values('abc',1,'abc') 
    insert into my_tj values('aaa',2,'abcde') 
    insert into my_tj values('tyu',2,'qqqqqqq') select * from my_tj where name like '%a%' and ((my_type='1' and len(app_id) > 2) or (my_type='2' and len(app_id) > 10))