1.TREE表:
[strID] [int] IDENTITY(1,1) NOT NULL,
[strName] [nvarchar](50) NOT NULL,
2.SubInfo
CREATE TABLE [dbo].[SubInfo](
[SuBID] [int] NOT NULL,
[SubTreeID] [nvarchar](max) NULL,
SQL语句为:
select a.strID,a.strName from Tree as a where exists(select * from SubInfo where charindex(cast(a.strID as varchar(max))+',',SubTreeID)>0)
这里红色加的分号必须要加上,不然的花,如果是215,连21也会区配.
查询后结果为:
如果SubTreeID:1,2,3,215则select a.strID,a.strName from Tree as a where exists(select * from SubInfo where charindex(cast(a.strID as varchar(max)),SubTreeID)>0)
查询结果为
本来21为思茅的是不应该查询出来的,所以要叫逗号来处理
4.select a.strID,a.strName,b.* from Tree as a join SubInfo as b on charindex(cast(a.strID as varchar(max))+',',SubTreeID)>0
这个是把SubInfo 表的数据也显示出来
结果为: