一个SQL语句求教

bunrise 2010-01-21 02:14:30
我的表中有这么个字段,格式为:A@@B@@C

我的要求将这个字段的值变为多条,即:1.A
2.B
3.C
在不用存储过程的情况下,用以条SQL写出来,
现在唯一能确定的是@@是分隔符,其他的都不确定
所以要求要写灵活一点

我自己写出一个,不够灵活

所以希望看看大家有什么好怕方法没有,谢谢!
...全文
199 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhigangxie 2010-01-22
  • 打赏
  • 举报
回复
估计狂浪也搞不定。请问有谁能在一条SQL命令中能开出可变的栈空间来吗?
vampireallen 2010-01-21
  • 打赏
  • 举报
回复

select substring_index('a@@b@@c', '@@', 1)
union
select right(substring_index('a@@b@@c', '@@', 2),1)
union
select right(substring_index('a@@b@@c', '@@', 3),1);


:P 你可以看看这样有没有扩展的可能
MySQL的
zl3450341 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 crazylaa 的回复:]
无语,
去oracle开发版,点名要wildwave(狂浪)给你搞定。
[/Quote]
哈哈 好想法
yanhui007 2010-01-21
  • 打赏
  • 举报
回复
mucrea 2010-01-21
  • 打赏
  • 举报
回复
一条sql搞出来难度太大,即使搞出来了估计也是相当耗时的。不如先查询后处理方便。
crazylaa 2010-01-21
  • 打赏
  • 举报
回复
无语,
去oracle开发版,点名要wildwave(狂浪)给你搞定。
bunrise 2010-01-21
  • 打赏
  • 举报
回复
人家前人插树后人乘凉
而我是前任插树我遭殃。。。
我也不想这样搞。
坚持奋斗中1 2010-01-21
  • 打赏
  • 举报
回复
为什么一定要用一条sql语句处理,先处理再对数据库操作或是先查询再处理,不行吗,楼主?
bayougeng 2010-01-21
  • 打赏
  • 举报
回复
一条SQL搞出来?太有难度了。
bunrise 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 feitian124 的回复:]
不用PLSQL怎么保持abc呢,保持在数据库中也要保持多次啊。一条sql怎么搞定?
秀下你的成果,学习下。
[/Quote]
select * from ed_survey t where regexp_like(t.answer, '@@');
with splitData as(
select *
from (select s.questionnum,
s.year,
decode(sign(t1.c_count),
1,
substr(s.answer, 1, t2.c_length),
0,
null,
null) a1,
decode(sign(t1.c_count - 1),
1,
substr(s.answer, t2.c_length + 3, t2.c_length),
0,
substr(s.answer, t2.c_length + 3, t2.c_length),
null) a2,
decode(sign(t1.c_count - 2),
1,
substr(s.answer, t2.c_length + 3, t2.c_length),
0,
substr(s.answer, t2.c_length + 3, t2.c_length),
null) a3
from ed_survey s,
(select t0.questionnum,
(length(t0.answer) -
length(replace(t0.answer, '@@', null))) / 2 c_count
from ed_survey t0
where regexp_like(t0.answer, '@@')) t1,
(select t.questionnum, instr(t.answer, '@@') - 1 c_length
from ed_survey t
where regexp_like(t.answer, '@@')) t2
where s.questionnum = t2.questionnum
and s.questionnum = t1.questionnum) checks), createData as (select d.questionnum,
d.year,
d.a1 answer
from splitData d
union all
select d.questionnum,
d.year,
d.a2 answer
from splitData d
union all
select d.questionnum,
d.year,
d.a3 answer
from splitData d)
select * from createData where answer is not null;



由于现在需求最多只有15个连接,所以我直接写死了
我现在就想和大家讨论下能否把decode和union all这两个地方写活
要不以后需求一变,得改代码
bunrise 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wqnljy 的回复:]
itemValues=split(字段,"@@")
for m=0 to ubound(itemValues)
  if itemValues(m)="" then
  exit for
  end if

canshu=canshu+"∵item∴"+(m+1)+"、"+itemValues(m)
        response.Write(canshu)

next
这是asp中的  改成java试试
[/Quote]

这个在JAVA很好实现的,主要是要用查出来的数据做其他运算和统计,所以希望在数据直接做
wqnljy 2010-01-21
  • 打赏
  • 举报
回复
itemValues=split(字段,"@@")
for m=0 to ubound(itemValues)
if itemValues(m)="" then
exit for
end if

canshu=canshu+"∵item∴"+(m+1)+"、"+itemValues(m)
response.Write(canshu)

next
这是asp中的 改成java试试
feitian124 2010-01-21
  • 打赏
  • 举报
回复
不用PLSQL怎么保持abc呢,保持在数据库中也要保持多次啊。一条sql怎么搞定?
秀下你的成果,学习下。
wanglf1207 2010-01-21
  • 打赏
  • 举报
回复
这个是挺难
liuahuilele 2010-01-21
  • 打赏
  • 举报
回复
????关注
bunrise 2010-01-21
  • 打赏
  • 举报
回复
。。我不太想用存储过程,所以写出来大家讨论一下
我自己写出了。。不过太长了。。。
相当于一个动态SQL的存储过程了。。
不吃鱼的熊 2010-01-21
  • 打赏
  • 举报
回复
一定要“一条SQL”搞掂吗?
道光2008 2010-01-21
  • 打赏
  • 举报
回复
那就是字符串分割嘛,一条sql,估计有难度

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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