♣
求一SQL语句的简单问题,不但少人来,给的答案也不对,再加100分,希望真高手和负责的人来看看,谢谢
原贴在:
http://community.csdn.net/expert/topic/4200/4200272.xml?temp=.3735468
id content
1 aa
2 bb
3 cc
1 dd
2 ee
3 ff
按照id分组,把content的内容合并起来(中间加上","),结果如下:
id content
1 aa,dd
2 bb,ee
3 cc,ff
用一句sql怎么写??
我想到 select ?? from table group by id,关键是??怎么写呢??
· 网友精彩回答:
create function f_str(@id int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret =
select @ret = @ret + , + contentn from table
set @ret = stuff(@ret,1,1,)
return @ret
end
select id,dbo.f_str(id) from table group by id
测试数据
create table q
(id int,
content varchar(20))
insert q
select 1,aa
union
select 2,bb
union
select 3,cc
union
select 1,dd
union
select 2,ee
union
select 3,ff
--------------
自定义函数
create function getc(@id int)
returns nvarchar(2000)
as
begin
declare @sql nvarchar(4000)
set @sql=
select @sql=@sql+,+content from q where id=@id
return (stuff(@sql,1,1,))
end
----------
执行语句
select distinct id,dbo.getc(id) as content from q
---------
执行结果
id content
1 aa,dd
2 bb,ee
3 cc,ff
楼上的很详细了。
这是类似细目表合成为合成表的问题,目前通用的时这种处理方法。
相对应的合成表转化为细目表。就是由这个结果返回成原来的记录,处理方法为:增加带自增型id的辅助表,来实现。
create function f1(@a int)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=
select @r=@r+,+content from table1 where id=@a
return(stuff(@r,1,1,))
end
go
--调用实现查询
select id,content=dbo.f1(id) from table1 group by id
估计是ms sql没有这样的函数,就只有自己定义一个了,mysql就有一个:
group_concat(expr)
完整句法如下:
group_concat([distinct] expr [,expr ...]
[order by {unsigned_integer | col_name | formula} [asc | desc] [,col ...]]
[separator str_val])
这个函数在 mysql 4.1 中被加入。函数返回一个字符串结果,该结果由分组中的值连接组合而成:
mysql> select student_name,
-> group_concat(test_score)
-> from student
-> group by student_name;
or
mysql> select student_name,
-> group_concat(distinct test_score
-> order by test_score desc separator " ")
-> from student
-> group by student_name;
在 mysql 中,你可以得到表达式结合体的连结值。通过使用 distinct 可以排除重复值。如果希望对结果中的值进行排序,可以使用 order by 子句。为了以倒序排序,可以在 order by 子句中用于排序的列名后添加一个 desc (递减 descending) 关键词。缺省为升序;这也可以通过使用 asc 关键词明确指定。 separator 是一个字符串值,它被用于插入到结果值中。缺省为一个逗号 (",")。你可以通过指定 separator "" 完全地移除这个分隔符。 在你的配置中,通过变量 group_concat_max_len 要以设置一个最大的长度。在运行时执行的句法如下:
set [session | global] group_concat_max_len = unsigned_integer;
如果最大长度被设置,结果值被剪切到这个最大长度。 group_concat() 函数是一个增强的 sybase sql anywhere 支持的基本 list() 函数。如果只有一个列,并且没有其它选项被指定,group_concat() 是向后兼容有极大限制的 list() 函数。 list() 有一个缺省的排序次序。
示例(译者注):
mysql> create table `ta` (
-> `id` smallint(5) unsigned not null default 0,
-> `name` char(60) not null default ,
-> key `id` (`id`)
-> ) type=myisam;
query ok, 0 rows affected (0.02 sec)
mysql> insert into `ta` values("1", "a"),("1", "b"),
-> ("1", "c"),("1", "d"),("2", "a"),
-> ("2", "b"),("2", "c"),("3", "d");
query ok, 8 rows affected (0.03 sec)
records: 8 duplicates: 0 warnings: 0
mysql> select * from `ta`;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 1 | b |
| 1 | c |
| 1 | d |
| 2 | a |
| 2 | b |
| 2 | c |
| 3 | d |
+----+------+
8 rows in set (0.00 sec)
mysql> select `id`,
-> group_concat(`name`)
-> from `ta`
-> group by `id`;
+----+----------------------+
| id | group_concat(`name`) |
+----+----------------------+
| 1 | a c b d |
| 2 | a c b |
| 3 | d |
+----+----------------------+
3 rows in set (0.03 sec)
# separator 缺省是一个空格而不是一个逗号
mysql> select `id`,
-> group_concat(distinct `name`
-> order by `name` desc separator ",") as result
-> from `ta`
-> group by `id`;
+----+---------+
| id | result |
+----+---------+
| 1 | d,c,b,a |
| 2 | c,b,a |
| 3 | d |
+----+---------+
3 rows in set (0.00 sec)
* 以上结果在 mysql 4.1 中测试
示例结束(译者注)
我写出来以后能给分吗?
表名@s,字段和内容相同
select id1 as [id],content1+,+content2 as content from
(select a.[id] as id1,a.content as content1,b.[id] as id2,b.content as content2 from @s a,@s b where a.[id]=b.[id]) as m
where id1=id2 and content1<content2
测试结果为:
id content
----------- ---------
1 aa,dd
2 bb,ee
3 cc,ff
用一句写不出来. 除非每组 id 的 content 是有限且已知的.
.- 更多问题:
- · 关于域名与WEB应用映射的问题
- · 请问我的ASPX文件中的循环语句为什么不会执行到?
- · [perl]关于STDERR一问
- · 求 com原理与应用 一书代码 送20分 :)
- · 请问如何让页面缓存超过5分钟?
- · [java新手求教]公司中一般搭建怎么样的环境来开发J2EE项目?
- · 高分求 众智科技 企业标识
- · 一个关于域的问题
- · 借这里的人气求一条SQL语句。
- · 请教一个关于OnPaint()的问题
- · 求购OA系统原代码
- · 高分求 众智科技 企业标识
- · 各位大哥,请问这到底是什么原因呀,我快要疯了!
- · 乱码-比较辣手的问题!!百度\GOOGLE\CSDN搜了三天都没有找到解决办法!!!!
- · toolbar的一个很奇怪的问题,请大家帮忙看看。
- · 小弟我在学习过程中,从来没有见到过这样奇怪的问题,我真的想不通了

