MySQL-重用计算值 [英] Mysql - reusing calculated values
本文介绍了MySQL-重用计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道该怎么表达这个问题,但确实如此. 我想重用我在查询中计算出的值来计算另一个值. 变量是我猜的正确词.这是我的查询:
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 word i guess. here is my query:
SELECT
t1.label as label,SUM(t1.totalEvents) as Entry,SUM(t2.totalEvents) as Back,
ROUND(Entry/Back*100,2) as 'Rate'
FROM
trackReports_daily t1
.... rest of query ...
在内部回合中,我想使用SUM(t1.totalEvents)返回的值,但是当我使用Entry
时,出现此错误Unknown column 'Entry' in 'field list'
Inside round i want to to use the value returned by SUM(t1.totalEvents), but when i use Entry
i get this error Unknown column 'Entry' in 'field list'
我又如何在不重新计算的情况下获得其中的值:
How else can i get the value in there without recalculating everytime like this:
ROUND(SUM(t2.totalEvents)/SUM(t1.totalEvents)*100,2)
推荐答案
您可以使用子查询:
SELECT label, Entry, Back, ROUND(Entry/Back*100,2) as 'Rate'
FROM (
SELECT SUM(t1.totalEvents) as Entry, SUM(t2.totalEvents) as Back, t1.label as label
FROM trackReports_daily t1
.... rest of query ...
) as temp;
这篇关于MySQL-重用计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文