~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
drop table if exists t1,t2,t3;
2
SELECT 1 FROM (SELECT 1) as a  GROUP BY SUM(1);
3
ERROR HY000: Invalid use of group function
4
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
5
spID int,
6
userID int,
7
score int,
1 by brian
clean slate
8
lsg char(40),
9
date date
10
);
11
INSERT INTO t1 VALUES (1,1,1,'','0000-00-00');
12
INSERT INTO t1 VALUES (2,2,2,'','0000-00-00');
13
INSERT INTO t1 VALUES (2,1,1,'','0000-00-00');
14
INSERT INTO t1 VALUES (3,3,3,'','0000-00-00');
15
CREATE TABLE t2 (
520.1.18 by Brian Aker
A bunch more test fixes.
16
userID int NOT NULL auto_increment,
1 by brian
clean slate
17
niName char(15),
18
passwd char(8),
19
mail char(50),
20
isAukt enum('N','Y') DEFAULT 'N',
21
vName char(30),
22
nName char(40),
23
adr char(60),
24
plz char(5),
25
ort char(35),
26
land char(20),
27
PRIMARY KEY (userID)
28
);
29
INSERT INTO t2 VALUES (1,'name','pass','mail','Y','v','n','adr','1','1','1');
30
INSERT INTO t2 VALUES (2,'name','pass','mail','Y','v','n','adr','1','1','1');
31
INSERT INTO t2 VALUES (3,'name','pass','mail','Y','v','n','adr','1','1','1');
32
INSERT INTO t2 VALUES (4,'name','pass','mail','Y','v','n','adr','1','1','1');
33
INSERT INTO t2 VALUES (5,'name','pass','mail','Y','v','n','adr','1','1','1');
34
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid;
35
userid	MIN(t1.score)
36
1	1
37
2	2
38
3	3
39
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID GROUP BY t2.userid ORDER BY NULL;
40
userid	MIN(t1.score)
41
1	1
42
2	2
43
3	3
44
SELECT t2.userid, MIN(t1.score) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2  GROUP BY t2.userid;
45
userid	MIN(t1.score)
46
1	1
47
2	2
48
SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2  GROUP BY t2.userid;
49
userid	MIN(t1.score+0.0)
50
1	1.0
51
2	2.0
52
SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2  GROUP BY t2.userid ORDER BY NULL;
53
userid	MIN(t1.score+0.0)
54
2	2.0
55
1	1.0
56
EXPLAIN SELECT t2.userid, MIN(t1.score+0.0) FROM t1, t2 WHERE t1.userID=t2.userID AND t1.spID=2  GROUP BY t2.userid ORDER BY NULL;
57
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
58
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	4	Using where; Using temporary
59
1	SIMPLE	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.userID	1	Using index
60
drop table t1,t2;
61
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
62
PID int NOT NULL auto_increment,
1 by brian
clean slate
63
payDate date DEFAULT '0000-00-00' NOT NULL,
64
recDate datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
520.1.18 by Brian Aker
A bunch more test fixes.
65
URID int DEFAULT '0' NOT NULL,
66
CRID int DEFAULT '0' NOT NULL,
67
amount int DEFAULT '0' NOT NULL,
68
operator int,
1 by brian
clean slate
69
method enum('unknown','cash','dealer','check','card','lazy','delayed','test') DEFAULT 'unknown' NOT NULL,
520.1.18 by Brian Aker
A bunch more test fixes.
70
DIID int,
1 by brian
clean slate
71
reason char(1) binary DEFAULT '' NOT NULL,
520.1.18 by Brian Aker
A bunch more test fixes.
72
code_id int,
73
qty int DEFAULT '0' NOT NULL,
1 by brian
clean slate
74
PRIMARY KEY (PID),
75
KEY URID (URID),
76
KEY reason (reason),
77
KEY method (method),
78
KEY payDate (payDate)
79
);
80
INSERT INTO t1 VALUES (1,'1970-01-01','1997-10-17 00:00:00',2529,1,21000,11886,'check',0,'F',16200,6);
81
SELECT COUNT(P.URID),SUM(P.amount),P.method, MIN(PP.recdate+0) > 19980501000000   AS IsNew FROM t1 AS P JOIN t1 as PP WHERE P.URID = PP.URID GROUP BY method,IsNew;
82
ERROR 42000: Can't group on 'IsNew'
83
drop table t1;
84
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
85
cid int NOT NULL auto_increment,
1 by brian
clean slate
86
firstname varchar(32) DEFAULT '' NOT NULL,
87
surname varchar(32) DEFAULT '' NOT NULL,
88
PRIMARY KEY (cid)
89
);
90
INSERT INTO t1 VALUES (1,'That','Guy');
91
INSERT INTO t1 VALUES (2,'Another','Gent');
92
CREATE TABLE t2 (
520.1.18 by Brian Aker
A bunch more test fixes.
93
call_id int NOT NULL auto_increment,
94
contact_id int DEFAULT '0' NOT NULL,
1 by brian
clean slate
95
PRIMARY KEY (call_id),
96
KEY contact_id (contact_id)
97
);
98
lock tables t1 read,t2 write;
99
INSERT INTO t2 VALUES (10,2);
100
INSERT INTO t2 VALUES (18,2);
101
INSERT INTO t2 VALUES (62,2);
102
INSERT INTO t2 VALUES (91,2);
103
INSERT INTO t2 VALUES (92,2);
104
SELECT cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid;
105
cid	CONCAT(firstname, ' ', surname)	COUNT(call_id)
106
SELECT cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid ORDER BY NULL;
107
cid	CONCAT(firstname, ' ', surname)	COUNT(call_id)
108
SELECT HIGH_PRIORITY cid, CONCAT(firstname, ' ', surname), COUNT(call_id) FROM t1 LEFT JOIN t2 ON cid=contact_id WHERE firstname like '%foo%' GROUP BY cid ORDER BY surname, firstname;
109
cid	CONCAT(firstname, ' ', surname)	COUNT(call_id)
110
drop table t2;
111
unlock tables;
112
drop table t1;
113
create table t1 (foo int);
114
insert into t1 values (1);
115
select 1+1, "a",count(*) from t1 where foo in (2);
116
1+1	a	count(*)
117
2	a	0
118
insert into t1 values (1);
119
select 1+1,"a",count(*) from t1 where foo in (2);
120
1+1	a	count(*)
121
2	a	0
122
drop table t1;
123
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
124
spID int,
125
userID int,
126
score int,
1 by brian
clean slate
127
key (spid),
128
key (score)
129
);
130
INSERT INTO t1 VALUES (1,1,1),(2,2,2),(2,1,1),(3,3,3),(4,3,3),(5,3,3),(6,3,3),(7,3,3);
131
explain select userid,count(*) from t1 group by userid desc;
132
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
133
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using temporary; Using filesort
134
explain select userid,count(*) from t1 group by userid desc order by null;
135
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
136
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using temporary
137
select userid,count(*) from t1 group by userid desc;
138
userid	count(*)
139
3	5
140
2	1
141
1	2
142
select userid,count(*) from t1 group by userid desc having (count(*)+1) IN (4,3);
143
userid	count(*)
144
1	2
145
select userid,count(*) from t1 group by userid desc having 3  IN (1,COUNT(*));
146
userid	count(*)
147
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid desc;
148
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
149
1	SIMPLE	t1	range	spID	spID	5	NULL	3	Using where; Using index
150
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid;
151
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
152
1	SIMPLE	t1	range	spID	spID	5	NULL	3	Using where; Using index
153
explain select spid,count(*) from t1 where spid between 1 and 2 group by spid order by null;
154
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
155
1	SIMPLE	t1	range	spID	spID	5	NULL	3	Using where; Using index
156
select spid,count(*) from t1 where spid between 1 and 2 group by spid;
157
spid	count(*)
158
1	1
159
2	2
160
select spid,count(*) from t1 where spid between 1 and 2 group by spid desc;
161
spid	count(*)
162
2	2
163
1	1
164
explain extended select sql_big_result spid,sum(userid) from t1 group by spid desc;
165
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
166
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	100.00	Using filesort
167
Warnings:
520.1.18 by Brian Aker
A bunch more test fixes.
168
Note	1003	select sql_big_result `test`.`t1`.`spID` AS `spid`,sum(`test`.`t1`.`userID`) AS `sum(userid)` from `test`.`t1` group by `test`.`t1`.`spID` desc
1 by brian
clean slate
169
explain select sql_big_result spid,sum(userid) from t1 group by spid desc order by null;
170
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
171
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using filesort
172
select sql_big_result spid,sum(userid) from t1 group by spid desc;
173
spid	sum(userid)
174
7	3
175
6	3
176
5	3
177
4	3
178
3	3
179
2	3
180
1	1
181
explain select sql_big_result score,count(*) from t1 group by score desc;
182
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
183
1	SIMPLE	t1	index	NULL	score	5	NULL	8	Using index; Using filesort
1 by brian
clean slate
184
explain select sql_big_result score,count(*) from t1 group by score desc order by null;
185
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
186
1	SIMPLE	t1	index	NULL	score	5	NULL	8	Using index; Using filesort
1 by brian
clean slate
187
select sql_big_result score,count(*) from t1 group by score desc;
188
score	count(*)
189
3	5
190
2	1
191
1	2
192
drop table t1;
193
create table t1 (a date default null, b date default null);
194
insert t1 values ('1999-10-01','2000-01-10'), ('1997-01-01','1998-10-01');
195
select a,min(b) c,count(distinct rand()) from t1 group by a having c<a + interval 1 day;
196
a	c	count(distinct rand())
197
drop table t1;
198
CREATE TABLE t1 (a char(1));
199
INSERT INTO t1 VALUES ('A'),('B'),('A'),('B'),('A'),('B'),(NULL),('a'),('b'),(NULL),('A'),('B'),(NULL);
200
SELECT a FROM t1 GROUP BY a;
201
a
202
NULL
203
A
204
B
205
SELECT a,count(*) FROM t1 GROUP BY a;
206
a	count(*)
207
NULL	3
208
A	5
209
B	5
210
SELECT a FROM t1 GROUP BY binary a;
211
a
212
NULL
213
A
214
B
215
a
216
b
217
SELECT a,count(*) FROM t1 GROUP BY binary a;
218
a	count(*)
219
NULL	3
220
A	4
221
B	4
222
a	1
223
b	1
224
SELECT binary a FROM t1 GROUP BY 1;
225
binary a 
226
NULL
227
A
228
B
229
a
230
b
231
SELECT binary a,count(*) FROM t1 GROUP BY 1;
232
binary a	count(*)
233
NULL	3
234
A	4
235
B	4
236
a	1
237
b	1
238
SELECT a FROM t1 GROUP BY a;
239
a
240
NULL
241
A
242
B
243
SELECT a,count(*) FROM t1 GROUP BY a;
244
a	count(*)
245
NULL	3
246
A	5
247
B	5
248
SELECT a FROM t1 GROUP BY binary a;
249
a
250
NULL
251
A
252
B
253
a
254
b
255
SELECT a,count(*) FROM t1 GROUP BY binary a;
256
a	count(*)
257
NULL	3
258
A	4
259
B	4
260
a	1
261
b	1
262
SELECT binary a FROM t1 GROUP BY 1;
263
binary a 
264
NULL
265
A
266
B
267
a
268
b
269
SELECT binary a,count(*) FROM t1 GROUP BY 1;
270
binary a	count(*)
271
NULL	3
272
A	4
273
B	4
274
a	1
275
b	1
276
drop table t1;
277
CREATE TABLE t1 (
278
`a` char(193) default NULL,
279
`b` char(63) default NULL
280
);
281
INSERT INTO t1 VALUES ('abc','def'),('hij','klm');
282
SELECT CONCAT(a, b) FROM t1 GROUP BY 1;
283
CONCAT(a, b)
284
abcdef
285
hijklm
286
SELECT CONCAT(a, b),count(*) FROM t1 GROUP BY 1;
287
CONCAT(a, b)	count(*)
288
abcdef	1
289
hijklm	1
290
SELECT CONCAT(a, b),count(distinct a) FROM t1 GROUP BY 1;
291
CONCAT(a, b)	count(distinct a)
292
abcdef	1
293
hijklm	1
294
SELECT 1 FROM t1 GROUP BY CONCAT(a, b);
295
1
296
1
297
1
298
INSERT INTO t1 values ('hij','klm');
299
SELECT CONCAT(a, b),count(*) FROM t1 GROUP BY 1;
300
CONCAT(a, b)	count(*)
301
abcdef	1
302
hijklm	2
303
DROP TABLE t1;
520.1.18 by Brian Aker
A bunch more test fixes.
304
create table t1 (One int, Two int, Three int, Four int);
1 by brian
clean slate
305
insert into t1 values (1,2,1,4),(1,2,2,4),(1,2,3,4),(1,2,4,4),(1,1,1,4),(1,1,2,4),(1,1,3,4),(1,1,4,4),(1,3,1,4),(1,3,2,4),(1,3,3,4),(1,3,4,4);
306
select One, Two, sum(Four) from t1 group by One,Two;
307
One	Two	sum(Four)
308
1	1	16
309
1	2	16
310
1	3	16
311
drop table t1;
312
create table t1 (id integer primary key not null auto_increment, gender char(1));
313
insert into t1 values (NULL, 'M'), (NULL, 'F'),(NULL, 'F'),(NULL, 'F'),(NULL, 'M');
314
create table t2 (user_id integer not null, date date);
315
insert into t2 values (1, '2002-06-09'),(2, '2002-06-09'),(1, '2002-06-09'),(3, '2002-06-09'),(4, '2002-06-09'),(4, '2002-06-09');
316
select u.gender as gender, count(distinct  u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender;
317
gender	dist_count	percentage
318
F	3	60.0000
319
M	1	20.0000
320
select u.gender as  gender, count(distinct  u.id) as dist_count, (count(distinct u.id)/5*100) as percentage from t1 u, t2 l where l.user_id = u.id group by u.gender  order by percentage;
321
gender	dist_count	percentage
322
M	1	20.0000
323
F	3	60.0000
324
drop table t1,t2;
325
CREATE TABLE t1 (ID1 int, ID2 int, ID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID
326
));
327
insert into t1 values (1,244,NULL),(2,243,NULL),(134,223,NULL),(185,186,NULL);
328
select S.ID as xID, S.ID1 as xID1 from t1 as S left join t1 as yS  on S.ID1 between yS.ID1 and yS.ID2;
329
xID	xID1
330
1	1
331
2	2
332
2	2
333
3	134
334
3	134
335
3	134
336
4	185
337
4	185
338
4	185
339
4	185
340
select S.ID as xID, S.ID1 as xID1, repeat('*',count(distinct yS.ID)) as Level from t1 as S left join t1 as yS  on S.ID1 between yS.ID1 and yS.ID2 group by xID order by xID1;
341
xID	xID1	Level
342
1	1	*
343
2	2	**
344
3	134	***
345
4	185	****
346
drop table t1;
347
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
348
pid int NOT NULL default '0',
349
c1id int default NULL,
350
c2id int default NULL,
351
value int NOT NULL default '0',
1 by brian
clean slate
352
UNIQUE KEY pid2 (pid,c1id,c2id),
353
UNIQUE KEY pid (pid,value)
354
) ENGINE=MyISAM;
355
INSERT INTO t1 VALUES (1, 1, NULL, 1),(1, 2, NULL, 2),(1, NULL, 3, 3),(1, 4, NULL, 4),(1, 5, NULL, 5);
356
CREATE TABLE t2 (
520.1.18 by Brian Aker
A bunch more test fixes.
357
id int NOT NULL default '0',
1 by brian
clean slate
358
active enum('Yes','No') NOT NULL default 'Yes',
359
PRIMARY KEY  (id)
360
) ENGINE=MyISAM;
361
INSERT INTO t2 VALUES (1, 'Yes'),(2, 'No'),(4, 'Yes'),(5, 'No');
362
CREATE TABLE t3 (
520.1.18 by Brian Aker
A bunch more test fixes.
363
id int NOT NULL default '0',
1 by brian
clean slate
364
active enum('Yes','No') NOT NULL default 'Yes',
365
PRIMARY KEY  (id)
366
);
367
INSERT INTO t3 VALUES (3, 'Yes');
368
select * from t1 AS m LEFT JOIN t2 AS c1 ON m.c1id = 
369
c1.id AND c1.active = 'Yes' LEFT JOIN t3 AS c2 ON m.c2id = c2.id AND 
370
c2.active = 'Yes' WHERE m.pid=1  AND (c1.id IS NOT NULL OR c2.id IS NOT NULL);
371
pid	c1id	c2id	value	id	active	id	active
372
1	1	NULL	1	1	Yes	NULL	NULL
373
1	NULL	3	3	NULL	NULL	3	Yes
374
1	4	NULL	4	4	Yes	NULL	NULL
375
select max(value) from t1 AS m LEFT JOIN t2 AS c1 ON 
376
m.c1id = c1.id AND c1.active = 'Yes' LEFT JOIN t3 AS c2 ON m.c2id = 
377
c2.id AND c2.active = 'Yes' WHERE m.pid=1  AND (c1.id IS NOT NULL OR c2.id IS 
378
NOT NULL);
379
max(value)
380
4
381
drop table t1,t2,t3;
382
create table t1 (a blob null);
383
insert into t1 values (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(""),(""),(""),("b");
384
select a,count(*) from t1 group by a;
385
a	count(*)
386
NULL	9
387
	3
388
b	1
389
select a,count(*) from t1 group by a;
390
a	count(*)
391
NULL	9
392
	3
393
b	1
394
drop table t1;
395
create table t1 (a int not null, b int not null);
396
insert into t1 values (1,1),(1,2),(3,1),(3,2),(2,2),(2,1);
397
create table t2 (a int not null, b int not null, key(a));
398
insert into t2 values (1,3),(3,1),(2,2),(1,1);
399
select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b;
400
a	b
401
1	1
402
1	3
403
2	2
404
3	1
405
select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b ORDER BY NULL;
406
a	b
407
1	3
408
3	1
409
2	2
410
1	1
411
explain select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b;
412
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
413
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	6	Using temporary; Using filesort
414
1	SIMPLE	t2	ALL	a	NULL	NULL	NULL	4	Using where; Using join buffer
415
explain select t1.a,t2.b from t1,t2 where t1.a=t2.a group by t1.a,t2.b ORDER BY NULL;
416
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
417
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	6	Using temporary
418
1	SIMPLE	t2	ALL	a	NULL	NULL	NULL	4	Using where; Using join buffer
419
drop table t1,t2;
420
create table t1 (a int, b int);
421
insert into t1 values (1, 4),(10, 40),(1, 4),(10, 43),(1, 4),(10, 41),(1, 4),(10, 43),(1, 4);
422
select a, MAX(b), INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000) from t1 group by a;
423
a	MAX(b)	INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000)
424
1	4	2
425
10	43	6
426
select a, MAX(b), CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end from t1 group by a;
427
a	MAX(b)	CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end 
428
1	4	4
429
10	43	43
430
select a, MAX(b), FIELD(MAX(b), '43', '4', '5') from t1 group by a;
431
a	MAX(b)	FIELD(MAX(b), '43', '4', '5')
432
1	4	2
433
10	43	1
434
select a, MAX(b), CONCAT_WS(MAX(b), '43', '4', '5') from t1 group by a;
435
a	MAX(b)	CONCAT_WS(MAX(b), '43', '4', '5')
436
1	4	434445
437
10	43	43434435
438
select a, MAX(b), ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f') from t1 group by a;
439
a	MAX(b)	ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f')
440
1	4	d
441
10	43	NULL
442
select a, MAX(b), MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') from t1 group by a;
443
a	MAX(b)	MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
444
1	4	c
445
10	43	a,b,d,f
446
drop table t1;
447
create table t1 (id int not null, qty int not null);
448
insert into t1 values (1,2),(1,3),(2,4),(2,5);
449
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sum(qty)>2 and cqty>1;
450
id	sqty	cqty
451
1	5	2
452
2	9	2
453
select id, sum(qty) as sqty from t1 group by id having sqty>2 and count(qty)>1;
454
id	sqty
455
1	5
456
2	9
457
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sqty>2 and cqty>1;
458
id	sqty	cqty
459
1	5	2
460
2	9	2
461
select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sum(qty)>2 and count(qty)>1;
462
id	sqty	cqty
463
1	5	2
464
2	9	2
465
select count(*), case interval(qty,2,3,4,5,6,7,8) when -1 then NULL when 0 then "zero" when 1 then "one" when 2 then "two" end as category from t1 group by category;
466
count(*)	category
467
2	NULL
468
1	one
469
1	two
470
select count(*), interval(qty,2,3,4,5,6,7,8) as category from t1 group by category;
471
count(*)	category
472
1	1
473
1	2
474
1	3
475
1	4
476
drop table t1;
477
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
478
userid int,
479
score int,
1 by brian
clean slate
480
key (score)
481
);
482
INSERT INTO t1 VALUES (1,1),(2,2),(1,1),(3,3),(3,3),(3,3),(3,3),(3,3);
483
SELECT userid,count(*) FROM t1 GROUP BY userid DESC;
484
userid	count(*)
485
3	5
486
2	1
487
1	2
488
EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC;
489
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
490
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using temporary; Using filesort
491
DROP TABLE t1;
492
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
493
i int default NULL,
494
j int default NULL
1 by brian
clean slate
495
);
496
INSERT INTO t1 VALUES (1,2),(2,3),(4,5),(3,5),(1,5),(23,5);
497
SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL;
498
i	COUNT(DISTINCT(i))
499
1	1
500
2	1
501
4	4
502
explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL;
503
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
504
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	6	Using filesort
505
DROP TABLE t1;
506
create table t1 (a int);
507
insert into t1 values(null);
508
select min(a) is null from t1;
509
min(a) is null 
510
1
511
select min(a) is null or null from t1;
512
min(a) is null or null 
513
1
514
select 1 and min(a) is null from t1;
515
1 and min(a) is null 
516
1
517
drop table t1;
518
create table t1 ( col1 int, col2 int );
519
insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2);
520
select group_concat( distinct col1 ) as alias from t1
521
group by col2 having alias like '%';
522
alias
523
1,2
524
1,2
525
1
526
drop table t1;
527
create table t1 (a integer, b integer, c integer);
528
insert into t1 (a,b) values (1,2),(1,3),(2,5);
529
select a, 0.1*0+1 r2, sum(1) r1 from t1 where a = 1 group  by a having r1>1 and r2=1;
530
a	r2	r1
531
1	1.0	2
532
select a, round(rand(100)*10) r2, sum(1) r1 from t1 where a = 1 group  by a having r1>1 and r2<=2;
533
a	r2	r1
534
1	2	2
535
select a,sum(b) from t1 where a=1 group by c;
536
a	sum(b)
537
1	5
538
select a*sum(b) from t1 where a=1 group by c;
539
a*sum(b)
540
5
541
select sum(a)*sum(b) from t1 where a=1 group by c;
542
sum(a)*sum(b)
543
10
544
select a,sum(b) from t1 where a=1 group by c having a=1;
545
a	sum(b)
546
1	5
547
select a as d,sum(b) from t1 where a=1 group by c having d=1;
548
d	sum(b)
549
1	5
550
select sum(a)*sum(b) as d from t1 where a=1 group by c having d > 0;
551
d
552
10
553
drop table t1;
554
create table t1(a int);
555
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(8),(9);
556
create table t2 (
557
a int,
558
b varchar(200) NOT NULL,
559
c varchar(50) NOT NULL,
560
d varchar(100) NOT NULL,
561
primary key (a,b(132),c,d),
562
key a (a,b)
520.1.18 by Brian Aker
A bunch more test fixes.
563
);
564
Warnings:
565
Warning	1071	Specified key was too long; max key length is 767 bytes
1 by brian
clean slate
566
insert into t2 select 
567
x3.a,  -- 3
568
concat('val-', x3.a + 3*x4.a), -- 12
569
concat('val-', @a:=x3.a + 3*x4.a + 12*C.a), -- 120
570
concat('val-', @a + 120*D.a)
571
from t1 x3, t1 x4, t1 C, t1 D where x3.a < 3 and x4.a < 4 and D.a < 4;
572
delete from t2  where a = 2 and b = 'val-2' order by a,b,c,d limit 30;
573
explain select c from t2 where a = 2 and b = 'val-2' group by c;
574
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
575
1	SIMPLE	t2	ref	PRIMARY,a	a	770	const,const	36	Using where; Using index
1 by brian
clean slate
576
select c from t2 where a = 2 and b = 'val-2' group by c;
577
c
578
val-74
579
val-98
580
drop table t1,t2;
520.1.18 by Brian Aker
A bunch more test fixes.
581
create table t1 (b int4 not null);
582
insert into t1 values(300000);
1 by brian
clean slate
583
select * from t1;
584
b
520.1.18 by Brian Aker
A bunch more test fixes.
585
300000
1 by brian
clean slate
586
select min(b) from t1;
587
min(b)
520.1.18 by Brian Aker
A bunch more test fixes.
588
300000
1 by brian
clean slate
589
drop table t1;
590
CREATE TABLE t1 (id int PRIMARY KEY, user_id int, hostname longtext);
591
INSERT INTO t1 VALUES
592
(1, 7, 'cache-dtc-af05.proxy.aol.com'),
593
(2, 3, 'what.ever.com'),
594
(3, 7, 'cache-dtc-af05.proxy.aol.com'),
595
(4, 7, 'cache-dtc-af05.proxy.aol.com');
596
SELECT hostname, COUNT(DISTINCT user_id) as no FROM t1
597
WHERE hostname LIKE '%aol%'
598
    GROUP BY hostname;
599
hostname	no
600
cache-dtc-af05.proxy.aol.com	1
601
DROP TABLE t1;
602
CREATE TABLE t1 (a  int, b int);
603
INSERT INTO t1 VALUES (1,2), (1,3);
604
SELECT a, b FROM t1 GROUP BY 'const';
605
a	b
606
1	2
607
SELECT DISTINCT a, b FROM t1 GROUP BY 'const';
608
a	b
609
1	2
610
DROP TABLE t1;
611
CREATE TABLE t1 (id INT, dt DATETIME);
612
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
613
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
614
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
615
INSERT INTO t1 VALUES ( 1, '2005-05-01 12:30:00' );
616
SELECT dt DIV 1 AS f, id FROM t1 GROUP BY f;
617
f	id
618
20050501123000	1
619
DROP TABLE t1;
620
CREATE TABLE t1 (id varchar(20) NOT NULL);
621
INSERT INTO t1 VALUES ('trans1'), ('trans2');
622
CREATE TABLE t2 (id varchar(20) NOT NULL, err_comment blob NOT NULL);
623
INSERT INTO t2 VALUES ('trans1', 'a problem');
624
SELECT COUNT(DISTINCT(t1.id)), LEFT(err_comment, 256) AS comment
625
FROM t1 LEFT JOIN t2 ON t1.id=t2.id GROUP BY comment;
626
COUNT(DISTINCT(t1.id))	comment
627
1	NULL
628
1	a problem
629
DROP TABLE t1, t2;
630
create table t1 (f1 date);
631
insert into t1 values('2005-06-06');
632
insert into t1 values('2005-06-06');
633
select date(left(f1+0,8)) from t1 group by 1;
634
date(left(f1+0,8))
635
2005-06-06
636
drop table t1;
637
CREATE TABLE t1 (n int);
638
INSERT INTO t1 VALUES (1);
639
SELECT n+1 AS n FROM t1 GROUP BY n;
640
n
641
2
642
Warnings:
643
Warning	1052	Column 'n' in group statement is ambiguous
644
DROP TABLE t1;
645
create table t1(f1 varchar(5) key);
646
insert into t1 values (1),(2);
647
select sql_buffer_result max(f1) is null from t1;
648
max(f1) is null 
649
0
650
select sql_buffer_result max(f1)+1 from t1;
651
max(f1)+1
652
3
653
drop table t1;
654
CREATE TABLE t1(a INT);
655
INSERT INTO t1 VALUES (1),(2);
656
SELECT a FROM t1 GROUP BY 'a';
657
a
658
1
659
SELECT a FROM t1 GROUP BY "a";
660
a
661
1
662
SELECT a FROM t1 GROUP BY `a`;
663
a
664
1
665
2
666
SELECT a FROM t1 GROUP BY "a";
667
a
668
1
669
SELECT a FROM t1 GROUP BY 'a';
670
a
671
1
672
SELECT a FROM t1 GROUP BY `a`;
673
a
674
1
675
2
676
SELECT a FROM t1 HAVING 'a' > 1;
677
a
678
Warnings:
679
Warning	1292	Truncated incorrect DOUBLE value: 'a'
680
SELECT a FROM t1 HAVING "a" > 1;
681
a
682
Warnings:
683
Warning	1292	Truncated incorrect DOUBLE value: 'a'
684
SELECT a FROM t1 HAVING `a` > 1;
685
a
686
2
687
SELECT a FROM t1 ORDER BY 'a' DESC;
688
a
689
1
690
2
691
SELECT a FROM t1 ORDER BY "a" DESC;
692
a
693
1
694
2
695
SELECT a FROM t1 ORDER BY `a` DESC;
696
a
697
2
698
1
699
DROP TABLE t1;
700
CREATE TABLE t1 (
520.1.18 by Brian Aker
A bunch more test fixes.
701
f1 int NOT NULL auto_increment primary key,
1 by brian
clean slate
702
f2 varchar(100) NOT NULL default ''
703
);
704
CREATE TABLE t2 (
705
f1 varchar(10) NOT NULL default '',
706
f2 char(3) NOT NULL default '',
707
PRIMARY KEY  (`f1`),
708
KEY `k1` (`f2`,`f1`)
709
);
710
INSERT INTO t1 values(NULL, '');
711
INSERT INTO `t2` VALUES ('486878','WDT'),('486910','WDT');
712
SELECT SQL_BUFFER_RESULT avg(t2.f1) FROM t1, t2 where t2.f2 = 'SIR' GROUP BY t1.f1;
713
avg(t2.f1)
714
SELECT avg(t2.f1) FROM t1, t2 where t2.f2 = 'SIR' GROUP BY t1.f1;
715
avg(t2.f1)
716
DROP TABLE t1, t2;
717
create table t1 (c1 char(3), c2 char(3));
718
create table t2 (c3 char(3), c4 char(3));
719
insert into t1 values ('aaa', 'bb1'), ('aaa', 'bb2');
720
insert into t2 values ('aaa', 'bb1'), ('aaa', 'bb2');
721
select t1.c1 as c2 from t1, t2 where t1.c2 = t2.c4
722
group by c2;
723
c2
724
aaa
725
aaa
726
Warnings:
727
Warning	1052	Column 'c2' in group statement is ambiguous
728
show warnings;
729
Level	Code	Message
730
Warning	1052	Column 'c2' in group statement is ambiguous
731
select t1.c1 as c2 from t1, t2 where t1.c2 = t2.c4
732
group by t1.c1;
733
c2
734
aaa
735
show warnings;
736
Level	Code	Message
737
drop table t1, t2;
738
CREATE TABLE t1 (a INT PRIMARY KEY, b INT, key (b));
739
INSERT INTO t1 VALUES (1,      1);
740
INSERT INTO t1 SELECT  a + 1 , MOD(a + 1 , 20) FROM t1;
741
INSERT INTO t1 SELECT  a + 2 , MOD(a + 2 , 20) FROM t1;
742
INSERT INTO t1 SELECT  a + 4 , MOD(a + 4 , 20) FROM t1;
743
INSERT INTO t1 SELECT  a + 8 , MOD(a + 8 , 20) FROM t1;
744
INSERT INTO t1 SELECT  a + 16, MOD(a + 16, 20) FROM t1;
745
INSERT INTO t1 SELECT  a + 32, MOD(a + 32, 20) FROM t1;
746
INSERT INTO t1 SELECT  a + 64, MOD(a + 64, 20) FROM t1;
747
SELECT MIN(b), MAX(b) from t1;
748
MIN(b)	MAX(b)
749
0	19
750
EXPLAIN SELECT b, sum(1) FROM t1 GROUP BY b;
751
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
752
1	SIMPLE	t1	index	NULL	b	5	NULL	128	Using index
753
EXPLAIN SELECT SQL_BIG_RESULT b, sum(1) FROM t1 GROUP BY b;
754
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
755
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	128	Using filesort
1 by brian
clean slate
756
SELECT b, sum(1) FROM t1 GROUP BY b;
757
b	sum(1)
758
0	6
759
1	7
760
2	7
761
3	7
762
4	7
763
5	7
764
6	7
765
7	7
766
8	7
767
9	6
768
10	6
769
11	6
770
12	6
771
13	6
772
14	6
773
15	6
774
16	6
775
17	6
776
18	6
777
19	6
778
SELECT SQL_BIG_RESULT b, sum(1) FROM t1 GROUP BY b;
779
b	sum(1)
780
0	6
781
1	7
782
2	7
783
3	7
784
4	7
785
5	7
786
6	7
787
7	7
788
8	7
789
9	6
790
10	6
791
11	6
792
12	6
793
13	6
794
14	6
795
15	6
796
16	6
797
17	6
798
18	6
799
19	6
800
DROP TABLE t1;
801
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
802
INSERT INTO t1 VALUES (1,1),(2,1),(3,2),(4,2),(5,3),(6,3);
803
SELECT MAX(a)-MIN(a) FROM t1 GROUP BY b;
804
MAX(a)-MIN(a)
805
1
806
1
807
1
808
SELECT CEILING(MIN(a)) FROM t1 GROUP BY b;
809
CEILING(MIN(a))
810
1
811
3
812
5
813
SELECT CASE WHEN AVG(a)>=0 THEN 'Positive' ELSE 'Negative' END FROM t1 
814
GROUP BY b;
815
CASE WHEN AVG(a)>=0 THEN 'Positive' ELSE 'Negative' END 
816
Positive
817
Positive
818
Positive
819
SELECT a + 1 FROM t1 GROUP BY a;
820
a + 1
821
2
822
3
823
4
824
5
825
6
826
7
827
SELECT a + b FROM t1 GROUP BY b;
828
a + b 
829
2
830
5
831
8
832
SELECT (SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1) 
833
FROM t1 AS t1_outer;
834
(SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1)
835
1
836
2
837
3
838
4
839
5
840
6
841
SELECT 1 FROM t1 as t1_outer GROUP BY a 
842
HAVING (SELECT t1_outer.a FROM t1 AS t1_inner GROUP BY b LIMIT 1);
843
1
844
1
845
1
846
1
847
1
848
1
849
1
850
SELECT (SELECT t1_outer.a FROM t1 AS t1_inner LIMIT 1) 
851
FROM t1 AS t1_outer GROUP BY t1_outer.b;
852
(SELECT t1_outer.a FROM t1 AS t1_inner LIMIT 1)
853
1
854
3
855
5
856
SELECT 1 FROM t1 as t1_outer GROUP BY a 
857
HAVING (SELECT t1_outer.b FROM t1 AS t1_inner LIMIT 1);
858
ERROR 42S22: Unknown column 'test.t1_outer.b' in 'field list'
859
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner LIMIT 1) 
860
FROM t1 AS t1_outer GROUP BY t1_outer.b;
861
(SELECT SUM(t1_inner.a) FROM t1 AS t1_inner LIMIT 1)
862
21
863
21
864
21
865
SELECT (SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
866
FROM t1 AS t1_outer;
867
(SELECT SUM(t1_inner.a) FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1)
868
3
869
3
870
3
871
3
872
3
873
3
874
SELECT (SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1) 
875
FROM t1 AS t1_outer GROUP BY t1_outer.b;
876
(SELECT SUM(t1_outer.a) FROM t1 AS t1_inner LIMIT 1)
877
3
878
7
879
11
880
SELECT 1 FROM t1 as t1_outer 
881
WHERE (SELECT t1_outer.b FROM t1 AS t1_inner GROUP BY t1_inner.b LIMIT 1);
882
1
883
1
884
1
885
1
886
1
887
1
888
1
889
SELECT b FROM t1 GROUP BY b HAVING CEILING(b) > 0;
890
b
891
1
892
2
893
3
894
SELECT 1 FROM t1 GROUP BY b HAVING b = 2 OR b = 3 OR SUM(a) > 12;
895
1
896
1
897
1
898
SELECT 1 FROM t1 GROUP BY b HAVING ROW (b,b) = ROW (1,1);
899
1
900
1
901
SELECT 1 FROM t1 GROUP BY b HAVING a = 2;
902
ERROR 42S22: Unknown column 'a' in 'having clause'
903
SELECT 1 FROM t1 GROUP BY SUM(b);
904
ERROR HY000: Invalid use of group function
905
SELECT b FROM t1 AS t1_outer GROUP BY a HAVING t1_outer.a IN 
906
(SELECT SUM(t1_inner.b)+t1_outer.b FROM t1 AS t1_inner GROUP BY t1_inner.a
907
HAVING SUM(t1_inner.b)+t1_outer.b > 5);
908
b
909
3
910
DROP TABLE t1;
911
create table t1(f1 int, f2 int);
912
select * from t1 group by f1;
913
f1	f2
914
select * from t1 group by f2;
915
f1	f2
916
select * from t1 group by f1, f2;
917
f1	f2
918
select t1.f1,t.* from t1, t1 t group by 1;
919
f1	f1	f2
920
drop table t1;
921
CREATE TABLE t1(
922
id INT AUTO_INCREMENT PRIMARY KEY, 
923
c1 INT NOT NULL, 
924
c2 INT NOT NULL,
925
UNIQUE KEY (c2,c1));
926
INSERT INTO t1(c1,c2) VALUES (5,1), (4,1), (3,5), (2,3), (1,3);
927
SELECT * FROM t1 ORDER BY c1;
928
id	c1	c2
929
5	1	3
930
4	2	3
931
3	3	5
932
2	4	1
933
1	5	1
934
SELECT * FROM t1 GROUP BY id ORDER BY c1;
935
id	c1	c2
936
5	1	3
937
4	2	3
938
3	3	5
939
2	4	1
940
1	5	1
941
SELECT * FROM t1 GROUP BY id ORDER BY id DESC;
942
id	c1	c2
943
5	1	3
944
4	2	3
945
3	3	5
946
2	4	1
947
1	5	1
948
SELECT * FROM t1 GROUP BY c2 ,c1, id ORDER BY c2, c1;
949
id	c1	c2
950
2	4	1
951
1	5	1
952
5	1	3
953
4	2	3
954
3	3	5
955
SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1;
956
id	c1	c2
957
3	3	5
958
5	1	3
959
4	2	3
960
2	4	1
961
1	5	1
962
SELECT * FROM t1 GROUP BY c2, c1, id ORDER BY c2 DESC, c1 DESC;
963
id	c1	c2
964
3	3	5
965
4	2	3
966
5	1	3
967
1	5	1
968
2	4	1
969
SELECT * FROM t1 GROUP BY c2  ORDER BY c2, c1;
970
id	c1	c2
520.1.18 by Brian Aker
A bunch more test fixes.
971
2	4	1
972
5	1	3
1 by brian
clean slate
973
3	3	5
974
SELECT * FROM t1 GROUP BY c2  ORDER BY c2 DESC, c1;
975
id	c1	c2
976
3	3	5
977
4	2	3
978
1	5	1
979
SELECT * FROM t1 GROUP BY c2  ORDER BY c2 DESC, c1 DESC;
980
id	c1	c2
981
3	3	5
982
4	2	3
983
1	5	1
984
DROP TABLE t1;
985
#
986
# Bug#27219: Aggregate functions in ORDER BY.  
987
#
988
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
989
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
990
CREATE TABLE t2 SELECT * FROM t1;
991
SELECT 1 FROM t1 ORDER BY COUNT(*);
992
1
993
1
994
SELECT 1 FROM t1 ORDER BY COUNT(*) + 1;
995
1
996
1
997
SELECT 1 FROM t1 ORDER BY COUNT(*) + a;
998
1
999
1
1000
SELECT 1 FROM t1 ORDER BY COUNT(*), 1;
1001
1
1002
1
1003
SELECT 1 FROM t1 ORDER BY COUNT(*), a;
1004
1
1005
1
1006
SELECT 1 FROM t1 ORDER BY SUM(a);
1007
1
1008
1
1009
SELECT 1 FROM t1 ORDER BY SUM(a + 1);
1010
1
1011
1
1012
SELECT 1 FROM t1 ORDER BY SUM(a) + 1;
1013
1
1014
1
1015
SELECT 1 FROM t1 ORDER BY SUM(a), b;
1016
1
1017
1
1018
SELECT a FROM t1 ORDER BY COUNT(b);
1019
a
1020
3
1021
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2);
1022
a
1023
3
1024
2
1025
3
1026
2
1027
3
1028
4
1029
SELECT t1.a FROM t1 ORDER BY (SELECT SUM(t2.a) FROM t2 ORDER BY t2.a);
1030
a
1031
3
1032
2
1033
3
1034
2
1035
3
1036
4
1037
SELECT t1.a FROM t1 ORDER BY (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1038
a
1039
3
1040
2
1041
3
1042
2
1043
3
1044
4
1045
SELECT t1.a FROM t1
1046
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.b) LIMIT 1);
1047
a
1048
3
1049
3
1050
3
1051
SELECT t1.a FROM t1 GROUP BY t1.a
1052
HAVING t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1053
a
1054
3
1055
SELECT t1.a FROM t1 GROUP BY t1.a
1056
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1057
a
1058
2
1059
3
1060
4
1061
SELECT t1.a FROM t1 GROUP BY t1.a
1062
HAVING t1.a IN (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1063
a
1064
3
1065
SELECT t1.a FROM t1 GROUP BY t1.a
1066
HAVING t1.a > ANY (SELECT t2.a FROM t2 ORDER BY t2.a, SUM(t2.b));
1067
a
1068
4
1069
SELECT t1.a FROM t1
1070
WHERE t1.a = (SELECT t2.a FROM t2 ORDER BY SUM(t1.b));
1071
a
1072
3
1073
3
1074
3
1075
SELECT 1 FROM t1 GROUP BY t1.a
1076
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1077
1
1078
1
1079
1
1080
1
1081
SELECT 1 FROM t1 GROUP BY t1.a
1082
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1083
1
1084
1
1085
1
1086
1
1087
SELECT 1 FROM t1 GROUP BY t1.a
1088
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY SUM(t2.a) LIMIT 1);
1089
1
1090
1
1091
1
1092
1
1093
SELECT 1 FROM t1 GROUP BY t1.a
1094
HAVING (SELECT AVG(SUM(t1.b) + 1) FROM t2 ORDER BY t2.a LIMIT 1);
1095
1
1096
1
1097
1
1098
1
1099
SELECT 1 FROM t1 GROUP BY t1.a
1100
HAVING (SELECT AVG(SUM(t1.b) + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
1101
1
1102
1
1103
1
1104
1
1105
SELECT 1 FROM t1 GROUP BY t1.a
1106
HAVING (SELECT AVG(t1.b + t2.b) FROM t2 ORDER BY t2.a LIMIT 1);
1107
1
1108
1
1109
1
1110
1
1111
SELECT t1.a FROM t1 
1112
WHERE t1.a = (SELECT t2.a FROM t2 GROUP BY t2.a
1113
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1);
1114
a
1115
4
1116
SELECT t1.a, SUM(t1.b) FROM t1 
1117
WHERE t1.a = (SELECT SUM(t2.b) FROM t2 GROUP BY t2.a
1118
ORDER BY SUM(t2.b), SUM(t1.b) LIMIT 1)
1119
GROUP BY t1.a;
1120
a	SUM(t1.b)
1121
4	4
1122
SELECT t1.a, SUM(t1.b) FROM t1 
1123
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
1124
ORDER BY SUM(t2.b) + SUM(t1.b) LIMIT 1)
1125
GROUP BY t1.a;
1126
a	SUM(t1.b)
1127
SELECT t1.a, SUM(t1.b) FROM t1 
1128
WHERE t1.a = (SELECT SUM(t2.b) FROM t2
1129
ORDER BY SUM(t2.b + t1.a) LIMIT 1)
1130
GROUP BY t1.a;
1131
a	SUM(t1.b)
1132
SELECT t1.a FROM t1 GROUP BY t1.a
1133
HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
1134
a
1135
select avg (
1136
(select
1137
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
1138
from t1 as outr order by outr.a limit 1))
1139
from t1 as most_outer;
1140
avg (
1141
(select
1142
(select sum(outr.a + innr.a) from t1 as innr limit 1) as tt
1143
from t1 as outr order by outr.a limit 1))
1144
29.0000
1145
select avg (
1146
(select (
1147
(select sum(outr.a + innr.a) from t1 as innr limit 1)) as tt
1148
from t1 as outr order by count(outr.a) limit 1)) as tt
1149
from t1 as most_outer;
1150
tt
1151
35.0000
1152
select (select sum(outr.a + t1.a) from t1 limit 1) as tt from t1 as outr order by outr.a;
1153
tt
1154
29
1155
29
1156
35
1157
35
1158
35
1159
41
1160
DROP TABLE t1, t2;
1161
End of 5.0 tests
1162
CREATE TABLE t1 (a INT, b INT,
1163
PRIMARY KEY (a),
1164
KEY i2(a,b));
1165
INSERT INTO t1 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
1166
INSERT INTO t1 SELECT a + 8,b FROM t1;
1167
INSERT INTO t1 SELECT a + 16,b FROM t1;
1168
INSERT INTO t1 SELECT a + 32,b FROM t1;
1169
INSERT INTO t1 SELECT a + 64,b FROM t1;
1170
INSERT INTO t1 SELECT a + 128,b FROM t1 limit 16;
1171
ANALYZE TABLE t1;
1172
Table	Op	Msg_type	Msg_text
1173
test.t1	analyze	status	OK
1174
EXPLAIN SELECT a FROM t1 WHERE a < 2;
1175
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1176
1	SIMPLE	t1	range	PRIMARY,i2	PRIMARY	4	NULL	1	Using where; Using index
1177
EXPLAIN SELECT a FROM t1 WHERE a < 2 ORDER BY a;
1178
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1179
1	SIMPLE	t1	range	PRIMARY,i2	PRIMARY	4	NULL	1	Using where; Using index
1180
EXPLAIN SELECT a FROM t1 WHERE a < 2 GROUP BY a;
1181
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1182
1	SIMPLE	t1	range	PRIMARY,i2	PRIMARY	4	NULL	1	Using where; Using index for group-by
1 by brian
clean slate
1183
EXPLAIN SELECT a FROM t1 IGNORE INDEX (PRIMARY,i2);
1184
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1185
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1186
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR JOIN (PRIMARY,i2);
1187
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1188
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1189
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR GROUP BY (PRIMARY,i2) GROUP BY a;
1190
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1191
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	Using index
1192
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY,i2) ORDER BY a;
1193
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1194
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	Using index
1195
SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY,i2) ORDER BY a;
1196
a
1197
1
1198
2
1199
3
1200
4
1201
5
1202
6
1203
7
1204
8
1205
9
1206
10
1207
11
1208
12
1209
13
1210
14
1211
15
1212
16
1213
17
1214
18
1215
19
1216
20
1217
21
1218
22
1219
23
1220
24
1221
25
1222
26
1223
27
1224
28
1225
29
1226
30
1227
31
1228
32
1229
33
1230
34
1231
35
1232
36
1233
37
1234
38
1235
39
1236
40
1237
41
1238
42
1239
43
1240
44
1241
45
1242
46
1243
47
1244
48
1245
49
1246
50
1247
51
1248
52
1249
53
1250
54
1251
55
1252
56
1253
57
1254
58
1255
59
1256
60
1257
61
1258
62
1259
63
1260
64
1261
65
1262
66
1263
67
1264
68
1265
69
1266
70
1267
71
1268
72
1269
73
1270
74
1271
75
1272
76
1273
77
1274
78
1275
79
1276
80
1277
81
1278
82
1279
83
1280
84
1281
85
1282
86
1283
87
1284
88
1285
89
1286
90
1287
91
1288
92
1289
93
1290
94
1291
95
1292
96
1293
97
1294
98
1295
99
1296
100
1297
101
1298
102
1299
103
1300
104
1301
105
1302
106
1303
107
1304
108
1305
109
1306
110
1307
111
1308
112
1309
113
1310
114
1311
115
1312
116
1313
117
1314
118
1315
119
1316
120
1317
121
1318
122
1319
123
1320
124
1321
125
1322
126
1323
127
1324
128
1325
129
1326
130
1327
131
1328
132
1329
133
1330
134
1331
135
1332
136
1333
137
1334
138
1335
139
1336
140
1337
141
1338
142
1339
143
1340
144
1341
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (PRIMARY)
1342
IGNORE INDEX FOR GROUP BY (i2) GROUP BY a;
1343
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1344
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	Using index
1345
EXPLAIN SELECT a FROM t1 IGNORE INDEX (PRIMARY) IGNORE INDEX FOR ORDER BY (i2);
1346
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1347
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	
1 by brian
clean slate
1348
EXPLAIN SELECT a FROM t1 FORCE INDEX (i2);
1349
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1350
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	
1 by brian
clean slate
1351
EXPLAIN SELECT a FROM t1 USE INDEX ();
1352
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1353
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1354
EXPLAIN SELECT a FROM t1 USE INDEX () USE INDEX (i2);
1355
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1356
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1357
EXPLAIN SELECT a FROM t1 
1358
FORCE INDEX (PRIMARY) 
1359
IGNORE INDEX FOR GROUP BY (i2)
1360
IGNORE INDEX FOR ORDER BY (i2)
1361
USE INDEX (i2);
1362
ERROR HY000: Incorrect usage of USE INDEX and FORCE INDEX
1363
EXPLAIN SELECT a FROM t1 USE INDEX (i2) USE INDEX ();
1364
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1365
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	
1 by brian
clean slate
1366
EXPLAIN SELECT a FROM t1 FORCE INDEX ();
629.2.6 by Monty
Updated test output with new and improved error messages.
1367
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near ')' at line 1
1 by brian
clean slate
1368
EXPLAIN SELECT a FROM t1 IGNORE INDEX ();
629.2.6 by Monty
Updated test output with new and improved error messages.
1369
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near ')' at line 1
1 by brian
clean slate
1370
EXPLAIN SELECT a FROM t1 USE INDEX FOR JOIN (i2) 
1371
USE INDEX FOR GROUP BY (i2) GROUP BY a;
1372
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1373
1	SIMPLE	t1	#	NULL	i2	#	NULL	#	#
1374
EXPLAIN SELECT a FROM t1 FORCE INDEX FOR JOIN (i2) 
1375
FORCE INDEX FOR GROUP BY (i2) GROUP BY a;
1376
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1377
1	SIMPLE	t1	range	NULL	i2	4	NULL	145	Using index for group-by
1378
EXPLAIN SELECT a FROM t1 USE INDEX () IGNORE INDEX (i2);
1379
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1380
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1381
EXPLAIN SELECT a FROM t1 IGNORE INDEX (i2) USE INDEX ();
1382
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1383
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	144	
1384
EXPLAIN SELECT a FROM t1 
1385
USE INDEX FOR GROUP BY (i2) 
1386
USE INDEX FOR ORDER BY (i2)
1387
USE INDEX FOR JOIN (i2);
1388
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1389
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	
1 by brian
clean slate
1390
EXPLAIN SELECT a FROM t1 
1391
USE INDEX FOR JOIN (i2) 
1392
USE INDEX FOR JOIN (i2) 
1393
USE INDEX FOR JOIN (i2,i2);
1394
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1395
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	144	
1 by brian
clean slate
1396
EXPLAIN SELECT 1 FROM t1 WHERE a IN
1397
(SELECT a FROM t1 USE INDEX (i2) IGNORE INDEX (i2));
1398
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1399
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	144	Start temporary
1400
1	PRIMARY	t1	eq_ref	PRIMARY,i2	PRIMARY	4	test.t1.a	1	Using index; End temporary
1401
CREATE TABLE t2 (a INT, b INT, KEY(a));
1402
INSERT INTO t2 VALUES (1, 1), (2, 2), (3,3), (4,4);
1403
EXPLAIN SELECT a, SUM(b) FROM t2 GROUP BY a LIMIT 2;
1404
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1405
1	SIMPLE	t2	index	NULL	a	5	NULL	2	
1406
EXPLAIN SELECT a, SUM(b) FROM t2 IGNORE INDEX (a) GROUP BY a LIMIT 2;
1407
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1408
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	Using temporary; Using filesort
1409
EXPLAIN SELECT 1 FROM t2 WHERE a IN
1410
(SELECT a FROM t1 USE INDEX (i2) IGNORE INDEX (i2));
1411
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1412
1	PRIMARY	t2	index	a	a	5	NULL	4	Using index; Start temporary
1413
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	144	Using where; End temporary; Using join buffer
1 by brian
clean slate
1414
SHOW VARIABLES LIKE 'old';
1415
Variable_name	Value
1416
old	OFF
1417
SET @@old = off;
1418
ERROR HY000: Variable 'old' is a read only variable
1419
DROP TABLE t1, t2;
1420
CREATE TABLE t1(
1421
a INT, 
1422
b INT NOT NULL, 
1423
c INT NOT NULL, 
1424
d INT, 
1425
UNIQUE KEY (c,b)
1426
);
1427
INSERT INTO t1 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4);
1428
CREATE TABLE t2(
1429
a INT,
1430
b INT,
1431
UNIQUE KEY(a,b)
1432
);
1433
INSERT INTO t2 VALUES (NULL, NULL), (NULL, NULL), (NULL, 1), (1, NULL), (1, 1), (1,2);
1434
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b,d;
1435
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1436
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using filesort
1437
SELECT c,b,d FROM t1 GROUP BY c,b,d;
1438
c	b	d
1439
1	1	50
1440
3	1	4
1441
3	2	40
1442
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b,d ORDER BY NULL;
1443
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1444
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
1445
SELECT c,b,d FROM t1 GROUP BY c,b,d ORDER BY NULL;
1446
c	b	d
1447
1	1	50
520.1.18 by Brian Aker
A bunch more test fixes.
1448
3	1	4
1 by brian
clean slate
1449
3	2	40
1450
EXPLAIN SELECT c,b,d FROM t1 ORDER BY c,b,d;
1451
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1452
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using filesort
1453
SELECT c,b,d FROM t1 ORDER BY c,b,d;
1454
c	b	d
1455
1	1	50
1456
3	1	4
1457
3	2	40
1458
EXPLAIN SELECT c,b,d FROM t1 GROUP BY c,b;
1459
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
520.1.18 by Brian Aker
A bunch more test fixes.
1460
1	SIMPLE	t1	index	NULL	c	8	NULL	3	
1 by brian
clean slate
1461
SELECT c,b,d FROM t1 GROUP BY c,b;
1462
c	b	d
1463
1	1	50
1464
3	1	4
1465
3	2	40
1466
EXPLAIN SELECT c,b   FROM t1 GROUP BY c,b;
1467
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1468
1	SIMPLE	t1	index	NULL	c	8	NULL	3	Using index
1469
SELECT c,b   FROM t1 GROUP BY c,b;
1470
c	b
1471
1	1
1472
3	1
1473
3	2
1474
EXPLAIN SELECT a,b from t2 ORDER BY a,b;
1475
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1476
1	SIMPLE	t2	index	NULL	a	10	NULL	6	Using index
1477
SELECT a,b from t2 ORDER BY a,b;
1478
a	b
1479
NULL	NULL
1480
NULL	NULL
1481
NULL	1
1482
1	NULL
1483
1	1
1484
1	2
1485
EXPLAIN SELECT a,b from t2 GROUP BY a,b;
1486
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1487
1	SIMPLE	t2	index	NULL	a	10	NULL	6	Using index
1488
SELECT a,b from t2 GROUP BY a,b;
1489
a	b
1490
NULL	NULL
1491
NULL	1
1492
1	NULL
1493
1	1
1494
1	2
1495
EXPLAIN SELECT a from t2 GROUP BY a;
1496
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1497
1	SIMPLE	t2	index	NULL	a	10	NULL	6	Using index
1498
SELECT a from t2 GROUP BY a;
1499
a
1500
NULL
1501
1
1502
EXPLAIN SELECT b from t2 GROUP BY b;
1503
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1504
1	SIMPLE	t2	index	NULL	a	10	NULL	6	Using index; Using temporary; Using filesort
1505
SELECT b from t2 GROUP BY b;
1506
b
1507
NULL
1508
1
1509
2
1510
DROP TABLE t1;
1511
CREATE TABLE t1 ( a INT, b INT );
1512
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1513
FROM t1;
1514
c	(SELECT a FROM t1 WHERE b = c)
1515
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1516
FROM t1 
1517
HAVING b = 10;
1518
c	(SELECT a FROM t1 WHERE b = c)
1519
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
1520
FROM t1 
1521
HAVING b = 10;
1522
ERROR 42S22: Reference 'c' not supported (reference to group function)
1523
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1524
FROM t1;
1525
c	(SELECT a FROM t1 WHERE b = c)
1526
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1527
FROM t1 
1528
HAVING b = 10;
1529
c	(SELECT a FROM t1 WHERE b = c)
1530
SELECT MAX(b) c, (SELECT a FROM t1 WHERE b = c)
1531
FROM t1 
1532
HAVING b = 10;
1533
ERROR 42S22: Reference 'c' not supported (reference to group function)
1534
INSERT INTO t1 VALUES (1, 1);
1535
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1536
FROM t1;
1537
c	(SELECT a FROM t1 WHERE b = c)
1538
1	1
1539
INSERT INTO t1 VALUES (2, 1);
1540
SELECT b c, (SELECT a FROM t1 WHERE b = c)
1541
FROM t1;
1542
ERROR 21000: Subquery returns more than 1 row
1543
DROP TABLE t1;