《sql逗号分开取字符串方法.docx》由会员分享,可在线阅读,更多相关《sql逗号分开取字符串方法.docx(2页珍藏版)》请在三一办公上搜索。
1、sql逗号分开取字符串方法sql字段为逗号分开的字符串值的关联查询 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.
2、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 表的数据也显示出来 结果为: