用SQL语句,如何把一列的数据横着显示??

spring0906 2009-02-20 04:56:22
数据库有表:
table1 中有一列name
查询的时候是这样显示的

name

moke
andy
Shina
Conroy

现在我要这样显示:

name: moke,andy,Shina,conroy

用SQL语句怎么实现啊?



...全文
857 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
AI1983 2009-02-20
  • 打赏
  • 举报
回复
学习一下
hwr_hwr 2009-02-20
  • 打赏
  • 举报
回复

coalesce 返回其参数中第一个非空表达式。

例如:
a1 a2 a3
1 5 null
null 3 2

COALESCE(a1, a2, a3)返回的就是:
1
3

dawugui 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 spring0906 的回复:]
isnull 和 coalesce有什么区别啊?
[/Quote]
ISNULL
使用指定的替换值替换 NULL。

语法
ISNULL ( check_expression , replacement_value )

参数
check_expression

将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。

replacement_value

在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。

返回类型
返回与 check_expression 相同的类型。

注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。

示例
A. 将 ISNULL 与 AVG 一起使用
下面的示例查找所有书的平均价格,用值 $10.00 替换 titles 表的 price 列中的所有 NULL 条目。

USE pubs
GO
SELECT AVG(ISNULL(price, $10.00))
FROM titles
GO

下面是结果集:

--------------------------
14.24

(1 row(s) affected)

B. 使用 ISNULL
下面的示例为 titles 表中的所有书选择书名、类型及价格。如果一个书名的价格是 NULL,那么在结果集中显示的价格为 0.00。

USE pubs
GO
SELECT SUBSTRING(title, 1, 15) AS Title, type AS Type,
ISNULL(price, 0.00) AS Price
FROM titles
GO

下面是结果集:

Title Type Price
--------------- ------------ --------------------------
The Busy Execut business 19.99
Cooking with Co business 11.95
You Can Combat business 2.99
Straight Talk A business 19.99
Silicon Valley mod_cook 19.99
The Gourmet Mic mod_cook 2.99
The Psychology UNDECIDED 0.00
But Is It User popular_comp 22.95
Secrets of Sili popular_comp 20.00
Net Etiquette popular_comp 0.00
Computer Phobic psychology 21.59
Is Anger the En psychology 10.95
Life Without Fe psychology 7.00
Prolonged Data psychology 19.99
Emotional Secur psychology 7.99
Onions, Leeks, trad_cook 20.95
Fifty Years in trad_cook 11.95
Sushi, Anyone? trad_cook 14.99


COALESCE
返回其参数中第一个非空表达式。

语法
COALESCE ( expression [ ,...n ] )

参数
expression

任何类型的表达式。

n

表示可以指定多个表达式的占位符。所有表达式必须是相同类型,或者可以隐性转换为相同的类型。

返回类型
将相同的值作为 expression 返回。

注释
如果所有自变量均为 NULL,则 COALESCE 返回 NULL 值。

COALESCE(expression1,...n) 与此 CASE 函数等价:

CASE
WHEN (expression1 IS NOT NULL) THEN expression1
...
WHEN (expressionN IS NOT NULL) THEN expressionN
ELSE NULL

示例
在下面的示例中,显示包含三列有关某个雇员每年工资收入信息的 wages 表:hourly_wage、salary 和 commission。但是,每个雇员只能接受一种付款方式。若要确定支付给所有雇员的工资总额,请使用 COALESCE 函数接受在 hourly_wage、salary 和 commission 中找到的非空值。

SET NOCOUNT ON
GO
USE master
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'wages')
DROP TABLE wages
GO
CREATE TABLE wages
(
emp_id tinyint identity,
hourly_wage decimal NULL,
salary decimal NULL,
commission decimal NULL,
num_sales tinyint NULL
)
GO
INSERT wages VALUES(10.00, NULL, NULL, NULL)
INSERT wages VALUES(20.00, NULL, NULL, NULL)
INSERT wages VALUES(30.00, NULL, NULL, NULL)
INSERT wages VALUES(40.00, NULL, NULL, NULL)
INSERT wages VALUES(NULL, 10000.00, NULL, NULL)
INSERT wages VALUES(NULL, 20000.00, NULL, NULL)
INSERT wages VALUES(NULL, 30000.00, NULL, NULL)
INSERT wages VALUES(NULL, 40000.00, NULL, NULL)
INSERT wages VALUES(NULL, NULL, 15000, 3)
INSERT wages VALUES(NULL, NULL, 25000, 2)
INSERT wages VALUES(NULL, NULL, 20000, 6)
INSERT wages VALUES(NULL, NULL, 14000, 4)
GO
SET NOCOUNT OFF
GO
SELECT CAST(COALESCE(hourly_wage * 40 * 52,
salary,
commission * num_sales) AS money) AS 'Total Salary'
FROM wages
GO

下面是结果集:

Total Salary
------------
20800.0000
41600.0000
62400.0000
83200.0000
10000.0000
20000.0000
30000.0000
40000.0000
45000.0000
50000.0000
120000.0000
56000.0000

(12 row(s) affected)

spring0906 2009-02-20
  • 打赏
  • 举报
回复
isnull 和 coalesce有什么区别啊?
dawugui 2009-02-20
  • 打赏
  • 举报
回复
create table tb(name varchar(10))
insert into tb values('moke')
insert into tb values('andy')
insert into tb values('Shina')
insert into tb values('Conroy')
go

--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.name + ',' from (select name from tb) as t
set @sql='select result =''' + left(@sql , len(@sql) - 1) + ''''
exec(@sql)

--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + name from tb
print @output

drop table tb

/*
result
----------------------
moke,andy,Shina,Conroy

moke,andy,Shina,Conroy
*/
spring0906 2009-02-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dawugui 的回复:]
SQL code/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/

create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go

--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result …
[/Quote]
谢谢了
spring0906 2009-02-20
  • 打赏
  • 举报
回复
有没有其他的方法呢?
dawugui 2009-02-20
  • 打赏
  • 举报
回复
/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/

create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go

--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result = ''' + left(@sql , len(@sql) - 1) + ''''
exec(@sql)
/*
result
----------
a,b,c,d,e,
*/

--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + col from tb
print @output
/*
a,b,c,d,e
*/

drop table tb
水族杰纶 2009-02-20
  • 打赏
  • 举报
回复
DECLARE @S VARCHAR(100)
SELECT @S=ISNULL(@S+',','')+NAME FROM TB
SELECT @S

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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