Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One easy way to do that is to use your original query as an inline view and write a SELECT statement using the inline view as a rowsource. (In MySQL parlance, the inline view is called a <i>derived table</i>):</p> <pre><code>SELECT v.BudgetAmount , v.Difference , v.BudgetAmount + v.Difference AS Total FROM ( -- original query here SELECT ... IFNULL(bl.amount, '---') AS BudgetAmount, IFNULL((bl.amount *1) - ( + bal1.amount ), '---') AS Difference FROM... ) v </code></pre> <p>Unlike other relational database systems, MySQL will actually force the inline view to be materialized (i.e. the query in the inline view is executed and the results are stored as a MyISAM table), and that has performance implications.</p> <p>But it is a way that you can reference the column aliases for use in other expressions. </p> <p>(The aliases assigned to the columns can't be referenced in SELECT list where they are assigned... they can only be referenced in the HAVING and ORDER BY clauses of the query.)</p> <p>To get the values for those columns added into another column in a single query, you can't reference the aliases, you have to to repeat the expressions, like this:</p> <pre><code>SELECT expr1 AS BudgetAmount , expr2 AS Difference , expr1 + expr2 AS Total FROM ... </code></pre> <p>To reference the aliases, they need to come from a row source referenced by the query, such as an inline view:</p> <pre><code>SELECT v.BudgetAmount , v.Difference , v.foo , v.BudgetAmount + v.Difference AS Total FROM ( SELECT expr1 AS BudgetAmount , expr2 AS Difference , foo FROM ... ) v </code></pre> <p>I'm not sure that's the answer you wanted to hear, but that's the way it is.</p> <hr> <p><b>EDIT:</b></p> <p>I misunderstood what you were asking. Your question said: "I want a column with the sum of ...". I took that to mean you wanted a query that returned the same number of rows, not that you wanted an additional ROW appended to the result set.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload