有个配置表
t_conf
(
    funccode           varchar2(30)     not null,      --属性代码
    funcvalue          number(10)        default  0  not null,      --属性值
)其中 ('user_name_case', 1)或者('user_name_case', 0)
分表代表funccode(user_name_case)对应的funcvalue为1则用户名大小写敏感,如果为0则用户名大小写不敏感用户表
user_name_case
(
userindex int,
username varchar2(36)
statue   int
)现在给出一个用户名v_username,查询该用户的userindex,但是需要先通过user_name_case来判断用户名username是否大小写敏感,
如果敏感的话则查询userindex的条件为username=v_username,如果不敏感则lower(username) == lower(v_username)。现在要求写一个复杂的sql语句能一次性的查询出该用户的userindex

解决方案 »

  1.   

    v_username和user_name_case有什么关系,怎么通过用户名查到这个用户是否大小敏感?
      

  2.   

    晕 你这个问题还有逻辑上的错误。假设用户名是对大小写不敏感的,那么你用条件lower(username) =lower(v_username)查出来的结果显然是多了。
      

  3.   


    with t_conf as 
    (select 'user_name_case' funccode ,0 funcvalue from dual) ,
    user_name_case as
    (select 1 userindex, 'A' username ,0 statue from dual
    union all
    select 2 ,'a',1 from dual)
    select userindex
      from user_name_case
     where (case
             when (select funcvalue from t_conf where funccode = 'user_name_case') = 1 then
              replace(username, 'a')
             else
              replace(lower(username), lower('a'))
           end) is null
      

  4.   

    我的帖子有点问题:
    用户表(表名写错了):
    t_user
    (
    userindex int,
    username varchar2(36)
    statue int
    )
    回复一下1、2楼的:
    v_username 是一个真实的用户名 eg:
    v_username := 'li_san';user_name_case就只是指表t_user中username是否大小敏感
      

  5.   


    select userindex from (
        select t1.userindex
          from user_name_case t1, t_conf t2
         where t2.funccode = 'user_name_case' and t2.funcvalue = 0
           and t1.username = 'AAA'
        union all  
        select t1.userindex
          from user_name_case t1, t_conf t2
         where t2.funccode = 'user_name_case' and t2.funcvalue = 1
           and lower(t1.username) = lower('AAA')    
    ) t where userindex is not null;
      

  6.   

    楼上是你想要的答案吗?如果没有,请给出两个表中几组具体的数据。我还是不大理解你的需求。
    还有,你是在过程中执行的吧?你想要‘一次性的查询出该用户的userindex’,是不是指用一条sql语句就能查出来结果?这个当然也能实现,但是既然是在过程中执行了,几条sql语句就没关系了,并不一定要一条sql才行,按照你说的思路用两条sql实现就很自然了。
      

  7.   


    这个不行。如果t_conf没有'user_name_case',按用户名敏感处理
      

  8.   


    with t_conf as 
    (select 'user_name_case' funccode ,0 funcvalue from dual) ,
    t_user as
    (select 1 userindex, 'A' username ,0 statue from dual
    union all
    select 2 ,'a',1 from dual)
    select userindex
      from t_user
     where (case
             when (select funcvalue from t_conf where funccode = 'user_name_case') = 1 then
              replace(username, 'a')
             else
              replace(lower(username), lower('a'))
           end) is null
    这个不行么?
      

  9.   

    一些数据:
    t_conf表(funccode|funcvalue): 这个表就是一个配置表每条记录作用不一样
    user_name_case|1
    auto_delete|1
    用户表(userindex|username|statue)其中userindex、username分别为唯一索引:
    168|wjj2|1
    159|008|1
    165|jsbc2|1
    164|jsbc3|1
      

  10.   

    3楼的应该符合你的需求,下面这个或许也可以,把'dD'替换成你的变量就可以。select userindex
      from t_user
     where ((select funcvalue from t_conf where funccode = 'user_name_case') = 1 and
           lower(username) = lower('dD'))
        or ((select funcvalue from t_conf where funccode = 'user_name_case') = 0 and
           username = 'dD')
      

  11.   

       呵呵!你不是求一个复杂的SQL吗?
      

  12.   

    3楼的确实有点意思,但是我查出来的结果怎么都是1呢?你的这个语句不能处理这种情况:如果t_conf没有'user_name_case',按用户名敏感处理
      

  13.   

    其中 'A'和'a'怎么理解的?当大小写敏感的时候,用户名可以大小写同时有,eg:'Li_San'
      

  14.   


    select userindex
      from t_user
     where ((select funcvalue from t_conf where funccode = 'user_name_case') = 1 and
           lower(username) = lower('dD'))
        or (nvl((select funcvalue from t_conf where funccode = 'user_name_case'),0) = 0 and
           username = 'dD')
      

  15.   

    with t_conf as (
    select 'user_name_case' funccode ,1 funcvalue from dual
    union all
    select 'auto_delete' funccode ,1 funcvalue from dual

    ,
    t_user as(
    select 168 userindex, 'wjj2' username ,1 statue from dual
    union all
    select 159 ,'008',1 from dual
    union all
    select 165 ,'jsbc2',1 from dual
    union all
    select 164 ,'jsbc3',1 from dual
    )
    select userindex
      from t_user
     where
           (case
             when (select funcvalue from t_conf where funccode = 'user_name_case') = 1 
             then
                 replace(username,v_username)
             else
                 replace(lower(username),lower(v_username))
             end) is null
      

  16.   

    with那块是构造一个表你要是用的,应该是
    select userindex
      from t_user
     where (case
             when (select funcvalue from t_conf where funccode = 'user_name_case') = 1 then
              replace(username, 'a')
             else
              replace(lower(username), lower('a'))
           end) is null把里面的‘a'替换成你传入的参数,应该没问题吧大小写敏感的话,这两个就不是一个人了
    li_san
    li_San
      

  17.   

    这个可以的 tks;你能不能写一个with语句的,小生也学学。 
      

  18.   

    有这么多人关注啊,既然能符合要求就可以了。with语句本身没什么意义,其实就相当于一个临时数据集。你是操作表数据的 ,不应该写with,楼上就是为了展示数据才那么写的。下班了。。
      

  19.   

    是的。
    你的sql语句也是Ok的。tks。