SQL left outer join的问题。

yajun_snow 2011-02-12 10:49:08
table a
Name Spec
001 1;2;3;4;5;6
002 1;2;3;4;5;6
003 1;3;4;5;6

table b

id value
1 a001
2 a002
3 a003
4 a004
5 a005
6 a006

a.Spec 对应 b.id,可以一对多,';’隔开

怎么查询到
Name=001,所对应的value的数据、

即得到
Name value
001 a001
001 a002
001 a003
001 a004
001 a005
001 a006

...全文
70 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2011-02-12
  • 打赏
  • 举报
回复
select a.name , b.value from a , b, where a.name = '001' and charindex(';' + cast(b.id as varchar) + ';' , ';' + a.spec + ';') > 0

select a.name , b.value from a , b, where a.name = '001' and ';' + a.spec + ';' like '%;' + cast(b.id as varchar) + ';%'
快溜 2011-02-12
  • 打赏
  • 举报
回复

select a.name,b.value from a left join b on 1=1
where charindex(';' + rtrim(b.id) + ';',';' + a.spec + ';') > 0
打一壶酱油 2011-02-12
  • 打赏
  • 举报
回复


select a.name,b.value from a,b
where charindex(';' + b.id + ';',';' + a.spec + ';') > 0
yajun_snow 2011-02-12
  • 打赏
  • 举报
回复
希望有比较高效的写法,谢谢、、

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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