2017-08-31 73 views
-1

我必須計算死亡的馬的年齡,並且multiply by 3以獲得他們可比的人類年。列出他們的horse_id,名稱和human years,訂購horse_idMYSQL計算並乘以年齡

我是一個noob,只是不明白我需要做什麼,在網上看這是我能得到的最接近的,Born和Died列是日期,但只有一年。

任何幫助,將不勝感激。

SELECT Horse_id, name, Born, Died, 
TIMESTAMPDIFF(YEAR, Died, Born)*3 AS 'Human Years' 
FROM horsedb.horse 
ORDER BY Horse_id; 
+0

您還可以向我們展示一些樣本輸入數據以及預期結果嗎? –

+0

不知道你的全表結構,所以 SELECT Horse_id,name,Born,Died, (死 - 生)* 3 AS'Human Years' FROM horsedb.horse ORDER BY Horse_id; – Balasubramanian

回答

0

您的嘗試看起來不錯 - 您從中得到了什麼結果?這裏是我想嘗試的:

SELECT Horse_id, name, Born, Died, 
     (YEAR(Died) - YEAR(Born)) * 3 AS 'Human Years' 
FROM horsedb.horse 
ORDER BY Horse_id; 
0

我會做幾個月的計算。 。 。將月數除以4.以下是一種方法:

SELECT Horse_id, name, Born, Died, 
     FLOOR((YEAR(Died)*12 + MONTH(Died)) - (YEAR(Born)*12 + Month(Born))/4) as HumanYears 
FROM horsedb.horse 
ORDER BY Horse_id;