summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/last_value.test
blob: 41cfdb5482d00c39066f84f9ee4fd4c35ff1a615 (plain)
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
43
44
45
46
#
# Tests for the LAST_VALUE function
#

--disable_warnings
drop table if exists t1;
drop database if exists mysqltest;
--enable_warnings

# CREATE TABLE `queue` (
#  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
#  `priority` int(11) DEFAULT NULL,
#  `state` int(11) DEFAULT NULL,
#  `pid` int(10) unsigned DEFAULT NULL,
#  `dat` varbinary(200) DEFAULT NULL,
#  PRIMARY KEY (`id`)
# )

CREATE TABLE t1 (a INT, b INT, c INT, d INT);
INSERT INTO t1 VALUES (1,3,0,NULL),(2,2,0,NULL),(3,4,0,NULL),(4,2,0,NULL);
SELECT * FROM t1;
UPDATE t1 SET c=LAST_VALUE(@last_a:=a,@last_b:=b,@last_c:=c,1), d=4211 WHERE c=0 ORDER BY b DESC LIMIT 1;
SELECT @last_a, @last_b, @last_c;
SELECT * FROM t1;
DROP TABLE t1;

#
# Test with different types
#
# PS protocol gives slightly different metadata for the length
--enable_metadata
--disable_ps_protocol
SELECT LAST_VALUE(@last_a:=1,@last_b:=1);
select @last_b;
--enable_ps_protocol
SELECT LAST_VALUE(@last_a:=1,@last_b:=1.0);
select @last_b;
SELECT LAST_VALUE(@last_a:=1,@last_b:="hello");
select @last_b;
SELECT date(LAST_VALUE(@last_a:=1,@last_b:="2001-02-03"));
select @last_b;
SELECT LAST_VALUE(@last_a:=1,@last_b:="2001-02-03",NULL);
select @last_b;
--disable_metadata
--error ER_PARSE_ERROR
SELECT LAST_VALUE();