mysql如何执行或解释子查询?

时间:2015-09-04 07:25:55

标签: mysql sql subquery mysql-workbench

我需要了解mysql如何解释或执行这样的子查询:

SELECT SUM(tp),SUM(ep),SUM(mp) FROM
(
    SELECT Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(tp,0) else 0 end) tp,   
           Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(ep,0) else 0 end) ep, 
           Avg(case date_format(PS_DATE,'%Y') when '2005' then nvl(mp,0) else 0 end) mp   
    FROM M_DAILY 
    WHERE PS_DATE >= date ('2005-01-01') and PS_DATE <= date ('2005-12-31');
) As T

有人可以解释如何解释这样的查询(用文字表示)!

1 个答案:

答案 0 :(得分:0)

对于2005年,它找到列tp,ep和mp的平均值的SUM。虽然这样做的空值被认为是0.由于2005年过滤了日期,上面的代码可以简化为

SELECT SUM(tp),SUM(ep),SUM(mp) FROM
(
    SELECT Avg(nvl(tp,0)) tp,   
           Avg(nvl(ep,0)) ep,   
           Avg(nvl(mp,0)) mp           
    FROM M_DAILY 
    WHERE PS_DATE >= date ('2005-01-01') and PS_DATE <= date ('2005-12-31');
) As T