select task_type, (select task_name from tvlwf_task_type d where b.task_type=d.task_type)
task_type_name from tvlwf_duty_skill b where group_id=10
现在效果是这样
1 KKKK_001 Assign Optical Path
2 GGGB_001 Survey
我要这样的效果
1 KKKK_001,GGGB_001 Assign Optical Path,Survey
合并成一行 同一列用逗号隔开
sql合并行问题
答案:3 悬赏:10 手机版
解决时间 2021-01-03 10:37
- 提问者网友:半生酒醒
- 2021-01-03 02:03
最佳答案
- 五星知识达人网友:等灯
- 2021-01-10 03:18
得用存储过程了,里面用一个游标。
create procedure MergeRows as
declare @type varchar(50),@name varchar(50)
declare @r1 varchar(90000),@r2 varchar(90000)
declare @c cursor
select @r1='',@r2=''
set @c=cursor forward_only read_only for select task_type, (select task_name from tvlwf_task_type d where b.task_type=d.task_type)
task_type_name from tvlwf_duty_skill b where group_id=10
open @c
fetch next from @c into @type,@name
while @fetch_status=0
begin
select @r1=@r1+','+@type,@r2=@r2+','+@name
fetch next from @c into @type,@name
end
close @c
deallocate @c
select substring(@r1,2,len(@r1-1)),substring(@r2,2,len(@r2-1))
create procedure MergeRows as
declare @type varchar(50),@name varchar(50)
declare @r1 varchar(90000),@r2 varchar(90000)
declare @c cursor
select @r1='',@r2=''
set @c=cursor forward_only read_only for select task_type, (select task_name from tvlwf_task_type d where b.task_type=d.task_type)
task_type_name from tvlwf_duty_skill b where group_id=10
open @c
fetch next from @c into @type,@name
while @fetch_status=0
begin
select @r1=@r1+','+@type,@r2=@r2+','+@name
fetch next from @c into @type,@name
end
close @c
deallocate @c
select substring(@r1,2,len(@r1-1)),substring(@r2,2,len(@r2-1))
全部回答
- 1楼网友:过活
- 2021-01-10 04:32
是MySQL吗? 如果是的话很方便,已经测试,保证好用
select GROUP_CONCAt(a.task_typeandname SEPARATOR ' ')
from (
select GROUP_CONCAt(task_type) task_typeandname from tvlwf_duty_skill where group_id=10
union all
select (select GROUP_CONCAt(task_name) from tvlwf_task_type d where b.task_type=d.task_type) task_typeandname
from tvlwf_duty_skill b where group_id=10
) as a
如果是sql server,它没有GROUP_CONCAT这个函数,请参考:
http://topic.csdn.net/t/20060509/14/4738795.html
- 2楼网友:冷風如刀
- 2021-01-10 03:40
只是想合并行的话,各个表的字段数和每个字段的数据类型要一样,
然后用union合并显示,例如:
select * from a union select * from b
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯