~ubuntu-branches/ubuntu/lucid/mysql-dfsg-5.1/lucid-security

1 by Chuck Short
Import upstream version 5.1.30
1
drop table if exists t1,t2;
2
set @sav_dpi= @@div_precision_increment;
3
set div_precision_increment= 5;
4
show variables like 'div_precision_increment';
5
Variable_name	Value
6
div_precision_increment	5
7
create table t1 (grp int, a bigint unsigned, c char(10) not null);
8
insert into t1 values (1,1,"a");
9
insert into t1 values (2,2,"b");
10
insert into t1 values (2,3,"c");
11
insert into t1 values (3,4,"E");
12
insert into t1 values (3,5,"C");
13
insert into t1 values (3,6,"D");
14
select a,c,sum(a) from t1 group by a;
15
a	c	sum(a)
16
1	a	1
17
2	b	2
18
3	c	3
19
4	E	4
20
5	C	5
21
6	D	6
22
select a,c,sum(a) from t1 where a > 10 group by a;
23
a	c	sum(a)
24
select sum(a) from t1 where a > 10;
25
sum(a)
26
NULL
27
select a from t1 order by rand(10);
28
a
29
2
30
6
31
1
32
3
33
5
34
4
35
select distinct a from t1 order by rand(10);
36
a
37
2
38
6
39
1
40
3
41
5
42
4
43
select count(distinct a),count(distinct grp) from t1;
44
count(distinct a)	count(distinct grp)
45
6	3
46
insert into t1 values (null,null,'');
47
select count(distinct a),count(distinct grp) from t1;
48
count(distinct a)	count(distinct grp)
49
6	3
50
select sum(all a),count(all a),avg(all a),std(all a),variance(all a),bit_or(all a),bit_and(all a),min(all a),max(all a),min(all c),max(all c) from t1;
51
sum(all a)	count(all a)	avg(all a)	std(all a)	variance(all a)	bit_or(all a)	bit_and(all a)	min(all a)	max(all a)	min(all c)	max(all c)
52
21	6	3.50000	1.70783	2.91667	7	0	1	6		E
53
select grp, sum(a),count(a),avg(a),std(a),variance(a),bit_or(a),bit_and(a),min(a),max(a),min(c),max(c) from t1 group by grp;
54
grp	sum(a)	count(a)	avg(a)	std(a)	variance(a)	bit_or(a)	bit_and(a)	min(a)	max(a)	min(c)	max(c)
55
NULL	NULL	0	NULL	NULL	NULL	0	18446744073709551615	NULL	NULL		
56
1	1	1	1.00000	0.00000	0.00000	1	1	1	1	a	a
57
2	5	2	2.50000	0.50000	0.25000	3	2	2	3	b	c
58
3	15	3	5.00000	0.81650	0.66667	7	4	4	6	C	E
59
select grp, sum(a)+count(a)+avg(a)+std(a)+variance(a)+bit_or(a)+bit_and(a)+min(a)+max(a)+min(c)+max(c) as sum from t1 group by grp;
60
grp	sum
61
NULL	NULL
62
1	7
63
2	20.25
0.2.1 by Norbert Tretkowski
Import upstream version 5.1.34
64
3	45.4831632475944
1 by Chuck Short
Import upstream version 5.1.30
65
create table t2 (grp int, a bigint unsigned, c char(10));
66
insert into t2 select grp,max(a)+max(grp),max(c) from t1 group by grp;
67
replace into t2 select grp, a, c from t1 limit 2,1;
68
select * from t2;
69
grp	a	c
70
NULL	NULL	
71
1	2	a
72
2	5	c
73
3	9	E
74
2	3	c
75
drop table t1,t2;
76
CREATE TABLE t1 (id int(11),value1 float(10,2));
77
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00);
78
CREATE TABLE t2 (id int(11),name char(20));
79
INSERT INTO t2 VALUES (1,'Set One'),(2,'Set Two');
80
select id, avg(value1), std(value1), variance(value1) from t1 group by id;
81
id	avg(value1)	std(value1)	variance(value1)
82
1	1.0000000	0.816497	0.666667
83
2	11.0000000	0.816497	0.666667
84
select name, avg(value1), std(value1), variance(value1) from t1, t2 where t1.id = t2.id group by t1.id;
85
name	avg(value1)	std(value1)	variance(value1)
86
Set One	1.0000000	0.816497	0.666667
87
Set Two	11.0000000	0.816497	0.666667
88
drop table t1,t2;
89
create table t1 (id int not null);
90
create table t2 (id int not null,rating int null);
91
insert into t1 values(1),(2),(3);
92
insert into t2 values(1, 3),(2, NULL),(2, NULL),(3, 2),(3, NULL);
93
select t1.id, avg(rating) from t1 left join t2 on ( t1.id = t2.id ) group by t1.id;
94
id	avg(rating)
95
1	3.00000
96
2	NULL
97
3	2.00000
98
select sql_small_result t2.id, avg(rating) from t2 group by t2.id;
99
id	avg(rating)
100
1	3.00000
101
2	NULL
102
3	2.00000
103
select sql_big_result t2.id, avg(rating) from t2 group by t2.id;
104
id	avg(rating)
105
1	3.00000
106
2	NULL
107
3	2.00000
108
select sql_small_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
109
id	avg(rating+0.0e0)
110
1	3
111
2	NULL
112
3	2
113
select sql_big_result t2.id, avg(rating+0.0e0) from t2 group by t2.id;
114
id	avg(rating+0.0e0)
115
1	3
116
2	NULL
117
3	2
118
drop table t1,t2;
119
create table t1 (a smallint(6) primary key, c char(10), b text);
120
INSERT INTO t1 VALUES (1,'1','1');
121
INSERT INTO t1 VALUES (2,'2','2');
122
INSERT INTO t1 VALUES (4,'4','4');
123
select count(*) from t1;
124
count(*)
125
3
126
select count(*) from t1 where a = 1;
127
count(*)
128
1
129
select count(*) from t1 where a = 100;
130
count(*)
131
0
132
select count(*) from t1 where a >= 10;
133
count(*)
134
0
135
select count(a) from t1 where a = 1;
136
count(a)
137
1
138
select count(a) from t1 where a = 100;
139
count(a)
140
0
141
select count(a) from t1 where a >= 10;
142
count(a)
143
0
144
select count(b) from t1 where b >= 2;
145
count(b)
146
2
147
select count(b) from t1 where b >= 10;
148
count(b)
149
0
150
select count(c) from t1 where c = 10;
151
count(c)
152
0
153
drop table t1;
154
CREATE TABLE t1 (d DATETIME, i INT);
155
INSERT INTO t1 VALUES (NOW(), 1);
156
SELECT COUNT(i), i, COUNT(i)*i FROM t1 GROUP BY i;
157
COUNT(i)	i	COUNT(i)*i
158
1	1	1
159
SELECT COUNT(i), (i+0), COUNT(i)*(i+0) FROM t1 GROUP BY i;
160
COUNT(i)	(i+0)	COUNT(i)*(i+0)
161
1	1	1
162
DROP TABLE t1;
163
create table t1 (
164
num float(5,2),
165
user char(20)
166
);
167
insert into t1 values (10.3,'nem'),(20.53,'monty'),(30.23,'sinisa');
168
insert into t1 values (30.13,'nem'),(20.98,'monty'),(10.45,'sinisa');
169
insert into t1 values (5.2,'nem'),(8.64,'monty'),(11.12,'sinisa');
170
select sum(num) from t1;
171
sum(num)
172
147.58
173
select sum(num) from t1 group by user;
174
sum(num)
175
50.15
176
45.63
177
51.80
178
drop table t1;
179
create table t1 (a1 int, a2 char(3), key k1(a1), key k2(a2));
180
insert into t1 values(10,'aaa'), (10,null), (10,'bbb'), (20,'zzz');
181
create table t2(a1 char(3), a2 int, a3 real, key k1(a1), key k2(a2, a1));
182
select * from t1;
183
a1	a2
184
10	aaa
185
10	NULL
186
10	bbb
187
20	zzz
188
select min(a2) from t1;
189
min(a2)
190
aaa
191
select max(t1.a1), max(t2.a2) from t1, t2;
192
max(t1.a1)	max(t2.a2)
193
NULL	NULL
194
select max(t1.a1) from t1, t2;
195
max(t1.a1)
196
NULL
197
select max(t2.a2), max(t1.a1) from t1, t2;
198
max(t2.a2)	max(t1.a1)
199
NULL	NULL
200
explain select min(a2) from t1;
201
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
202
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
203
explain select max(t1.a1), max(t2.a2) from t1, t2;
204
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
205
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
206
insert into t2 values('AAA', 10, 0.5);
207
insert into t2 values('BBB', 20, 1.0);
208
select t1.a1, t1.a2, t2.a1, t2.a2 from t1,t2;
209
a1	a2	a1	a2
210
10	aaa	AAA	10
211
10	aaa	BBB	20
212
10	NULL	AAA	10
213
10	NULL	BBB	20
214
10	bbb	AAA	10
215
10	bbb	BBB	20
216
20	zzz	AAA	10
217
20	zzz	BBB	20
218
select max(t1.a1), max(t2.a1) from t1, t2 where t2.a2=9;
219
max(t1.a1)	max(t2.a1)
220
NULL	NULL
221
select max(t2.a1), max(t1.a1) from t1, t2 where t2.a2=9;
222
max(t2.a1)	max(t1.a1)
223
NULL	NULL
224
select t1.a1, t1.a2, t2.a1, t2.a2 from t1 left outer join t2 on t1.a1=10;
225
a1	a2	a1	a2
226
10	aaa	AAA	10
227
10	aaa	BBB	20
228
10	NULL	AAA	10
229
10	NULL	BBB	20
230
10	bbb	AAA	10
231
10	bbb	BBB	20
232
20	zzz	NULL	NULL
233
select max(t1.a2) from t1 left outer join t2 on t1.a1=10;
234
max(t1.a2)
235
zzz
236
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=20;
237
max(t2.a1)
238
BBB
239
select max(t2.a1) from t2 left outer join t1 on t2.a2=10 where t2.a2=10;
240
max(t2.a1)
241
AAA
242
select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1 and 1=0 where t2.a1='AAA';
243
max(t2.a1)
244
NULL
245
select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.a1=10;
246
max(t1.a2)	max(t2.a1)
247
zzz	BBB
248
drop table t1,t2;
249
CREATE TABLE t1 (a int, b int);
250
select count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1;
251
count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
252
0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
253
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
254
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
255
insert into t1 values (1,null);
256
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
257
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
258
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
259
insert into t1 values (1,null);
260
insert into t1 values (2,null);
261
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
262
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
263
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
264
2	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
265
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
266
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
267
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
268
2	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
269
insert into t1 values (2,1);
270
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
271
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
272
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
273
2	1	1	1.00000	0.00000	1	1	1	1
274
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
275
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
276
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
277
2	1	1	1.00000	0.00000	1	1	1	1
278
insert into t1 values (3,1);
279
select a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b) from t1 group by a;
280
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)
281
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0
282
2	1	1	1.00000	0.00000	1	1	1	1
283
3	1	1	1.00000	0.00000	1	1	1	1
284
select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
285
a	count(b)	sum(b)	avg(b)	std(b)	min(b)	max(b)	bit_and(b)	bit_or(b)	bit_xor(b)
286
1	0	NULL	NULL	NULL	NULL	NULL	18446744073709551615	0	0
287
2	1	1	1.00000	0.00000	1	1	1	1	1
288
3	1	1	1.00000	0.00000	1	1	1	1	1
289
explain extended select SQL_BIG_RESULT a,count(b), sum(b), avg(b), std(b), min(b), max(b), bit_and(b), bit_or(b), bit_xor(b) from t1 group by a;
290
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
291
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	100.00	Using filesort
292
Warnings:
293
Note	1003	select sql_big_result `test`.`t1`.`a` AS `a`,count(`test`.`t1`.`b`) AS `count(b)`,sum(`test`.`t1`.`b`) AS `sum(b)`,avg(`test`.`t1`.`b`) AS `avg(b)`,std(`test`.`t1`.`b`) AS `std(b)`,min(`test`.`t1`.`b`) AS `min(b)`,max(`test`.`t1`.`b`) AS `max(b)`,bit_and(`test`.`t1`.`b`) AS `bit_and(b)`,bit_or(`test`.`t1`.`b`) AS `bit_or(b)`,bit_xor(`test`.`t1`.`b`) AS `bit_xor(b)` from `test`.`t1` group by `test`.`t1`.`a`
294
drop table t1;
295
create table t1 (col int);
296
insert into t1 values (-1), (-2), (-3);
297
select bit_and(col), bit_or(col) from t1;
298
bit_and(col)	bit_or(col)
299
18446744073709551612	18446744073709551615
300
select SQL_BIG_RESULT bit_and(col), bit_or(col) from t1 group by col;
301
bit_and(col)	bit_or(col)
302
18446744073709551613	18446744073709551613
303
18446744073709551614	18446744073709551614
304
18446744073709551615	18446744073709551615
305
drop table t1;
306
create table t1 (a int);
307
select avg(2) from t1;
308
avg(2)
309
NULL
310
drop table t1;
311
create table t1(
312
a1 char(3) primary key,
313
a2 smallint,
314
a3 char(3),
315
a4 real,
316
a5 date,
317
key k1(a2,a3),
318
key k2(a4 desc,a1),
319
key k3(a5,a1)
320
);
321
create table t2(
322
a1 char(3) primary key,
323
a2 char(17),
324
a3 char(2),
325
a4 char(3),
326
key k1(a3, a2),
327
key k2(a4)
328
);
329
insert into t1 values('AME',0,'SEA',0.100,date'1942-02-19');
330
insert into t1 values('HBR',1,'SEA',0.085,date'1948-03-05');
331
insert into t1 values('BOT',2,'SEA',0.085,date'1951-11-29');
332
insert into t1 values('BMC',3,'SEA',0.085,date'1958-09-08');
333
insert into t1 values('TWU',0,'LAX',0.080,date'1969-10-05');
334
insert into t1 values('BDL',0,'DEN',0.080,date'1960-11-27');
335
insert into t1 values('DTX',1,'NYC',0.080,date'1961-05-04');
336
insert into t1 values('PLS',1,'WDC',0.075,date'1949-01-02');
337
insert into t1 values('ZAJ',2,'CHI',0.075,date'1960-06-15');
338
insert into t1 values('VVV',2,'MIN',0.075,date'1959-06-28');
339
insert into t1 values('GTM',3,'DAL',0.070,date'1977-09-23');
340
insert into t1 values('SSJ',null,'CHI',null,date'1974-03-19');
341
insert into t1 values('KKK',3,'ATL',null,null);
342
insert into t1 values('XXX',null,'MIN',null,null);
343
insert into t1 values('WWW',1,'LED',null,null);
344
insert into t2 values('TKF','Seattle','WA','AME');
345
insert into t2 values('LCC','Los Angeles','CA','TWU');
346
insert into t2 values('DEN','Denver','CO','BDL');
347
insert into t2 values('SDC','San Diego','CA','TWU');
348
insert into t2 values('NOL','New Orleans','LA','GTM');
349
insert into t2 values('LAK','Los Angeles','CA','TWU');
350
insert into t2 values('AAA','AAA','AA','AME');
351
select * from t1;
352
a1	a2	a3	a4	a5
353
AME	0	SEA	0.1	1942-02-19
354
HBR	1	SEA	0.085	1948-03-05
355
BOT	2	SEA	0.085	1951-11-29
356
BMC	3	SEA	0.085	1958-09-08
357
TWU	0	LAX	0.08	1969-10-05
358
BDL	0	DEN	0.08	1960-11-27
359
DTX	1	NYC	0.08	1961-05-04
360
PLS	1	WDC	0.075	1949-01-02
361
ZAJ	2	CHI	0.075	1960-06-15
362
VVV	2	MIN	0.075	1959-06-28
363
GTM	3	DAL	0.07	1977-09-23
364
SSJ	NULL	CHI	NULL	1974-03-19
365
KKK	3	ATL	NULL	NULL
366
XXX	NULL	MIN	NULL	NULL
367
WWW	1	LED	NULL	NULL
368
select * from t2;
369
a1	a2	a3	a4
370
TKF	Seattle	WA	AME
371
LCC	Los Angeles	CA	TWU
372
DEN	Denver	CO	BDL
373
SDC	San Diego	CA	TWU
374
NOL	New Orleans	LA	GTM
375
LAK	Los Angeles	CA	TWU
376
AAA	AAA	AA	AME
377
explain 
378
select min(a1) from t1;
379
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
380
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
381
select min(a1) from t1;
382
min(a1)
383
AME
384
explain 
385
select max(a4) from t1;
386
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
387
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
388
select max(a4) from t1;
389
max(a4)
390
0.1
391
explain 
392
select min(a5), max(a5) from t1;
393
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
394
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
395
select min(a5), max(a5) from t1;
396
min(a5)	max(a5)
397
1942-02-19	1977-09-23
398
explain 
399
select min(a3) from t1 where a2 = 2;
400
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
401
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
402
select min(a3) from t1 where a2 = 2;
403
min(a3)
404
CHI
405
explain 
406
select min(a1), max(a1) from t1 where a4 = 0.080;
407
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
408
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
409
select min(a1), max(a1) from t1 where a4 = 0.080;
410
min(a1)	max(a1)
411
BDL	TWU
412
explain 
413
select min(t1.a5), max(t2.a3) from t1, t2;
414
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
415
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
416
select min(t1.a5), max(t2.a3) from t1, t2;
417
min(t1.a5)	max(t2.a3)
418
1942-02-19	WA
419
explain 
420
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
421
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
422
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
423
select min(t1.a3), max(t2.a2) from t1, t2 where t1.a2 = 0 and t2.a3 = 'CA';
424
min(t1.a3)	max(t2.a2)
425
DEN	San Diego
426
explain 
427
select min(a1) from t1 where a1 > 'KKK';
428
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
429
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
430
select min(a1) from t1 where a1 > 'KKK';
431
min(a1)
432
PLS
433
explain 
434
select min(a1) from t1 where a1 >= 'KKK';
435
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
436
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
437
select min(a1) from t1 where a1 >= 'KKK';
438
min(a1)
439
KKK
440
explain 
441
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
442
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
443
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
444
select max(a3) from t1 where a2 = 2 and a3 < 'SEA';
445
max(a3)
446
MIN
447
explain 
448
select max(a5) from t1 where a5 < date'1970-01-01';
449
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
450
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
451
select max(a5) from t1 where a5 < date'1970-01-01';
452
max(a5)
453
1969-10-05
454
explain 
455
select max(a3) from t1 where a2 is null;
456
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
457
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
458
select max(a3) from t1 where a2 is null;
459
max(a3)
460
MIN
461
explain 
462
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
463
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
464
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
465
select max(a3) from t1 where a2 = 0 and a3 between 'K' and 'Q';
466
max(a3)
467
LAX
468
explain
469
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
470
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
471
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
472
select min(a1), max(a1) from t1 where a1 between 'A' and 'P';
473
min(a1)	max(a1)
474
AME	KKK
475
explain 
476
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
477
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
478
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
479
select max(a3) from t1 where a3 < 'SEA' and a2 = 2 and a3 <= 'MIN';
480
max(a3)
481
MIN
482
explain 
483
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
484
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
485
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
486
select max(a3) from t1 where a3 = 'MIN' and a2 = 2;
487
max(a3)
488
MIN
489
explain 
490
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
491
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
492
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
493
select max(a3) from t1 where a3 = 'DEN' and a2 = 2;
494
max(a3)
495
NULL
496
explain
497
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
498
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
499
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
500
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 = 'CA';
501
max(t1.a3)	min(t2.a2)
502
CHI	Los Angeles
503
explain
504
select max(a3) from t1 where a2 is null and a2 = 2;
505
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
506
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
507
select max(a3) from t1 where a2 is null and a2 = 2;
508
max(a3)
509
NULL
510
explain
511
select max(a2) from t1 where a2 >= 1;
512
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
513
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
514
select max(a2) from t1 where a2 >= 1;
515
max(a2)
516
3
517
explain
518
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
519
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
521
select min(a3) from t1 where a2 = 2 and a3 < 'SEA';
522
min(a3)
523
CHI
524
explain
525
select min(a3) from t1 where a2 = 4;
526
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
527
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
528
select min(a3) from t1 where a2 = 4;
529
min(a3)
530
NULL
531
explain
532
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
533
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
534
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
535
select min(a3) from t1 where a2 = 2 and a3 > 'SEA';
536
min(a3)
537
NULL
538
explain
539
select (min(a4)+max(a4))/2 from t1;
540
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
541
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
542
select (min(a4)+max(a4))/2 from t1;
543
(min(a4)+max(a4))/2
544
0.085
545
explain
546
select min(a3) from t1 where 2 = a2;
547
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
548
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
549
select min(a3) from t1 where 2 = a2;
550
min(a3)
551
CHI
552
explain
553
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
554
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
555
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
556
select max(a3) from t1 where a2 = 2 and 'SEA' > a3;
557
max(a3)
558
MIN
559
explain
560
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
561
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
562
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No matching min/max row
563
select max(a3) from t1 where a2 = 2 and 'SEA' < a3;
564
max(a3)
565
NULL
566
explain
567
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
568
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
569
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
570
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI';
571
min(a3)
572
CHI
573
explain
574
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
575
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
576
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
577
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 < 'SEA';
578
min(a3)
579
CHI
580
explain
581
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
582
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
583
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
584
select min(a3) from t1 where a2 = 2 and a3 >= 'CHI' and a3 = 'MIN';
585
min(a3)
586
MIN
587
explain
588
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
589
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
590
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
591
select min(a3) from t1 where a2 = 2 and a3 >= 'SEA' and a3 = 'MIN';
592
min(a3)
593
NULL
594
explain
595
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
596
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
597
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
598
select min(t1.a1), min(t2.a4) from t1,t2 where t1.a1 < 'KKK' and t2.a4 < 'KKK';
599
min(t1.a1)	min(t2.a4)
600
AME	AME
601
explain 
602
select min(a1) from t1 where a1 > 'KKK' or a1 < 'XXX';
603
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
604
1	SIMPLE	t1	index	PRIMARY	PRIMARY	3	NULL	15	Using where; Using index
605
explain 
606
select min(a1) from t1 where a1 != 'KKK';
607
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
608
1	SIMPLE	t1	index	PRIMARY	PRIMARY	3	NULL	15	Using where; Using index
609
explain
610
select max(a3) from t1 where a2 < 2 and a3 < 'SEA';
611
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
612
1	SIMPLE	t1	range	k1	k1	3	NULL	6	Using where; Using index
613
explain
614
select max(t1.a3), min(t2.a2) from t1, t2 where t1.a2 = 2 and t1.a3 < 'MIN' and t2.a3 > 'CA';
615
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
616
1	SIMPLE	t1	range	k1	k1	7	NULL	1	Using where; Using index
617
1	SIMPLE	t2	range	k1	k1	3	NULL	4	Using where; Using index; Using join buffer
618
explain
619
select min(a4 - 0.01) from t1;
620
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
621
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using index
622
explain
623
select max(a4 + 0.01) from t1;
624
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
625
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using index
626
explain
627
select min(a3) from t1 where (a2 +1 ) is null;
628
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
629
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
630
explain
631
select min(a3) from t1 where (a2 + 1) = 2;
632
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
633
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
634
explain
635
select min(a3) from t1 where 2 = (a2 + 1);
636
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
637
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
638
explain
639
select min(a2) from t1 where a2 < 2 * a2 - 8;
640
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
641
1	SIMPLE	t1	index	NULL	k1	7	NULL	15	Using where; Using index
642
explain
643
select min(a1) from t1  where a1 between a3 and 'KKK';
644
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
645
1	SIMPLE	t1	ALL	PRIMARY	NULL	NULL	NULL	15	Using where
646
explain
647
select min(a4) from t1  where (a4 + 0.01) between 0.07 and 0.08;
648
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
649
1	SIMPLE	t1	index	NULL	k2	12	NULL	15	Using where; Using index
650
explain
651
select concat(min(t1.a1),min(t2.a4)) from t1, t2 where t2.a4 <> 'AME';
652
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
653
1	SIMPLE	t2	range	k2	k2	4	NULL	6	Using where; Using index
654
1	SIMPLE	t1	index	NULL	PRIMARY	3	NULL	15	Using index; Using join buffer
655
drop table t1, t2;
656
create table t1 (a char(10));
657
insert into t1 values ('a'),('b'),('c');
658
select coercibility(max(a)) from t1;
659
coercibility(max(a))
660
2
661
drop table t1;
662
create table t1 (a char character set latin2);
663
insert into t1 values ('a'),('b');
664
select charset(max(a)), coercibility(max(a)),
665
charset(min(a)), coercibility(min(a)) from t1;
666
charset(max(a))	coercibility(max(a))	charset(min(a))	coercibility(min(a))
667
latin2	2	latin2	2
668
show create table t1;
669
Table	Create Table
670
t1	CREATE TABLE `t1` (
671
  `a` char(1) CHARACTER SET latin2 DEFAULT NULL
672
) ENGINE=MyISAM DEFAULT CHARSET=latin1
673
create table t2 select max(a),min(a) from t1;
674
show create table t2;
675
Table	Create Table
676
t2	CREATE TABLE `t2` (
677
  `max(a)` char(1) CHARACTER SET latin2 DEFAULT NULL,
678
  `min(a)` char(1) CHARACTER SET latin2 DEFAULT NULL
679
) ENGINE=MyISAM DEFAULT CHARSET=latin1
680
drop table t2;
681
create table t2 select concat(a) from t1;
682
show create table t2;
683
Table	Create Table
684
t2	CREATE TABLE `t2` (
685
  `concat(a)` varchar(1) CHARACTER SET latin2 DEFAULT NULL
686
) ENGINE=MyISAM DEFAULT CHARSET=latin1
687
drop table t2,t1;
688
create table t1 (a int);
689
insert into t1 values (1);
690
select max(a) as b from t1 having b=1;
691
b
692
1
693
select a from t1 having a=1;
694
a
695
1
696
drop table t1;
697
create table t1 (a int);
698
select variance(2) from t1;
699
variance(2)
700
NULL
701
select stddev(2) from t1;
702
stddev(2)
703
NULL
704
drop table t1;
705
create table t1 (a int);
706
insert into t1 values (1),(2);
707
prepare stmt1 from 'SELECT COUNT(*) FROM t1';
708
execute stmt1;
709
COUNT(*)
710
2
711
execute stmt1;
712
COUNT(*)
713
2
714
execute stmt1;
715
COUNT(*)
716
2
717
deallocate prepare stmt1;
718
drop table t1;
719
create table t1 (a int, primary key(a));
720
insert into t1 values (1),(2);
721
prepare stmt1 from 'SELECT max(a) FROM t1';
722
execute stmt1;
723
max(a)
724
2
725
execute stmt1;
726
max(a)
727
2
728
execute stmt1;
729
max(a)
730
2
731
deallocate prepare stmt1;
732
drop table t1;
733
CREATE TABLE t1 (a int primary key);
734
INSERT INTO t1 VALUES (1),(2),(3),(4);
735
SELECT MAX(a) FROM t1 WHERE a > 5;
736
MAX(a)
737
NULL
738
SELECT MIN(a) FROM t1 WHERE a < 0;
739
MIN(a)
740
NULL
741
DROP TABLE t1;
742
CREATE TABLE t1 (
743
id int(10) unsigned NOT NULL auto_increment,
744
val enum('one','two','three') NOT NULL default 'one',
745
PRIMARY KEY  (id)
746
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
747
INSERT INTO t1 VALUES
748
(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');
749
select val, count(*) from t1 group by val;
750
val	count(*)
751
one	2
752
two	2
753
three	1
754
drop table t1;
755
CREATE TABLE t1 (
756
id int(10) unsigned NOT NULL auto_increment,
757
val set('one','two','three') NOT NULL default 'one',
758
PRIMARY KEY  (id)
759
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
760
INSERT INTO t1 VALUES
761
(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');
762
select val, count(*) from t1 group by val;
763
val	count(*)
764
one	2
765
two	2
766
three	1
767
drop table t1;
768
create table t1(a int, b datetime);
769
insert into t1 values (1, NOW()), (2, NOW());
770
create table t2 select MAX(b) from t1 group by a;
771
show create table t2;
772
Table	Create Table
773
t2	CREATE TABLE `t2` (
774
  `MAX(b)` datetime DEFAULT NULL
775
) ENGINE=MyISAM DEFAULT CHARSET=latin1
776
drop table t1, t2;
777
create table t1(f1 datetime);
778
insert into t1 values (now());
779
create table t2 select f2 from (select max(now()) f2 from t1) a;
780
show columns from t2;
781
Field	Type	Null	Key	Default	Extra
782
f2	datetime	YES		NULL	
783
drop table t2;
784
create table t2 select f2 from (select now() f2 from t1) a;
785
show columns from t2;
786
Field	Type	Null	Key	Default	Extra
787
f2	datetime	NO		0000-00-00 00:00:00	
788
drop table t2, t1;
789
CREATE TABLE t1(
790
id int PRIMARY KEY,
791
a  int,
792
b  int,
793
INDEX i_b_id(a,b,id),
794
INDEX i_id(a,id)
795
);
796
INSERT INTO t1 VALUES 
797
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
798
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
799
MAX(id)
800
NULL
801
DROP TABLE t1;
802
CREATE TABLE t1(
803
id int PRIMARY KEY,
804
a  int,
805
b  int,
806
INDEX i_id(a,id),
807
INDEX i_b_id(a,b,id)
808
);
809
INSERT INTO t1 VALUES 
810
(1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);
811
SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;
812
MAX(id)
813
NULL
814
DROP TABLE t1;
815
CREATE TABLE t1 (id int PRIMARY KEY, b char(3), INDEX(b));
816
INSERT INTO t1 VALUES (1,'xx'), (2,'aa');
817
SELECT * FROM t1;
818
id	b
819
1	xx
820
2	aa
821
SELECT MAX(b) FROM t1 WHERE b < 'ppppp';
822
MAX(b)
823
aa
824
SHOW WARNINGS;
825
Level	Code	Message
826
SELECT MAX(b) FROM t1 WHERE b < 'pp';
827
MAX(b)
828
aa
829
DROP TABLE t1;
830
CREATE TABLE t1 (id int PRIMARY KEY, b char(16), INDEX(b(4)));
831
INSERT INTO t1 VALUES (1, 'xxxxbbbb'), (2, 'xxxxaaaa');
832
SELECT MAX(b) FROM t1;
833
MAX(b)
834
xxxxbbbb
835
EXPLAIN SELECT MAX(b) FROM t1;
836
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
837
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	
838
DROP TABLE t1;
839
CREATE TABLE t1 (id int , b varchar(512), INDEX(b(250))) COLLATE latin1_bin;
840
INSERT INTO t1 VALUES
841
(1,CONCAT(REPEAT('_', 250), "qq")), (1,CONCAT(REPEAT('_', 250), "zz")),
842
(1,CONCAT(REPEAT('_', 250), "aa")), (1,CONCAT(REPEAT('_', 250), "ff"));
843
SELECT MAX(b) FROM t1;
844
MAX(b)
845
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________zz
846
EXPLAIN SELECT MAX(b) FROM t1;
847
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
848
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	4	
849
DROP TABLE t1;
850
CREATE TABLE t1 (a INT, b INT);
851
INSERT INTO t1 VALUES (1,1),(1,2),(2,3);
852
SELECT (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
853
(SELECT COUNT(DISTINCT t1.b))
854
2
855
1
856
SELECT (SELECT COUNT(DISTINCT 12)) FROM t1 GROUP BY t1.a;
857
(SELECT COUNT(DISTINCT 12))
858
1
859
1
860
SELECT AVG(2), BIT_AND(2), BIT_OR(2), BIT_XOR(2), COUNT(*), COUNT(12),
861
COUNT(DISTINCT 12), MIN(2),MAX(2),STD(2), VARIANCE(2),SUM(2),
862
GROUP_CONCAT(2),GROUP_CONCAT(DISTINCT 2);
863
AVG(2)	BIT_AND(2)	BIT_OR(2)	BIT_XOR(2)	COUNT(*)	COUNT(12)	COUNT(DISTINCT 12)	MIN(2)	MAX(2)	STD(2)	VARIANCE(2)	SUM(2)	GROUP_CONCAT(2)	GROUP_CONCAT(DISTINCT 2)
864
2.00000	2	2	2	1	1	1	2	2	0.00000	0.00000	2	2	2
865
DROP TABLE t1;
866
create table t2 (ff double);
867
insert into t2 values (2.2);
868
select cast(sum(distinct ff) as decimal(5,2)) from t2;
869
cast(sum(distinct ff) as decimal(5,2))
870
2.20
871
select cast(sum(distinct ff) as signed) from t2;
872
cast(sum(distinct ff) as signed)
873
2
874
select cast(variance(ff) as decimal(10,3)) from t2;
875
cast(variance(ff) as decimal(10,3))
876
0.000
877
select cast(min(ff) as decimal(5,2)) from t2;
878
cast(min(ff) as decimal(5,2))
879
2.20
880
create table t1 (df decimal(5,1));
881
insert into t1 values(1.1);
882
insert into t1 values(2.2);
883
select cast(sum(distinct df) as signed) from t1;
884
cast(sum(distinct df) as signed)
885
3
886
select cast(min(df) as signed) from t1;
887
cast(min(df) as signed)
1.1.5 by Marc Deslauriers
Import upstream version 5.1.61
888
1
1 by Chuck Short
Import upstream version 5.1.30
889
select 1e8 * sum(distinct df) from t1;
890
1e8 * sum(distinct df)
891
330000000
892
select 1e8 * min(df) from t1;
893
1e8 * min(df)
894
110000000
895
create table t3 (ifl int);
896
insert into t3 values(1), (2);
897
select cast(min(ifl) as decimal(5,2)) from t3;
898
cast(min(ifl) as decimal(5,2))
899
1.00
900
drop table t1, t2, t3;
901
CREATE TABLE t1 (id int(11),value1 float(10,2));
902
INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);
903
select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id;
904
id	stddev_pop(value1)	var_pop(value1)	stddev_samp(value1)	var_samp(value1)
905
1	0.816497	0.666667	1.000000	1.000000
906
2	1.118034	1.250000	1.290994	1.666667
907
DROP TABLE t1;
908
CREATE TABLE t1 (col1 decimal(16,12));
909
INSERT INTO t1 VALUES (-5.00000000001),(-5.00000000002),(-5.00000000003),(-5.00000000000),(-5.00000000001),(-5.00000000002);
910
insert into t1 select * from t1;
911
select col1,count(col1),sum(col1),avg(col1) from t1 group by col1;
912
col1	count(col1)	sum(col1)	avg(col1)
913
-5.000000000030	2	-10.000000000060	-5.00000000003000000
914
-5.000000000020	4	-20.000000000080	-5.00000000002000000
915
-5.000000000010	4	-20.000000000040	-5.00000000001000000
916
-5.000000000000	2	-10.000000000000	-5.00000000000000000
917
DROP TABLE t1;
918
create table t1 (col1 decimal(16,12));
919
insert into t1 values (-5.00000000001);
920
insert into t1 values (-5.00000000001);
921
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
922
col1	sum(col1)	max(col1)	min(col1)
923
-5.000000000010	-10.000000000020	-5.000000000010	-5.000000000010
924
delete from t1;
925
insert into t1 values (5.00000000001);
926
insert into t1 values (5.00000000001);
927
select col1,sum(col1),max(col1),min(col1) from t1 group by col1;
928
col1	sum(col1)	max(col1)	min(col1)
929
5.000000000010	10.000000000020	5.000000000010	5.000000000010
930
DROP TABLE t1;
931
CREATE TABLE t1 (a VARCHAR(400));
932
INSERT INTO t1 (a) VALUES ("A"), ("a"), ("a "), ("a   "),
933
("B"), ("b"), ("b "), ("b   ");
934
SELECT COUNT(DISTINCT a) FROM t1;
935
COUNT(DISTINCT a)
936
2
937
DROP TABLE t1;
938
CREATE TABLE t1 (a int, b int, c int);
939
INSERT INTO t1 (a, b, c) VALUES
940
(1,1,1), (1,1,2), (1,1,3),
941
(1,2,1), (1,2,2), (1,2,3),
942
(1,3,1), (1,3,2), (1,3,3),
943
(2,1,1), (2,1,2), (2,1,3),
944
(2,2,1), (2,2,2), (2,2,3),
945
(2,3,1), (2,3,2), (2,3,3),
946
(3,1,1), (3,1,2), (3,1,3),
947
(3,2,1), (3,2,2), (3,2,3),
948
(3,3,1), (3,3,2), (3,3,3);
949
SELECT b/c as v, a FROM t1 ORDER BY v;
950
v	a
951
0.33333	3
952
0.33333	1
953
0.33333	2
954
0.50000	1
955
0.50000	2
956
0.50000	3
957
0.66667	2
958
0.66667	1
959
0.66667	3
960
1.00000	3
961
1.00000	2
962
1.00000	3
963
1.00000	1
964
1.00000	2
965
1.00000	3
966
1.00000	2
967
1.00000	1
968
1.00000	1
969
1.50000	3
970
1.50000	2
971
1.50000	1
972
2.00000	1
973
2.00000	3
974
2.00000	2
975
3.00000	3
976
3.00000	2
977
3.00000	1
978
SELECT b/c as v, SUM(a) FROM t1 GROUP BY v;
979
v	SUM(a)
980
0.33333	6
981
0.50000	6
982
0.66667	6
983
1.00000	18
984
1.50000	6
985
2.00000	6
986
3.00000	6
987
SELECT SUM(a) FROM t1 GROUP BY b/c;
988
SUM(a)
989
6
990
6
991
6
992
18
993
6
994
6
995
6
996
DROP TABLE t1;
997
set div_precision_increment= @sav_dpi;
998
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
999
INSERT INTO t1 VALUES (1,1), (2,2);
1000
CREATE TABLE t2 (a INT PRIMARY KEY, b INT);
1001
INSERT INTO t2 VALUES (1,1), (3,3);
1002
SELECT SQL_NO_CACHE 
1003
(SELECT SUM(c.a) FROM t1 ttt, t2 ccc 
1004
WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid   
1005
FROM t1 t, t2 c WHERE t.a = c.b;
1006
minid
1007
1
1008
DROP TABLE t1,t2;
1009
create table t1 select variance(0);
1010
show create table t1;
1011
Table	Create Table
1012
t1	CREATE TABLE `t1` (
1013
  `variance(0)` double(8,4) DEFAULT NULL
1014
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1015
drop table t1;
1016
create table t1 select stddev(0);
1017
show create table t1;
1018
Table	Create Table
1019
t1	CREATE TABLE `t1` (
1020
  `stddev(0)` double(8,4) DEFAULT NULL
1021
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1022
drop table t1;
1023
create table bug22555 (i smallint primary key auto_increment, s1 smallint, s2 smallint, e decimal(30,10), o double);
1024
insert into bug22555 (s1, s2, e, o) values (53, 78, 11.4276528, 6.828112), (17, 78, 5.916793, 1.8502951), (18, 76, 2.679231, 9.17975591), (31, 62, 6.07831, 0.1), (19, 41, 5.37463, 15.1), (83, 73, 14.567426, 7.959222), (92, 53, 6.10151, 13.1856852), (7, 12, 13.92272, 3.442007), (92, 35, 11.95358909, 6.01376678), (38, 84, 2.572, 7.904571);
1025
select std(s1/s2) from bug22555 group by i;
1026
std(s1/s2)
1027
0.00000000
1028
0.00000000
1029
0.00000000
1030
0.00000000
1031
0.00000000
1032
0.00000000
1033
0.00000000
1034
0.00000000
1035
0.00000000
1036
0.00000000
1037
select std(e) from bug22555 group by i;
1038
std(e)
1039
0.00000000000000
1040
0.00000000000000
1041
0.00000000000000
1042
0.00000000000000
1043
0.00000000000000
1044
0.00000000000000
1045
0.00000000000000
1046
0.00000000000000
1047
0.00000000000000
1048
0.00000000000000
1049
select std(o) from bug22555 group by i;
1050
std(o)
1051
0
1052
0
1053
0
1054
0
1055
0
1056
0
1057
0
1058
0
1059
0
1060
0
1061
drop table bug22555;
1062
create table bug22555 (i smallint, s1 smallint, s2 smallint, o1 double, o2 double, e1 decimal, e2 decimal);
1063
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1064
select i, count(*) from bug22555 group by i;
1065
i	count(*)
1066
1	1
1067
2	1
1068
3	1
1069
select std(s1/s2) from bug22555 where i=1;
1070
std(s1/s2)
1071
0.00000000
1072
select std(s1/s2) from bug22555 where i=2;
1073
std(s1/s2)
1074
0.00000000
1075
select std(s1/s2) from bug22555 where i=3;
1076
std(s1/s2)
1077
0.00000000
1078
select std(s1/s2) from bug22555 where i=1 group by i;
1079
std(s1/s2)
1080
0.00000000
1081
select std(s1/s2) from bug22555 where i=2 group by i;
1082
std(s1/s2)
1083
0.00000000
1084
select std(s1/s2) from bug22555 where i=3 group by i;
1085
std(s1/s2)
1086
0.00000000
1087
select std(s1/s2) from bug22555 group by i order by i;
1088
std(s1/s2)
1089
0.00000000
1090
0.00000000
1091
0.00000000
1092
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1093
i	count(*)	std(o1/o2)
1094
1	1	0
1095
2	1	0
1096
3	1	0
1097
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1098
i	count(*)	std(e1/e2)
1099
1	1	0.00000000
1100
2	1	0.00000000
1101
3	1	0.00000000
1102
set @saved_div_precision_increment=@@div_precision_increment;
1103
set div_precision_increment=19;
1104
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
1105
i	count(*)	variance(s1/s2)
1106
1	1	0.000000000000000000000000000000
1107
2	1	0.000000000000000000000000000000
1108
3	1	0.000000000000000000000000000000
1109
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1110
i	count(*)	variance(o1/o2)
1111
1	1	0
1112
2	1	0
1113
3	1	0
1114
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1115
i	count(*)	variance(e1/e2)
1116
1	1	0.000000000000000000000000000000
1117
2	1	0.000000000000000000000000000000
1118
3	1	0.000000000000000000000000000000
1119
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1120
i	count(*)	std(s1/s2)
1121
1	1	0.000000000000000000000000000000
1122
2	1	0.000000000000000000000000000000
1123
3	1	0.000000000000000000000000000000
1124
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1125
i	count(*)	std(o1/o2)
1126
1	1	0
1127
2	1	0
1128
3	1	0
1129
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1130
i	count(*)	std(e1/e2)
1131
1	1	0.000000000000000000000000000000
1132
2	1	0.000000000000000000000000000000
1133
3	1	0.000000000000000000000000000000
1134
set div_precision_increment=20;
1135
select i, count(*), variance(s1/s2) from bug22555 group by i order by i;
1136
i	count(*)	variance(s1/s2)
1137
1	1	0.000000000000000000000000000000
1138
2	1	0.000000000000000000000000000000
1139
3	1	0.000000000000000000000000000000
1140
select i, count(*), variance(o1/o2) from bug22555 group by i order by i;
1141
i	count(*)	variance(o1/o2)
1142
1	1	0
1143
2	1	0
1144
3	1	0
1145
select i, count(*), variance(e1/e2) from bug22555 group by i order by i;
1146
i	count(*)	variance(e1/e2)
1147
1	1	0.000000000000000000000000000000
1148
2	1	0.000000000000000000000000000000
1149
3	1	0.000000000000000000000000000000
1150
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1151
i	count(*)	std(s1/s2)
1152
1	1	0.000000000000000000000000000000
1153
2	1	0.000000000000000000000000000000
1154
3	1	0.000000000000000000000000000000
1155
select i, count(*), std(o1/o2) from bug22555 group by i order by i;
1156
i	count(*)	std(o1/o2)
1157
1	1	0
1158
2	1	0
1159
3	1	0
1160
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1161
i	count(*)	std(e1/e2)
1162
1	1	0.000000000000000000000000000000
1163
2	1	0.000000000000000000000000000000
1164
3	1	0.000000000000000000000000000000
1165
set @@div_precision_increment=@saved_div_precision_increment;
1166
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1167
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1168
insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);
1169
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1170
i	count(*)	std(s1/s2)
1171
1	4	0.00000000
1172
2	4	0.00000000
1173
3	4	0.00000000
1174
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1175
i	count(*)	round(std(o1/o2), 16)
1176
1	4	0.0000000000000000
1177
2	4	0.0000000000000000
1178
3	4	0.0000000000000000
1179
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1180
i	count(*)	std(e1/e2)
1181
1	4	0.00000000
1182
2	4	0.00000000
1183
3	4	0.00000000
1184
select std(s1/s2) from bug22555;
1185
std(s1/s2)
1186
0.21325764
1187
select std(o1/o2) from bug22555;
1188
std(o1/o2)
0.2.1 by Norbert Tretkowski
Import upstream version 5.1.34
1189
0.213257635866493
1 by Chuck Short
Import upstream version 5.1.30
1190
select std(e1/e2) from bug22555;
1191
std(e1/e2)
1192
0.21325764
1193
set @saved_div_precision_increment=@@div_precision_increment;
1194
set div_precision_increment=19;
1195
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1196
i	count(*)	std(s1/s2)
1197
1	4	0.000000000000000000000000000000
1198
2	4	0.000000000000000000000000000000
1199
3	4	0.000000000000000000000000000000
1200
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1201
i	count(*)	round(std(o1/o2), 16)
1202
1	4	0.0000000000000000
1203
2	4	0.0000000000000000
1204
3	4	0.0000000000000000
1205
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1206
i	count(*)	std(e1/e2)
1207
1	4	0.000000000000000000000000000000
1208
2	4	0.000000000000000000000000000000
1209
3	4	0.000000000000000000000000000000
1210
select round(std(s1/s2), 17) from bug22555;
1211
round(std(s1/s2), 17)
1212
0.21325763586649341
1213
select std(o1/o2) from bug22555;
1214
std(o1/o2)
0.2.1 by Norbert Tretkowski
Import upstream version 5.1.34
1215
0.213257635866493
1 by Chuck Short
Import upstream version 5.1.30
1216
select round(std(e1/e2), 17) from bug22555;
1217
round(std(e1/e2), 17)
1218
0.21325763586649341
1219
set div_precision_increment=20;
1220
select i, count(*), std(s1/s2) from bug22555 group by i order by i;
1221
i	count(*)	std(s1/s2)
1222
1	4	0.000000000000000000000000000000
1223
2	4	0.000000000000000000000000000000
1224
3	4	0.000000000000000000000000000000
1225
select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;
1226
i	count(*)	round(std(o1/o2), 16)
1227
1	4	0.0000000000000000
1228
2	4	0.0000000000000000
1229
3	4	0.0000000000000000
1230
select i, count(*), std(e1/e2) from bug22555 group by i order by i;
1231
i	count(*)	std(e1/e2)
1232
1	4	0.000000000000000000000000000000
1233
2	4	0.000000000000000000000000000000
1234
3	4	0.000000000000000000000000000000
1235
select round(std(s1/s2), 17) from bug22555;
1236
round(std(s1/s2), 17)
1237
0.21325763586649341
1238
select std(o1/o2) from bug22555;
1239
std(o1/o2)
0.2.1 by Norbert Tretkowski
Import upstream version 5.1.34
1240
0.213257635866493
1 by Chuck Short
Import upstream version 5.1.30
1241
select round(std(e1/e2), 17) from bug22555;
1242
round(std(e1/e2), 17)
1243
0.21325763586649341
1244
set @@div_precision_increment=@saved_div_precision_increment;
1245
drop table bug22555;
1246
create table bug22555 (s smallint, o double, e decimal);
1247
insert into bug22555 values (1,1,1),(2,2,2),(3,3,3),(6,6,6),(7,7,7);
1248
select var_samp(s), var_pop(s) from bug22555;
1249
var_samp(s)	var_pop(s)
1250
6.7000	5.3600
1251
select var_samp(o), var_pop(o) from bug22555;
1252
var_samp(o)	var_pop(o)
1253
6.7	5.36
1254
select var_samp(e), var_pop(e) from bug22555;
1255
var_samp(e)	var_pop(e)
1256
6.7000	5.3600
1257
drop table bug22555;
1258
create table bug22555 (s smallint, o double, e decimal);
1259
insert into bug22555 values (null,null,null),(null,null,null);
1260
select var_samp(s) as 'null', var_pop(s) as 'null' from bug22555;
1261
null	null
1262
NULL	NULL
1263
select var_samp(o) as 'null', var_pop(o) as 'null' from bug22555;
1264
null	null
1265
NULL	NULL
1266
select var_samp(e) as 'null', var_pop(e) as 'null' from bug22555;
1267
null	null
1268
NULL	NULL
1269
insert into bug22555 values (1,1,1);
1270
select var_samp(s) as 'null', var_pop(s) as '0' from bug22555;
1271
null	0
1272
NULL	0.0000
1273
select var_samp(o) as 'null', var_pop(o) as '0' from bug22555;
1274
null	0
1275
NULL	0
1276
select var_samp(e) as 'null', var_pop(e) as '0' from bug22555;
1277
null	0
1278
NULL	0.0000
1279
insert into bug22555 values (2,2,2);
1280
select var_samp(s) as '0.5', var_pop(s) as '0.25' from bug22555;
1281
0.5	0.25
1282
0.5000	0.2500
1283
select var_samp(o) as '0.5', var_pop(o) as '0.25' from bug22555;
1284
0.5	0.25
1285
0.5	0.25
1286
select var_samp(e) as '0.5', var_pop(e) as '0.25' from bug22555;
1287
0.5	0.25
1288
0.5000	0.2500
1289
drop table bug22555;
1290
create table t1 (a decimal(20));
1291
insert into t1 values (12345678901234567890);
1292
select count(a) from t1;
1293
count(a)
1294
1
1295
select count(distinct a) from t1;
1296
count(distinct a)
1297
1
1298
drop table t1;
1299
CREATE TABLE t1 (a INT, b INT);
1300
INSERT INTO t1 VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8);
1301
INSERT INTO t1 SELECT a, b+8       FROM t1;
1302
INSERT INTO t1 SELECT a, b+16      FROM t1;
1303
INSERT INTO t1 SELECT a, b+32      FROM t1;
1304
INSERT INTO t1 SELECT a, b+64      FROM t1;
1305
INSERT INTO t1 SELECT a, b+128     FROM t1;
1306
INSERT INTO t1 SELECT a, b+256     FROM t1;
1307
INSERT INTO t1 SELECT a, b+512     FROM t1;
1308
INSERT INTO t1 SELECT a, b+1024    FROM t1;
1309
INSERT INTO t1 SELECT a, b+2048    FROM t1;
1310
INSERT INTO t1 SELECT a, b+4096    FROM t1;
1311
INSERT INTO t1 SELECT a, b+8192    FROM t1;
1312
INSERT INTO t1 SELECT a, b+16384   FROM t1;
1313
INSERT INTO t1 SELECT a, b+32768   FROM t1;
1314
SELECT a,COUNT(DISTINCT b) AS cnt FROM t1 GROUP BY a HAVING cnt > 50;
1315
a	cnt
1316
1	65536
1317
SELECT a,SUM(DISTINCT b) AS sumation FROM t1 GROUP BY a HAVING sumation > 50;
1318
a	sumation
1319
1	2147516416
1320
SELECT a,AVG(DISTINCT b) AS average FROM t1 GROUP BY a HAVING average > 50;
1321
a	average
1322
1	32768.5000
1323
DROP TABLE t1;
1324
CREATE TABLE t1 ( a INT, b INT, KEY(a) );
1325
INSERT INTO t1 VALUES (NULL, 1), (NULL, 2);
1326
EXPLAIN SELECT MIN(a), MIN(b) FROM t1;
1327
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1328
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	2	
1329
SELECT MIN(a), MIN(b) FROM t1;
1330
MIN(a)	MIN(b)
1331
NULL	1
1332
CREATE TABLE t2( a INT, b INT, c INT, KEY(a, b) );
1333
INSERT INTO t2 ( a, b, c ) VALUES ( 1, NULL, 2 ), ( 1, 3, 4 ), ( 1, 4, 4 );
1334
EXPLAIN SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1335
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1336
1	SIMPLE	t2	ref	a	a	5	const	2	Using where
1337
SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;
1338
MIN(b)	MIN(c)
1339
3	2
1340
CREATE TABLE t3 (a INT, b INT, c int, KEY(a, b));
1341
INSERT INTO t3 VALUES (1, NULL, 1), (2, NULL, 2),  (2, NULL, 2),  (3, NULL, 3);
1342
EXPLAIN SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1343
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1344
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1345
SELECT MIN(a), MIN(b) FROM t3 where a = 2;
1346
MIN(a)	MIN(b)
1347
2	NULL
1348
CREATE TABLE t4 (a INT, b INT, c int, KEY(a, b));
1349
INSERT INTO t4 VALUES (1, 1, 1), (2, NULL, 2),  (2, NULL, 2),  (3, 1, 3);
1350
EXPLAIN SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1351
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1352
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1353
SELECT MIN(a), MIN(b) FROM t4 where a = 2;
1354
MIN(a)	MIN(b)
1355
2	NULL
1356
SELECT MIN(b), min(c) FROM t4 where a = 2;
1357
MIN(b)	min(c)
1358
NULL	2
1359
CREATE TABLE t5( a INT, b INT, KEY( a, b) );
1360
INSERT INTO t5 VALUES( 1, 1 ), ( 1, 2 );
1361
EXPLAIN SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1362
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1363
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1364
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;
1365
MIN(a)	MIN(b)
1366
1	1
1367
SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1 and b > 1;
1368
MIN(a)	MIN(b)
1369
1	2
1370
DROP TABLE t1, t2, t3, t4, t5;
1371
CREATE TABLE t1 (a INT);
1372
INSERT INTO t1 values (),(),();
1373
SELECT (SELECT SLEEP(0) FROM t1 ORDER BY AVG(DISTINCT a) ) as x FROM t1 
1374
GROUP BY x;
1375
x
1376
0
1377
SELECT 1 FROM t1 GROUP BY (SELECT SLEEP(0) FROM t1 ORDER BY AVG(DISTINCT a) );
1378
1
1379
1
1380
DROP TABLE t1;
1381
CREATE TABLE t1 (a int, b date NOT NULL, KEY k1 (a,b));
1382
SELECT MIN(b) FROM t1 WHERE a=1 AND b>'2007-08-01';
1383
MIN(b)
1384
NULL
1385
DROP TABLE t1;
1386
CREATE TABLE t1 (a INT);
1387
INSERT INTO t1 VALUES (1),(2),(3),(4);
1388
SET SQL_MODE=ONLY_FULL_GROUP_BY;
1389
SELECT a FROM t1 HAVING COUNT(*)>2;
1390
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1391
SELECT COUNT(*), a FROM t1;
1392
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1393
SET SQL_MODE=DEFAULT;
1394
SELECT a FROM t1 HAVING COUNT(*)>2;
1395
a
1396
1
1397
SELECT COUNT(*), a FROM t1;
1398
COUNT(*)	a
1399
4	1
1400
DROP TABLE t1;
1401
set SQL_MODE=ONLY_FULL_GROUP_BY;
1402
CREATE TABLE t1 (a INT);
1403
INSERT INTO t1 VALUES (1),(2),(3),(4);
1404
CREATE VIEW v1 AS SELECT a,(a + 1) AS y FROM t1;
1405
EXPLAIN EXTENDED SELECT y FROM v1 GROUP BY v1.y;
1406
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1407
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary; Using filesort
1408
Warnings:
1409
Note	1003	select (`test`.`t1`.`a` + 1) AS `y` from `test`.`t1` group by (`test`.`t1`.`a` + 1)
1410
DROP VIEW v1;
1411
DROP TABLE t1;
1412
SET SQL_MODE=DEFAULT;
1413
CREATE TABLE t1(a DOUBLE);
1414
INSERT INTO t1 VALUES (10), (20);
1415
SELECT AVG(a), CAST(AVG(a) AS DECIMAL) FROM t1;
1416
AVG(a)	CAST(AVG(a) AS DECIMAL)
1417
15	15
1418
DROP TABLE t1;
1419
CREATE TABLE derived1 (a bigint(21));
1420
INSERT INTO derived1 VALUES (2);
1421
CREATE TABLE D (
1422
pk int(11) NOT NULL AUTO_INCREMENT,
1423
int_nokey int(11) DEFAULT NULL,
1424
int_key int(11) DEFAULT NULL,
1425
filler blob,
1426
PRIMARY KEY (pk),
1427
KEY int_key (int_key)
1428
);
1429
INSERT INTO D VALUES 
1430
(39,40,4,repeat('  X', 42)),
1431
(43,56,4,repeat('  X', 42)),
1432
(47,12,4,repeat('  X', 42)),
1433
(71,28,4,repeat('  X', 42)),
1434
(76,54,4,repeat('  X', 42)),
1435
(83,45,4,repeat('  X', 42)),
1436
(105,53,12,NULL);
1437
SELECT 
1438
(SELECT COUNT( int_nokey ) 
1439
FROM derived1 AS X 
1440
WHERE 
1441
X.int_nokey < 61 
1442
GROUP BY pk 
1443
LIMIT 1) 
1444
FROM D AS X 
1445
WHERE X.int_key < 13  
1446
GROUP BY int_nokey LIMIT 1;
1447
(SELECT COUNT( int_nokey ) 
1448
FROM derived1 AS X 
1449
WHERE 
1450
X.int_nokey < 61 
1451
GROUP BY pk 
1452
LIMIT 1)
1453
1
1454
DROP TABLE derived1;
1455
DROP TABLE D;
1.1.1 by Mathias Gug
Import upstream version 5.1.31
1456
CREATE TABLE t1 (a INT, b INT);
1457
INSERT INTO t1 VALUES (1,1), (1,2), (1,3);
1458
SET SQL_MODE='ONLY_FULL_GROUP_BY';
1459
SELECT COUNT(*) FROM t1;
1460
COUNT(*)
1461
3
1462
SELECT COUNT(*) FROM t1 where a=1;
1463
COUNT(*)
1464
3
1465
SELECT COUNT(*),a FROM t1;
1466
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1467
SELECT COUNT(*) FROM t1 a JOIN t1 b ON a.a= b.a;
1468
COUNT(*)
1469
9
1470
SELECT COUNT(*), (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a) 
1471
FROM t1 outr;
1472
ERROR 42000: Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
1473
SELECT COUNT(*) FROM t1 a JOIN t1 outr 
1474
ON a.a= (SELECT count(*) FROM t1 inr WHERE inr.a = outr.a);
1475
COUNT(*)
1476
0
1477
SET SQL_MODE=default;
1478
DROP TABLE t1;
1 by Chuck Short
Import upstream version 5.1.30
1479
End of 5.0 tests
0.2.6 by Norbert Tretkowski
Import upstream version 5.1.41
1480
#
1481
# BUG#47280 - strange results from count(*) with order by multiple 
1482
#             columns without where/group
1483
# 
1484
# 
1485
# Initialize test
1486
# 
1487
CREATE TABLE t1 (
1488
pk INT NOT NULL,
1489
i INT,
1490
PRIMARY KEY (pk)
1491
);
1492
INSERT INTO t1 VALUES (1,11),(2,12),(3,13);
1493
#
1494
# Start test
1495
# All the following queries shall return 1 record
1496
#
1497
1498
# Masking all correct values {11...13} for column i in this result. 
1499
SELECT MAX(pk) as max, i
1500
FROM t1
1501
ORDER BY max;
1502
max	i
1503
3	#
1504
1505
EXPLAIN
1506
SELECT MAX(pk) as max, i
1507
FROM t1
1508
ORDER BY max;
1509
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1510
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using temporary
1511
1512
# Only 11 is correct for collumn i in this result
1513
SELECT MAX(pk) as max, i
1514
FROM t1
1515
WHERE pk<2
1516
ORDER BY max;
1517
max	i
1518
1	11
1519
#
1520
# Cleanup
1521
#
1522
DROP TABLE t1;
1.1.5 by Marc Deslauriers
Import upstream version 5.1.61
1523
#
1524
# Bug#43668: Wrong comparison and MIN/MAX for YEAR(2)
1525
#
1526
create table t1 (f1 year(2), f2 year(4), f3 date, f4 datetime);
1527
insert into t1 values
1528
(98,1998,19980101,"1998-01-01 00:00:00"),
1529
(00,2000,20000101,"2000-01-01 00:00:01"),
1530
(02,2002,20020101,"2002-01-01 23:59:59"),
1531
(60,2060,20600101,"2060-01-01 11:11:11"),
1532
(70,1970,19700101,"1970-11-11 22:22:22"),
1533
(NULL,NULL,NULL,NULL);
1534
select min(f1),max(f1) from t1;
1535
min(f1)	max(f1)
1536
70	60
1537
select min(f2),max(f2) from t1;
1538
min(f2)	max(f2)
1539
1970	2060
1540
select min(f3),max(f3) from t1;
1541
min(f3)	max(f3)
1542
1970-01-01	2060-01-01
1543
select min(f4),max(f4) from t1;
1544
min(f4)	max(f4)
1545
1970-11-11 22:22:22	2060-01-01 11:11:11
1546
select a.f1 as a, b.f1 as b, a.f1 > b.f1 as gt,
1547
a.f1 < b.f1 as lt, a.f1<=>b.f1 as eq
1548
from t1 a, t1 b;
1549
a	b	gt	lt	eq
1550
98	98	0	0	1
1551
00	98	1	0	0
1552
02	98	1	0	0
1553
60	98	1	0	0
1554
70	98	0	1	0
1555
NULL	98	NULL	NULL	0
1556
98	00	0	1	0
1557
00	00	0	0	1
1558
02	00	1	0	0
1559
60	00	1	0	0
1560
70	00	0	1	0
1561
NULL	00	NULL	NULL	0
1562
98	02	0	1	0
1563
00	02	0	1	0
1564
02	02	0	0	1
1565
60	02	1	0	0
1566
70	02	0	1	0
1567
NULL	02	NULL	NULL	0
1568
98	60	0	1	0
1569
00	60	0	1	0
1570
02	60	0	1	0
1571
60	60	0	0	1
1572
70	60	0	1	0
1573
NULL	60	NULL	NULL	0
1574
98	70	1	0	0
1575
00	70	1	0	0
1576
02	70	1	0	0
1577
60	70	1	0	0
1578
70	70	0	0	1
1579
NULL	70	NULL	NULL	0
1580
98	NULL	NULL	NULL	0
1581
00	NULL	NULL	NULL	0
1582
02	NULL	NULL	NULL	0
1583
60	NULL	NULL	NULL	0
1584
70	NULL	NULL	NULL	0
1585
NULL	NULL	NULL	NULL	1
1586
select a.f1 as a, b.f2 as b, a.f1 > b.f2 as gt,
1587
a.f1 < b.f2 as lt, a.f1<=>b.f2 as eq
1588
from t1 a, t1 b;
1589
a	b	gt	lt	eq
1590
98	1998	0	0	1
1591
00	1998	1	0	0
1592
02	1998	1	0	0
1593
60	1998	1	0	0
1594
70	1998	0	1	0
1595
NULL	1998	NULL	NULL	0
1596
98	2000	0	1	0
1597
00	2000	0	0	1
1598
02	2000	1	0	0
1599
60	2000	1	0	0
1600
70	2000	0	1	0
1601
NULL	2000	NULL	NULL	0
1602
98	2002	0	1	0
1603
00	2002	0	1	0
1604
02	2002	0	0	1
1605
60	2002	1	0	0
1606
70	2002	0	1	0
1607
NULL	2002	NULL	NULL	0
1608
98	2060	0	1	0
1609
00	2060	0	1	0
1610
02	2060	0	1	0
1611
60	2060	0	0	1
1612
70	2060	0	1	0
1613
NULL	2060	NULL	NULL	0
1614
98	1970	1	0	0
1615
00	1970	1	0	0
1616
02	1970	1	0	0
1617
60	1970	1	0	0
1618
70	1970	0	0	1
1619
NULL	1970	NULL	NULL	0
1620
98	NULL	NULL	NULL	0
1621
00	NULL	NULL	NULL	0
1622
02	NULL	NULL	NULL	0
1623
60	NULL	NULL	NULL	0
1624
70	NULL	NULL	NULL	0
1625
NULL	NULL	NULL	NULL	1
1626
select a.f1 as a, b.f3 as b, a.f1 > b.f3 as gt,
1627
a.f1 < b.f3 as lt, a.f1<=>b.f3 as eq
1628
from t1 a, t1 b;
1629
a	b	gt	lt	eq
1630
98	1998-01-01	0	1	0
1631
00	1998-01-01	1	0	0
1632
02	1998-01-01	1	0	0
1633
60	1998-01-01	1	0	0
1634
70	1998-01-01	0	1	0
1635
NULL	1998-01-01	NULL	NULL	0
1636
98	2000-01-01	0	1	0
1637
00	2000-01-01	0	1	0
1638
02	2000-01-01	1	0	0
1639
60	2000-01-01	1	0	0
1640
70	2000-01-01	0	1	0
1641
NULL	2000-01-01	NULL	NULL	0
1642
98	2002-01-01	0	1	0
1643
00	2002-01-01	0	1	0
1644
02	2002-01-01	0	1	0
1645
60	2002-01-01	1	0	0
1646
70	2002-01-01	0	1	0
1647
NULL	2002-01-01	NULL	NULL	0
1648
98	2060-01-01	0	1	0
1649
00	2060-01-01	0	1	0
1650
02	2060-01-01	0	1	0
1651
60	2060-01-01	0	1	0
1652
70	2060-01-01	0	1	0
1653
NULL	2060-01-01	NULL	NULL	0
1654
98	1970-01-01	1	0	0
1655
00	1970-01-01	1	0	0
1656
02	1970-01-01	1	0	0
1657
60	1970-01-01	1	0	0
1658
70	1970-01-01	0	1	0
1659
NULL	1970-01-01	NULL	NULL	0
1660
98	NULL	NULL	NULL	0
1661
00	NULL	NULL	NULL	0
1662
02	NULL	NULL	NULL	0
1663
60	NULL	NULL	NULL	0
1664
70	NULL	NULL	NULL	0
1665
NULL	NULL	NULL	NULL	1
1666
select a.f1 as a, b.f4 as b, a.f1 > b.f4 as gt,
1667
a.f1 < b.f4 as lt, a.f1<=>b.f4 as eq
1668
from t1 a, t1 b;
1669
a	b	gt	lt	eq
1670
98	1998-01-01 00:00:00	0	1	0
1671
00	1998-01-01 00:00:00	1	0	0
1672
02	1998-01-01 00:00:00	1	0	0
1673
60	1998-01-01 00:00:00	1	0	0
1674
70	1998-01-01 00:00:00	0	1	0
1675
NULL	1998-01-01 00:00:00	NULL	NULL	0
1676
98	2000-01-01 00:00:01	0	1	0
1677
00	2000-01-01 00:00:01	0	1	0
1678
02	2000-01-01 00:00:01	1	0	0
1679
60	2000-01-01 00:00:01	1	0	0
1680
70	2000-01-01 00:00:01	0	1	0
1681
NULL	2000-01-01 00:00:01	NULL	NULL	0
1682
98	2002-01-01 23:59:59	0	1	0
1683
00	2002-01-01 23:59:59	0	1	0
1684
02	2002-01-01 23:59:59	0	1	0
1685
60	2002-01-01 23:59:59	1	0	0
1686
70	2002-01-01 23:59:59	0	1	0
1687
NULL	2002-01-01 23:59:59	NULL	NULL	0
1688
98	2060-01-01 11:11:11	0	1	0
1689
00	2060-01-01 11:11:11	0	1	0
1690
02	2060-01-01 11:11:11	0	1	0
1691
60	2060-01-01 11:11:11	0	1	0
1692
70	2060-01-01 11:11:11	0	1	0
1693
NULL	2060-01-01 11:11:11	NULL	NULL	0
1694
98	1970-11-11 22:22:22	1	0	0
1695
00	1970-11-11 22:22:22	1	0	0
1696
02	1970-11-11 22:22:22	1	0	0
1697
60	1970-11-11 22:22:22	1	0	0
1698
70	1970-11-11 22:22:22	0	1	0
1699
NULL	1970-11-11 22:22:22	NULL	NULL	0
1700
98	NULL	NULL	NULL	0
1701
00	NULL	NULL	NULL	0
1702
02	NULL	NULL	NULL	0
1703
60	NULL	NULL	NULL	0
1704
70	NULL	NULL	NULL	0
1705
NULL	NULL	NULL	NULL	1
1706
select *, f1 = f2 from t1;
1707
f1	f2	f3	f4	f1 = f2
1708
98	1998	1998-01-01	1998-01-01 00:00:00	1
1709
00	2000	2000-01-01	2000-01-01 00:00:01	1
1710
02	2002	2002-01-01	2002-01-01 23:59:59	1
1711
60	2060	2060-01-01	2060-01-01 11:11:11	1
1712
70	1970	1970-01-01	1970-11-11 22:22:22	1
1713
NULL	NULL	NULL	NULL	NULL
1714
drop table t1;
1715
#
1716
# Bug #54465: assert: field_types == 0 || field_types[field_pos] ==
1717
#             MYSQL_TYPE_LONGLONG
1718
#
1719
CREATE TABLE t1 (a INT);
1720
INSERT INTO t1 VALUES (1), (2);
1721
SELECT MAX((SELECT 1 FROM t1 ORDER BY @var LIMIT 1)) m FROM t1 t2, t1 
1722
ORDER BY t1.a;
1723
m
1724
1
1725
DROP TABLE t1;
1726
#
1727
#  Bug#58030 crash in Item_func_geometry_from_text::val_str
1728
#
1729
SELECT MAX(TIMESTAMP(RAND(0)));
1730
SELECT MIN(TIMESTAMP(RAND(0)));
1731
#
1732
#  Bug#58177 crash and valgrind warnings in decimal and protocol sending functions...
1733
#
1734
SELECT MIN(GET_LOCK('aaaaaaaaaaaaaaaaa',0) / '0b1111111111111111111111111111111111111111111111111111111111111111111111111' ^ (RAND()));
1735
SELECT MIN(GET_LOCK('aaaaaaaaaaaaaaaaa',0) / '0b1111111111111111111111111111111111111111111111111111111111111111111111111' ^ (RAND()));
1736
SELECT MIN(GET_LOCK('aaaaaaaaaaaaaaaaa',0) / '0b1111111111111111111111111111111111111111111111111111111111111111111111111' ^ (RAND()));
1737
SELECT MIN(GET_LOCK('aaaaaaaaaaaaaaaaa',0) / '0b1111111111111111111111111111111111111111111111111111111111111111111111111' ^ (RAND()));
1738
SELECT RELEASE_LOCK('aaaaaaaaaaaaaaaaa');
1739
#
1740
# Bug #11766094 - 59132: MIN() AND MAX() REMOVE UNSIGNEDNESS 
1741
#
1742
CREATE TABLE t1 (a BIGINT UNSIGNED);
1743
INSERT INTO t1 VALUES (18446668621106209655);
1744
SELECT MAX(LENGTH(a)), LENGTH(MAX(a)), MIN(a), MAX(a), CONCAT(MIN(a)), CONCAT(MAX(a)) FROM t1;
1745
MAX(LENGTH(a))	LENGTH(MAX(a))	MIN(a)	MAX(a)	CONCAT(MIN(a))	CONCAT(MAX(a))
1746
20	20	18446668621106209655	18446668621106209655	18446668621106209655	18446668621106209655
1747
DROP TABLE t1;
1748
#
1749
# Bug #11766270  59343: YEAR(4): INCORRECT RESULT AND VALGRIND WARNINGS WITH MIN/MAX, UNION
1750
#
1751
CREATE TABLE t1(f1 YEAR(4));
1752
INSERT INTO t1 VALUES (0000),(2001);
1753
(SELECT MAX(f1) FROM t1) UNION (SELECT MAX(f1) FROM t1);
1754
Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
1755
def				MAX(f1)	MAX(f1)	13	4	4	Y	32864	0	63
1756
MAX(f1)
1757
2001
1758
DROP TABLE t1;
1759
#
0.2.6 by Norbert Tretkowski
Import upstream version 5.1.41
1760
End of 5.1 tests