首页 > 数据库技术 > 详细

将数据库中的数据横向显示

时间:2018-04-19 16:25:04      阅读:180      评论:0      收藏:0      [点我收藏+]
name city money
张三 北京 100
张三 上海 200
张三 广州 300

面试题:test表中有以上数据,使用SQL查询出下列效果。

name 北京 上海 广州
张三 100 200 300

将数据库字段中的数据横向显示,考察的是对于case when判断语句的使用,下列代码为答案。

select name,
       max(case city when "北京" then money end) as "北京",
       max(case city when "上海" then money end) as "上海",
       max(case city when "广州" then money end) as "广州"
from test;

将数据库中的数据横向显示

原文:https://www.cnblogs.com/luminglm/p/8883930.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!