我有一個表結構如下:如何將兩個別名添加到通過MySql查詢創建的表中?
year
----------
1995
2000
2010
*(前三行)
我想創建一個「差異」列其使用輸入年份:
ind | year | diff
-----------------
1 | 1995 | 0
2 | 2000 | 5
3 | 2010 | 10
爲了做到這一點,我試圖創建和ind列和修改這裏建議:http://www.mysqltutorial.org/mysql-tips/mysql-compare-calculate-difference-successive-rows/)
雖然我不成功w第i個給第二別名我已經創建表:
SET @row_number = 0;
SELECT
g1.ind
(g2.year - g1.year) AS diff
FROM (
select * from
(SELECT
(@row_number:[email protected]_number + 1) AS ind, year
FROM
movies) t
) g1
INNER JOIN
g1 g2 ON g2.ind = g1.ind + 1
的錯誤是: 錯誤代碼:1146表「db_name.g1」不存在
ID始終是連續的? – Strawberry
我已經重新編輯我的問題 - 實際上沒有,因此我創建了一個連續的id列但是現在我有問題將別名添加到我創建的表 – Bugu
因此,您想要創建一個diff列和ind列? – Strawberry