1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
|
select last_value(1,2);
select last_value(19,12,14,13);
select last_value('a','b','c','d');
DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
`id` int(11) NOT null AUTO_INCREMENT,
`name` varchar(20) DEFAULT null,
`type` enum('First','Second','Third','Last') DEFAULT null,
`set` SET('First','Second','Third','Last') DEFAULT null,
`num` int(11) NOT null DEFAULT 0,
`price` decimal(10,2) NOT null DEFAULT 0.00,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num`,`price`) VALUES
(1,'Art 1','First','Last',1,10.50),
(2,'Art 2','Second','First,Last',3,12.50),
(3,'Art 3','Second','Second,Last',2,15.50),
(4,'Art 4','Third','First,Second',5,25.50),
(5,'Art 5','Last','First,Last',2,5.50),
(6,'Art 6','First','First',12,1.50),
(7,'Art 7','Third','First,Third',20,1.75),
(8,'Art 8','Last','Second,Third',100,11.90);
SELECT * FROM `test_table`;
SELECT * FROM `test_table` where id=last_value(@n:=name, @t:=`type`, @s:=`set`, 1);
select @n, @t, @s;
delete FROM `test_table` where id=last_value(@n:=name, @t:=`type`, @s:=`set`, 8);
select @n, @t, @s;
update `test_table` set `type`='first' where last_value(@n:=name, @t:=`type`, @s:=`set`, 1);
select @n, @t, @s;
SELECT * FROM `test_table`
|