我在Java servlet中运行以下查询:
SELECT * FROM table order by updated desc limit 2;
我想比较同一列中最近的两个值,以确定它们是否不同。我该怎么做呢?
答案 0 :(得分:2)
这是一个想法...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,value INT NOT NULL);
INSERT INTO my_table VALUES (1,5),(2,6),(3,6);
SELECT i
FROM
( SELECT id
, CASE WHEN @prev=value THEN @i:='same' ELSE @i:='different' END i
, @prev:=value prev
FROM my_table
, ( SELECT @prev:=null,@i:=null) vars
ORDER
BY id
) x
ORDER
BY id DESC
LIMIT 1;
+------+
| i |
+------+
| same |
+------+