2016-05-22 22 views
1

以下是我正在使用的代碼。如何將列的最大值存儲爲變量,然後將其用於MySQL中的計算?

Select max(heart_rate) as Max_heart, Count(*) from exercise_logs 
    where heart_rate between (0.5*Max_heart) and (0.9*Max_heart); 

我是新與SQL,但我覺得這本來是很容易的東西,如R.不管怎麼說,因爲我需要在SQL中完成的,對我怎麼做到這一點有什麼建議?

我的數據是這樣的

type   minutes calories heart_rate 
    biking   30  100  110 
    biking   10  30  105 
    dancing   15  200  120 
    dancing   15  165  120 
    tree climbing 30  70  90 
    tree climbing 25  72  80 
    rowing   30  70  90 
    hiking   60  80  85 
+0

什麼是所需的輸出應該是什麼? – Rahul

+0

我懷疑你做得比它需要的更復雜。你能更徹底地描述你的問題嗎? – Strawberry

回答

0

你想用User-Defined Variables。它應該像

declare var1, var2 INT; 

    Select var1 = max(heart_rate), var2 = Count(*) 
    from exercise_logs 
    where heart_rate between (0.5*Max_heart) and (0.9*Max_heart); 

    select var1 + var2 as Data; 
+0

我不'認爲他問如何將結果分配給一個變量。我想他想找到一個整體最大心率,然後找到導致心率在最大心率的50%和90%之間的活動。 – Alex

0

試試這個

-- Finds maximum heart rate from all activities 
SET @Max_heart := (SELECT max(heart_rate) FROM exercise_logs); 

-- Selects activities that generate between 50% and 90% of the maximum heart rate 
Select * 
FROM exercise_logs 
where heart_rate between (0.5* @Max_heart) and (0.9*@Max_heart); 
相關問題