您的位置:首页 > 数据库 > MySQL

MYSQL中 case when then else end使用举例

2018-03-07 16:46 549 查看
       就是写着玩,假设存在这么一张表 test,表中有两个字段currency_code(币种编码'HKD', 'USD')、amount(金额),
现在有这么一个需求,要求分别统计各币种对应的金额,怎么办,可以使用这种sql句法"case when then else end"。
     具体写法:
     select sum(case when currency_code='HKD' then amount else 0 end) as 'hkd_amount',
               sum(case when currency_code='USD' then amount else 0 end) as 'usd_amount'
     from test;

     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: