帮帮忙。。。在查询时怎么把数据值显示为列名

zlRun 2010-03-23 11:10:50
create table userInfo
(
id identity(1,1) primary key,
user_name varchar(20) not null,---------用户名
user_sex int defalut(0)--------用户性别
)

在这个表中性别用0和1 表示
要求查出来的时候是
id 男 女
001 张三
002 李四
...全文
99 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
myloveral 2010-03-23
  • 打赏
  • 举报
回复
select id ,
max(case user_sex when 0 then [user_name] else null end) '男',
max(case user_sex when 1 then [user_name] else null end) '女'
from userinfo
group by id
永生天地 2010-03-23
  • 打赏
  • 举报
回复
select id , 
max(case user_sex when 0 then [user_name] else '' end) '男',
max(case user_sex when 1 then [user_name] else '' end) '女'
from userinfo
group by id , [user_name]


这样真的不错
黄_瓜 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dawugui 的回复:]
SQL code
create table userInfo
(
id int ,
[user_name] varchar(20) not null,---------用户名
user_sex int
)

insert into userInfo values('001' ,'张三',0)
insert into userInfo values('002' ,'李四',1)
……
[/Quote]
up
黄_瓜 2010-03-23
  • 打赏
  • 举报
回复
想要这样的结果
id    男       女
001 null 张三
002 李四 null
dawugui 2010-03-23
  • 打赏
  • 举报
回复
create table userInfo
(
id int ,
[user_name] varchar(20) not null,---------用户名
user_sex int
)

insert into userInfo values('001' ,'张三',0)
insert into userInfo values('002' ,'李四',1)


select id , [user_name] , case when user_sex = 0 then '男' else '女' end user_sex from userinfo
/*
id user_name user_sex
----------- -------------------- --------
1 张三 男
2 李四 女

(所影响的行数为 2 行)
*/

select id , [user_name] ,
max(case user_sex when 0 then '男' else '' end) '男',
max(case user_sex when 1 then '女' else '' end) '女'
from userinfo
group by id , [user_name]
/*
id user_name 男 女
----------- -------------------- ---- ----
2 李四 女
1 张三 男

(所影响的行数为 2 行)

*/

drop table userInfo
Mr_Nice 2010-03-23
  • 打赏
  • 举报
回复
select id,
max(case when user_sex ='男' then user_name else '' end) as '男',
max(case when user_sex ='女' then user_name else '' end) as '女'
from tb
group by id
黄_瓜 2010-03-23
  • 打赏
  • 举报
回复
select SUBSTRING('男女',user_sex+1,1) from tb
bancxc 2010-03-23
  • 打赏
  • 举报
回复
select id,
max(case when user_sex ='男' then user_name else null end) '男',
max(case when user_sex ='女' then user_name else null end) '女'
from tb
group by id
Mr_Nice 2010-03-23
  • 打赏
  • 举报
回复
行列转换!
dawugui 2010-03-23
  • 打赏
  • 举报
回复
select id , user_name , case when user_sex when 0 then '男' else '女' end user_sex from userinfo
bancxc 2010-03-23
  • 打赏
  • 举报
回复
行转列 等乌龟
老黎 2010-03-23
  • 打赏
  • 举报
回复
路过拜小F神
--小F-- 2010-03-23
  • 打赏
  • 举报
回复
膜拜以上各位
whlcy 2010-03-23
  • 打赏
  • 举报
回复
study!!!

34,576

社区成员

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

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