mysql: 比较同一张表的相邻两行的某一字段的值
2015-04-17 01:35
351 查看
来源:http://www.iteye.com/topic/322307
觉得这个问题比较有趣,发出来给有用的人参考:
/*Column Information For - test.tt*/
------------------------------------
Field Type Collation Null Key Default Extra Privileges Comment
------ ------- --------- ------ ------ ------- ------ ------------------------------- -------
id int(11) (NULL) YES (NULL) select,insert,update,references
value int(11) (NULL) YES (NULL) select,insert,update,references
SQL:
select a.id, a.value - b.value as diff from
(select id ,value, @num := @num + 1 as row_number from (SELECT @num:=0) r,tt order by id) a,
(select id ,value, @num2 := @num2 + 1 as row_number from (SELECT @num2:=0) r2, tt order by id) b
where a.row_number + 1 = b.row_number ;
觉得这个问题比较有趣,发出来给有用的人参考:
/*Column Information For - test.tt*/
------------------------------------
Field Type Collation Null Key Default Extra Privileges Comment
------ ------- --------- ------ ------ ------- ------ ------------------------------- -------
id int(11) (NULL) YES (NULL) select,insert,update,references
value int(11) (NULL) YES (NULL) select,insert,update,references
SQL:
select a.id, a.value - b.value as diff from
(select id ,value, @num := @num + 1 as row_number from (SELECT @num:=0) r,tt order by id) a,
(select id ,value, @num2 := @num2 + 1 as row_number from (SELECT @num2:=0) r2, tt order by id) b
where a.row_number + 1 = b.row_number ;
相关文章推荐
- Mysql 一张表中某一列多条记录更新为不同的值,效率比较高
- mysql根据某一张表的字段更新另一张表的字段
- MySql 按某一字段分组,同时取每组的前三行数据
- mysql语句从一个表中查询出数据,再插入到另一张表中(表字段不一样)
- mysql 将查询出来的某一字段组合成字符串
- mysql按某一字段分组取最大(小)值所在行的数据
- mysql删除表中某一字段重复的记录
- 【mysql技巧】按某一字段分组取最大(小)值所在行的数据
- Mysql 获取当前时间的前几天和字段中的时间进行比较
- 把一个表某个字段内容复制到另一张表的某个字段。MYSQL
- mysql中查询某一字段或字段集合重…
- Mysql 比较两个数据库的表,表字段是否匹配,升级更新找sql有用
- mysql中记录某一字段中重复的个数并且按照次数排序
- 比较详细的MySQL字段类型说明
- mysql一个字段多个值关联另一张表
- mysql 如何实现在一张表里某个字段根据另一个字段自增长
- 同一张表,合并某相同字段,然后提取某一字段数据
- CppSQLite3Statement用法和得到数据库表的某一字段的最大值或一张表数据的条数
- 多个表连接时的某一字段值更新到对应的另一张表中的某个字段中
- mySql字段设置varchar的时间比较