sql 多表连接 条件列值不等

yinxubest87 2009-12-23 04:50:54
表1的数据为:
a b
001 1
002 2
003 3

表2的数据为:

c d
我 [1 2]
们 [1 2]
你们 [2 3]

我要出来 的数据 为
c d a
我 [1 2] 001
我 [1 2] 002
们 [1 2] 001
等。。。。。。

即 表1 的列 b 值 只要被包含于 表2 的 d 值 则列为等值连接 ?

请实现,谢谢!

...全文
246 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuengia 2009-12-24
  • 打赏
  • 举报
回复
收藏了,感谢分享
yinxubest87 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wypc520 的回复:]
SQL codeSELECT C,D,AFROM 表1,表2WHERE 表2.DLIKE'[%'+B+' %]'OR 表2.DLIKE'[%'+B+']'
[/Quote]


你这个  加了  [ 这种半边括号的特殊字符 会有错误 ?
Terry717 2009-12-23
  • 打赏
  • 举报
回复
4楼 的写法不错 帮顶!
yinxubest87 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wypc520 的回复:]
引用 8 楼 yinxubest87 的回复:
引用 1 楼 herowang 的回复:
select *
from tb1 join tb2 on d like '%'+b+'%'


对的,而且简单!


12 和 1或者2 会有错误
[/Quote]

哦,YES,呵呵,谢谢!

破折号 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yinxubest87 的回复:]
引用 1 楼 herowang 的回复:
select *
from tb1 join tb2 on d like '%'+b+'%'



对的,而且简单!
[/Quote]

12 和 1或者2 会有错误
yinxubest87 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 herowang 的回复:]
select *
from tb1 join tb2 on d like '%'+b+'%'
[/Quote]


对的,而且简单!
chuifengde 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chuifengde 的回复:]
SQL codeSELECT c,d,aFROM
a, bWHEREcharindex(','+ltrim(b)+',',replace(replace(replace(d,'[',','),'',','),']',''))>0
[/Quote]
丢了个逗号
SELECT c,d,aFROM 
a, bWHEREcharindex(','+ltrim(b)+',',replace(replace(replace(d,'[',','),'',','),']',','))>
破折号 2009-12-23
  • 打赏
  • 举报
回复

SELECT C,D,A FROM 表1,表2 WHERE 表2.D LIKE '[%'+B+' %]' OR 表2.D LIKE '[% '+B+']'
--小F-- 2009-12-23
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-23 16:58:40
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([a] varchar(3),[b] int)
insert [a]
select '001',1 union all
select '002',2 union all
select '003',3
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([c] varchar(4),[d] varchar(20))
insert [b]
select '我','[1 2]' union all
select '们','[1 2]' union all
select '你们','[2 3]'
--------------开始查询--------------------------
select
c,d,a
from
a, b
where
charindex(','+ltrim(b)+',',replace(replace(replace(d,'[',','),' ',','),']',''))>0
----------------结果----------------------------
/* c d a
---- -------------------- ----
我 [1 2] 001
们 [1 2] 001
你们 [2 3] 002

(3 行受影响)

*/
dawugui 2009-12-23
  • 打赏
  • 举报
回复
create table t1(a varchar(10), b int)
insert into t1 values('001' , 1 )
insert into t1 values('002' , 2 )
insert into t1 values('003' , 3 )
create table t2(c varchar(10), d varchar(10))
insert into t2 values('我' , '[1 2]')
insert into t2 values('们' , '[1 2]')
insert into t2 values('你们', '[2 3]')
go

select distinct t2.c , t2.d , t1.a from t2 , t1 where charindex(' ' + cast(t1.b as varchar) + ' ' , ' ' + substring(t2.d,2,len(t2.d)-2) + ' ') > 0
/*
c d a
---------- ---------- ----------
们 [1 2] 001
们 [1 2] 002
你们 [2 3] 002
你们 [2 3] 003
我 [1 2] 001
我 [1 2] 002

(所影响的行数为 6 行)
*/


select distinct t2.c , t2.d , t1.a from t2 , t1 where ' ' + substring(t2.d,2,len(t2.d)-2) + ' ' like '% ' + cast(t1.b as varchar) + ' %'
/*
c d a
---------- ---------- ----------
们 [1 2] 001
们 [1 2] 002
你们 [2 3] 002
你们 [2 3] 003
我 [1 2] 001
我 [1 2] 002

(所影响的行数为 6 行)
*/
drop table t1 , t2
chuifengde 2009-12-23
  • 打赏
  • 举报
回复
SELECT c,d,a FROM 
a, b
WHERE charindex(','+ltrim(b)+',',replace(replace(replace(d,'[',','),' ',','),']',''))>0
dawugui 2009-12-23
  • 打赏
  • 举报
回复
select t2.c , t2.d , t1.a from t2 , t1 where charindex(' ' + t1.b + ' ' , ' ' + substring(t2.d,2,len(t2.d)-2) + ' ') > 0
  • 打赏
  • 举报
回复
select *
from tb1 join tb2 on d like '%'+b+'%'

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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