日期:2014-05-17 浏览次数:20446 次
CREATE TABLE [dbo].[Table_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[StrA] [nvarchar](50) NULL,
[StrB] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO Table_A(StrA,StrB)VALUES('A','A')
INSERT INTO Table_A(StrA,StrB)VALUES('B','B')
INSERT INTO Table_A(StrA,StrB)VALUES('C','C')
INSERT INTO Table_A(StrA,StrB)VALUES('D','D')
CREATE TABLE [dbo].[Table_B](
[ID] [int] IDENTITY(1,1) NOT NULL,
[A] [nvarchar](50) NULL,
[B] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO Table_B(A,B) VALUES('A,B','AB')
INSERT INTO Table_B(A,B) VALUES('A,B,C','ABC')
INSERT INTO Table_B(A,B) VALUES('A,B,D','ABD')
INSERT INTO Table_B(A,B) VALUES('A,B,C,D','ABCD')
我想要的结果是:
select * from Table_A where StrA in (select A from Table_B where B='ABCD')
--显示出Table_A的四条数据。请高手指点。
select * from table_A a ,table_B b where charindex(','+a.StrA+',',','+b.a+',')>0
CREATE TABLE [dbo].[Table_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[StrA] [nvarchar](50) NULL,
[StrB] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO Table_A(StrA,StrB)VALUES('A','A')
INSERT INTO Table_A(StrA,StrB)VALUES('B','B')
INSERT INTO Table_A(StrA,StrB)VALUES('C','C')
INSERT INTO Table_A(StrA,StrB)VALUES('D','D')
CREATE TABLE [dbo].[Table_B](
[ID] [int] IDENTITY(1,1) NOT NULL,
[A] [nvarchar](50) NULL,
[B] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO Table_B(A,B) VALUES('A,B','AB')
INSERT INTO Table_B(A,B) VALUES('A,B,C','ABC')
INSERT INTO Table_B(A,B) VALUES('A,B,D','ABD')
INSERT INTO Table_B(A,B) VALUES('A,B,C,D','ABCD')
--我想要的结果是:
select * from Table_A where CHARINDEX(StrA,(select A from Table_B where B='ABCD'))>0
--显示出Table_A的四条数据。请高手指点。
/*
ID StrA StrB
--------------------------
1 A A
2 B B
3 C C
4 D D
*/
CREATE TABLE [dbo].[Table_A](
[ID] [int] IDENTITY(1,1) NOT NULL,
[StrA] [nvarchar](50) NULL,
[StrB] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO Table_A(StrA,StrB)VALUES('A','A')
INSERT INTO Table_A(StrA,StrB)VALUES('B','B')
INSERT INTO Table_A(StrA,StrB)VALUES('C','C')
INSERT INTO Table_A(StrA,StrB)VALUES('D','D')
CREATE TABLE [dbo].[Table_B](
[ID] [int] IDENTI