作者ilay (take it easy)
看板Database
标题[MySQL5] 关於 left join
时间Wed Oct 21 22:29:56 2009
有一个表如下:
(int)id (float)mile
-------------------------------
1 6976.8
2 7576.4
3 9879.9
4 12078.0
数据大概长这样 因为我想生出一个新的 column
内容为 mile 的後项减掉前项
预期的结果应该是这样
(int)id (float)mile new column
-----------------------------------------------
1 6976.8 NULL
2 7576.4 7576.4-6976.8
3 9879.9 9879.9-7576.4
4 12078.0 12078.0-9879.9
5 NULL
code:
select t1.id,t1.mile,(t2.mile-t1.mile) from table t1 left join table t2
on t1.id=t2.id-1 order by t1.id asc
(int)id (float)mile new column
-----------------------------------------------
1 6976.8 7576.4-6976.8
2 7576.4 9879.9-7576.4
3 9879.9 12078.0-9879.9
4 12078.0 -xxxxxxxxxxx
结果是有两个问题
1. new column 的 rows 全部往前排了,但这还好
2. 最後一笔的资料根本没意义 可以踢掉吗
感谢指教
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.40.32.65