zoukankan      html  css  js  c++  java
  • sql查询将列里面的值替换为别的值但是实际值不变

    数据库有一张表BUG(缺陷记录表) 里面有字段severity(严重程度): 
    severity的值实际为1,2,3,4,但希望在查询结果中将severity的1,2,3,4值显示为其他的值,但severity的实际值不会改变;
    例如:数据表的结构和数据如下:
    bug_id name severity
    1 张三 1
    2 李四 2
    3 王五 3
    4 马六 4
    5 周王 5
    要在查询结果中将severity实际值显示为中文,如下:
    severity实际值 severity 显示结果
    1 紧急
    2 高
    3 中
    4 低
    查询语句:
    select bug_id,name,
    (case severity
    when 1 then '紧急'
    when 2 then '高'
    when 3 then '中'
    when 4 then '低'
    else '其他'
    end)
    from BUG;

    查询结果:
    bug_id       name      severity
       1         张三           紧急
       2         李四           高
       3         王五           中
       4         马六           低
       5         周王           其他

    语句解释:
    select
    (case 字段名 
    when  实际值1  then  替换值1
    when  实际值2  then  替换值2
    …………
    when  实际值n  then  替换值n
    else  替换值x
    end)
    from 表名;

    1、case 字段名:要替换显示值的字段;

    2、when  实际值1  then  替换值1:当查询结果的字段值为实际值1时,将结果显示为替换值1;

    3、else 替换值x:当查询结果字段值的实际值不满足前面所有条件时,将期显示为替换值x;

    4、end:替换语句结束;

    5、要将多个字段的显示值都替换成其他值时,只需增加多个(case 字段名 when 实际值 then 替换值 end)语句,中间使用逗号分隔;

    原文网址:http://sujing1981.blog.163.com/blog/static/270251452013226105127924/

  • 相关阅读:
    Java I/O
    iOS AppsFlyer的使用注意事项
    Star Schema and Snowflake Schema
    SSB基准测试
    ES Route
    CPS(Cyber-Physical Systems)白皮书-摘选
    蓄电池放电容量与环境温度的关系
    时间序列分析(二)
    时间序列分析(一)
    IndexR
  • 原文地址:https://www.cnblogs.com/fengyie55/p/3620620.html
Copyright © 2011-2022 走看看