mysql基于别名值的加减法

lnlaulya  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(358)

我有个问题 SUM() 基于枚举值输入/输出(见下文)

我想扩大计算范围,再加上第二个 SELECT 基于 TOTAL IN 以及 TOTAL OUT 使用此公式的输出:
预算+总投入-总产出=总利润
问题是输出错误(见下文)

应该是的 £17,408.90 我尝试扩展的查询是:

SELECT
  f.id_festival AS ID,
  CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL IN`,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL OUT`,
  (SELECT
      CONCAT('£ ', ROUND(SUM(f.festival_budget + 'TOTAL IN') - 'TOTAL OUT', 2))) AS PROFIT
FROM festival f
  INNER JOIN payment p
    ON p.id_festival = f.id_festival
WHERE f.id_festival = 1
GROUP BY f.id_festival

我不知道那115.000是从哪里来的。任何帮助都将不胜感激。

gmxoilav

gmxoilav1#

'Total In' 是字符串。如果在数值表达式中使用它,那么mysql会将其转换为数字。这个数字是多少 0 . 绳子 'Total In' 与别名为的列没有任何关系 Total In .
在mysql中,在表达式中使用表别名基本上有两种选择:(1)重复表达式(2) 使用cte。

SELECT f.id_festival AS ID,
       CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
       CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL IN`,
       CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL OUT`,
       CONCAT('£ ',
              ROUND(f.festival_budget +
                    SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount
                             WHEN p.pmt_type = 'Payment OUT' THEN - p.pmt_amount
                             ELSE 0
                        END),
                    2)
             ) AS PROFIT
FROM festival f INNER JOIN
     payment p
     ON p.id_festival = f.id_festival
WHERE f.id_festival = 1
GROUP BY f.id_festival;

确实,在某些情况下,可以在子查询中使用别名来获得相同的效果。但我不建议这样做,因为我还没有找到能够保证这一点的文档。

rpppsulh

rpppsulh2#

请尝试:

SELECT ID, Budget, `TOTAL IN`, `TOTAL OUT`, CONCAT('£ ', ROUND(SUM((Budget   + `TOTAL IN`) - `TOTAL OUT` ))) AS PROFIT FROM 
 (SELECT f.id_festival AS ID, 
 CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
 CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN   p.pmt_amount ELSE 0 END), 2)) AS `TOTAL IN`,
 CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN   p.pmt_amount ELSE 0 END), 2)) AS `TOTAL OUT`,
 FROM festival f
 INNER JOIN payment p
 ON p.id_festival = f.id_festival
 WHERE f.id_festival = 1
 GROUP BY f.id_festival) a;
8zzbczxx

8zzbczxx3#

试试这个code:-

SELECT
  f.id_festival AS ID,
  CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL_IN`,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL_OUT`,
  (SELECT
      CONCAT('£ ', ROUND(SUM(f.festival_budget + 'TOTAL_IN') - 'TOTAL_OUT', 2))  AS PROFIT
FROM festival f
  INNER JOIN payment p
    ON p.id_festival = f.id_festival
WHERE f.id_festival = 1
GROUP BY f.id_festival
zqry0prt

zqry0prt4#

无需再选择:

SELECT
  f.id_festival AS ID,
  CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL IN`,
  CONCAT('£ ', ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2)) AS `TOTAL OUT`,
  CONCAT('£ ', 
    ROUND(
        f.festival_budget 
        +ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)
        -ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2)
        , 2)
    ) AS `PROFIT`
FROM festival f
  INNER JOIN payment p
    ON p.id_festival = f.id_festival
WHERE f.id_festival = 1
GROUP BY f.id_festival

相关问题