~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
# Initialise
2
--disable_warnings
3
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
4
--enable_warnings
5
select (select 2);
6
explain extended select (select 2);
7
SELECT (SELECT 1) UNION SELECT (SELECT 2);
8
explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
9
SELECT (SELECT (SELECT 0 UNION SELECT 0));
10
explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
11
-- error 1247
12
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
13
-- error 1247
14
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
15
SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
16
-- error 1247
17
SELECT (SELECT a) as a;
18
EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
19
SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
20
-- error 1054
21
SELECT (SELECT 1), a;
22
SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
23
-- error 1054
24
SELECT 1 FROM (SELECT (SELECT a) b) c;
25
SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
26
-- error 1241
27
SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
28
SELECT 1 IN (SELECT 1);
29
SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
30
-- error ER_BAD_FIELD_ERROR
31
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
32
-- error ER_BAD_FIELD_ERROR
33
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
34
SELECT (SELECT 1,2,3) = ROW(1,2,3);
35
SELECT (SELECT 1,2,3) = ROW(1,2,1);
36
SELECT (SELECT 1,2,3) < ROW(1,2,1);
37
SELECT (SELECT 1,2,3) > ROW(1,2,1);
38
SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
39
SELECT ROW(1,2,3) = (SELECT 1,2,3);
40
SELECT ROW(1,2,3) = (SELECT 1,2,1);
41
SELECT ROW(1,2,3) < (SELECT 1,2,1);
42
SELECT ROW(1,2,3) > (SELECT 1,2,1);
43
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
44
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
45
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
46
SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
47
SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
48
SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
49
SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
50
51
-- error 1241
52
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
53
54
SELECT 1 as a,(SELECT a+a) b,(SELECT b);
55
56
create table t1 (a int);
57
create table t2 (a int, b int);
58
create table t3 (a int);
59
create table t4 (a int not null, b int not null);
60
insert into t1 values (2);
61
insert into t2 values (1,7),(2,7);
62
insert into t4 values (4,8),(3,8),(5,9);
63
-- error 1247
64
select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
65
select (select a from t1 where t1.a=t2.a), a from t2;
66
select (select a from t1 where t1.a=t2.b), a from t2;
67
select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
68
select (select a from t3), a from t2;
69
select * from t2 where t2.a=(select a from t1);
70
insert into t3 values (6),(7),(3);
71
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
72
(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 order by a limit 2) limit 3;
73
(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
74
explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
75
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
76
select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 
77
(select * from t2 where a>1) as tt;
78
explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 
79
(select * from t2 where a>1) as tt;
80
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
81
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
82
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
83
select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
84
explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
85
select * from t3 where exists (select * from t2 where t2.b=t3.a);
86
select * from t3 where not exists (select * from t2 where t2.b=t3.a);
87
select * from t3 where a in (select b from t2);
88
select * from t3 where a not in (select b from t2);
89
select * from t3 where a = some (select b from t2);
90
select * from t3 where a <> any (select b from t2);
91
92
# Rewrite: select * from t3 where not exists (select b from t2 where a <> b);
93
select * from t3 where a = all (select b from t2);
94
95
select * from t3 where a <> all (select b from t2);
96
insert into t2 values (100, 5);
97
select * from t3 where a < any (select b from t2);
98
select * from t3 where a < all (select b from t2);
99
select * from t3 where a >= any (select b from t2);
100
explain extended select * from t3 where a >= any (select b from t2);
101
select * from t3 where a >= all (select b from t2);
102
delete from t2 where a=100;
103
-- error 1241
104
select * from t3 where a in (select a,b from t2);
105
-- error 1241
106
select * from t3 where a in (select * from t2);
107
insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
108
# empty set
109
select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
110
insert into t2 values (2,10);
111
select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
112
delete from t2 where a=2 and b=10;
113
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
114
create table t5 (a int);
115
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
116
insert into t5 values (5);
117
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
118
insert into t5 values (2);
119
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
120
explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
121
-- error 1242
122
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
123
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
124
create table t7( uq int primary key, name char(25));
125
insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
126
insert into t6 values (1,1),(1,2),(2,2),(1,3);
127
select * from t6 where exists (select * from t7 where uq = clinic_uq);
128
explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
129
130
# not unique fields
131
-- error 1052
132
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
133
134
# different tipes & group functions
135
drop table t1,t2,t3;
136
137
CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
138
INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
139
CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
140
INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
141
CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
142
INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
143
SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
144
SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
145
SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
146
147
CREATE TABLE `t8` (
148
  `pseudo` varchar(35) character set latin1 NOT NULL default '',
149
  `email` varchar(60) character set latin1 NOT NULL default '',
150
  PRIMARY KEY  (`pseudo`),
151
  UNIQUE KEY `email` (`email`)
152
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
153
154
INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
155
INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
156
INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
157
EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
158
-- error 1241
159
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
160
t8 WHERE pseudo='joce');
161
-- error 1241
162
SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
163
pseudo='joce');
164
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
165
-- error 1242
166
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
167
168
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
169
170
#searchconthardwarefr3 forumconthardwarefr7
171
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
172
  `topic` bigint unsigned NOT NULL default '0',
1 by brian
clean slate
173
  `date` date NOT NULL default '0000-00-00',
174
  `pseudo` varchar(35) character set latin1 NOT NULL default '',
175
  PRIMARY KEY  (`pseudo`,`date`,`topic`),
176
  KEY `topic` (`topic`)
177
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
178
INSERT INTO t1 (topic,date,pseudo) VALUES
179
('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
180
EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
181
EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
182
SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
183
SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
184
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
185
-- error 1242
186
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
187
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
188
drop table t1;
189
190
#forumconthardwarefr7 searchconthardwarefr7
191
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
192
  `numeropost` bigint unsigned NOT NULL auto_increment,
193
  `maxnumrep` int unsigned NOT NULL default '0',
1 by brian
clean slate
194
  PRIMARY KEY  (`numeropost`),
195
  UNIQUE KEY `maxnumrep` (`maxnumrep`)
196
) ENGINE=MyISAM ROW_FORMAT=FIXED;
197
198
INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
199
200
CREATE TABLE `t2` (
201
      `mot` varchar(30) NOT NULL default '',
223 by Brian Aker
Cleanup int() work.
202
      `topic` bigint unsigned NOT NULL default '0',
1 by brian
clean slate
203
      `date` date NOT NULL default '0000-00-00',
204
      `pseudo` varchar(35) NOT NULL default '',
205
       PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
206
    ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
207
208
INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
209
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
210
SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20;
211
-- error 1054
212
SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
213
-- error 1054
214
SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
215
216
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
217
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
218
SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
219
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
220
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
221
SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
222
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
223
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
224
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
225
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
226
SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
227
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
228
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
229
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
230
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
231
drop table t1,t2;
232
233
#forumconthardwarefr7
234
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
235
  `numeropost` bigint unsigned NOT NULL auto_increment,
236
  `maxnumrep` int unsigned NOT NULL default '0',
1 by brian
clean slate
237
  PRIMARY KEY  (`numeropost`),
238
  UNIQUE KEY `maxnumrep` (`maxnumrep`)
239
) ENGINE=MyISAM ROW_FORMAT=FIXED;
240
241
INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
242
-- error 1242
243
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
244
-- error 1242
245
select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
246
drop table t1;
247
248
create table t1 (a int);
249
insert into t1 values (1),(2),(3);
250
(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
251
drop table t1;
252
253
#iftest
254
CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
255
INSERT INTO t1 VALUES ();
256
-- error 1242
257
SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
258
drop table t1;
259
260
# threadhardwarefr7
261
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
262
  `numeropost` bigint unsigned NOT NULL default '0',
263
  `numreponse` int unsigned NOT NULL auto_increment,
1 by brian
clean slate
264
  `pseudo` varchar(35) NOT NULL default '',
265
  PRIMARY KEY  (`numeropost`,`numreponse`),
266
  UNIQUE KEY `numreponse` (`numreponse`),
267
  KEY `pseudo` (`pseudo`,`numeropost`)
268
) ENGINE=MyISAM;
269
-- error 1247
270
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
271
-- error 1054
272
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
273
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
274
INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
275
-- error 1242
276
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
277
EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
278
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
279
drop table t1;
280
223 by Brian Aker
Cleanup int() work.
281
CREATE TABLE t1 (a int);
1 by brian
clean slate
282
INSERT INTO t1 VALUES (1);
283
SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
284
drop table t1;
285
286
#update with subselects
287
create table t1 (a int NOT NULL, b int, primary key (a));
288
create table t2 (a int NOT NULL, b int, primary key (a));
289
insert into t1 values (0, 10),(1, 11),(2, 12);
290
insert into t2 values (1, 21),(2, 22),(3, 23);
291
select * from t1;
292
-- error 1093
293
update t1 set b= (select b from t1);
294
-- error 1242
295
update t1 set b= (select b from t2);
296
update t1 set b= (select b from t2 where t1.a = t2.a);
297
select * from t1;
298
drop table t1, t2;
299
300
#delete with subselects
301
create table t1 (a int NOT NULL, b int, primary key (a));
302
create table t2 (a int NOT NULL, b int, primary key (a));
303
insert into t1 values (0, 10),(1, 11),(2, 12);
304
insert into t2 values (1, 21),(2, 12),(3, 23);
305
select * from t1;
306
select * from t1 where b = (select b from t2 where t1.a = t2.a);
307
-- error 1093
308
delete from t1 where b = (select b from t1);
309
-- error 1242
310
delete from t1 where b = (select b from t2);
311
delete from t1 where b = (select b from t2 where t1.a = t2.a);
312
select * from t1;
313
drop table t1, t2;
314
315
#multi-delete with subselects
316
317
create table t11 (a int NOT NULL, b int, primary key (a));
318
create table t12 (a int NOT NULL, b int, primary key (a));
319
create table t2 (a int NOT NULL, b int, primary key (a));
320
insert into t11 values (0, 10),(1, 11),(2, 12);
321
insert into t12 values (33, 10),(22, 11),(2, 12);
322
insert into t2 values (1, 21),(2, 12),(3, 23);
323
select * from t11;
324
select * from t12;
325
-- error 1093
326
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
327
-- error 1242
328
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
329
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
330
select * from t11;
331
select * from t12;
332
drop table t11, t12, t2;
333
334
#insert with subselects
335
CREATE TABLE t1 (x int);
336
create table t2 (a int);
337
create table t3 (b int);
338
insert into t2 values (1);
339
insert into t3 values (1),(2);
340
-- error 1093
341
INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
342
-- error 1242
343
INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
344
INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
345
select * from t1;
346
insert into t2 values (1);
347
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
348
-- sleep 1
349
select * from t1;
350
INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
351
select * from t1;
352
# After this, only data based on old t1 records should have been added.
353
INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
354
select * from t1;
355
-- error 1054
356
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
357
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
358
-- sleep 1
359
select * from t1;
360
#
361
#TODO: should be uncommented after bug 380 fix pushed
362
#INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
363
#select * from t1;
364
drop table t1, t2, t3;
365
366
#replace with subselects
367
CREATE TABLE t1 (x int not null, y int, primary key (x));
368
create table t2 (a int);
369
create table t3 (a int);
370
insert into t2 values (1);
371
insert into t3 values (1),(2);
372
select * from t1;
373
-- error 1093
374
replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
375
-- error 1242
376
replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
377
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
378
select * from t1;
379
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
380
select * from t1;
381
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
382
-- sleep 1
383
select * from t1;
384
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
385
-- sleep 1
386
select * from t1;
387
replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
388
select * from t1;
389
drop table t1, t2, t3;
390
391
-- error 1096
392
SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
393
223 by Brian Aker
Cleanup int() work.
394
CREATE TABLE t2 (id int default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
1 by brian
clean slate
395
INSERT INTO t2 VALUES (1),(2);
396
SELECT * FROM t2 WHERE id IN (SELECT 1);
397
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
398
SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
399
SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
400
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
401
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
402
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
403
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
404
-- error 1093
405
INSERT INTO t2 VALUES ((SELECT * FROM t2));
406
-- error 1093
407
INSERT INTO t2 VALUES ((SELECT id FROM t2));
408
SELECT * FROM t2;
223 by Brian Aker
Cleanup int() work.
409
CREATE TABLE t1 (id int default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
1 by brian
clean slate
410
INSERT INTO t1 values (1),(1);
411
-- error 1242
412
UPDATE t2 SET id=(SELECT * FROM t1);
413
drop table t2, t1;
414
415
#NULL test
416
create table t1 (a int);
417
insert into t1 values (1),(2),(3);
418
select 1 IN (SELECT * from t1);
419
select 10 IN (SELECT * from t1);
420
select NULL IN (SELECT * from t1);
421
update t1 set a=NULL where a=2;
422
select 1 IN (SELECT * from t1);
423
select 3 IN (SELECT * from t1);
424
select 10 IN (SELECT * from t1);
425
select 1 > ALL (SELECT * from t1);
426
select 10 > ALL (SELECT * from t1);
427
select 1 > ANY (SELECT * from t1);
428
select 10 > ANY (SELECT * from t1);
429
drop table t1;
430
create table t1 (a varchar(20));
431
insert into t1 values ('A'),('BC'),('DEF');
432
select 'A' IN (SELECT * from t1);
433
select 'XYZS' IN (SELECT * from t1);
434
select NULL IN (SELECT * from t1);
435
update t1 set a=NULL where a='BC';
436
select 'A' IN (SELECT * from t1);
437
select 'DEF' IN (SELECT * from t1);
438
select 'XYZS' IN (SELECT * from t1);
439
select 'A' > ALL (SELECT * from t1);
440
select 'XYZS' > ALL (SELECT * from t1);
441
select 'A' > ANY (SELECT * from t1);
442
select 'XYZS' > ANY (SELECT * from t1);
443
drop table t1;
444
create table t1 (a float);
445
insert into t1 values (1.5),(2.5),(3.5);
446
select 1.5 IN (SELECT * from t1);
447
select 10.5 IN (SELECT * from t1);
448
select NULL IN (SELECT * from t1);
449
update t1 set a=NULL where a=2.5;
450
select 1.5 IN (SELECT * from t1);
451
select 3.5 IN (SELECT * from t1);
452
select 10.5 IN (SELECT * from t1);
453
select 1.5 > ALL (SELECT * from t1);
454
select 10.5 > ALL (SELECT * from t1);
455
select 1.5 > ANY (SELECT * from t1);
456
select 10.5 > ANY (SELECT * from t1);
457
explain extended select (select a+1) from t1;
458
select (select a+1) from t1;
459
drop table t1;
460
461
#
462
# Null with keys
463
#
464
223 by Brian Aker
Cleanup int() work.
465
CREATE TABLE t1 (a int NOT NULL default '0', PRIMARY KEY  (a));
466
CREATE TABLE t2 (a int default '0', INDEX (a));
1 by brian
clean slate
467
INSERT INTO t1 VALUES (1),(2),(3),(4);
468
INSERT INTO t2 VALUES (1),(2),(3);
469
SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
470
explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
223 by Brian Aker
Cleanup int() work.
471
CREATE TABLE t3 (a int default '0');
1 by brian
clean slate
472
INSERT INTO t3 VALUES (1),(2),(3);
473
SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
474
explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
475
drop table t1,t2,t3;
476
477
#LIMIT is not supported now
478
create table t1 (a float);
479
-- error 1235
480
select 10.5 IN (SELECT * from t1 LIMIT 1);
481
-- error 1235
482
select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
483
drop table t1;
484
485
create table t1 (a int, b int, c varchar(10));
486
create table t2 (a int);
487
insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
488
insert into t2 values (1),(2),(NULL);
489
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t1 where a=t2.a)  from t2;
490
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2;
491
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t1 where a=t2.a) from t2;
492
drop table t1,t2;
493
494
create table t1 (a int, b real, c varchar(10));
495
insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
496
select ROW(1, 1, 'a') IN (select a,b,c from t1);
497
select ROW(1, 2, 'a') IN (select a,b,c from t1);
498
select ROW(1, 1, 'a') IN (select b,a,c from t1);
499
select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
500
select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
501
select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
502
select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
503
select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
504
select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
505
-- error 1235
506
select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
507
drop table t1;
508
509
#test of uncacheable subqueries
223 by Brian Aker
Cleanup int() work.
510
CREATE TABLE t1 (a int);
1 by brian
clean slate
511
EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
512
EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
513
drop table t1;
514
515
516
CREATE TABLE `t1` (
517
  `mot` varchar(30) character set latin1 NOT NULL default '',
223 by Brian Aker
Cleanup int() work.
518
  `topic` bigint unsigned NOT NULL default '0',
1 by brian
clean slate
519
  `date` date NOT NULL default '0000-00-00',
520
  `pseudo` varchar(35) character set latin1 NOT NULL default '',
521
  PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
522
  KEY `pseudo` (`pseudo`,`date`,`topic`),
523
  KEY `topic` (`topic`)
524
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
525
526
CREATE TABLE `t2` (
527
  `mot` varchar(30) character set latin1 NOT NULL default '',
223 by Brian Aker
Cleanup int() work.
528
  `topic` bigint unsigned NOT NULL default '0',
1 by brian
clean slate
529
  `date` date NOT NULL default '0000-00-00',
530
  `pseudo` varchar(35) character set latin1 NOT NULL default '',
531
  PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
532
  KEY `pseudo` (`pseudo`,`date`,`topic`),
533
  KEY `topic` (`topic`)
534
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
535
536
CREATE TABLE `t3` (
223 by Brian Aker
Cleanup int() work.
537
  `numeropost` bigint unsigned NOT NULL auto_increment,
538
  `maxnumrep` int unsigned NOT NULL default '0',
1 by brian
clean slate
539
  PRIMARY KEY  (`numeropost`),
540
  UNIQUE KEY `maxnumrep` (`maxnumrep`)
541
) ENGINE=MyISAM CHARSET=latin1;
542
INSERT INTO t1 (mot, topic, pseudo) VALUES ('joce','1','joce'),('test','2','test');
543
544
INSERT INTO t2 (mot, topic, pseudo) VALUES ('joce','1','joce'),('test','2','test');
545
546
INSERT INTO t3 VALUES (1,1);
547
548
SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
549
numeropost=topic);
550
select * from t1;
551
DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
552
EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
553
select * from t1;
554
555
drop table t1, t2, t3;
556
557
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
558
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
559
SHOW CREATE TABLE t1;
560
drop table t1;
561
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
562
SHOW CREATE TABLE t1;
563
drop table t1;
564
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
565
SHOW CREATE TABLE t1;
566
drop table t1;
567
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
568
select * from t1;
569
SHOW CREATE TABLE t1;
570
drop table t1;
571
572
create table t1 (a int);
573
insert into t1 values (1), (2), (3);
574
explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
575
from t1;
576
drop table t1;
577
578
#
579
# error in IN
580
#
581
-- error 1146
582
select t1.Continent, t2.Name, t2.Population from t1 LEFT JOIN t2 ON t1.Code = t2.Country  where t2.Population IN (select max(t2.Population) AS Population from t2, t1 where t2.Country = t1.Code group by Continent);
583
584
#
585
# complex subquery
586
#
587
588
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
589
  ID int NOT NULL auto_increment,
1 by brian
clean slate
590
  name char(35) NOT NULL default '',
591
  t2 char(3) NOT NULL default '',
592
  District char(20) NOT NULL default '',
223 by Brian Aker
Cleanup int() work.
593
  Population int NOT NULL default '0',
1 by brian
clean slate
594
  PRIMARY KEY  (ID)
595
) ENGINE=MyISAM;
596
597
INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
598
INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
599
INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
600
601
CREATE TABLE t2 (
602
  Code char(3) NOT NULL default '',
603
  Name char(52) NOT NULL default '',
604
  Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
605
  Region char(26) NOT NULL default '',
606
  SurfaceArea float(10,2) NOT NULL default '0.00',
223 by Brian Aker
Cleanup int() work.
607
  IndepYear smallint default NULL,
608
  Population int NOT NULL default '0',
1 by brian
clean slate
609
  LifeExpectancy float(3,1) default NULL,
610
  GNP float(10,2) default NULL,
611
  GNPOld float(10,2) default NULL,
612
  LocalName char(45) NOT NULL default '',
613
  GovernmentForm char(45) NOT NULL default '',
614
  HeadOfState char(60) default NULL,
223 by Brian Aker
Cleanup int() work.
615
  Capital int default NULL,
1 by brian
clean slate
616
  Code2 char(2) NOT NULL default '',
617
  PRIMARY KEY  (Code)
618
) ENGINE=MyISAM;
619
620
INSERT INTO t2 VALUES ('AUS','Australia','Oceania','Australia and New Zealand',7741220.00,1901,18886000,79.8,351182.00,392911.00,'Australia','Constitutional Monarchy, Federation','Elisabeth II',135,'AU');
621
INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Azärbaycan','Federal Republic','Heydär Äliyev',144,'AZ');
622
623
select t2.Continent, t1.Name, t1.Population from t2 LEFT JOIN t1 ON t2.Code = t1.t2  where t1.Population IN (select max(t1.Population) AS Population from t1, t2 where t1.t2 = t2.Code group by Continent); 
624
625
drop table t1, t2;
626
627
#
628
# constants in IN
629
#
630
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
631
  `id` bigint unsigned NOT NULL auto_increment,
1 by brian
clean slate
632
  `pseudo` varchar(35) character set latin1 NOT NULL default '',
633
  PRIMARY KEY  (`id`),
634
  UNIQUE KEY `pseudo` (`pseudo`)
635
) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
636
INSERT INTO t1 (pseudo) VALUES ('test');
637
SELECT 0 IN (SELECT 1 FROM t1 a);
638
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
639
INSERT INTO t1 (pseudo) VALUES ('test1');
640
SELECT 0 IN (SELECT 1 FROM t1 a);
641
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
642
drop table t1;
643
644
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
645
  `i` int NOT NULL default '0',
1 by brian
clean slate
646
  PRIMARY KEY  (`i`)
647
) ENGINE=MyISAM CHARSET=latin1;
648
649
INSERT INTO t1 VALUES (1);
650
UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
651
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
652
-- error 1054
653
UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
654
select * from t1;
655
drop table t1;
656
657
#
658
# Multi update test
659
#
660
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
661
  id int default NULL
1 by brian
clean slate
662
) ENGINE=MyISAM CHARSET=latin1;
663
INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
664
CREATE TABLE t2 (
223 by Brian Aker
Cleanup int() work.
665
  id int default NULL,
1 by brian
clean slate
666
  name varchar(15) default NULL
667
) ENGINE=MyISAM CHARSET=latin1;
668
669
INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
670
update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
671
select * from t2;
672
drop table t1,t2;
673
674
#
675
# correct NULL in <CONSTANT> IN (SELECT ...)
676
#
677
create table t1 (a int, unique index indexa (a)); 
678
insert into t1 values (-1), (-4), (-2), (NULL); 
679
select -10 IN (select a from t1 FORCE INDEX (indexa)); 
680
drop table t1;
681
682
#
683
# Test optimization for sub selects
684
#
685
create table t1 (id int not null auto_increment primary key, salary int, key(salary));
686
insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
687
explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
688
drop table t1;
689
690
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
691
  ID int unsigned NOT NULL auto_increment,
692
  SUB_ID int unsigned NOT NULL default '0',
693
  REF_ID int unsigned default NULL,
694
  REF_SUB int unsigned default '0',
1 by brian
clean slate
695
  PRIMARY KEY (ID,SUB_ID),
696
  UNIQUE KEY t1_PK (ID,SUB_ID),
697
  KEY t1_FK (REF_ID,REF_SUB),
698
  KEY t1_REFID (REF_ID)
699
) ENGINE=MyISAM CHARSET=cp1251;
700
INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
701
SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
702
DROP TABLE t1;
703
704
#
705
# uninterruptable update
706
#
707
create table t1 (a int, b int);
708
create table t2 (a int, b int);
709
710
insert into t1 values (1,0), (2,0), (3,0);
711
insert into t2 values (1,1), (2,1), (3,1), (2,2);
712
713
update ignore t1 set b=(select b from t2 where t1.a=t2.a);
714
select * from t1;
715
716
drop table t1, t2;
717
718
#
719
# reduced subselect in ORDER BY & GROUP BY clauses
720
#
721
722
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
723
  `id` bigint unsigned NOT NULL auto_increment,
1 by brian
clean slate
724
  `pseudo` varchar(35) NOT NULL default '',
725
  `email` varchar(60) NOT NULL default '',
726
  PRIMARY KEY  (`id`),
727
  UNIQUE KEY `email` (`email`),
728
  UNIQUE KEY `pseudo` (`pseudo`)
729
) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
730
INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
731
SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
732
drop table if exists t1;
733
734
(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
735
736
#
737
# IN subselect optimization test
738
#
739
create table t1 (a int not null, b int, primary key (a));
740
create table t2 (a int not null, primary key (a));
741
create table t3 (a int not null, b int, primary key (a));
742
insert into t1 values (1,10), (2,20), (3,30),  (4,40);
743
insert into t2 values (2), (3), (4), (5);
744
insert into t3 values (10,3), (20,4), (30,5);
745
select * from t2 where t2.a in (select a from t1);
746
explain extended select * from t2 where t2.a in (select a from t1);
747
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
748
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
749
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
750
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
751
drop table t1, t2, t3;
752
create table t1 (a int, b int, index a (a,b));
753
create table t2 (a int, index a (a));
754
create table t3 (a int, b int, index a (a));
755
insert into t1 values (1,10), (2,20), (3,30), (4,40);
756
disable_query_log;
757
# making table large enough
206.1.1 by Stewart Smith
cut 'make test' time in half.
758
set autocommit=0;
759
begin;
1 by brian
clean slate
760
let $1 = 10000;
761
while ($1)
762
 {
763
  eval insert into t1 values (rand()*100000+200,rand()*100000); 
764
  dec $1;
765
 }
206.1.1 by Stewart Smith
cut 'make test' time in half.
766
commit;
767
set autocommit=1;
1 by brian
clean slate
768
enable_query_log;
769
insert into t2 values (2), (3), (4), (5);
770
insert into t3 values (10,3), (20,4), (30,5);
771
select * from t2 where t2.a in (select a from t1);
772
explain extended select * from t2 where t2.a in (select a from t1);
773
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
774
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
775
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
776
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
777
insert into t1 values (3,31);
778
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
779
select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
780
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
781
drop table t1, t2, t3;
782
783
#
784
# alloc_group_fields() working
785
#
786
create table t1 (a int, b int);
787
create table t2 (a int, b int);
788
create table t3 (a int, b int);
789
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
790
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
791
insert into t3 values (3,3), (2,2), (1,1); 
792
select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
793
drop table t1,t2,t3;
794
795
#
796
# aggregate functions in HAVING test
797
#
798
create table t1 (s1 int);
799
create table t2 (s1 int);
800
insert into t1 values (1);
801
insert into t2 values (1);
802
select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
803
drop table t1,t2;
804
805
#
806
# update subquery with wrong field (to force name resolving
807
# in UPDATE name space)
808
#
809
create table t1 (s1 int);
810
create table t2 (s1 int);
811
insert into t1 values (1);
812
insert into t2 values (1);
813
-- error 1054
814
update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
815
DROP TABLE t1, t2;
816
817
#
818
# collation test
819
#
820
CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
821
                 s2 CHAR(5) COLLATE latin1_swedish_ci);
822
INSERT INTO t1 VALUES ('z','?');
823
-- error 1267
824
select * from t1 where s1 > (select max(s2) from t1);
825
-- error 1267
826
select * from t1 where s1 > any (select max(s2) from t1);
827
drop table t1;
828
829
#
830
# aggregate functions reinitialization
831
#
832
create table t1(toid int,rd int);
833
create table t2(userid int,pmnew int,pmtotal int);
834
insert into t2 values(1,0,0),(2,0,0);
835
insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2);
836
select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1);
837
drop table t1, t2;
838
839
#
840
# row union
841
#
842
create table t1 (s1 char(5));
843
-- error 1241
844
select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
845
insert into t1 values ('tttt');
846
select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
847
explain extended (select * from t1);
848
(select * from t1);
849
drop table t1;
850
851
#
852
# IN optimisation test results
853
#
854
create table t1 (s1 char(5), index s1(s1));
855
create table t2 (s1 char(5), index s1(s1));
856
insert into t1 values ('a1'),('a2'),('a3');
857
insert into t2 values ('a1'),('a2');
858
select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
859
select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
860
select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
861
select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
862
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
863
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
864
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
865
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
866
drop table t1,t2;
867
868
#
869
# correct ALL optimisation
870
#
871
create table t2 (a int, b int);
872
create table t3 (a int);
873
insert into t3 values (6),(7),(3);
874
select * from t3 where a >= all (select b from t2);
875
explain extended select * from t3 where a >= all (select b from t2);
876
select * from t3 where a >= some (select b from t2);
877
explain extended select * from t3 where a >= some (select b from t2);
878
select * from t3 where a >= all (select b from t2 group by 1);
879
explain extended select * from t3 where a >= all (select b from t2 group by 1);
880
select * from t3 where a >= some (select b from t2 group by 1);
881
explain extended select * from t3 where a >= some (select b from t2 group by 1);
882
select * from t3 where NULL >= any (select b from t2);
883
explain extended select * from t3 where NULL >= any (select b from t2);
884
select * from t3 where NULL >= any (select b from t2 group by 1);
885
explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
886
select * from t3 where NULL >= some (select b from t2);
887
explain extended select * from t3 where NULL >= some (select b from t2);
888
select * from t3 where NULL >= some (select b from t2 group by 1);
889
explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
890
#
891
# optimized static ALL/ANY with grouping
892
#
893
insert into t2 values (2,2), (2,1), (3,3), (3,1);
894
select * from t3 where a > all (select max(b) from t2 group by a);
895
explain extended select * from t3 where a > all (select max(b) from t2 group by a);
896
drop table t2, t3;
897
898
#
899
# correct used_tables()
900
#
901
223 by Brian Aker
Cleanup int() work.
902
CREATE TABLE `t1` ( `id` bigint NOT NULL auto_increment, `taskid` bigint NOT NULL default '0', `dbid` int NOT NULL default '0', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY  (`id`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=3 ;
1 by brian
clean slate
903
INSERT INTO `t1` (`id`, `taskid`, `dbid`, `create_date`,`last_update`) VALUES (1, 1, 15, '2003-09-29 10:31:36', '2003-09-29 10:31:36'), (2, 1, 21, now(), now());
223 by Brian Aker
Cleanup int() work.
904
CREATE TABLE `t2` (`db_id` int NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint NOT NULL default '0',`secondary_uid` smallint NOT NULL default '0',PRIMARY KEY  (`db_id`),UNIQUE KEY `name_2` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;
1 by brian
clean slate
905
INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0);
223 by Brian Aker
Cleanup int() work.
906
CREATE TABLE `t3` (`taskgenid` bigint NOT NULL auto_increment,`dbid` int NOT NULL default '0',`taskid` int NOT NULL default '0',`mon` tinyint NOT NULL default '1',`tues` tinyint NOT NULL default '1',`wed` tinyint NOT NULL default '1',`thur` tinyint NOT NULL default '1',`fri` tinyint NOT NULL default '1',`sat` tinyint NOT NULL default '0',`sun` tinyint NOT NULL default '0',`how_often` smallint NOT NULL default '1',`userid` smallint NOT NULL default '0',`active` tinyint NOT NULL default '1',PRIMARY KEY  (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;
1 by brian
clean slate
907
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
223 by Brian Aker
Cleanup int() work.
908
CREATE TABLE `t4` (`task_id` smallint NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1 by brian
clean slate
909
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
910
select  dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4  WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
911
SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;
912
drop table t1,t2,t3,t4;
913
914
#
915
# cardinality check
916
#
223 by Brian Aker
Cleanup int() work.
917
CREATE TABLE t1 (id int default NULL) ENGINE=MyISAM CHARSET=latin1;
1 by brian
clean slate
918
INSERT INTO t1 VALUES (1),(5);
223 by Brian Aker
Cleanup int() work.
919
CREATE TABLE t2 (id int default NULL) ENGINE=MyISAM CHARSET=latin1;
1 by brian
clean slate
920
INSERT INTO t2 VALUES (2),(6);
921
-- error 1241
922
select * from t1 where (1,2,6) in (select * from t2);
923
DROP TABLE t1,t2;
924
925
#
926
# optimized ALL/ANY with union
927
#
928
create table t1 (s1 char);
929
insert into t1 values ('e');
930
select * from t1 where 'f' > any (select s1 from t1);
931
select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
932
explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
933
drop table t1;
934
935
#
936
# filesort in subquery (restoring join_tab)
937
#
938
CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
939
INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
940
CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
941
INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
942
select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c;
943
drop table t1, t2;
944
945
#
946
# unresolved field error
947
#
948
create table t1 (s1 int); 
949
create table t2 (s1 int);
950
-- error 1054
951
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
952
-- error 1054
953
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
954
-- error 1054
955
select count(*) from t2 group by t1.s2;
956
drop table t1, t2;
957
958
#
959
# fix_fields() in add_ref_to_table_cond()
960
#
961
CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
962
CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
963
INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
964
INSERT INTO t2 VALUES (100, 200, 'C');
965
SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
966
DROP TABLE t1, t2;
967
223 by Brian Aker
Cleanup int() work.
968
CREATE TABLE t1 (a int);
1 by brian
clean slate
969
INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
970
SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
971
DROP TABLE t1;
972
973
#
974
# Bug 2479
975
#
976
977
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
978
  `id` int NOT NULL auto_increment,
979
  `id_cns` tinyint unsigned NOT NULL default '0',
1 by brian
clean slate
980
  `tipo` enum('','UNO','DUE') NOT NULL default '',
223 by Brian Aker
Cleanup int() work.
981
  `anno_dep` smallint unsigned NOT NULL default '0',
982
  `particolare` bigint unsigned NOT NULL default '0',
983
  `generale` bigint unsigned NOT NULL default '0',
984
  `bis` tinyint unsigned NOT NULL default '0',
1 by brian
clean slate
985
  PRIMARY KEY  (`id`),
986
  UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
987
  UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
988
);
989
INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0);
990
CREATE TABLE `t2` (
223 by Brian Aker
Cleanup int() work.
991
  `id` tinyint unsigned NOT NULL auto_increment,
992
  `max_anno_dep` smallint unsigned NOT NULL default '0',
1 by brian
clean slate
993
  PRIMARY KEY  (`id`)
994
);
995
INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
996
997
SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns;
998
999
DROP TABLE t1, t2;
1000
1001
#
1002
# GLOBAL LIMIT
1003
#
1004
create table t1 (a int);
1005
insert into t1 values (1), (2), (3);
1006
SET SQL_SELECT_LIMIT=1;
1007
select sum(a) from (select * from t1) as a;
1008
select 2 in (select * from t1);
1009
SET SQL_SELECT_LIMIT=default;
1010
drop table t1;
1011
1012
#
1013
# Bug #3118: subselect + order by
1014
#
1015
1016
CREATE TABLE t1 (a int, b int, INDEX (a));
1017
INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1018
SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1019
DROP TABLE t1;
1020
1021
# Item_cond fix field
1022
#
1023
create table t1(val varchar(10));
1024
insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1025
select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');
1026
drop table t1;
1027
1028
#
1029
# ref_or_null replacing with ref
1030
#
1031
create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1032
insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12');
1033
select * from t1 where id not in (select id from t1 where id < 8);
1034
select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
1035
explain extended select * from t1 where id not in (select id from t1 where id < 8);
1036
explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
1037
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1038
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1039
insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
1040
select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
1041
explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
1042
drop table t1,t2;
1043
1044
#
1045
# Static tables & rund() in subqueries
1046
#
1047
create table t1 (a int);
1048
insert into t1 values (1);
1049
explain select benchmark(1000, (select a from t1 where a=rand()));
1050
drop table t1;
1051
1052
#
1053
# bug 3188
1054
#
1055
create table t1(id int);
1056
create table t2(id int);
1057
create table t3(flag int);
1058
-- error 1064
1059
select (select * from t3 where id not null) from t1, t2;
1060
drop table t1,t2,t3;
1061
1062
#
1063
# aggregate functions (Bug #3505)
1064
#
1065
CREATE TABLE t1 (id INT);
1066
CREATE TABLE t2 (id INT);
1067
INSERT INTO t1 VALUES (1), (2);
1068
INSERT INTO t2 VALUES (1);
1069
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1070
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1071
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
1072
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
1073
DROP TABLE t1,t2;
1074
1075
#
1076
# ALL/ANY test
1077
#
1078
CREATE TABLE t1 ( a int, b int );
1079
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1080
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1081
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1082
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1083
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1084
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1085
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1086
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1087
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1088
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1089
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1090
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1091
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1092
# with index
1093
ALTER TABLE t1 ADD INDEX (a);
1094
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1095
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1096
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1097
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1098
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1099
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1100
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1101
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1102
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1103
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1104
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1105
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1106
# having clause test
1107
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
1108
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
1109
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
1110
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
1111
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
1112
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
1113
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
1114
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
1115
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
1116
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
1117
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
1118
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
1119
# union test
1120
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1121
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1122
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1123
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1124
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1125
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1126
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1127
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1128
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1129
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1130
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1131
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1132
# union + having test
1133
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1134
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1135
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1136
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1137
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1138
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1139
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1140
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1141
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1142
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1143
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1144
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1145
# row tests
1146
# < > >= <= and = ALL/ <> ANY do not support row operation
1147
-- error 1241
1148
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
1149
-- error 1241
1150
SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1151
-- error 1241
1152
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1153
-- error 1241
1154
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
1155
-- error 1241
1156
SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1157
-- error 1241
1158
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1159
-- error 1241
1160
SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
1161
-- error 1241
1162
SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
1163
# following should be converted to IN
1164
-- error 1241
1165
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
1166
-- error 1241
1167
SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1168
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1169
-- error 1241
1170
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
1171
-- error 1241
1172
SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1173
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1174
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
1175
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
1176
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
1177
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
1178
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
1179
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
1180
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
1181
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
1182
# without optimisation
1183
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1184
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1185
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1186
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1187
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1188
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1189
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1190
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1191
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1192
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1193
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1194
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1195
# without optimisation + having
1196
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
1197
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
1198
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
1199
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1200
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1201
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
1202
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
1203
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
1204
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
1205
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1206
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1207
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
1208
# EXISTS in string contence
1209
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
1210
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
1211
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
1212
DROP TABLE t1;
1213
CREATE TABLE t1 ( a double, b double );
1214
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1215
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
1216
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
1217
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
1218
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
1219
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
1220
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
1221
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
1222
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
1223
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
1224
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
1225
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
1226
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
1227
DROP TABLE t1;
1228
CREATE TABLE t1 ( a char(1), b char(1));
1229
INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
1230
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
1231
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
1232
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
1233
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
1234
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
1235
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
1236
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
1237
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
1238
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
1239
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
1240
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
1241
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
1242
DROP TABLE t1;
1243
1244
1245
#
1246
# SELECT(EXISTS * ...)optimisation
1247
#
1248
create table t1 (a int, b int);
1249
insert into t1 values (1,2),(3,4);
1250
select * from t1 up where exists (select * from t1 where t1.a=up.a);
1251
explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
1252
drop table t1;
1253
1254
#
1255
# Bug #4102: subselect in HAVING
1256
#
1257
1258
CREATE TABLE t1 (t1_a int);
1259
INSERT INTO t1 VALUES (1);
1260
CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
1261
INSERT INTO t2 VALUES (1, 1), (1, 2);
1262
SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
1263
  HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
1264
DROP TABLE t1, t2;
1265
1266
#
1267
# Test problem with NULL and derived tables (Bug #4097)
1268
#
1269
223 by Brian Aker
Cleanup int() work.
1270
CREATE TABLE t1 (id int default NULL,name varchar(10) default NULL);
1 by brian
clean slate
1271
INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
223 by Brian Aker
Cleanup int() work.
1272
CREATE TABLE t2 (id int default NULL, pet varchar(10) default NULL);
1 by brian
clean slate
1273
INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
1274
SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
1275
drop table t1,t2;
1276
1277
#
1278
# Aggregate function comparation with ALL/ANY/SOME subselect
1279
#
223 by Brian Aker
Cleanup int() work.
1280
CREATE TABLE `t1` ( `a` int default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1 by brian
clean slate
1281
insert into t1 values (1);
223 by Brian Aker
Cleanup int() work.
1282
CREATE TABLE `t2` ( `b` int default NULL, `a` int default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1 by brian
clean slate
1283
insert into t2 values (1,2);
1284
select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
1285
drop table t1,t2;
1286
1287
#
1288
# BUG#5003 - like in subselect
1289
#
1290
CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`KUERZEL` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin,`IZAANALYSEART_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`IZAPMKZ_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin);
1291
CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
1292
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
1293
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
1294
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
1295
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
1296
SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
1297
drop table t1;
1298
1299
#
1300
# Optimized IN with compound index
1301
#
223 by Brian Aker
Cleanup int() work.
1302
CREATE TABLE `t1` ( `aid` int NOT NULL default '0', `bid` int NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
1303
CREATE TABLE `t2` ( `aid` int NOT NULL default '0', `bid` int NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
1 by brian
clean slate
1304
insert into t1 values (1,1),(1,2),(2,1),(2,2);
1305
insert into t2 values (1,2),(2,2);
1306
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1307
alter table t2 drop primary key;
1308
alter table t2 add key KEY1 (aid, bid);
1309
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1310
alter table t2 drop key KEY1;
1311
alter table t2 add primary key (bid, aid);
1312
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1313
drop table t1,t2;
1314
1315
#
1316
# resolving fields of grouped outer SELECT
1317
#
1318
CREATE TABLE t1 (howmanyvalues bigint, avalue int);
1319
INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
1320
SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
1321
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1322
CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
1323
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
1324
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1325
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
1326
drop table t1;
1327
1328
create table t1 (x int);
1329
select  (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
1330
drop table t1;
1331
1332
#
1333
# Test of correct maybe_null flag returning by subquwery for temporary table
1334
# creation
1335
#
223 by Brian Aker
Cleanup int() work.
1336
CREATE TABLE `t1` ( `master` int unsigned NOT NULL default '0', `map` smallint unsigned NOT NULL default '0', `slave` int unsigned NOT NULL default '0', `access` int unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
1 by brian
clean slate
1337
INSERT INTO `t1` VALUES (1,0,0,700),(1,1,1,400),(1,5,5,400),(1,12,12,400),(1,12,32,400),(4,12,32,400);
223 by Brian Aker
Cleanup int() work.
1338
CREATE TABLE `t2` ( `id` int unsigned NOT NULL default '0', `pid` int unsigned NOT NULL default '0', `map` smallint unsigned NOT NULL default '0', `level` tinyint unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY  (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
1 by brian
clean slate
1339
INSERT INTO `t2` VALUES (6,5,12,7,'a'),(12,0,0,7,'a'),(12,1,0,7,'a'),(12,5,5,7,'a'),(12,5,12,7,'a');
1340
-- error 1054
1341
SELECT b.sc FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
1342
SELECT b.ac FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
1343
drop tables t1,t2;
1344
1345
#
1346
# Subselect in non-select command just after connection
1347
#
1348
connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
1349
connection root;
1350
set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
1351
1352
#
1353
# primary query with temporary table and subquery with groupping
1354
#
1355
create table t1 (a int, b int);
1356
create table t2 (a int, b int);
1357
insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
1358
insert into t2 values (1,3),(2,1);
1359
select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
1360
drop table t1, t2;
1361
1362
#
1363
# Equal operation under row and empty subquery
1364
#
1365
create table t1 (s1 int,s2 int);
1366
insert into t1 values (20,15);
1367
select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
1368
drop table t1;
1369
1370
#
1371
# ALL/ANY with NULL
1372
#
1373
create table t1 (s1 int);
1374
insert into t1 values (1),(null);
1375
select * from t1 where s1 < all (select s1 from t1);
1376
select s1, s1 < all (select s1 from t1) from t1;
1377
drop table t1;
1378
1379
#
1380
# reference on changable fields from subquery
1381
#
1382
CREATE TABLE t1 (
1383
  Code char(3) NOT NULL default '',
1384
  Name char(52) NOT NULL default '',
1385
  Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1386
  Region char(26) NOT NULL default '',
1387
  SurfaceArea float(10,2) NOT NULL default '0.00',
223 by Brian Aker
Cleanup int() work.
1388
  IndepYear smallint default NULL,
1389
  Population int NOT NULL default '0',
1 by brian
clean slate
1390
  LifeExpectancy float(3,1) default NULL,
1391
  GNP float(10,2) default NULL,
1392
  GNPOld float(10,2) default NULL,
1393
  LocalName char(45) NOT NULL default '',
1394
  GovernmentForm char(45) NOT NULL default '',
1395
  HeadOfState char(60) default NULL,
223 by Brian Aker
Cleanup int() work.
1396
  Capital int default NULL,
1 by brian
clean slate
1397
  Code2 char(2) NOT NULL default ''
1398
) ENGINE=MyISAM;
1399
INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
1400
INSERT INTO t1 VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,0,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS');
1401
INSERT INTO t1 VALUES ('ATF','French Southern territories','Antarctica','Antarctica',7780.00,0,0,NULL,0.00,NULL,'Terres australes françaises','Nonmetropolitan Territory of France','Jacques Chirac',NULL,'TF');
1402
INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','Micronesia/Caribbean',16.00,0,0,NULL,0.00,NULL,'United States Minor Outlying Islands','Dependent Territory of the US','George W. Bush',NULL,'UM');
1403
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
1404
SELECT DISTINCT Continent AS c FROM t1 outr WHERE 
1405
  Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND 
1406
  Population < 200);
1407
drop table t1;
1408
1409
#
1410
# Test for BUG#7885: Server crash when 'any' subselect compared to
1411
# non-existant field.
1412
#
1413
create table t1 (a1 int);
1414
create table t2 (b1 int);
1415
--error 1054
1416
select * from t1 where a2 > any(select b1 from t2);
1417
select * from t1 where a1 > any(select b1 from t2);
1418
drop table t1,t2;
1419
1420
1421
#
1422
# Comparison subquery with * and row
1423
#
1424
create table t1 (a integer, b integer);
1425
select (select * from t1) = (select 1,2);
1426
select (select 1,2) = (select * from t1);
1427
# queries whih can be converted to IN
1428
select  row(1,2) = ANY (select * from t1);
1429
select  row(1,2) != ALL (select * from t1);
1430
drop table t1;
1431
1432
#
1433
# Comparison subquery and row with nested rows
1434
#
1435
create table t1 (a integer, b integer);
1436
-- error 1241
1437
select row(1,(2,2)) in (select * from t1 );
1438
-- error 1241
1439
select row(1,(2,2)) = (select * from t1 );
1440
-- error 1241
1441
select (select * from t1) = row(1,(2,2));
1442
drop table t1;
1443
1444
#
1445
# Forward reference detection
1446
#
1447
create  table t1 (a integer);
1448
insert into t1 values (1);
1449
-- error 1247
1450
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1451
-- error 1247
1452
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1453
select 1 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
1454
-- error 1247
1455
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1456
drop table t1;
1457
1458
#
1459
# Test for BUG#8218
1460
#
1461
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
1462
  categoryId int NOT NULL,
1463
  courseId int NOT NULL,
1 by brian
clean slate
1464
  startDate datetime NOT NULL,
1465
  endDate datetime NOT NULL,
1466
  createDate datetime NOT NULL,
1467
  modifyDate timestamp NOT NULL,
1468
  attributes text NOT NULL
1469
);
1470
INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1471
(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1472
(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1473
(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
1474
(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
1475
(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1476
(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1477
(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1478
(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
1479
1480
CREATE TABLE t2 (
223 by Brian Aker
Cleanup int() work.
1481
  userId int NOT NULL,
1482
  courseId int NOT NULL,
1 by brian
clean slate
1483
  date datetime NOT NULL
1484
);
1485
INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
1486
(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
1487
(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
1488
(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
1489
(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
1490
(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
1491
1492
1493
CREATE TABLE t3 (
223 by Brian Aker
Cleanup int() work.
1494
  groupId int NOT NULL,
1495
  parentId int NOT NULL,
1 by brian
clean slate
1496
  startDate datetime NOT NULL,
1497
  endDate datetime NOT NULL,
1498
  createDate datetime NOT NULL,
1499
  modifyDate timestamp NOT NULL,
223 by Brian Aker
Cleanup int() work.
1500
  ordering int
1 by brian
clean slate
1501
);
1502
INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
1503
1504
CREATE TABLE t4 (
223 by Brian Aker
Cleanup int() work.
1505
  id int NOT NULL,
1506
  groupTypeId int NOT NULL,
1 by brian
clean slate
1507
  groupKey varchar(50) NOT NULL,
1508
  name text,
223 by Brian Aker
Cleanup int() work.
1509
  ordering int,
1 by brian
clean slate
1510
  description text,
1511
  createDate datetime NOT NULL,
1512
  modifyDate timestamp NOT NULL
1513
);
1514
INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
1515
(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
1516
1517
CREATE TABLE t5 (
223 by Brian Aker
Cleanup int() work.
1518
  userId int NOT NULL,
1519
  groupId int NOT NULL,
1 by brian
clean slate
1520
  createDate datetime NOT NULL,
1521
  modifyDate timestamp NOT NULL
1522
);
1523
INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
1524
1525
select
1526
  count(distinct t2.userid) pass,
1527
  groupstuff.*,
1528
  count(t2.courseid) crse,
1529
  t1.categoryid, 
1530
  t2.courseid,
1531
  date_format(date, '%b%y') as colhead
1532
from t2   
1533
join t1 on t2.courseid=t1.courseid  
1534
join
1535
(
1536
  select 
1537
    t5.userid,  
1538
    parentid,  
1539
    parentgroup,  
1540
    childid,  
1541
    groupname,  
1542
    grouptypeid  
1543
  from t5 
1544
  join 
1545
  (
1546
     select t4.id as parentid,  
1547
       t4.name as parentgroup,  
1548
       t4.id as childid,  
1549
       t4.name as groupname,  
1550
       t4.grouptypeid  
1551
     from   t4  
1552
  ) as gin on t5.groupid=gin.childid 
1553
) as groupstuff on t2.userid = groupstuff.userid 
1554
group by 
1555
  groupstuff.groupname, colhead , t2.courseid;
1556
1557
drop table t1, t2, t3, t4, t5;
1558
1559
#
1560
# Transformation in left expression of subquery (BUG#8888)
1561
#
1562
create table t1 (a int);
1563
insert into t1 values (1), (2), (3);
1564
SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
1565
drop table t1;
1566
1567
#
1568
# single row subqueries and row operations (code covarage improvement)
1569
#
1570
create table t1 (a int, b int);
1571
insert into t1 values (1,2);
1572
-- error 1241
1573
select 1 = (select * from t1);
1574
-- error 1241
1575
select (select * from t1) = 1;
1576
-- error 1241
1577
select (1,2) = (select a from t1);
1578
-- error 1241
1579
select (select a from t1) = (1,2);
1580
-- error 1241
1581
select (1,2,3) = (select * from t1);
1582
-- error 1241
1583
select (select * from t1) = (1,2,3);
1584
drop table t1;
1585
1586
#
1587
# Item_int_with_ref check (BUG#10020)
1588
#
1589
CREATE TABLE `t1` (
223 by Brian Aker
Cleanup int() work.
1590
  `itemid` bigint unsigned NOT NULL auto_increment,
1591
  `sessionid` bigint unsigned default NULL,
1592
  `time` int unsigned NOT NULL default '0',
1 by brian
clean slate
1593
  `data` text collate latin1_general_ci NOT NULL,
1594
  PRIMARY KEY  (`itemid`)
1595
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
325 by Brian Aker
Remove SET
1596
INSERT INTO `t1` VALUES (1, 1, 1, '');
1 by brian
clean slate
1597
CREATE TABLE `t2` (
223 by Brian Aker
Cleanup int() work.
1598
  `sessionid` bigint unsigned NOT NULL auto_increment,
1599
  `pid` int unsigned NOT NULL default '0',
1600
  `date` int unsigned NOT NULL default '0',
1 by brian
clean slate
1601
  `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1602
  PRIMARY KEY  (`sessionid`)
1603
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1604
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1605
SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
1606
drop tables t1,t2;
1607
1608
# BUG#11821 : Select from subselect using aggregate function on an enum
1609
# segfaults:
1610
create table t1 (fld enum('0','1'));
1611
insert into t1 values ('1');
1612
select * from (select max(fld) from t1) as foo;
1613
drop table t1;
1614
1615
#
1616
# Bug #11867: queries with ROW(,elems>) IN (SELECT DISTINCT <cols> FROM ...)
1617
#
1618
1619
CREATE TABLE t1 (one int, two int, flag char(1));
1620
CREATE TABLE t2 (one int, two int, flag char(1));
1621
INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
1622
INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
1623
1624
SELECT * FROM t1
1625
  WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
1626
SELECT * FROM t1
1627
  WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
1628
1629
insert into t2 values (null,null,'N');
1630
insert into t2 values (null,3,'0');
1631
insert into t2 values (null,5,'0');
1632
insert into t2 values (10,null,'0');
1633
insert into t1 values (10,3,'0');
1634
insert into t1 values (10,5,'0');
1635
insert into t1 values (10,10,'0');
1636
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
1637
SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
1638
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
1639
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
1640
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
1641
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
1642
explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
1643
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
1644
DROP TABLE t1,t2;
1645
1646
#
1647
# Bug #12392: where cond with IN predicate for rows and NULL values in table 
1648
#
1649
1650
CREATE TABLE t1 (a char(5), b char(5));
1651
INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
1652
1653
SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
1654
1655
DROP TABLE t1;
1656
1657
#
1658
# Bug #11479: subquery over left join with an empty inner table 
1659
#
1660
1661
CREATE TABLE t1 (a int);
1662
CREATE TABLE t2 (a int, b int);
1663
CREATE TABLE t3 (b int NOT NULL);
1664
INSERT INTO t1 VALUES (1), (2), (3), (4);
1665
INSERT INTO t2 VALUES (1,10), (3,30);
1666
1667
SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1668
  WHERE t3.b IS NOT NULL OR t2.a > 10;
1669
SELECT * FROM t1
1670
  WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1671
                       WHERE t3.b IS NOT NULL OR t2.a > 10);
1672
1673
DROP TABLE t1,t2,t3;
1674
1675
#
1676
# Bug#18503: Queries with a quantified subquery returning empty set may
1677
# return a wrong result. 
1678
#
1679
CREATE TABLE t1 (f1 INT);
1680
CREATE TABLE t2 (f2 INT);
1681
INSERT INTO t1 VALUES (1);
1682
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
1683
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
1684
INSERT INTO t2 VALUES (1);
1685
INSERT INTO t2 VALUES (2);
1686
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
1687
DROP TABLE t1, t2;
1688
1689
# BUG#20975 Wrong query results for subqueries within NOT
1690
create table t1 (s1 char);
1691
insert into t1 values (1),(2);
1692
1693
select * from t1 where (s1 < any (select s1 from t1));
1694
select * from t1 where not (s1 < any (select s1 from t1));
1695
1696
select * from t1 where (s1 < ALL (select s1+1 from t1));
1697
select * from t1 where not(s1 < ALL (select s1+1 from t1));
1698
1699
select * from t1 where (s1+1 = ANY (select s1 from t1));
1700
select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
1701
1702
select * from t1 where (s1 = ALL (select s1/s1 from t1));
1703
select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
1704
drop table t1;
1705
1706
#
1707
# Bug #16255: Subquery in where
1708
#
1709
create table t1 (
1710
  retailerID varchar(8) NOT NULL,
223 by Brian Aker
Cleanup int() work.
1711
  statusID   int unsigned NOT NULL,
1 by brian
clean slate
1712
  changed    datetime NOT NULL,
1713
  UNIQUE KEY retailerID (retailerID, statusID, changed)
1714
);
1715
1716
INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
1717
INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
1718
INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
1719
INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
1720
INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
1721
INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
1722
1723
select * from t1 r1 
1724
  where (r1.retailerID,(r1.changed)) in 
1725
         (SELECT r2.retailerId,(max(changed)) from t1 r2 
1726
          group by r2.retailerId);
1727
drop table t1;
1728
1729
#
1730
# Bug #21180: Subselect with index for both WHERE and ORDER BY 
1731
#             produces empty result
1732
#
1733
create table t1(a int, primary key (a));
1734
insert into t1 values (10);
1735
1736
create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
1737
insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
1738
1739
explain SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 
1740
  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 
1741
             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1742
SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 
1743
  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 
1744
            ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1745
1746
explain SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 
1747
  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 
1748
            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1749
SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 
1750
  ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 
1751
            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1752
1753
drop table t1,t2;
1754
1755
#
1756
# Bug #21853: assert failure for a grouping query with
1757
#             an ALL/ANY quantified subquery in HAVING 
1758
#
1759
1760
CREATE TABLE t1 (                  
1761
  field1 int NOT NULL,                 
1762
  field2 int NOT NULL,                 
1763
  field3 int NOT NULL,                 
1764
  PRIMARY KEY  (field1,field2,field3)  
1765
);
1766
CREATE TABLE t2 (             
1767
  fieldA int NOT NULL,            
1768
  fieldB int NOT NULL,            
1769
  PRIMARY KEY  (fieldA,fieldB)     
1770
); 
1771
1772
INSERT INTO t1 VALUES
1773
  (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
1774
INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
1775
1776
SELECT field1, field2, COUNT(*)
1777
  FROM t1 GROUP BY field1, field2;
1778
1779
SELECT field1, field2
1780
  FROM  t1
1781
    GROUP BY field1, field2
1782
      HAVING COUNT(*) >= ALL (SELECT fieldB 
1783
                                FROM t2 WHERE fieldA = field1);
1784
SELECT field1, field2
1785
  FROM  t1
1786
    GROUP BY field1, field2
1787
      HAVING COUNT(*) < ANY (SELECT fieldB 
1788
                               FROM t2 WHERE fieldA = field1);
1789
1790
DROP TABLE t1, t2;
1791
1792
#
1793
# Bug #23478: not top-level IN subquery returning a non-empty result set
1794
#             with possible NULL values by index access from the outer query
1795
#
1796
1797
CREATE TABLE t1(a int, INDEX (a));
1798
INSERT INTO t1 VALUES (1), (3), (5), (7);
1799
INSERT INTO t1 VALUES (NULL);
1800
1801
CREATE TABLE t2(a int);
1802
INSERT INTO t2 VALUES (1),(2),(3);
1803
1804
EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
1805
SELECT a, a IN (SELECT a FROM t1) FROM t2;
1806
1807
DROP TABLE t1,t2;
1808
1809
#
1810
# Bug #11302: getObject() returns a String for a sub-query of type datetime
1811
#
1812
CREATE TABLE t1 (a DATETIME);
1813
INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
1814
1815
CREATE TABLE t2 AS SELECT 
1816
  (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a 
1817
   FROM t1 WHERE a > '2000-01-01';
1818
SHOW CREATE TABLE t2;
1819
1820
CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01'); 
1821
SHOW CREATE TABLE t3;
1822
1823
DROP TABLE t1,t2,t3;
1824
1825
#
1826
# Bug 24653: sorting by expressions containing subselects
1827
#            that return more than one row
1828
#
1829
1830
CREATE TABLE t1 (a int);
1831
INSERT INTO t1 VALUES (2), (4), (1), (3);
1832
1833
CREATE TABLE t2 (b int, c int);
1834
INSERT INTO t2 VALUES
1835
  (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
1836
1837
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
1838
--error 1242
1839
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
1840
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
1841
--error 1242
1842
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
1843
1844
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
1845
--error 1242
1846
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
1847
1848
1849
SELECT a FROM t1 GROUP BY a
1850
  HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
1851
                (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
1852
--error 1242
1853
SELECT a FROM t1 GROUP BY a
1854
  HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
1855
                (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
1856
1857
SELECT a FROM t1 GROUP BY a
1858
  HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
1859
                (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
1860
--error 1242
1861
SELECT a FROM t1 GROUP BY a
1862
  HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
1863
                (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
1864
1865
SELECT a FROM t1
1866
  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
1867
                  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
1868
--error 1242
1869
SELECT a FROM t1
1870
  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
1871
                  (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
1872
1873
SELECT a FROM t1
1874
  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
1875
                  (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
1876
--error 1242
1877
SELECT a FROM t1
1878
  ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
1879
                  (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
1880
1881
DROP TABLE t1,t2;
1882
1883
# End of 4.1 tests
1884
1885
#
1886
#decimal-related tests
1887
#
1888
create table t1 (df decimal(5,1));
1889
insert into t1 values(1.1);
1890
insert into t1 values(2.2);
1891
1892
select * from t1 where df <= all (select avg(df) from t1 group by df);
1893
select * from t1 where df >= all (select avg(df) from t1 group by df);
1894
drop table t1;
1895
1896
create table t1 (df decimal(5,1));
1897
insert into t1 values(1.1);
1898
select 1.1 * exists(select * from t1);
1899
drop table t1;
1900
1901
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
1902
  grp int default NULL,
1 by brian
clean slate
1903
  a decimal(10,2) default NULL);
1904
1905
insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
1906
select * from t1;
1907
select min(a) from t1 group by grp;
1908
drop table t1;
1909
1910
#
1911
# Test for bug #9338: lame substitution of c1 instead of c2 
1912
#
1913
1914
CREATE table t1 ( c1 integer );
1915
INSERT INTO t1 VALUES ( 1 );
1916
INSERT INTO t1 VALUES ( 2 );
1917
INSERT INTO t1 VALUES ( 3 );
1918
1919
CREATE TABLE t2 ( c2 integer );
1920
INSERT INTO t2 VALUES ( 1 );
1921
INSERT INTO t2 VALUES ( 4 );
1922
INSERT INTO t2 VALUES ( 5 );
1923
1924
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
1925
1926
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
1927
  WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
1928
1929
DROP TABLE t1,t2;
1930
1931
#
1932
# Test for bug #9516: wrong evaluation of not_null_tables attribute in SQ 
1933
#
1934
CREATE TABLE t1 ( c1 integer );
1935
INSERT INTO t1 VALUES ( 1 );
1936
INSERT INTO t1 VALUES ( 2 );
1937
INSERT INTO t1 VALUES ( 3 );
1938
INSERT INTO t1 VALUES ( 6 ); 
1939
 
1940
CREATE TABLE t2 ( c2 integer );
1941
INSERT INTO t2 VALUES ( 1 );
1942
INSERT INTO t2 VALUES ( 4 );
1943
INSERT INTO t2 VALUES ( 5 );
1944
INSERT INTO t2 VALUES ( 6 );
1945
1946
CREATE TABLE t3 ( c3 integer );
1947
INSERT INTO t3 VALUES ( 7 );
1948
INSERT INTO t3 VALUES ( 8 );
1949
1950
SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2 
1951
  WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
1952
1953
DROP TABLE t1,t2,t3;
1954
1955
#
1956
# Correct building of equal fields list (do not include outer
1957
# fields) (BUG#6384)
1958
#
1959
CREATE TABLE t1 (EMPNUM   CHAR(3));
1960
CREATE TABLE t2 (EMPNUM   CHAR(3) );
1961
INSERT INTO t1 VALUES ('E1'),('E2');
1962
INSERT INTO t2 VALUES ('E1');
1963
DELETE FROM t1
1964
WHERE t1.EMPNUM NOT IN
1965
      (SELECT t2.EMPNUM
1966
       FROM t2
1967
       WHERE t1.EMPNUM = t2.EMPNUM);
1968
select * from t1;
1969
DROP TABLE t1,t2;
1970
1971
#
1972
# Test for bug #11487: range access in a subquery
1973
#
1974
1975
CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
1976
INSERT INTO t1 VALUES (1, 1);
1977
CREATE TABLE t2 (select_id BIGINT, values_id BIGINT, 
1978
                 PRIMARY KEY(select_id,values_id));
1979
INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
1980
1981
SELECT values_id FROM t1 
1982
WHERE values_id IN (SELECT values_id FROM t2
1983
                    WHERE select_id IN (1, 0));
1984
SELECT values_id FROM t1 
1985
WHERE values_id IN (SELECT values_id FROM t2
1986
                    WHERE select_id BETWEEN 0 AND 1);
1987
SELECT values_id FROM t1 
1988
WHERE values_id IN (SELECT values_id FROM t2
1989
                    WHERE select_id = 0 OR select_id = 1);
1990
1991
DROP TABLE t1, t2;
1992
1993
# BUG#11821 : Select from subselect using aggregate function on an enum
1994
# segfaults:
1995
create table t1 (fld enum('0','1'));
1996
insert into t1 values ('1');
1997
select * from (select max(fld) from t1) as foo;
1998
drop table t1;
1999
2000
#
2001
# Test for bug #11762: subquery with an aggregate function in HAVING
2002
#
2003
2004
CREATE TABLE t1 (a int, b int);
2005
CREATE TABLE t2 (c int, d int);
2006
CREATE TABLE t3 (e int);
2007
2008
INSERT INTO t1 VALUES 
2009
  (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
2010
INSERT INTO t2 VALUES
2011
  (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
2012
INSERT INTO t3 VALUES (10), (30), (10), (20) ;
2013
2014
SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
2015
SELECT * FROM t2;
2016
SELECT * FROM t3;
2017
2018
SELECT a FROM t1 GROUP BY a
2019
  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
2020
SELECT a FROM t1 GROUP BY a
2021
  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
2022
SELECT a FROM t1 GROUP BY a
2023
  HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
2024
SELECT a FROM t1 GROUP BY a
2025
  HAVING a IN (SELECT c FROM t2
2026
                 WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2027
SELECT a FROM t1 GROUP BY a
2028
  HAVING a IN (SELECT c FROM t2
2029
                 WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2030
SELECT a FROM t1 GROUP BY a
2031
  HAVING a IN (SELECT c FROM t2
2032
                 WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2033
SELECT a FROM t1 GROUP BY a
2034
  HAVING a IN (SELECT c FROM t2
2035
                 WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
2036
SELECT a FROM t1 GROUP BY a
2037
  HAVING a IN (SELECT c FROM t2
2038
                 WHERE MIN(b) < d AND 
2039
                       EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2040
2041
SELECT a, SUM(a) FROM t1 GROUP BY a;
2042
2043
SELECT a FROM t1
2044
   WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
2045
SELECT a FROM t1 GROUP BY a
2046
   HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
2047
2048
SELECT a FROM t1
2049
   WHERE a < 3 AND
2050
         EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
2051
SELECT a FROM t1
2052
   WHERE a < 3 AND
2053
         EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
2054
2055
SELECT t1.a FROM t1 GROUP BY t1.a
2056
  HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
2057
                       HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2058
                                       HAVING SUM(t1.a+t2.c) < t3.e/4));
2059
SELECT t1.a FROM t1 GROUP BY t1.a
2060
       HAVING t1.a > ALL(SELECT t2.c FROM t2
2061
                           WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2062
                                          HAVING SUM(t1.a+t2.c) < t3.e/4));
2063
-- error 1111
2064
SELECT t1.a FROM t1 GROUP BY t1.a
2065
       HAVING t1.a > ALL(SELECT t2.c FROM t2
2066
                           WHERE EXISTS(SELECT t3.e FROM t3 
2067
                                          WHERE SUM(t1.a+t2.c) < t3.e/4));
2068
-- error 1111 
2069
SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
2070
2071
SELECT t1.a FROM t1 GROUP BY t1.a
2072
  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2073
                    HAVING AVG(t2.c+SUM(t1.b)) > 20);
2074
SELECT t1.a FROM t1 GROUP BY t1.a
2075
  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2076
                    HAVING AVG(SUM(t1.b)) > 20);
2077
2078
SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
2079
  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2080
                    HAVING t2.c+sum > 20);
2081
2082
DROP TABLE t1,t2,t3;
2083
2084
#
2085
# Test for bug #16603: GROUP BY in a row subquery with a quantifier 
2086
#                      when an index is defined on the grouping field
2087
2088
CREATE TABLE t1 (a varchar(5), b varchar(10));
2089
INSERT INTO t1 VALUES
2090
  ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
2091
  ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
2092
2093
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2094
EXPLAIN
2095
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2096
2097
ALTER TABLE t1 ADD INDEX(a);
2098
2099
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2100
EXPLAIN
2101
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2102
2103
DROP TABLE t1;
2104
2105
#
2106
# Bug#17366: Unchecked Item_int results in server crash
2107
#
2108
create table t1( f1 int,f2 int);
2109
insert into t1 values (1,1),(2,2);
2110
select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1';
2111
drop table t1;
2112
2113
#
2114
# Bug #18306: server crash on delete using subquery.
2115
#
2116
2117
create table t1 (c int, key(c));                              
2118
insert into t1 values (1142477582), (1142455969);
2119
create table t2 (a int, b int);
2120
insert into t2 values (2, 1), (1, 0);
2121
delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
2122
drop table t1, t2;
2123
2124
#
2125
# Bug#19077: A nested materialized derived table is used before being populated.
2126
#
2127
create table t1 (i int, j bigint);
2128
insert into t1 values (1, 2), (2, 2), (3, 2);
2129
select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
2130
drop table t1;
2131
2132
# 
2133
# Bug#19700: subselect returning BIGINT always returned it as SIGNED
2134
#
2135
CREATE TABLE t1 (i BIGINT UNSIGNED);
2136
INSERT INTO t1 VALUES (10000000000000000000); # > MAX SIGNED BIGINT 9323372036854775807
2137
INSERT INTO t1 VALUES (1);
2138
2139
CREATE TABLE t2 (i BIGINT UNSIGNED);
2140
INSERT INTO t2 VALUES (10000000000000000000); # same as first table
2141
INSERT INTO t2 VALUES (1);
2142
2143
/* simple test */
2144
SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
2145
2146
/* subquery test */
2147
SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
2148
2149
/* subquery test with cast*/
2150
SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
2151
2152
DROP TABLE t1;
2153
DROP TABLE t2;
2154
2155
# 
2156
# Bug#20519: subselect with LIMIT M, N
2157
#
2158
2159
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
2160
  id bigint unsigned NOT NULL auto_increment,
1 by brian
clean slate
2161
  name varchar(255) NOT NULL,
2162
  PRIMARY KEY  (id)
2163
);
2164
INSERT INTO t1 VALUES
2165
  (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
2166
2167
CREATE TABLE t2 (
223 by Brian Aker
Cleanup int() work.
2168
  id bigint unsigned NOT NULL auto_increment,
2169
  mid bigint unsigned NOT NULL,
1 by brian
clean slate
2170
  date date NOT NULL,
2171
  PRIMARY KEY  (id)
2172
);
2173
INSERT INTO t2 VALUES 
2174
  (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
2175
  (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
2176
2177
SELECT *,
2178
      (SELECT date FROM t2 WHERE mid = t1.id
2179
         ORDER BY date DESC LIMIT 0, 1) AS date_last,
2180
      (SELECT date FROM t2 WHERE mid = t1.id
2181
         ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
2182
  FROM t1;
2183
SELECT *,
2184
      (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
2185
         ORDER BY date DESC LIMIT 1, 1) AS date_count
2186
  FROM t1;
2187
SELECT *,
2188
      (SELECT date FROM t2 WHERE mid = t1.id
2189
         ORDER BY date DESC LIMIT 0, 1) AS date_last,
2190
      (SELECT date FROM t2 WHERE mid = t1.id
2191
         ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
2192
  FROM t1;
2193
DROP TABLE t1,t2;
2194
2195
#
2196
# Bug#20869: subselect with range access by DESC
2197
#
2198
2199
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
2200
  i1 int NOT NULL default '0',
2201
  i2 int NOT NULL default '0',
1 by brian
clean slate
2202
  t datetime NOT NULL default '0000-00-00 00:00:00',
2203
  PRIMARY KEY  (i1,i2,t)
2204
);
2205
INSERT INTO t1 VALUES 
2206
(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
2207
(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
2208
(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
2209
(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
2210
(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
2211
(24,2,'2005-05-27 12:40:06');
2212
2213
CREATE TABLE t2 (
223 by Brian Aker
Cleanup int() work.
2214
  i1 int NOT NULL default '0',
2215
  i2 int NOT NULL default '0',
1 by brian
clean slate
2216
  t datetime default NULL,
2217
  PRIMARY KEY  (i1)
2218
);
2219
INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
2220
2221
EXPLAIN
2222
SELECT * FROM t1,t2
2223
  WHERE t1.t = (SELECT t1.t FROM t1 
2224
                  WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
2225
                    ORDER BY t1.t DESC LIMIT 1);
2226
SELECT * FROM t1,t2
2227
  WHERE t1.t = (SELECT t1.t FROM t1 
2228
                  WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
2229
                    ORDER BY t1.t DESC LIMIT 1);
2230
2231
DROP TABLE t1, t2;
2232
2233
#
2234
# Bug#14654 : Cannot select from the same table twice within a UNION
2235
# statement 
2236
#
2237
CREATE TABLE t1 (i INT);
2238
2239
(SELECT i FROM t1) UNION (SELECT i FROM t1);
2240
SELECT * FROM t1 WHERE NOT EXISTS 
2241
  (
2242
   (SELECT i FROM t1) UNION 
2243
   (SELECT i FROM t1)
2244
  );
2245
2246
#TODO:not supported
2247
--error ER_PARSE_ERROR
2248
SELECT * FROM t1 
2249
WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
2250
2251
#TODO:not supported
2252
--error 1064
2253
explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
2254
  from t1;
2255
2256
explain select * from t1 where not exists 
2257
  ((select t11.i from t1 t11) union (select t12.i from t1 t12));
2258
2259
DROP TABLE t1;
2260
2261
#
2262
# Bug #21540: Subqueries with no from and aggregate functions return 
2263
#              wrong results
2264
CREATE TABLE t1 (a INT, b INT);
2265
CREATE TABLE t2 (a INT);
2266
INSERT INTO t2 values (1);
2267
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
2268
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2269
SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
2270
  FROM t1 GROUP BY t1.a;
2271
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
2272
SELECT (
2273
  SELECT (
2274
      SELECT (
2275
        SELECT COUNT(DISTINCT t1.b)
2276
      )
2277
  ) 
2278
  FROM t1 GROUP BY t1.a LIMIT 1) 
2279
FROM t1 t2
2280
GROUP BY t2.a;
2281
DROP TABLE t1,t2;  
2282
2283
#
2284
# Bug #21727: Correlated subquery that requires filesort:
2285
#             slow with big sort_buffer_size 
2286
#
2287
2288
CREATE TABLE t1 (a int, b int auto_increment, PRIMARY KEY (b));
2289
CREATE TABLE t2 (x int auto_increment, y int, z int,
2290
                 PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
2291
disable_query_log;
206.1.1 by Stewart Smith
cut 'make test' time in half.
2292
set autocommit=0;
2293
begin;
1 by brian
clean slate
2294
let $1=3000;
2295
while ($1)
2296
{
2297
  eval INSERT INTO t1(a) VALUES(RAND()*1000);
2298
  eval SELECT MAX(b) FROM t1 INTO @id;
2299
  let $2=10;
2300
  while ($2)
2301
  {
2302
    eval INSERT INTO t2(y,z) VALUES(@id,RAND()*1000);
2303
    dec $2;
2304
  } 
2305
  dec $1;
2306
}
206.1.1 by Stewart Smith
cut 'make test' time in half.
2307
commit;
2308
set autocommit=1;
1 by brian
clean slate
2309
enable_query_log;
2310
2311
SET SESSION sort_buffer_size = 32 * 1024;
2312
SELECT COUNT(*) 
2313
  FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2314
          FROM t1) t;
2315
2316
SET SESSION sort_buffer_size = 8 * 1024 * 1024;
2317
SELECT COUNT(*) 
2318
  FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2319
          FROM t1) t;
2320
201 by Brian Aker
Convert default engine to Innodb
2321
DROP TABLE t2,t1;
1 by brian
clean slate
2322
2323
#
2324
# Bug #25219: EXIST subquery with UNION over a mix of
2325
#             correlated and uncorrelated selects
2326
#
2327
2328
CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
2329
CREATE TABLE t2 (c int);
2330
2331
INSERT INTO t1 VALUES ('aa', 1);
2332
INSERT INTO t2 VALUES (1);
2333
2334
SELECT * FROM t1
2335
  WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2336
                UNION
2337
                SELECT c from t2 WHERE c=t1.c);
2338
2339
INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
2340
2341
SELECT * FROM t1
2342
  WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2343
                UNION
2344
                SELECT c from t2 WHERE c=t1.c);
2345
2346
INSERT INTO t2 VALUES (2);
2347
CREATE TABLE t3 (c int);
2348
INSERT INTO t3 VALUES (1);
2349
2350
SELECT * FROM t1
2351
  WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
2352
                UNION
2353
                SELECT c from t2 WHERE c=t1.c);
2354
2355
DROP TABLE t1,t2,t3;
2356
2357
#
2358
# Bug#21904 (parser problem when using IN with a double "(())")
2359
#
2360
2361
--disable_warnings
2362
DROP TABLE IF EXISTS t1;
2363
DROP TABLE IF EXISTS t2;
2364
DROP TABLE IF EXISTS t1xt2;
2365
--enable_warnings
2366
2367
CREATE TABLE t1 (
223 by Brian Aker
Cleanup int() work.
2368
  id_1 int NOT NULL,
1 by brian
clean slate
2369
  t varchar(4) DEFAULT NULL
2370
);
2371
2372
CREATE TABLE t2 (
223 by Brian Aker
Cleanup int() work.
2373
  id_2 int NOT NULL,
1 by brian
clean slate
2374
  t varchar(4) DEFAULT NULL
2375
);
2376
2377
CREATE TABLE t1xt2 (
223 by Brian Aker
Cleanup int() work.
2378
  id_1 int NOT NULL,
2379
  id_2 int NOT NULL
1 by brian
clean slate
2380
);
2381
2382
INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
2383
2384
INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
2385
2386
INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
2387
2388
# subselect returns 0 rows
2389
2390
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2391
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2392
2393
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2394
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2395
2396
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2397
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2398
2399
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2400
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2401
2402
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2403
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
2404
2405
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2406
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
2407
2408
insert INTO t1xt2 VALUES (1, 12);
2409
2410
# subselect returns 1 row
2411
2412
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2413
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2414
2415
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2416
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2417
2418
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2419
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2420
2421
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2422
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2423
2424
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2425
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2426
2427
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2428
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2429
2430
insert INTO t1xt2 VALUES (2, 12);
2431
2432
# subselect returns more than 1 row
2433
2434
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2435
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2436
2437
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2438
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2439
2440
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2441
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2442
2443
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2444
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2445
2446
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2447
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2448
2449
SELECT DISTINCT t1.id_1 FROM t1 WHERE
2450
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2451
2452
DROP TABLE t1;
2453
DROP TABLE t2;
2454
DROP TABLE t1xt2;
2455
2456
#
2457
# Bug #26728: derived table with concatanation of literals in select list
2458
#  
2459
2460
CREATE TABLE t1 (a int);
2461
INSERT INTO t1 VALUES (3), (1), (2);           
2462
2463
SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
2464
SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
2465
2466
DROP table t1;
2467
2468
#
2469
# Bug #27257: COUNT(*) aggregated in outer query
2470
#  
2471
2472
CREATE TABLE t1 (a int, b int);
2473
CREATE TABLE t2 (m int, n int);
2474
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2475
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2476
2477
SELECT COUNT(*), a,
2478
       (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
2479
  FROM t1 GROUP BY a;
2480
2481
SELECT COUNT(*), a,
2482
       (SELECT MIN(m) FROM t2 WHERE m = count(*))
2483
  FROM t1 GROUP BY a;
2484
2485
SELECT COUNT(*), a       
2486
  FROM t1 GROUP BY a
2487
    HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
2488
2489
DROP TABLE t1,t2;
2490
2491
#
2492
# Bug #27229: GROUP_CONCAT in subselect with COUNT() as an argument 
2493
#  
2494
2495
CREATE TABLE t1 (a int, b int);
2496
CREATE TABLE t2 (m int, n int);
2497
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2498
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2499
2500
SELECT COUNT(*) c, a,
2501
       (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
2502
  FROM t1 GROUP BY a;
2503
2504
SELECT COUNT(*) c, a,
2505
       (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
2506
  FROM t1 GROUP BY a;
2507
2508
DROP table t1,t2;
2509
2510
#
2511
# Bug#27321: Wrong subquery result in a grouping select
2512
#
2513
CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
2514
INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
2515
(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
2516
(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
2517
2518
SELECT a, MAX(b),
2519
  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test 
2520
  FROM t1 GROUP BY a;
2521
SELECT a x, MAX(b),
2522
  (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
2523
  FROM t1 GROUP BY a;
2524
SELECT a, AVG(b),
2525
  (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
2526
  FROM t1 WHERE t1.d=0 GROUP BY a;
2527
2528
SELECT tt.a,
2529
 (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2530
  LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 
2531
  FROM t1 as tt;
2532
2533
SELECT tt.a,
2534
 (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2535
  LIMIT 1)
2536
  FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 
2537
  FROM t1 as tt GROUP BY tt.a;
2538
2539
SELECT tt.a, MAX(
2540
 (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2541
  LIMIT 1)
2542
  FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test 
2543
  FROM t1 as tt GROUP BY tt.a;
2544
2545
DROP TABLE t1;
2546
2547
#
2548
# Bug #27363: nested aggregates in outer, subquery / sum(select
2549
# count(outer))
2550
#
2551
CREATE TABLE t1 (a INT); INSERT INTO t1 values (1),(1),(1),(1);
2552
CREATE TABLE t2 (x INT); INSERT INTO t1 values (1000),(1001),(1002);
2553
2554
--error ER_INVALID_GROUP_FUNC_USE
2555
SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
2556
--error ER_INVALID_GROUP_FUNC_USE
2557
SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
2558
2559
--error ER_INVALID_GROUP_FUNC_USE
2560
SELECT 
2561
  SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
2562
FROM t1;
2563
2564
--error ER_INVALID_GROUP_FUNC_USE
2565
SELECT t1.a as XXA, 
2566
   SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
2567
FROM t1;
2568
2569
DROP TABLE t1,t2;
2570
2571
#
2572
# Bug #27807: Server crash when executing subquery with EXPLAIN
2573
#  
2574
CREATE TABLE t1 (a int, b int, KEY (a)); 
2575
INSERT INTO t1 VALUES (1,1),(2,1);
2576
EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
2577
DROP TABLE t1;
2578
2579
#
2580
# Bug #28377: grouping query with a correlated subquery in WHERE condition
2581
#  
2582
2583
CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
2584
INSERT INTO t1 VALUES
2585
  (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
2586
CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
2587
INSERT INTO t2 VALUES (7), (5), (1), (3);
2588
2589
SELECT id, st FROM t1 
2590
  WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2591
SELECT id, st FROM t1 
2592
  WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2593
    GROUP BY id;
2594
2595
SELECT id, st FROM t1 
2596
  WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2597
SELECT id, st FROM t1 
2598
  WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2599
    GROUP BY id;
2600
2601
DROP TABLE t1,t2;
2602
2603
#
2604
# Bug #28728: crash with EXPLAIN EXTENDED for a query with a derived table
2605
#             over a grouping subselect
2606
# 
2607
2608
CREATE TABLE t1 (a int);
2609
2610
INSERT INTO t1 VALUES (1), (2);
2611
2612
EXPLAIN EXTENDED
2613
SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
2614
2615
DROP TABLE t1;
2616
2617
#
2618
# Bug #28811: crash for query containing subquery with ORDER BY and LIMIT 1 
2619
#
2620
 
2621
CREATE TABLE t1 (
2622
  a varchar(255) default NULL,
2623
  b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2624
  INDEX idx(a,b)
2625
);
2626
CREATE TABLE t2 (
2627
  a varchar(255) default NULL
2628
);
2629
2630
INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
2631
INSERT INTO t1 SELECT * FROM t1;
2632
INSERT INTO t1 SELECT * FROM t1;
2633
INSERT INTO t1 SELECT * FROM t1;
2634
INSERT INTO t1 SELECT * FROM t1;
2635
INSERT INTO t1 SELECT * FROM t1;
2636
INSERT INTO t1 SELECT * FROM t1;
2637
INSERT INTO t1 SELECT * FROM t1;
2638
INSERT INTO t1 SELECT * FROM t1;
2639
INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
2640
INSERT INTO `t2` VALUES ('abcdefghijk');
2641
INSERT INTO `t2` VALUES ('asdf');
2642
2643
SET session sort_buffer_size=8192;
2644
2645
SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
2646
2647
DROP TABLE t1,t2;
2648
2649
2650
#
2651
# Bug #27333: subquery grouped for aggregate of outer query / no aggregate
2652
# of subquery
2653
#
2654
CREATE TABLE t1 (a INTEGER, b INTEGER);
2655
CREATE TABLE t2 (x INTEGER);
2656
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
2657
INSERT INTO t2 VALUES (1), (2);
2658
2659
# wasn't failing, but should
2660
--error ER_SUBQUERY_NO_1_ROW
2661
SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
2662
2663
# fails as it should
2664
--error ER_SUBQUERY_NO_1_ROW
2665
SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
2666
2667
SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
2668
DROP TABLE t1,t2;
2669
2670
# second test case from 27333
2671
CREATE TABLE t1 (a INT, b INT);
2672
INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
2673
2674
# returns no rows, when it should
2675
SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
2676
AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
2677
GROUP BY a1.a;
2678
DROP TABLE t1;
2679
2680
#test cases from 29297
2681
CREATE TABLE t1 (a INT);
2682
CREATE TABLE t2 (a INT);
2683
INSERT INTO t1 VALUES (1),(2);
2684
INSERT INTO t2 VALUES (1),(2);
2685
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
2686
--error ER_SUBQUERY_NO_1_ROW
2687
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
2688
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
2689
DROP TABLE t1,t2;
2690
2691
#
2692
# Bug #31884: Assertion + crash in subquery in the SELECT clause.
2693
#
2694
2695
CREATE TABLE t1 (a1 INT, a2 INT);
2696
CREATE TABLE t2 (b1 INT, b2 INT);
2697
2698
INSERT INTO t1 VALUES (100, 200);
2699
INSERT INTO t1 VALUES (101, 201);
2700
INSERT INTO t2 VALUES (101, 201);
2701
INSERT INTO t2 VALUES (103, 203);
2702
2703
SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
2704
DROP TABLE t1, t2;
2705
2706
#
2707
# Bug #30788: Inconsistent retrieval of char/varchar
2708
#
2709
2710
CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
2711
INSERT INTO t1 VALUES ('a', 'aa');
2712
INSERT INTO t1 VALUES ('a', 'aaa');
2713
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
2714
CREATE INDEX I1 ON t1 (a);
2715
CREATE INDEX I2 ON t1 (b);
2716
EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
2717
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
2718
2719
CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
2720
INSERT INTO t2 SELECT * FROM t1;
2721
CREATE INDEX I1 ON t2 (a);
2722
CREATE INDEX I2 ON t2 (b);
2723
EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
2724
SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
2725
EXPLAIN
2726
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
2727
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
2728
2729
DROP TABLE t1,t2;
2730
2731
#
2732
# Bug #32400: Complex SELECT query returns correct result only on some
2733
# occasions
2734
#
2735
2736
CREATE TABLE t1(a INT, b INT);
2737
INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
2738
2739
--error ER_BAD_FIELD_ERROR
2740
EXPLAIN 
2741
SELECT a AS out_a, MIN(b) FROM t1
2742
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
2743
GROUP BY a;
2744
2745
--error ER_BAD_FIELD_ERROR
2746
SELECT a AS out_a, MIN(b) FROM t1
2747
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
2748
GROUP BY a;
2749
2750
EXPLAIN 
2751
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
2752
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
2753
GROUP BY a;
2754
2755
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
2756
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
2757
GROUP BY a;
2758
2759
DROP TABLE t1;
2760
2761
2762
#
2763
# Bug #32036: EXISTS within a WHERE clause with a UNION crashes MySQL 5.122
2764
#
2765
2766
CREATE TABLE t1 (a INT);
2767
CREATE TABLE t2 (a INT);
2768
2769
INSERT INTO t1 VALUES (1),(2);
2770
INSERT INTO t2 VALUES (1),(2);
2771
2772
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
2773
EXPLAIN EXTENDED
2774
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
2775
2776
2777
EXPLAIN EXTENDED
2778
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION 
2779
                               (SELECT 1 FROM t2 WHERE t1.a = t2.a));
2780
2781
DROP TABLE t1,t2;
2782
2783
#
2784
# BUG#33794 "MySQL crashes executing specific query on specific dump"
2785
#
2786
CREATE TABLE t4 (
2787
  f7 varchar(32) collate utf8_bin NOT NULL default '',
2788
  f10 varchar(32) collate utf8_bin default NULL,
2789
  PRIMARY KEY  (f7)
2790
);
2791
INSERT INTO t4 VALUES(1,1), (2,null);
2792
2793
CREATE TABLE t2 (
2794
  f4 varchar(32) collate utf8_bin NOT NULL default '',
2795
  f2 varchar(50) collate utf8_bin default NULL,
2796
  f3 varchar(10) collate utf8_bin default NULL,
2797
  PRIMARY KEY  (f4),
2798
  UNIQUE KEY uk1 (f2)
2799
);
2800
INSERT INTO t2 VALUES(1,1,null), (2,2,null);
2801
2802
CREATE TABLE t1 (
2803
  f8 varchar(32) collate utf8_bin NOT NULL default '',
2804
  f1 varchar(10) collate utf8_bin default NULL,
2805
  f9 varchar(32) collate utf8_bin default NULL,
2806
  PRIMARY KEY  (f8)
2807
);
2808
INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
2809
2810
CREATE TABLE t3 (
2811
  f6 varchar(32) collate utf8_bin NOT NULL default '',
2812
  f5 varchar(50) collate utf8_bin default NULL,
2813
  PRIMARY KEY (f6)
2814
);
2815
INSERT INTO t3 VALUES (1,null), (2,null);
2816
2817
SELECT
2818
  IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
2819
  IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
2820
  SUM(
2821
    IF(
2822
      (SELECT VPC.f2
2823
       FROM t2 VPC, t4 a2, t2 a3
2824
       WHERE
2825
         VPC.f4 = a2.f10 AND a3.f2 = a4
2826
       LIMIT 1) IS NULL, 
2827
       0, 
2828
       t3.f5
2829
    )
2830
  ) AS a6
2831
FROM 
2832
  t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
2833
GROUP BY a4;
2834
2835
DROP TABLE t1, t2, t3, t4;
2836
2837
--echo End of 5.0 tests.
2838
2839
#
2840
#  Test [NOT] IN truth table (both as top-level and general predicate).
2841
#
2842
2843
create table t_out (subcase char(3),
2844
                    a1 char(2), b1 char(2), c1 char(2));
2845
create table t_in  (a2 char(2), b2 char(2), c2 char(2));
2846
2847
insert into t_out values ('A.1','2a', NULL, '2a');
2848
#------------------------- A.2 - impossible
2849
insert into t_out values ('A.3', '2a', NULL, '2a');
2850
insert into t_out values ('A.4', '2a', NULL, 'xx');
2851
insert into t_out values ('B.1', '2a', '2a', '2a');
2852
insert into t_out values ('B.2', '2a', '2a', '2a');
2853
insert into t_out values ('B.3', '3a', 'xx', '3a');
2854
insert into t_out values ('B.4', 'xx', '3a', '3a');
2855
2856
insert into t_in values ('1a', '1a', '1a');
2857
insert into t_in values ('2a', '2a', '2a');
2858
insert into t_in values (NULL, '2a', '2a');
2859
insert into t_in values ('3a', NULL, '3a');
2860
-- echo 
2861
-- echo Test general IN semantics (not top-level)
2862
-- echo 
2863
-- echo case A.1
2864
select subcase,
2865
       (a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
2866
       (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
2867
from t_out where subcase = 'A.1';
2868
2869
-- echo case A.2 - impossible
2870
2871
-- echo case A.3
2872
select subcase,
2873
       (a1, b1, c1)     IN (select * from t_in) pred_in,
2874
       (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
2875
from t_out where subcase = 'A.3';
2876
2877
-- echo case A.4
2878
select subcase,
2879
       (a1, b1, c1)     IN (select * from t_in) pred_in,
2880
       (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
2881
from t_out where subcase = 'A.4';
2882
2883
-- echo case B.1
2884
select subcase,
2885
       (a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
2886
       (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
2887
from t_out where subcase = 'B.1';
2888
2889
-- echo case B.2
2890
select subcase,
2891
       (a1, b1, c1)     IN (select * from t_in) pred_in,
2892
       (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
2893
from t_out where subcase = 'B.2';
2894
2895
-- echo case B.3
2896
select subcase,
2897
       (a1, b1, c1)     IN (select * from t_in) pred_in,
2898
       (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
2899
from t_out where subcase = 'B.3';
2900
2901
-- echo case B.4
2902
select subcase,
2903
       (a1, b1, c1)     IN (select * from t_in) pred_in,
2904
       (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
2905
from t_out where subcase = 'B.4';
2906
2907
-- echo 
2908
-- echo Test IN as top-level predicate, and
2909
-- echo as non-top level for cases A.3, B.3 (the only cases with NULL result).
2910
-- echo 
2911
-- echo case A.1
2912
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2913
where subcase = 'A.1' and
2914
      (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
2915
2916
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2917
where subcase = 'A.1' and
2918
      (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
2919
2920
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2921
where subcase = 'A.1' and
2922
      NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
2923
2924
-- echo case A.3
2925
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2926
where subcase = 'A.3' and
2927
      (a1, b1, c1) IN (select * from t_in);
2928
2929
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2930
where subcase = 'A.3' and
2931
      (a1, b1, c1) NOT IN (select * from t_in);
2932
2933
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2934
where subcase = 'A.3' and
2935
      NOT((a1, b1, c1) IN (select * from t_in));
2936
# test non-top level result indirectly
2937
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
2938
where subcase = 'A.3' and
2939
      ((a1, b1, c1) IN (select * from t_in)) is NULL and
2940
      ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
2941
2942
-- echo case A.4
2943
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2944
where subcase = 'A.4' and
2945
      (a1, b1, c1) IN (select * from t_in);
2946
2947
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2948
where subcase = 'A.4' and
2949
      (a1, b1, c1) NOT IN (select * from t_in);
2950
2951
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2952
where subcase = 'A.4' and
2953
      NOT((a1, b1, c1) IN (select * from t_in));
2954
2955
-- echo case B.1
2956
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2957
where subcase = 'B.1' and
2958
      (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
2959
2960
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2961
where subcase = 'B.1' and
2962
      (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
2963
2964
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2965
where subcase = 'B.1' and
2966
      NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
2967
2968
-- echo case B.2
2969
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2970
where subcase = 'B.2' and
2971
      (a1, b1, c1) IN (select * from t_in);
2972
2973
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2974
where subcase = 'B.2' and
2975
      (a1, b1, c1) NOT IN (select * from t_in);
2976
2977
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2978
where subcase = 'B.2' and
2979
      NOT((a1, b1, c1) IN (select * from t_in));
2980
2981
-- echo case B.3
2982
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
2983
where subcase = 'B.3' and
2984
      (a1, b1, c1) IN (select * from t_in);
2985
2986
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
2987
where subcase = 'B.3' and
2988
      (a1, b1, c1) NOT IN (select * from t_in);
2989
2990
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
2991
where subcase = 'B.3' and
2992
      NOT((a1, b1, c1) IN (select * from t_in));
2993
# test non-top level result indirectly
2994
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
2995
where subcase = 'B.3' and
2996
      ((a1, b1, c1) IN (select * from t_in)) is NULL and
2997
      ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
2998
2999
-- echo case B.4
3000
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
3001
where subcase = 'B.4' and
3002
      (a1, b1, c1) IN (select * from t_in);
3003
3004
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
3005
where subcase = 'B.4' and
3006
      (a1, b1, c1) NOT IN (select * from t_in);
3007
3008
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
3009
where subcase = 'B.4' and
3010
      NOT((a1, b1, c1) IN (select * from t_in));
3011
3012
drop table t_out;
3013
drop table t_in;
3014
3015
3016
#
3017
# Bug#20835 (literal string with =any values)
3018
#
3019
CREATE TABLE t1 (s1 char(1));
3020
INSERT INTO t1 VALUES ('a');
3021
SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
3022
DROP TABLE t1;
3023
3024
#
3025
# Bug#33204: INTO is allowed in subselect, causing inconsistent results
3026
#
3027
CREATE TABLE t1( a INT );
3028
INSERT INTO t1 VALUES (1),(2);
3029
3030
CREATE TABLE t2( a INT, b INT );
3031
3032
--error ER_PARSE_ERROR
3033
SELECT * 
3034
FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
3035
--error ER_PARSE_ERROR
3036
SELECT * 
3037
FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
3038
--error ER_PARSE_ERROR
3039
SELECT * 
3040
FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
3041
3042
--error ER_PARSE_ERROR
3043
SELECT * FROM ( 
3044
  SELECT 1 a 
3045
  UNION 
3046
  SELECT a INTO @var FROM t1 WHERE a = 2 
3047
) t1a;
3048
3049
--error ER_PARSE_ERROR
3050
SELECT * FROM ( 
3051
  SELECT 1 a 
3052
  UNION 
3053
  SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 
3054
) t1a;
3055
3056
--error ER_PARSE_ERROR
3057
SELECT * FROM ( 
3058
  SELECT 1 a 
3059
  UNION 
3060
  SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 
3061
) t1a;
3062
3063
SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
3064
3065
SELECT * FROM ( 
3066
  SELECT a FROM t1 WHERE a = 2 
3067
  UNION 
3068
  SELECT a FROM t1 WHERE a = 2 
3069
) t1a;
3070
3071
SELECT * FROM ( 
3072
  SELECT 1 a 
3073
  UNION 
3074
  SELECT a FROM t1 WHERE a = 2 
3075
  UNION 
3076
  SELECT a FROM t1 WHERE a = 2 
3077
) t1a;
3078
3079
# This was not allowed previously. Possibly, it should be allowed on the future.
3080
# For now, the intent is to keep the fix as non-intrusive as possible.
3081
--error ER_PARSE_ERROR
3082
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
3083
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
3084
SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
3085
--error ER_PARSE_ERROR
3086
SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
3087
--error ER_PARSE_ERROR
3088
SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
3089
--error ER_PARSE_ERROR
3090
SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
3091
3092
--error ER_PARSE_ERROR
3093
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
3094
--error ER_PARSE_ERROR
3095
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
3096
--error ER_PARSE_ERROR
3097
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
3098
3099
--error ER_PARSE_ERROR
3100
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
3101
--error ER_PARSE_ERROR
3102
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
3103
--error ER_PARSE_ERROR
3104
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
3105
3106
SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
3107
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
3108
SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
3109
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
3110
3111
# Test of rule
3112
# table_factor:  '(' get_select_lex query_expression_body ')' opt_table_alias
3113
# UNION should not be allowed inside the parentheses, nor should
3114
# aliases after.
3115
# 
3116
SELECT * FROM t1 JOIN  (SELECT 1 UNION SELECT 1) alias ON 1;
3117
--error ER_PARSE_ERROR
3118
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
3119
--error ER_PARSE_ERROR
3120
SELECT * FROM t1 JOIN  (t1 t1a UNION SELECT 1)  ON 1;
3121
--error ER_PARSE_ERROR
3122
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
3123
--error ER_PARSE_ERROR
3124
SELECT * FROM t1 JOIN  (t1 t1a)  t1a ON 1;
3125
--error ER_PARSE_ERROR
3126
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
3127
3128
SELECT * FROM t1 JOIN  (t1 t1a)  ON 1;
3129
SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
3130
3131
SELECT * FROM (t1 t1a);
3132
SELECT * FROM ((t1 t1a));
3133
3134
SELECT * FROM t1 JOIN  (SELECT 1 t1a) alias ON 1;
3135
SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
3136
3137
SELECT * FROM t1 JOIN  (SELECT 1 a)  a ON 1;
3138
SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
3139
3140
# For the join, TABLE_LIST::select_lex == NULL
3141
# Check that we handle this.
3142
--error ER_PARSE_ERROR
3143
SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
3144
3145
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
3146
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
3147
SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
3148
3149
--error ER_PARSE_ERROR
3150
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
3151
--error ER_PARSE_ERROR
3152
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
3153
--error ER_PARSE_ERROR
3154
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
3155
3156
SELECT * FROM t1 WHERE a = ( SELECT 1 );
3157
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
3158
--error ER_PARSE_ERROR
3159
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
3160
--error ER_PARSE_ERROR
3161
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
3162
--error ER_PARSE_ERROR
3163
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
3164
3165
--error ER_PARSE_ERROR
3166
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
3167
--error ER_PARSE_ERROR
3168
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
3169
--error ER_PARSE_ERROR
3170
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
3171
3172
--error ER_PARSE_ERROR
3173
SELECT ( SELECT 1 INTO @v );
3174
--error ER_PARSE_ERROR
3175
SELECT ( SELECT 1 INTO OUTFILE 'file' );
3176
--error ER_PARSE_ERROR
3177
SELECT ( SELECT 1 INTO DUMPFILE 'file' );
3178
3179
--error ER_PARSE_ERROR
3180
SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
3181
--error ER_PARSE_ERROR
3182
SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
3183
--error ER_PARSE_ERROR
3184
SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
3185
3186
# Make sure context is popped when we leave the nested select
3187
SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
3188
SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
3189
3190
# Make sure we have feature F561 (see .yy file)
3191
SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
3192
3193
# Make sure the parser does not allow nested UNIONs anywhere
3194
3195
--error ER_PARSE_ERROR
3196
SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
3197
--error ER_PARSE_ERROR
3198
( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
3199
3200
--error ER_PARSE_ERROR
3201
SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3202
--error ER_PARSE_ERROR
3203
SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
3204
SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
3205
SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
3206
3207
--error ER_PARSE_ERROR
3208
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3209
--error ER_PARSE_ERROR
3210
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
3211
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
3212
3213
--error ER_PARSE_ERROR
3214
SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3215
--error ER_PARSE_ERROR
3216
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3217
--error ER_PARSE_ERROR
3218
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3219
--error ER_PARSE_ERROR
3220
SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
3221
3222
--error ER_PARSE_ERROR
3223
SELECT * FROM t1 WHERE a =     ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
3224
--error ER_PARSE_ERROR
3225
SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
3226
--error ER_PARSE_ERROR
3227
SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
3228
--error ER_PARSE_ERROR
3229
SELECT * FROM t1 WHERE a IN    ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
3230
3231
SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
3232
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
3233
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
3234
SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
3235
3236
--error ER_PARSE_ERROR
3237
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
3238
SELECT EXISTS(SELECT 1+1);
3239
--error ER_PARSE_ERROR
3240
SELECT EXISTS(SELECT 1+1 INTO @test);
3241
--error ER_PARSE_ERROR
3242
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
3243
3244
--error ER_PARSE_ERROR
3245
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
3246
--error ER_PARSE_ERROR
3247
SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
3248
3249
DROP TABLE t1, t2;