美文网首页
将数据库中的数据横向显示

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

作者: RLM233 | 来源:发表于2018-04-11 14:35 被阅读12次
    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.haomeiwen.com/subject/fxrrbxtx.html