Mysql,检查字段值是否更改? [英] Mysql, Check field value change?
本文介绍了Mysql,检查字段值是否更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MySQL,检查字段值是否更改?如果更改值,则显示不同的值
Mysql, Check field value change? if change the value then show different value
表:a
id size name
--------------
1 500 abc
2 200 bcd
3 400 xyz
4 500 bbb
5 200 aaa
select @s:=@s+1 as index, id, size, name from a,(SELECT @s:= 0) AS s order by size;
在此查询数据中.
index id size name
--------------------
1 2 200 bcd
2 5 200 aaa
3 3 400 xyz
4 1 500 abc
5 4 500 bbb
当大小改变时,我需要改变索引. 我想获取此类数据.喜欢.
I need to get index change when size is change. I want to get this type of data. like.
index id size name
--------------------
1 2 200 bcd
1 5 200 aaa
2 3 400 xyz
3 1 500 abc
3 4 500 bbb
推荐答案
这应该可以解决问题:
SELECT
@s := @s + (@prev_size != a.size) `index`,
id,
@prev_size := a.size size,
name
FROM a, (SELECT @s := 0, @prev_size := -1) s
ORDER BY a.size
输出:
| INDEX | ID | SIZE | NAME |
|-------|----|------|------|
| 1 | 2 | 200 | bcd |
| 1 | 5 | 200 | aaa |
| 2 | 3 | 400 | xyz |
| 3 | 1 | 500 | abc |
| 3 | 4 | 500 | bbb |
小提琴此处.
这篇关于Mysql,检查字段值是否更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文