Mysql - reusing calculated values

后端 未结 3 829
生来不讨喜
生来不讨喜 2020-12-19 12:06

I don\'t know exactly how to word this question but here it is. I want to reuse values that i calculated in my query to calculate another value. Variables is the correct w

3条回答
  •  太阳男子
    2020-12-19 12:32

    One option would be to use a subquery in the FROM clause like:

    SELECT 
        t1.label as label,SUM(t1.totalEvents) as Entry,t2.Back,  
        ROUND(t1.Entry/t2.Back*100,2) as 'Rate'
    FROM 
        (SELECT *, SUM(totalEvents) as Entry FROM trackReports_daily) t1
        (SELECT *, SUM(totalEvents) as Back FROM someTable) t2
    .... rest of query ...
    

    If you want to reuse Rate in further queries then it will get complicated, and I do not know if this will have optimal performance, but I think it will do what you asked for in your example.

提交回复
热议问题