Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

variables - Mysql - reusing calculated values

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 ...

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Take a look at this example

select (select @t1:=sum(field1)),(select @t2:=sum(field2)),@t1/@t2 from table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...