开发者

summation of results in mysql and then dividing the result by 2

Again, to continue with my mysql noob questions :) I want the following to be just one result:

select sum(sales_today) from table1
union
select sum(sales_yesterday) from table1

So right now that gives me two results, how do I make it give me only one, the summation of the two results? And how开发者_C百科 can I do to apply further equations to the result, such as dividing it by 2?

Thank you!


SELECT (SUM(sales_today) + SUM(sales_yesterday)) / 2 FROM table1

For more details on what you can use, see MySQL Functions and Operators.


You can add Expressions like you add fields. For easier access you should use the "AS"-keyword to give meaningful names:

select sum(sales_today) as TodaysSales,
    sum(sales_yesterday) as YesterdaysSales,
    ((sum(sales_today) + sum(sales_yesterday)) / 2) as TwoDayAverage 
from table1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜