sql合并问题

deng1234 2009-02-28 07:32:58
表一: Tbtype: id ,answer ,userId 问题表
1 问题1 1
2 问题2 1
3 问题3 1

表二: Tbuser: userId,userName,content 用户表
1 user 3333

最后的结果是: 1 user 3333 问题1 问题2 问题3

请问这样的sql如何写

...全文
224 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2009-03-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 deng1234 的回复:]
问题要三列,已不确定,要根据userId来确定有多少列.
[/Quote]
[Quote=引用 5 楼 deng1234 的回复:]
最后要三列,除了创建处理函数还有别的方法吗,

我用的是hibernate查询最好是不创建函数,用的是sql2000
[/Quote]
[Quote=引用 7 楼 deng1234 的回复:]
不想用存储过程来写,因为用了hibernate.想用纯对像来操作数据库。
那除了分开来查询估计是没有别的方法了。
[/Quote]

--2000得用函数,2005可以用语句,或是自己使用游标完成,参考如下:

/*
标题:按某字段合并字符串之一(简单合并)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-06
地点:广东深圳

描述:将如下形式的数据按id字段合并value字段。
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id value
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)
*/
--1、sql2000中只能用自定义的函数解决
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


--2、sql2005中的方法
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id

drop table tb


--3、使用游标合并数据
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
declare @t table(id int,value varchar(100))--定义结果集表变量
--定义游标并进行合并处理
declare my_cursor cursor local for
select id , value from tb
declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
open my_cursor
fetch my_cursor into @id , @value
select @id_old = @id , @s=''
while @@FETCH_STATUS = 0
begin
if @id = @id_old
select @s = @s + ',' + cast(@value as varchar)
else
begin
insert @t values(@id_old , stuff(@s,1,1,''))
select @s = ',' + cast(@value as varchar) , @id_old = @id
end
fetch my_cursor into @id , @value
END
insert @t values(@id_old , stuff(@s,1,1,''))
close my_cursor
deallocate my_cursor

select * from @t
drop table tb
伴老思源 2009-03-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 dawugui 的回复:]
引用 4 楼 deng1234 的回复:
问题要三列,已不确定,要根据userId来确定有多少列.

引用 5 楼 deng1234 的回复:
最后要三列,除了创建处理函数还有别的方法吗,

我用的是hibernate查询最好是不创建函数,用的是sql2000

引用 7 楼 deng1234 的回复:
不想用存储过程来写,因为用了hibernate.想用纯对像来操作数据库。
那除了分开来查询估计是没有别的方法了。


--2000得用函数,2005可以用语句,或是自己使用…
[/Quote]
乌龟~
I服了u~!
readfuture 2009-03-02
  • 打赏
  • 举报
回复
帮顶
lzfrab 2009-03-02
  • 打赏
  • 举报
回复
从dawugui那学的,谢谢


if object_id('tb') is not null drop table tb
create table tb (id int,qus varchar(50),uid int)
insert into tb
select 1,'问题1',1 union all
select 2,'问题2',1 union all
select 3,'问题3',1 union all
select 4,'问题4',2

if object_id('tc') is not null drop table tc
create table tc (id int,uname varchar(50),content varchar(100))
insert into tc
select 1,'user','aaa' union all
select 2,'u2','bbb'

--=============
declare @sql varchar(8000)
set @sql = 'select uid , uname '
select @sql = @sql + ' , max(case cn when ''' + cast(cn as varchar) + ''' then qus else '''' end)
[qus' + cast(cn as varchar) + ']'
from (select distinct cn from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a) m
set @sql = @sql + ' from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a
join tc as b on a.uid = b.id group by uid,uname order by uid'
select @sql
exec(@sql)


--->打印出的sql
select uid , uname ,
max(case cn when '1' then qus else '' end) [qus1] ,
max(case cn when '2' then qus else '' end) [qus2] ,
max(case cn when '3' then qus else '' end) [qus3]
from (select *,cn=row_number() over(partition by uid order by uid) from tb) as a
join tc as b on a.uid = b.id group by uid,uname order by uid

--=========结果
1 user 问题1 问题2 问题3
2 u2 问题4
claro 2009-03-01
  • 打赏
  • 举报
回复
帮顶。
ws_hgo 2009-02-28
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090209/08/a945701c-e0d5-40cb-85f2-f4f56ac2999b.html
ChinaJiaBing 2009-02-28
  • 打赏
  • 举报
回复


if OBJECT_ID('表1') is not null
drop table 表1
if OBJECT_ID('表2')is not null
drop table 表2
if OBJECT_ID('c_f')is not null
drop function c_f
go
create table 表1 (id int,answer nvarchar(10),userid int)
insert into 表1 select 1,'问题1',1
union all select 2,'问题2',1
union all select 3,'问题3',1
create table 表2 (userid int,username nvarchar(10),content int)
insert into 表2 select 1,'user',3333
go
create function c_f (@userid int)
returns nvarchar(100)
as
begin
declare @str nvarchar(100)
set @str=''
select @str=@str + ' '+ answer from 表1 a
join 表2 b on a.userid=b.userid
where a.userid=@userid
return @str
end
go
select B.* ,列=dbo.c_f(B.userid) from 表1 a join 表2 B
on a.userid=B.userid
group by b.userid,b.username,b.content
ChinaJiaBing 2009-02-28
  • 打赏
  • 举报
回复


if OBJECT_ID('表1') is not null
drop table 表1
if OBJECT_ID('表2')is not null
drop table 表2
if OBJECT_ID('c_f')is not null
drop function c_f
go
create table 表1 (id int,answer nvarchar(10),userid int)
insert into 表1 select 1,'问题1',1
union all select 2,'问题2',1
union all select 3,'问题3',1
create table 表2 (userid int,username nvarchar(10),content int)
insert into 表2 select 1,'user',3333
go
create function c_f (@userid int)
returns nvarchar(100)
as
begin
declare @str nvarchar(100)
set @str=''
select @str=@str + ' '+ answer from 表1 a
--join 表2 b on a.userid=b.userid
where a.userid=@userid
--group by a.userid
return stuff(@str,1,1,'')
end
go
select top 1 B.* ,列=dbo.c_f(A..id) from 表1 a join 表2 B
on a.userid=B.userid
userid username content 列
----------- ---------- ----------- ----------------------------------------------------------------------------------------------------
1 user 3333 问题1 问题2 问题3

(1 行受影响)

you_tube 2009-02-28
  • 打赏
  • 举报
回复
行列转换,精华贴里有
-狙击手- 2009-02-28
  • 打赏
  • 举报
回复
合并行
Andy__Huang 2009-02-28
  • 打赏
  • 举报
回复
行列转换,搜索一下有很多老帖子的解决方法都可以参考
这种已经是很简单的问题了

deng1234 2009-02-28
  • 打赏
  • 举报
回复
不想用存储过程来写,因为用了hibernate.想用纯对像来操作数据库。
那除了分开来查询估计是没有别的方法了。
liangCK 2009-02-28
  • 打赏
  • 举报
回复
动态的话..要用存储过程来做.
deng1234 2009-02-28
  • 打赏
  • 举报
回复
最后要三列,除了创建处理函数还有别的方法吗,

我用的是hibernate查询最好是不创建函数,用的是sql2000
deng1234 2009-02-28
  • 打赏
  • 举报
回复
问题要三列,已不确定,要根据userId来确定有多少列.
liangCK 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 deng1234 的帖子:]
表一: Tbtype: id ,answer ,userId 问题表
1 问题1 1
2 问题2 1
3 问题3 1

表二: Tbuser: userId,userName,content 用户表
1 user 3333

最后的结果是: 1 user 3333 问题1 问题2 问题3
请问这样的sql如何写
[/Quote]

这是三个列还是一个列?..

如果是一个列.则根据2楼的合并列去写..
如果是三个列.则使用行转列.
liangCK 2009-02-28
  • 打赏
  • 举报
回复
问题描述:
无论是在sql 2000,还是在 sql 2005 中,都没有提供字符串的聚合函数,
所以,当我们在处理下列要求时,会比较麻烦:
有表tb, 如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即, group by id, 求 value 的和(字符串相加)

1. 旧的解决方法

-- 1. 创建处理函数
CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value
FROM tb
WHERE id=@id
RETURN STUFF(@r, 1, 1, '')
END
GO
-- 调用函数

SELECt id, values=dbo.f_str(id)
FROM tb
GROUP BY id

-- 2. 新的解决方法(适用于2005及以后版本)
-- 示例数据
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'

-- 查询处理
SELECT *
FROM(
SELECT DISTINCT
id
FROM @t
)A
OUTER APPLY(
SELECT
[values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM @t N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N

/*--结果
id values
----------- ----------------
1 aa,bb
2 aaa,bbb,ccc
(2 行受影响)
--*/

CSDN 社区帖子地址

附: 合并与分拆的CLR, sql2005的示例中有:
在安装sql 2005的示例后,默认安装目录为
drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\StringUtilities中
liangCK 2009-02-28
  • 打赏
  • 举报
回复
2000?2005?

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧