+ -
当前位置:首页 → 问答吧 → 一段差不多的Sql 执行结果不同。求一个解决方案

一段差不多的Sql 执行结果不同。求一个解决方案

时间:2011-12-26

来源:互联网

SQL code

  select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
    (select '''' || replace(st.person,',',''',''') ||'''' s from eso_review_standardaccount st
  where st.deviceid='2273')   中间这段sql 输出是   -- 'l45254','c00152559' 查询结果为空

 )



select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
 'l45254','c00152559'    --这样查询是有值的。。。好郁闷啊。。这是什么情况? 
 )


实在是找不到解决方案,请大家帮忙看下,谢谢


作者: CoCo__   发布时间: 2011-12-26

把建表和插入数据的SQL脚本贴一下,我实测一下,看看情况。

作者: LuiseRADL   发布时间: 2011-12-26

create table TPL_USER_T
(
  FNAME VARCHAR2(200), --lXX 223344
  LNAME VARCHAR2(200),
  w3_account VARCHAR2(200) --l223344
  )

eso_review_standardaccount 这个表的 person 字段数据就是 l223344,l54530 多值 ,号分割

用 eso_review_standardaccount 和 TPL_USER_T查询 person 字段和w3_account 关联

作者: CoCo__   发布时间: 2011-12-26

select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
 'l45254','c00152559' 
 )

你这样查询,IN里面相当于2个选项,而你第一种方法, 'l45254','c00152559'作为一个字符串返回,相当于一个选项,当然查不到了,相当于这样的一个变量"'l45254','c00152559'"

作者: java3344520   发布时间: 2011-12-26

第一个SQL in中的sql 执行之后返回的值有问题,
虽然你想拼成'AAA','BBB'的形式,但毕竟是用sql返回的,所以实际执行的时候是作为一个值。

两种方法解决
一,in后面的sql先取得数据,然后传给外面的sql
二,还是用sql,
可以用like查询
select wm_concat(ur.lname) from tpl_user_t ur,
(select st.person s from eso_review_standardaccount st
  where st.deviceid='2273') t
where ur.w3_account like t.s || ',%
'or ur.w3_account like '%,' || t.s

作者: zhucz_1982   发布时间: 2011-12-26

上面的sql 中 or前面的逗号 应该是在上一行。

select wm_concat(ur.lname) from tpl_user_t ur,
(select st.person s from eso_review_standardaccount st
  where st.deviceid='2273') t
where ur.w3_account like t.s || ',%'
or ur.w3_account like '%,' || t.s

作者: zhucz_1982   发布时间: 2011-12-26

引用 5 楼 zhucz_1982 的回复:
上面的sql 中 or前面的逗号 应该是在上一行。

select wm_concat(ur.lname) from tpl_user_t ur,
(select st.person s from eso_review_standardaccount st
where st.deviceid='2273') t
where ur.w3_account like t.s || ',%'……


亲 这样还是不行的啊 。。。 执行还是空值

作者: CoCo__   发布时间: 2011-12-26

SQL code
select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
[color=#FF0000]select * from(select regexp_substr(st.person||',','[^,]+',1,rownum) s 
from eso_review_standardaccount st
connect by rownum<=length(regexp_replace(st.person||',','[^,]+')))[/color])

试试,我自己测试通过了

作者: xpingping   发布时间: 2011-12-26

引用 6 楼 coco__ 的回复:
引用 5 楼 zhucz_1982 的回复:
上面的sql 中 or前面的逗号 应该是在上一行。

select wm_concat(ur.lname) from tpl_user_t ur,
(select st.person s from eso_review_standardaccount st
where st.deviceid='2273') t
where ur.w3_ac……


不知道你的 st.person 这个字段的值是什么样的?

作者: zhucz_1982   发布时间: 2011-12-26

引用 7 楼 xpingping 的回复:
SQL code

select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
[color=#FF0000]select * from(select regexp_substr(st.person||',','[^,]+',1,rownum) s 
from eso_review_standardaccou……


亲,如果针对条件查询 这个是正确的。。

可是我这是表之间的关联 如果把那个where条件去掉,。就没有达到效果。。


因为是很多行关联起来的

作者: CoCo__   发布时间: 2011-12-26

SQL code
with table_linshi as
(select wm_concat(person) person_sum from eso_review_standardaccount)
select wm_concat(ur.lname) from tpl_user_t ur where ur.w3_account in (
select * from(select regexp_substr(person_sum||',','[^,]+',1,rownum) s from table_linshi
connect by rownum<=length(regexp_replace(person_sum||',','[^,]+')))
)

作者: xpingping   发布时间: 2011-12-26

热门下载

更多