我通过语句查询
select *,
(stuff((SELECT ','+b.[typename] from dj_table2 b
where b.supportingTypeID=a.supportingTypeID and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%' FOR xml path('')), 1, 1, '')
)
from dj_table1 a
得到的结果是
其中红色的部分输出就出现错误了,麻烦大神们帮忙看看
其中生成表的语句:
CREATE TABLE [dbo].[dj_table1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[typeID] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[supportingtypeID] [int] NULL,
CONSTRAINT [PK_dj_table1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
go
CREATE TABLE [dbo].[dj_table2](
[typeID] [int] IDENTITY(1,1) NOT NULL,
[typeName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[supportingtypeId] [int] NULL,
CONSTRAINT [PK_dj_table2] PRIMARY KEY CLUSTERED
(
[typeID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
7 个解决方案
#1
改成这样试试:
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and charindex(','+cast(b.typeID AS nvarchar(10))+',',','+a.typeID+',')>0 FOR xml path('')), 1, 1, '') ) from dj_table1 a
#2
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and ','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '') ) from dj_table1 a
逗号的地方注意
#3
嘿嘿,这么写可以,谢谢
#4
其实,你的问题在于:
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'
select *,
(stuff((SELECT ','+b.[typename] from dj_table2 b
where b.supportingTypeID=a.supportingTypeID
and
','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '')
)
from dj_table1 a
#5
我也知道
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'这里使用Like有问题,只要重叠的数字,都会把个位的信息也查出来,但是这种用到多个函数的时候,就容易晕了
谢谢大师的指导
#6
谢谢帮忙解决问题
#7
呵呵,确实容易晕,不过多看看就习惯了
#1
改成这样试试:
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and charindex(','+cast(b.typeID AS nvarchar(10))+',',','+a.typeID+',')>0 FOR xml path('')), 1, 1, '') ) from dj_table1 a
#2
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and ','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '') ) from dj_table1 a
逗号的地方注意
#3
改成这样试试:
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and charindex(','+cast(b.typeID AS nvarchar(10))+',',','+a.typeID+',')>0 FOR xml path('')), 1, 1, '') ) from dj_table1 a
嘿嘿,这么写可以,谢谢
#4
其实,你的问题在于:
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'
select *,
(stuff((SELECT ','+b.[typename] from dj_table2 b
where b.supportingTypeID=a.supportingTypeID
and
','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '')
)
from dj_table1 a
#5
其实,你的问题在于:
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'
select *,
(stuff((SELECT ','+b.[typename] from dj_table2 b
where b.supportingTypeID=a.supportingTypeID
and
','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '')
)
from dj_table1 a
我也知道
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'这里使用Like有问题,只要重叠的数字,都会把个位的信息也查出来,但是这种用到多个函数的时候,就容易晕了
谢谢大师的指导
#6
select *, (stuff((SELECT ','+b.[typename] from dj_table2 b where b.supportingTypeID=a.supportingTypeID and ','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '') ) from dj_table1 a
逗号的地方注意
谢谢帮忙解决问题
#7
其实,你的问题在于:
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'
select *,
(stuff((SELECT ','+b.[typename] from dj_table2 b
where b.supportingTypeID=a.supportingTypeID
and
','+a.typeID+',' LIKE '%,'+cast(b.typeID AS nvarchar(10))+',%' FOR xml path('')), 1, 1, '')
)
from dj_table1 a
我也知道
and a.typeID LIKE '%'+cast(b.typeID AS nvarchar(10))+'%'这里使用Like有问题,只要重叠的数字,都会把个位的信息也查出来,但是这种用到多个函数的时候,就容易晕了