求一个复杂的sql语句,要求见内

adixia 2010-07-01 09:10:06
有个配置表
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
...全文
150 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 zhangcunhua 的回复:]
with那块是构造一个表

你要是用的,应该是

SQL code

select userindex
from t_user
where (case
when (select funcvalue from t_conf where funccode = 'user_name_case') = 1 then
replace(usern……
[/Quote]


是的。
你的sql语句也是Ok的。tks。
vber1010 2010-07-01
  • 打赏
  • 举报
回复
有这么多人关注啊,既然能符合要求就可以了。with语句本身没什么意义,其实就相当于一个临时数据集。你是操作表数据的 ,不应该写with,楼上就是为了展示数据才那么写的。下班了。。
adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 vber1010 的回复:]


引用 14 楼 adixia 的回复:
引用 12 楼 vber1010 的回复:
3楼的应该符合你的需求,下面这个或许也可以,把'dD'替换成你的变量就可以。

SQL code

select userindex
from t_user
where ((select funcvalue from t_conf where funccod……
[/Quote]


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

cyousor 2010-07-01
  • 打赏
  • 举报
回复
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
gelyon 2010-07-01
  • 打赏
  • 举报
回复
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
vber1010 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 adixia 的回复:]
引用 12 楼 vber1010 的回复:
3楼的应该符合你的需求,下面这个或许也可以,把'dD'替换成你的变量就可以。

SQL code

select userindex
from t_user
where ((select funcvalue from t_conf where funccode = 'user_name_case') = 1 and
lower(usern……
[/Quote]

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')

adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhangcunhua 的回复:]
SQL code

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)
selec……
[/Quote]


其中 'A'和'a'怎么理解的?当大小写敏感的时候,用户名可以大小写同时有,eg:'Li_San'
adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 vber1010 的回复:]
3楼的应该符合你的需求,下面这个或许也可以,把'dD'替换成你的变量就可以。

SQL code

select userindex
from t_user
where ((select funcvalue from t_conf where funccode = 'user_name_case') = 1 and
lower(username) = lower(……
[/Quote]


3楼的确实有点意思,但是我查出来的结果怎么都是1呢?

你的这个语句不能处理这种情况:如果t_conf没有'user_name_case',按用户名敏感处理
碧水幽幽泉 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 adixia 的回复:]
引用 9 楼 zhangcunhua 的回复:
SQL code

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 ……
[/Quote]
呵呵!你不是求一个复杂的SQL吗?
vber1010 2010-07-01
  • 打赏
  • 举报
回复
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')

adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhangcunhua 的回复:]
SQL code

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)
selec……
[/Quote]

你这个语法好复杂,没用过,我先学习一下。
adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 vber1010 的回复:]
楼上是你想要的答案吗?如果没有,请给出两个表中几组具体的数据。我还是不大理解你的需求。
还有,你是在过程中执行的吧?你想要‘一次性的查询出该用户的userindex’,是不是指用一条sql语句就能查出来结果?这个当然也能实现,但是既然是在过程中执行了,几条sql语句就没关系了,并不一定要一条sql才行,按照你说的思路用两条sql实现就很自然了。
[/Quote]
一些数据:


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
cyousor 2010-07-01
  • 打赏
  • 举报
回复

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


这个不行么?
adixia 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 quiettown 的回复:]
SQL code

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'
……
[/Quote]



这个不行。

如果t_conf没有'user_name_case',按用户名敏感处理
adixia 2010-07-01
  • 打赏
  • 举报
回复
不是在过程中执行。在C语言开发的接口中执行,不想多次调用C接口的。稍等我找数据
vber1010 2010-07-01
  • 打赏
  • 举报
回复
楼上是你想要的答案吗?如果没有,请给出两个表中几组具体的数据。我还是不大理解你的需求。
还有,你是在过程中执行的吧?你想要‘一次性的查询出该用户的userindex’,是不是指用一条sql语句就能查出来结果?这个当然也能实现,但是既然是在过程中执行了,几条sql语句就没关系了,并不一定要一条sql才行,按照你说的思路用两条sql实现就很自然了。
quiettown 2010-07-01
  • 打赏
  • 举报
回复

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;
adixia 2010-07-01
  • 打赏
  • 举报
回复
我的帖子有点问题:
用户表(表名写错了):
t_user
(
userindex int,
username varchar2(36)
statue int
)


回复一下1、2楼的:
v_username 是一个真实的用户名 eg:
v_username := 'li_san';

user_name_case就只是指表t_user中username是否大小敏感
cyousor 2010-07-01
  • 打赏
  • 举报
回复

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


vber1010 2010-07-01
  • 打赏
  • 举报
回复
晕 你这个问题还有逻辑上的错误。假设用户名是对大小写不敏感的,那么你用条件lower(username) =lower(v_username)查出来的结果显然是多了。
加载更多回复(1)

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧