数据查询的SQL

密码测试 2009-10-16 03:58:02
product表中有个varchar2型字段: delivery_ids,里面放置的是另一个表delivery的主键列表,数据格式如下:
,1,2,3,4,11,
现在我要根据delivery_ids来关联查询delivery的信息。
该如何写这个SQL语句?

我现在这样写,但是不行:
select *
from delivery d
where to_char(d.id) in
(select REPLACE('''0 ' || t.support_dm || ' 0''', ',', ''', ''')
from product t
where t.id = 901);

子查询
select REPLACE('''0 ' || t.support_dm || ' 0''', ',', ''', ''')
from product t
where t.id = 901
的结果是:'0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ' 0'
但是,oracle把它看作一个字符串了。所以主查询是无法查询到数据的。
各位大虾给个方案,谢了!
...全文
92 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bw555 的回复:]
SQL codeselect d.*from delivery d ,product twhere instr( t.support_dm,','||d.id||',')>0and t.id=901;

[/Quote]
最好不要用表关联,如果d的一条记录会对应t表的多条记录,结果将会出现重复
bw555 2009-10-16
  • 打赏
  • 举报
回复
select d.* 
from delivery d ,product t
where instr( t.support_dm,','||d.id||',')>0
and t.id = 901;

archwuke1 2009-10-16
  • 打赏
  • 举报
回复
select *
from delivery d
where instr(select REPLACE('''0 ' || t.support_dm || ' 0''', ',', ''', ''')
from product t
where t.id = 901),','||to_char(d.id)||',',1)>0
小灰狼W 2009-10-16
  • 打赏
  • 举报
回复
我晕,看错了,引号是你查询中自己加的啊
那就用1楼的代码
小灰狼W 2009-10-16
  • 打赏
  • 举报
回复
select * from delivery d
where exists(select 1 from product t
where instr(delivery_ids,','''||d.id||''',')>0
and id=901)
小灰狼W 2009-10-16
  • 打赏
  • 举报
回复
select * from delivery d
where exists(select 1 from product t
where instr(delivery_ids,','||d.id||',')>0
and id=901)

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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