Mysql中笔试问题解决

一、问题提出

1、表结构rru如下

  

 2、要求查询显示结果为

  


二、问题解决

   方式一:

     SELECT DISTINCT c.`year` AS YEAR,
(SELECT amcount FROM product WHERE YEAR= c.`YEAR`  AND  MONTH = "1"  )AS m1,
(SELECT amcount FROM product WHERE YEAR = c.`YEAR`  AND  MONTH = "2")  AS m2,
(SELECT amcount FROM product WHERE YEAR = c.`YEAR`   AND  MONTH = "3")AS m3,
(SELECT amcount FROM product WHERE YEAR = c.`YEAR`  AND  MONTH = "4")AS m1#

FROM product  c

   方式二:

     SELECT  YEAR,
SUM(CASE WHEN MONTH='1' THEN amcount END) AS m1,
SUM(CASE WHEN MONTH='2' THEN amcount END) AS m2,
SUM(CASE WHEN MONTH='3' THEN amcount END) AS m3,
SUM(CASE WHEN MONTH='4' THEN amcount END) AS m4#

FROM product GROUP BY year


解释:    case when  then

SELECT            
    case                   -------------如果
    when sex='1' then '男' -------------sex='1',则返回值'男'
    when sex='2' then '女' -------------sex='2',则返回值'女'  
    else 0                 -------------其他的返回'其他’
    end                    -------------结束
from   sys_user            --------整体理解: 在sys_user表中如果sex='1',则返回值'男'如果sex='2',则返回值'女' 否则返回'其他’


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值