(两种结构,两种方式)根据主表ID,查询子表的信息并用逗号分隔 - MS-SQL Server
2011-09-09 16:13
399 查看
结构一
根据主表ID,查询子表的信息并用逗号分隔
详情如下:
1主表:
select * from subcom
ID Name
1 总公司
2 公司1
3 公司2
2子表
select * from SubcomEarningOrg
ID SubCom EarningOrg
1 1 258
2 1 260
3 1 262
4 1 263
5 1 265
6 1 266
7 1 267
8 1 268
9 1 271
10 1 272
11 2 408
12 3 422
其中subcom 为主表的ID ,EarningOrg为另外一张基础表的ID。
基础表
select * from BaseData
ID Name
258 一车队
260 二车队
262 三车队
263 四车队
265 五车队
266 六车队
267 七车队
268 八车队
271 九车队
272 十车队
现在要求查询出主表所对应的子表的所有在一条主表对应的记录并用逗号隔开如
主表Name 基础Name
总公司 一车队,二车队,三车队,四车队,五车队,六车队,七车队,八车队,九车队,十车队
答: 这样来实现
create table t(id int,name nvarchar(30))
create table t1(id int,SubCom int, EarningOrg int)
create table t2(id int, name nvarchar(30))
insert into t
select 1, '总公司' union all
select 2, '公司1' union all
select 3, '公司2'
insert into t1
select 1, 1, 258 union all
select 2, 1 ,260 union all
select 3, 1 ,262 union all
select 4, 1 ,263 union all
select 5, 1 ,265 union all
select 6, 1 ,266 union all
select 7, 1 ,267 union all
select 8, 1 ,268 union all
select 9, 1 ,271 union all
select 10, 1, 272 union all
select 11, 2, 408 union all
select 12, 3, 422
insert into t2
select 258, '一车队' union all
select 260, '二车队' union all
select 262, '三车队' union all
select 263, '四车队' union all
select 265, '五车队' union all
select 266, '六车队' union all
select 267, '七车队' union all
select 268, '八车队' union all
select 271, '九车队' union all
select 272, '十车队'
create function gY(@id as int)
returns nvarchar(100)
as
begin
declare @s nvarchar(100)
set @s=''
select @s=@s+ISNULL(t2.name+',','') from t1,t2 where t1.SubCom=@id and t1.EarningOrg=t2.id
IF @s<>''
return substring(@s,1,len(@s)-1)
return ''
end
go
select name,基础name=dbo.gY(id) from t where dbo.gY(id)<>''
drop table t
drop table t1
drop table t2
结构二
根据主表ID,查询子表的信息并用逗号分隔
详情如下:
1主表:
select * from subcom
ID Name subid
1 总公司 258,260,262
2 公司1 408
3 公司2 422
2子表
select * from BaseData
ID Name
258 一车队
260 二车队
262 三车队
263 四车队
265 五车队
266 六车队
267 七车队
268 八车队
271 九车队
272 十车队
现在要求查询出主表所对应的子表的所有在一条主表对应的记录并用逗号隔开如
主表Name 基础Name
总公司 一车队,二车队,三车队
SQL函数实现
/*
详细设计构思:http://www.cnblogs.com/chengulv/archive/2011/09/09/2172634.html
根据主键串 获得用逗号分割的其他字段内容。(如:总经理,业务员,仓库)
*/
CREATE PROCEDURE [dbo].[sp_comma]
@r nvarchar(4000) output,
@showField nvarchar(255), --显示的字段名 如: 'groupName'
@tableName nvarchar(255), --表名 如: 's_adminGroup'
@whereField nvarchar(255), --条件字段名 如: 'iid'
@whereValue nvarchar(255), --条件字段值 如: '2,7'
@split nvarchar(2) = ','
AS
declare @sql nvarchar(4000)
set @sql = 'set @g='''';select @g = @g + isnull(' + @showField + ' + ''' + @split + ''','''') from '+ @tableName + ' where '+ @whereField + ' in ('+ @whereValue + ');if @g<>'''' set @g = substring(@g,1,len(@g)-1)'
exec sp_executesql @sql, N'@g nvarchar(2000) output', @r output
--if @gname<>'' set @gname = substring(@gname,1,len(@gname)-1)
GO
根据主表ID,查询子表的信息并用逗号分隔
详情如下:
1主表:
select * from subcom
ID Name
1 总公司
2 公司1
3 公司2
2子表
select * from SubcomEarningOrg
ID SubCom EarningOrg
1 1 258
2 1 260
3 1 262
4 1 263
5 1 265
6 1 266
7 1 267
8 1 268
9 1 271
10 1 272
11 2 408
12 3 422
其中subcom 为主表的ID ,EarningOrg为另外一张基础表的ID。
基础表
select * from BaseData
ID Name
258 一车队
260 二车队
262 三车队
263 四车队
265 五车队
266 六车队
267 七车队
268 八车队
271 九车队
272 十车队
现在要求查询出主表所对应的子表的所有在一条主表对应的记录并用逗号隔开如
主表Name 基础Name
总公司 一车队,二车队,三车队,四车队,五车队,六车队,七车队,八车队,九车队,十车队
答: 这样来实现
create table t(id int,name nvarchar(30))
create table t1(id int,SubCom int, EarningOrg int)
create table t2(id int, name nvarchar(30))
insert into t
select 1, '总公司' union all
select 2, '公司1' union all
select 3, '公司2'
insert into t1
select 1, 1, 258 union all
select 2, 1 ,260 union all
select 3, 1 ,262 union all
select 4, 1 ,263 union all
select 5, 1 ,265 union all
select 6, 1 ,266 union all
select 7, 1 ,267 union all
select 8, 1 ,268 union all
select 9, 1 ,271 union all
select 10, 1, 272 union all
select 11, 2, 408 union all
select 12, 3, 422
insert into t2
select 258, '一车队' union all
select 260, '二车队' union all
select 262, '三车队' union all
select 263, '四车队' union all
select 265, '五车队' union all
select 266, '六车队' union all
select 267, '七车队' union all
select 268, '八车队' union all
select 271, '九车队' union all
select 272, '十车队'
create function gY(@id as int)
returns nvarchar(100)
as
begin
declare @s nvarchar(100)
set @s=''
select @s=@s+ISNULL(t2.name+',','') from t1,t2 where t1.SubCom=@id and t1.EarningOrg=t2.id
IF @s<>''
return substring(@s,1,len(@s)-1)
return ''
end
go
select name,基础name=dbo.gY(id) from t where dbo.gY(id)<>''
drop table t
drop table t1
drop table t2
结构二
根据主表ID,查询子表的信息并用逗号分隔
详情如下:
1主表:
select * from subcom
ID Name subid
1 总公司 258,260,262
2 公司1 408
3 公司2 422
2子表
select * from BaseData
ID Name
258 一车队
260 二车队
262 三车队
263 四车队
265 五车队
266 六车队
267 七车队
268 八车队
271 九车队
272 十车队
现在要求查询出主表所对应的子表的所有在一条主表对应的记录并用逗号隔开如
主表Name 基础Name
总公司 一车队,二车队,三车队
SQL函数实现
/*
详细设计构思:http://www.cnblogs.com/chengulv/archive/2011/09/09/2172634.html
根据主键串 获得用逗号分割的其他字段内容。(如:总经理,业务员,仓库)
*/
CREATE PROCEDURE [dbo].[sp_comma]
@r nvarchar(4000) output,
@showField nvarchar(255), --显示的字段名 如: 'groupName'
@tableName nvarchar(255), --表名 如: 's_adminGroup'
@whereField nvarchar(255), --条件字段名 如: 'iid'
@whereValue nvarchar(255), --条件字段值 如: '2,7'
@split nvarchar(2) = ','
AS
declare @sql nvarchar(4000)
set @sql = 'set @g='''';select @g = @g + isnull(' + @showField + ' + ''' + @split + ''','''') from '+ @tableName + ' where '+ @whereField + ' in ('+ @whereValue + ');if @g<>'''' set @g = substring(@g,1,len(@g)-1)'
exec sp_executesql @sql, N'@g nvarchar(2000) output', @r output
--if @gname<>'' set @gname = substring(@gname,1,len(@gname)-1)
GO
相关文章推荐
- SQL查询ID以逗号分隔,id对应的详细信息
- oracle wm_concat(column)函数实现查询相同id字段,内容以逗号分隔
- sql server 表结构信息查询
- mysql 查询某个字段信息,由多个id逗号隔开拼装成
- SQL SERVER 表结构信息查询(详细)
- sql,scope_identity,procedure,tran,substring,cast,convert,charindex,插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的id,更新角色信息,同时更新权限信息
- 查询表中一个字段用逗号分隔的结果集,sql server 和 mysql的写法区别
- sql server 查询所有表的字段的详细信息的存储过程(在 SQL Server 2005 中查询表结构及索引 )
- Sql Server 查询表结构和索引信息
- Android根据图片文件名获取它的资源ID 的两种方式
- 查询表结构和索引信息 Sql Server
- 在非SQL客户端使用命令行方式定期连接SQL Server 服务器并模拟用户查询操作,同时输出信息内容
- 根据ID集合查询符合某种类型的用户信息,并按其他类型分类
- sql server 获得指定表结构详细信息 可根据表生成文本框等应用
- SSH框架利用注解方式实现根据多条件动态分页查询图书信息(传对象)
- 表结构信息查询 sql server 2005
- Android根据资源文件名获取它的资源ID 的两种方式
- Mybatis根据id查询用户信息
- 查询SQL SERVER表结构信息
- MyBatis一对一关系映射:根据班级id查询班级信息并查出辅导员的信息