3
drop table if exists t1,t2;
6
--error ER_BAD_FIELD_ERROR
8
set @a := connection_id() + 3;
9
select @a - connection_id();
14
# Check using and setting variables with SELECT DISTINCT
16
CREATE TABLE t1 ( i int not null, v int not null,index (i));
17
insert into t1 values (1,1),(1,3),(2,1);
18
create table t2 (i int not null, unique (i));
19
insert into t2 select distinct i from t1;
21
select distinct t2.i,@vv1:=if(sv1.i,1,0),@vv2:=if(sv2.i,1,0),@vv3:=if(sv3.i,1,0), @vv1+@vv2+@vv3 from t2 left join t1 as sv1 on sv1.i=t2.i and sv1.v=1 left join t1 as sv2 on sv2.i=t2.i and sv2.v=2 left join t1 as sv3 on sv3.i=t2.i and sv3.v=3;
22
explain select * from t1 where i=@vv1;
23
select @vv1,i,v from t1 where i=@vv1;
24
explain select * from t1 where @vv1:=@vv1+1 and i=@vv1;
25
explain select @vv1:=i from t1 where i=@vv1;
26
explain select * from t1 where i=@vv1;
29
# Check types of variables
31
select @a:=10, @b:=1, @a > @b, @a < @b;
32
# Note that here a and b will be avaluated as number
33
select @a:="10", @b:="1", @a > @b, @a < @b;
34
# Note that here a and b will be avaluated as strings
35
select @a:=10, @b:=2, @a > @b, @a < @b;
36
select @a:="10", @b:="2", @a > @b, @a < @b;
42
create table t1 (id int, d double, c char(10));
43
insert into t1 values (1,2.0, "test");
45
update t1 SET id=(@c:=@c+1);
48
update t1 set id=(@c:=@c+1);
52
select @d,(@d:=id),@d from t1;
53
select @e,(@e:=d),@e from t1;
54
select @f,(@f:=c),@f from t1;
56
select @g,(@g:=c),@g from t1;
57
select @c, @d, @e, @f;
58
select @d:=id, @e:=id, @f:=id, @g:=@id from t1;
59
select @c, @d, @e, @f, @g;
63
select @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b, @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b;
67
# Item_func_set_user_var sets update_query_id, Item_func_get_user_var checks it
69
create table t1 (i int not null);
70
insert t1 values (1),(2),(2),(3),(3),(3);
71
select @a:=0; select @a, @a:=@a+count(*), count(*), @a from t1 group by i;
72
select @a:=0; select @a+0, @a:=@a+0+count(*), count(*), @a+0 from t1 group by i;
75
select @a,@a:="hello",@a,@a:=3,@a,@a:="hello again" from t1 group by i;
76
select @a,@a:="hello",@a,@a:=3,@a,@a:="hello again" from t1 group by i;
80
# Bug #6321 strange error:
81
# string function FIELD(<uservariable content NULL>, ...)
84
select FIELD( @var,'1it','Hit') as my_column;
87
# Bug #9286 SESSION/GLOBAL should be disallowed for user variables
89
--error ER_PARSE_ERROR
93
# Bug #10724 @@local not preserved in column name of select
95
# The value doesn't actually matter, we just care about the column name
97
select @@local.max_allowed_packet;
99
select @@session.max_allowed_packet;
101
select @@global.max_allowed_packet;
103
select @@max_allowed_packet;
105
select @@Max_Allowed_Packet;
109
select @@global.version;
111
--echo End of 4.1 tests
113
# Bug #6598: problem with cast(NULL as signed integer);
116
set @first_var= NULL;
117
create table t1 select @first_var;
118
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
119
show create table t1;
122
# This is not supported by Drizzle
123
--error ER_PARSE_ERROR
124
set @first_var= cast(NULL as integer);
125
#create table t1 select @first_var;
126
#--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
127
#show create table t1;
130
set @first_var= NULL;
131
create table t1 select @first_var;
132
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
133
show create table t1;
135
set @first_var= concat(NULL);
136
create table t1 select @first_var;
137
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
138
show create table t1;
141
set @first_var= cast(NULL as CHAR);
142
create table t1 select @first_var;
143
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
144
show create table t1;
148
# Bug #7498 User variable SET saves SIGNED BIGINT as BIGINT
151
# First part, set user var to large number and select it
152
set @a=18446744071710965857;
155
# Second part, set user var from large number in table
157
CREATE TABLE `bigfailure` (
158
`afield` BIGINT NOT NULL
160
INSERT INTO `bigfailure` VALUES (18446744071710965857);
161
SELECT * FROM bigfailure;
162
select * from (SELECT afield FROM bigfailure) as b;
163
select * from bigfailure where afield = (SELECT afield FROM bigfailure);
164
select * from bigfailure where afield = 18446744071710965857;
165
# This is fixed in 5.0, to be uncommented there
166
select * from bigfailure where afield = '18446744071710965857';
167
select * from bigfailure where afield = 18446744071710965856+1;
169
SET @a := (SELECT afield FROM bigfailure);
171
SET @a := (select afield from (SELECT afield FROM bigfailure) as b);
173
SET @a := (select * from bigfailure where afield = (SELECT afield FROM bigfailure));
176
drop table bigfailure;
179
# Bug#16861: User defined variable can have a wrong value if a tmp table was
182
create table t1(f1 int, f2 int);
183
insert into t1 values (1,2),(2,3),(3,1);
184
select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
186
# Bug 310977, uncomment this test after the bug is fixed
187
#create table t2 as select @var:=f2 from t1 group by f1 order by f2 desc limit 1;
194
# Bug#19024 - SHOW COUNT(*) WARNINGS not return Errors
196
--error ER_PARSE_ERROR
197
insert into city 'blah';
198
SHOW COUNT(*) WARNINGS;
199
SHOW COUNT(*) ERRORS;
202
# Bug#28494: Grouping by Item_func_set_user_var produces incorrect result.
204
create table t1(f1 int, f2 varchar(2), f3 float, f4 decimal(2,1));
205
insert into t1 values
206
(1, "a", 1.5, 1.6), (1, "a", 1.5, 1.6), (2, "b", 2.5, 2.6),
207
(3, "c", 3.5, 3.6), (4, "d", 4.5, 4.6), (1, "a", 1.5, 1.6),
208
(3, "c", 3.5, 3.6), (1, "a", 1.5, 1.6);
209
select @a:=f1, count(f1) from t1 group by 1 desc;
210
select @a:=f1, count(f1) from t1 group by 1 asc;
211
select @a:=f2, count(f2) from t1 group by 1 desc;
212
select @a:=f3, count(f3) from t1 group by 1 desc;
213
select @a:=f4, count(f4) from t1 group by 1 desc;
217
# Bug#32482: Crash for a query with ORDER BY a user variable.
219
create table t1 (f1 int);
220
insert into t1 values (2), (1);
221
select @i := f1 as j from t1 order by 1;
223
# Bug #32260: User variables in query cause server crash
225
create table t1(a int);
226
insert into t1 values(5),(4),(4),(3),(2),(2),(2),(1);
229
set @prev_score := NULL;
230
# Disable the result log as we assign a value to a user variable in one part
231
# of a statement and use the same variable in other part of the same statement,
232
# so we can get unexpected results.
234
select @rownum := @rownum + 1 as row,
235
@rank := IF(@prev_score!=a, @rownum, @rank) as rank,
236
@prev_score := a as score
237
from t1 order by score desc;
241
--echo End of 5.1 tests