mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / t / subselect.test
blobaec0db59843fb9e136a6be4e790e5d25a55979cb
1 # Initialise
2 --disable_warnings
3 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
4 --enable_warnings
6 select (select 2);
7 explain extended select (select 2);
8 SELECT (SELECT 1) UNION SELECT (SELECT 2);
9 explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
10 SELECT (SELECT (SELECT 0 UNION SELECT 0));
11 explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
12 -- error ER_ILLEGAL_REFERENCE
13 SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
14 -- error ER_ILLEGAL_REFERENCE
15 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;
16 SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
17 -- error ER_ILLEGAL_REFERENCE
18 SELECT (SELECT a) as a;
19 EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
20 SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
21 -- error ER_BAD_FIELD_ERROR
22 SELECT (SELECT 1), a;
23 SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
24 -- error ER_BAD_FIELD_ERROR
25 SELECT 1 FROM (SELECT (SELECT a) b) c;
26 SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
27 -- error ER_OPERAND_COLUMNS
28 SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
29 SELECT 1 IN (SELECT 1);
30 SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
31 -- error ER_WRONG_USAGE
32 select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
33 -- error ER_WRONG_PARAMETERS_TO_PROCEDURE
34 SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
35 -- error ER_BAD_FIELD_ERROR
36 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
37 -- error ER_BAD_FIELD_ERROR
38 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
39 SELECT (SELECT 1,2,3) = ROW(1,2,3);
40 SELECT (SELECT 1,2,3) = ROW(1,2,1);
41 SELECT (SELECT 1,2,3) < ROW(1,2,1);
42 SELECT (SELECT 1,2,3) > ROW(1,2,1);
43 SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
44 SELECT ROW(1,2,3) = (SELECT 1,2,3);
45 SELECT ROW(1,2,3) = (SELECT 1,2,1);
46 SELECT ROW(1,2,3) < (SELECT 1,2,1);
47 SELECT ROW(1,2,3) > (SELECT 1,2,1);
48 SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
49 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
50 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
51 SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
52 SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
53 SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
54 SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
56 -- error ER_OPERAND_COLUMNS
57 SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
59 SELECT 1 as a,(SELECT a+a) b,(SELECT b);
61 create table t1 (a int);
62 create table t2 (a int, b int);
63 create table t3 (a int);
64 create table t4 (a int not null, b int not null);
65 insert into t1 values (2);
66 insert into t2 values (1,7),(2,7);
67 insert into t4 values (4,8),(3,8),(5,9);
68 -- error ER_ILLEGAL_REFERENCE
69 select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
70 select (select a from t1 where t1.a=t2.a), a from t2;
71 select (select a from t1 where t1.a=t2.b), a from t2;
72 select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
73 select (select a from t3), a from t2;
74 select * from t2 where t2.a=(select a from t1);
75 insert into t3 values (6),(7),(3);
76 select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
77 (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;
78 (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);
79 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);
80 select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
81 select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
82 (select * from t2 where a>1) as tt;
83 explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
84 (select * from t2 where a>1) as tt;
85 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);
86 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);
87 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);
88 select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
89 explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
90 select * from t3 where exists (select * from t2 where t2.b=t3.a);
91 select * from t3 where not exists (select * from t2 where t2.b=t3.a);
92 select * from t3 where a in (select b from t2);
93 select * from t3 where a not in (select b from t2);
94 select * from t3 where a = some (select b from t2);
95 select * from t3 where a <> any (select b from t2);
97 # Rewrite: select * from t3 where not exists (select b from t2 where a <> b);
98 select * from t3 where a = all (select b from t2);
100 select * from t3 where a <> all (select b from t2);
101 insert into t2 values (100, 5);
102 select * from t3 where a < any (select b from t2);
103 select * from t3 where a < all (select b from t2);
104 select * from t3 where a >= any (select b from t2);
105 explain extended select * from t3 where a >= any (select b from t2);
106 select * from t3 where a >= all (select b from t2);
107 delete from t2 where a=100;
108 -- error ER_OPERAND_COLUMNS
109 select * from t3 where a in (select a,b from t2);
110 -- error ER_OPERAND_COLUMNS
111 select * from t3 where a in (select * from t2);
112 insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
113 # empty set
114 select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
115 insert into t2 values (2,10);
116 select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
117 delete from t2 where a=2 and b=10;
118 select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
119 create table t5 (a int);
120 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
121 insert into t5 values (5);
122 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
123 insert into t5 values (2);
124 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
125 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;
126 -- error ER_SUBQUERY_NO_1_ROW
127 select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
128 create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
129 create table t7( uq int primary key, name char(25));
130 insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
131 insert into t6 values (1,1),(1,2),(2,2),(1,3);
132 select * from t6 where exists (select * from t7 where uq = clinic_uq);
133 explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
135 # not unique fields
136 -- error ER_NON_UNIQ_ERROR
137 select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
139 # different tipes & group functions
140 drop table t1,t2,t3;
142 CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
143 INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
144 CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
145 INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
146 CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
147 INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
148 SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
149 SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
150 SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
152 CREATE TABLE `t8` (
153   `pseudo` varchar(35) character set latin1 NOT NULL default '',
154   `email` varchar(60) character set latin1 NOT NULL default '',
155   PRIMARY KEY  (`pseudo`),
156   UNIQUE KEY `email` (`email`)
157 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
159 INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
160 INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
161 INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
162 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');
163 -- error ER_OPERAND_COLUMNS
164 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
165 t8 WHERE pseudo='joce');
166 -- error ER_OPERAND_COLUMNS
167 SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
168 pseudo='joce');
169 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
170 -- error ER_SUBQUERY_NO_1_ROW
171 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
173 drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
175 #searchconthardwarefr3 forumconthardwarefr7
176 CREATE TABLE `t1` (
177   `topic` mediumint(8) unsigned NOT NULL default '0',
178   `date` date NOT NULL default '0000-00-00',
179   `pseudo` varchar(35) character set latin1 NOT NULL default '',
180   PRIMARY KEY  (`pseudo`,`date`,`topic`),
181   KEY `topic` (`topic`)
182 ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
183 INSERT INTO t1 (topic,date,pseudo) VALUES
184 ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
185 EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
186 EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
187 SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
188 SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
189 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
190 -- error ER_SUBQUERY_NO_1_ROW
191 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
192 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
193 drop table t1;
195 #forumconthardwarefr7 searchconthardwarefr7
196 CREATE TABLE `t1` (
197   `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
198   `maxnumrep` int(10) unsigned NOT NULL default '0',
199   PRIMARY KEY  (`numeropost`),
200   UNIQUE KEY `maxnumrep` (`maxnumrep`)
201 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
203 INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
205 CREATE TABLE `t2` (
206       `mot` varchar(30) NOT NULL default '',
207       `topic` mediumint(8) unsigned NOT NULL default '0',
208       `date` date NOT NULL default '0000-00-00',
209       `pseudo` varchar(35) NOT NULL default '',
210        PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
211     ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
213 INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
214 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
215 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;
216 -- error ER_BAD_FIELD_ERROR
217 SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
218 -- error ER_BAD_FIELD_ERROR
219 SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
221 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
222 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
223 SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
224 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
225 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
226 SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
227 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
228 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
229 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
230 SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
231 SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
232 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
233 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
234 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
235 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
236 drop table t1,t2;
238 #forumconthardwarefr7
239 CREATE TABLE `t1` (
240   `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
241   `maxnumrep` int(10) unsigned NOT NULL default '0',
242   PRIMARY KEY  (`numeropost`),
243   UNIQUE KEY `maxnumrep` (`maxnumrep`)
244 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
246 INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
247 -- error ER_SUBQUERY_NO_1_ROW
248 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
249 -- error ER_SUBQUERY_NO_1_ROW
250 select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
251 drop table t1;
253 create table t1 (a int);
254 insert into t1 values (1),(2),(3);
255 (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
256 drop table t1;
258 #iftest
259 CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
260 INSERT INTO t1 VALUES ();
261 -- error ER_SUBQUERY_NO_1_ROW
262 SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
263 drop table t1;
265 # threadhardwarefr7
266 CREATE TABLE `t1` (
267   `numeropost` mediumint(8) unsigned NOT NULL default '0',
268   `numreponse` int(10) unsigned NOT NULL auto_increment,
269   `pseudo` varchar(35) NOT NULL default '',
270   PRIMARY KEY  (`numeropost`,`numreponse`),
271   UNIQUE KEY `numreponse` (`numreponse`),
272   KEY `pseudo` (`pseudo`,`numeropost`)
273 ) ENGINE=MyISAM;
274 -- error ER_ILLEGAL_REFERENCE
275 SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
276 -- error ER_BAD_FIELD_ERROR
277 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
278 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
279 INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
280 -- error ER_SUBQUERY_NO_1_ROW
281 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
282 EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
283 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
284 drop table t1;
286 CREATE TABLE t1 (a int(1));
287 INSERT INTO t1 VALUES (1);
288 SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
289 drop table t1;
291 #update with subselects
292 create table t1 (a int NOT NULL, b int, primary key (a));
293 create table t2 (a int NOT NULL, b int, primary key (a));
294 insert into t1 values (0, 10),(1, 11),(2, 12);
295 insert into t2 values (1, 21),(2, 22),(3, 23);
296 select * from t1;
297 -- error ER_UPDATE_TABLE_USED
298 update t1 set b= (select b from t1);
299 -- error ER_SUBQUERY_NO_1_ROW
300 update t1 set b= (select b from t2);
301 update t1 set b= (select b from t2 where t1.a = t2.a);
302 select * from t1;
303 drop table t1, t2;
305 #delete with subselects
306 create table t1 (a int NOT NULL, b int, primary key (a));
307 create table t2 (a int NOT NULL, b int, primary key (a));
308 insert into t1 values (0, 10),(1, 11),(2, 12);
309 insert into t2 values (1, 21),(2, 12),(3, 23);
310 select * from t1;
311 select * from t1 where b = (select b from t2 where t1.a = t2.a);
312 -- error ER_UPDATE_TABLE_USED
313 delete from t1 where b = (select b from t1);
314 -- error ER_SUBQUERY_NO_1_ROW
315 delete from t1 where b = (select b from t2);
316 delete from t1 where b = (select b from t2 where t1.a = t2.a);
317 select * from t1;
318 drop table t1, t2;
320 #multi-delete with subselects
322 create table t11 (a int NOT NULL, b int, primary key (a));
323 create table t12 (a int NOT NULL, b int, primary key (a));
324 create table t2 (a int NOT NULL, b int, primary key (a));
325 insert into t11 values (0, 10),(1, 11),(2, 12);
326 insert into t12 values (33, 10),(22, 11),(2, 12);
327 insert into t2 values (1, 21),(2, 12),(3, 23);
328 select * from t11;
329 select * from t12;
330 -- error ER_UPDATE_TABLE_USED
331 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
332 -- error ER_SUBQUERY_NO_1_ROW
333 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
334 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
335 select * from t11;
336 select * from t12;
337 drop table t11, t12, t2;
339 #insert with subselects
340 CREATE TABLE t1 (x int);
341 create table t2 (a int);
342 create table t3 (b int);
343 insert into t2 values (1);
344 insert into t3 values (1),(2);
345 -- error ER_UPDATE_TABLE_USED
346 INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
347 -- error ER_SUBQUERY_NO_1_ROW
348 INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
349 INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
350 select * from t1;
351 insert into t2 values (1);
352 let $row_count_before= `SELECT COUNT(*) FROM t1`;
353 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
354 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
355 --source include/wait_condition.inc
356 select * from t1;
357 INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
358 select * from t1;
359 # After this, only data based on old t1 records should have been added.
360 INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
361 select * from t1;
362 -- error ER_BAD_FIELD_ERROR
363 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
364 let $row_count_before= `SELECT COUNT(*) FROM t1`;
365 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
366 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
367 --source include/wait_condition.inc
368 select * from t1;
370 #TODO: should be uncommented after Bug#380 fix pushed
371 #INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
372 #select * from t1;
373 drop table t1, t2, t3;
375 #replace with subselects
376 CREATE TABLE t1 (x int not null, y int, primary key (x));
377 create table t2 (a int);
378 create table t3 (a int);
379 insert into t2 values (1);
380 insert into t3 values (1),(2);
381 select * from t1;
382 -- error ER_UPDATE_TABLE_USED
383 replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
384 -- error ER_SUBQUERY_NO_1_ROW
385 replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
386 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
387 select * from t1;
388 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
389 select * from t1;
390 let $row_count_before= `SELECT COUNT(*) FROM t1`;
391 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
392 # We get one additional row
393 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
394 --source include/wait_condition.inc
395 select * from t1;
396 let $row_count_before= `SELECT COUNT(*) FROM t1 WHERE y = 2`;
397 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
398 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1 WHERE y = 2;
399 --source include/wait_condition.inc
400 select * from t1;
401 replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
402 select * from t1;
403 drop table t1, t2, t3;
405 -- error ER_NO_TABLES_USED
406 SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
408 CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
409 INSERT INTO t2 VALUES (1),(2);
410 SELECT * FROM t2 WHERE id IN (SELECT 1);
411 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
412 SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
413 SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
414 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
415 EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
416 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
417 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
418 -- error ER_UPDATE_TABLE_USED
419 INSERT INTO t2 VALUES ((SELECT * FROM t2));
420 -- error ER_UPDATE_TABLE_USED
421 INSERT INTO t2 VALUES ((SELECT id FROM t2));
422 SELECT * FROM t2;
423 CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
424 INSERT INTO t1 values (1),(1);
425 -- error ER_SUBQUERY_NO_1_ROW
426 UPDATE t2 SET id=(SELECT * FROM t1);
427 drop table t2, t1;
429 #NULL test
430 create table t1 (a int);
431 insert into t1 values (1),(2),(3);
432 select 1 IN (SELECT * from t1);
433 select 10 IN (SELECT * from t1);
434 select NULL IN (SELECT * from t1);
435 update t1 set a=NULL where a=2;
436 select 1 IN (SELECT * from t1);
437 select 3 IN (SELECT * from t1);
438 select 10 IN (SELECT * from t1);
439 select 1 > ALL (SELECT * from t1);
440 select 10 > ALL (SELECT * from t1);
441 select 1 > ANY (SELECT * from t1);
442 select 10 > ANY (SELECT * from t1);
443 drop table t1;
444 create table t1 (a varchar(20));
445 insert into t1 values ('A'),('BC'),('DEF');
446 select 'A' IN (SELECT * from t1);
447 select 'XYZS' IN (SELECT * from t1);
448 select NULL IN (SELECT * from t1);
449 update t1 set a=NULL where a='BC';
450 select 'A' IN (SELECT * from t1);
451 select 'DEF' IN (SELECT * from t1);
452 select 'XYZS' IN (SELECT * from t1);
453 select 'A' > ALL (SELECT * from t1);
454 select 'XYZS' > ALL (SELECT * from t1);
455 select 'A' > ANY (SELECT * from t1);
456 select 'XYZS' > ANY (SELECT * from t1);
457 drop table t1;
458 create table t1 (a float);
459 insert into t1 values (1.5),(2.5),(3.5);
460 select 1.5 IN (SELECT * from t1);
461 select 10.5 IN (SELECT * from t1);
462 select NULL IN (SELECT * from t1);
463 update t1 set a=NULL where a=2.5;
464 select 1.5 IN (SELECT * from t1);
465 select 3.5 IN (SELECT * from t1);
466 select 10.5 IN (SELECT * from t1);
467 select 1.5 > ALL (SELECT * from t1);
468 select 10.5 > ALL (SELECT * from t1);
469 select 1.5 > ANY (SELECT * from t1);
470 select 10.5 > ANY (SELECT * from t1);
471 explain extended select (select a+1) from t1;
472 select (select a+1) from t1;
473 drop table t1;
476 # Null with keys
479 CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY  (a));
480 CREATE TABLE t2 (a int(11) default '0', INDEX (a));
481 INSERT INTO t1 VALUES (1),(2),(3),(4);
482 INSERT INTO t2 VALUES (1),(2),(3);
483 SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
484 explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
485 CREATE TABLE t3 (a int(11) default '0');
486 INSERT INTO t3 VALUES (1),(2),(3);
487 SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
488 explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
489 drop table t1,t2,t3;
491 #LIMIT is not supported now
492 create table t1 (a float);
493 -- error ER_NOT_SUPPORTED_YET
494 select 10.5 IN (SELECT * from t1 LIMIT 1);
495 -- error ER_NOT_SUPPORTED_YET
496 select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
497 drop table t1;
499 create table t1 (a int, b int, c varchar(10));
500 create table t2 (a int);
501 insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
502 insert into t2 values (1),(2),(NULL);
503 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;
504 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;
505 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;
506 drop table t1,t2;
508 create table t1 (a int, b real, c varchar(10));
509 insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
510 select ROW(1, 1, 'a') IN (select a,b,c from t1);
511 select ROW(1, 2, 'a') IN (select a,b,c from t1);
512 select ROW(1, 1, 'a') IN (select b,a,c from t1);
513 select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
514 select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
515 select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
516 select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
517 select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
518 select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
519 -- error ER_NOT_SUPPORTED_YET
520 select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
521 drop table t1;
524 # DO & SET
526 create table t1 (a int);
527 insert into t1 values (1);
528 do @a:=(SELECT a from t1);
529 select @a;
530 set @a:=2;
531 set @a:=(SELECT a from t1);
532 select @a;
533 drop table t1;
534 -- error ER_NO_SUCH_TABLE
535 do (SELECT a from t1);
536 -- error ER_NO_SUCH_TABLE
537 set @a:=(SELECT a from t1);
539 CREATE TABLE t1 (a int, KEY(a));
540 HANDLER t1 OPEN;
541 -- error ER_PARSE_ERROR
542 HANDLER t1 READ a=((SELECT 1));
543 HANDLER t1 CLOSE;
544 drop table t1;
546 create table t1 (a int);
547 create table t2 (b int);
548 insert into t1 values (1),(2);
549 insert into t2 values (1);
550 select a from t1 where a in (select a from t1 where a in (select b from t2));
551 drop table t1, t2;
553 create table t1 (a int, b int);
554 create table t2 like t1;
555 insert into t1 values (1,2),(1,3),(1,4),(1,5);
556 insert into t2 values (1,2),(1,3);
557 select * from t1 where row(a,b) in (select a,b from t2);
558 drop table t1, t2;
560 CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;
561 INSERT INTO t1 VALUES (1);
562 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
563 select * from t1;
564 drop table t1;
566 #test of uncacheable subqueries
567 CREATE TABLE t1 (a int(1));
568 EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
569 EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
570 EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
571 drop table t1;
574 CREATE TABLE `t1` (
575   `mot` varchar(30) character set latin1 NOT NULL default '',
576   `topic` mediumint(8) unsigned NOT NULL default '0',
577   `date` date NOT NULL default '0000-00-00',
578   `pseudo` varchar(35) character set latin1 NOT NULL default '',
579   PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
580   KEY `pseudo` (`pseudo`,`date`,`topic`),
581   KEY `topic` (`topic`)
582 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
584 CREATE TABLE `t2` (
585   `mot` varchar(30) character set latin1 NOT NULL default '',
586   `topic` mediumint(8) unsigned NOT NULL default '0',
587   `date` date NOT NULL default '0000-00-00',
588   `pseudo` varchar(35) character set latin1 NOT NULL default '',
589   PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
590   KEY `pseudo` (`pseudo`,`date`,`topic`),
591   KEY `topic` (`topic`)
592 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
594 CREATE TABLE `t3` (
595   `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
596   `maxnumrep` int(10) unsigned NOT NULL default '0',
597   PRIMARY KEY  (`numeropost`),
598   UNIQUE KEY `maxnumrep` (`maxnumrep`)
599 ) ENGINE=MyISAM CHARSET=latin1;
600 INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
602 INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
604 INSERT INTO t3 VALUES (1,1);
606 SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
607 numeropost=topic);
608 select * from t1;
609 DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
610 EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
611 select * from t1;
613 drop table t1, t2, t3;
615 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
616 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
617 SHOW CREATE TABLE t1;
618 drop table t1;
619 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
620 SHOW CREATE TABLE t1;
621 drop table t1;
622 CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
623 SHOW CREATE TABLE t1;
624 drop table t1;
625 CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
626 select * from t1;
627 SHOW CREATE TABLE t1;
628 drop table t1;
630 create table t1 (a int);
631 insert into t1 values (1), (2), (3);
632 explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
633 from t1;
634 drop table t1;
637 # error in IN
639 -- error ER_NO_SUCH_TABLE
640 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);
643 # complex subquery
646 CREATE TABLE t1 (
647   ID int(11) NOT NULL auto_increment,
648   name char(35) NOT NULL default '',
649   t2 char(3) NOT NULL default '',
650   District char(20) NOT NULL default '',
651   Population int(11) NOT NULL default '0',
652   PRIMARY KEY  (ID)
653 ) ENGINE=MyISAM;
655 INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
656 INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
657 INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
659 CREATE TABLE t2 (
660   Code char(3) NOT NULL default '',
661   Name char(52) NOT NULL default '',
662   Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
663   Region char(26) NOT NULL default '',
664   SurfaceArea float(10,2) NOT NULL default '0.00',
665   IndepYear smallint(6) default NULL,
666   Population int(11) NOT NULL default '0',
667   LifeExpectancy float(3,1) default NULL,
668   GNP float(10,2) default NULL,
669   GNPOld float(10,2) default NULL,
670   LocalName char(45) NOT NULL default '',
671   GovernmentForm char(45) NOT NULL default '',
672   HeadOfState char(60) default NULL,
673   Capital int(11) default NULL,
674   Code2 char(2) NOT NULL default '',
675   PRIMARY KEY  (Code)
676 ) ENGINE=MyISAM;
678 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');
679 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');
681 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);
683 drop table t1, t2;
686 # constants in IN
688 CREATE TABLE `t1` (
689   `id` mediumint(8) unsigned NOT NULL auto_increment,
690   `pseudo` varchar(35) character set latin1 NOT NULL default '',
691   PRIMARY KEY  (`id`),
692   UNIQUE KEY `pseudo` (`pseudo`)
693 ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
694 INSERT INTO t1 (pseudo) VALUES ('test');
695 SELECT 0 IN (SELECT 1 FROM t1 a);
696 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
697 INSERT INTO t1 (pseudo) VALUES ('test1');
698 SELECT 0 IN (SELECT 1 FROM t1 a);
699 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
700 drop table t1;
702 CREATE TABLE `t1` (
703   `i` int(11) NOT NULL default '0',
704   PRIMARY KEY  (`i`)
705 ) ENGINE=MyISAM CHARSET=latin1;
707 INSERT INTO t1 VALUES (1);
708 UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
709 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
710 -- error ER_BAD_FIELD_ERROR
711 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
712 select * from t1;
713 drop table t1;
716 # Multi update test
718 CREATE TABLE t1 (
719   id int(11) default NULL
720 ) ENGINE=MyISAM CHARSET=latin1;
721 INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
722 CREATE TABLE t2 (
723   id int(11) default NULL,
724   name varchar(15) default NULL
725 ) ENGINE=MyISAM CHARSET=latin1;
727 INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
728 update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
729 select * from t2;
730 drop table t1,t2;
733 # correct NULL in <CONSTANT> IN (SELECT ...)
735 create table t1 (a int, unique index indexa (a));
736 insert into t1 values (-1), (-4), (-2), (NULL);
737 select -10 IN (select a from t1 FORCE INDEX (indexa));
738 drop table t1;
741 # Test optimization for sub selects
743 create table t1 (id int not null auto_increment primary key, salary int, key(salary));
744 insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
745 explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
746 drop table t1;
748 CREATE TABLE t1 (
749   ID int(10) unsigned NOT NULL auto_increment,
750   SUB_ID int(3) unsigned NOT NULL default '0',
751   REF_ID int(10) unsigned default NULL,
752   REF_SUB int(3) unsigned default '0',
753   PRIMARY KEY (ID,SUB_ID),
754   UNIQUE KEY t1_PK (ID,SUB_ID),
755   KEY t1_FK (REF_ID,REF_SUB),
756   KEY t1_REFID (REF_ID)
757 ) ENGINE=MyISAM CHARSET=cp1251;
758 INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
759 SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
760 DROP TABLE t1;
763 # uninterruptable update
765 create table t1 (a int, b int);
766 create table t2 (a int, b int);
768 insert into t1 values (1,0), (2,0), (3,0);
769 insert into t2 values (1,1), (2,1), (3,1), (2,2);
771 update ignore t1 set b=(select b from t2 where t1.a=t2.a);
772 select * from t1;
774 drop table t1, t2;
777 # reduced subselect in ORDER BY & GROUP BY clauses
780 CREATE TABLE `t1` (
781   `id` mediumint(8) unsigned NOT NULL auto_increment,
782   `pseudo` varchar(35) NOT NULL default '',
783   `email` varchar(60) NOT NULL default '',
784   PRIMARY KEY  (`id`),
785   UNIQUE KEY `email` (`email`),
786   UNIQUE KEY `pseudo` (`pseudo`)
787 ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
788 INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
789 SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
790 drop table if exists t1;
792 (SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
795 # IN subselect optimization test
797 create table t1 (a int not null, b int, primary key (a));
798 create table t2 (a int not null, primary key (a));
799 create table t3 (a int not null, b int, primary key (a));
800 insert into t1 values (1,10), (2,20), (3,30),  (4,40);
801 insert into t2 values (2), (3), (4), (5);
802 insert into t3 values (10,3), (20,4), (30,5);
803 select * from t2 where t2.a in (select a from t1);
804 explain extended select * from t2 where t2.a in (select a from t1);
805 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
806 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
807 select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
808 explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
809 drop table t1, t2, t3;
810 create table t1 (a int, b int, index a (a,b));
811 create table t2 (a int, index a (a));
812 create table t3 (a int, b int, index a (a));
813 insert into t1 values (1,10), (2,20), (3,30), (4,40);
814 disable_query_log;
815 # making table large enough
816 let $1 = 10000;
817 while ($1)
819   eval insert into t1 values (rand()*100000+200,rand()*100000);
820   dec $1;
822 enable_query_log;
823 insert into t2 values (2), (3), (4), (5);
824 insert into t3 values (10,3), (20,4), (30,5);
825 select * from t2 where t2.a in (select a from t1);
826 explain extended select * from t2 where t2.a in (select a from t1);
827 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
828 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
829 select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
830 explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
831 insert into t1 values (3,31);
832 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
833 select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
834 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
835 drop table t1, t2, t3;
838 # alloc_group_fields() working
840 create table t1 (a int, b int);
841 create table t2 (a int, b int);
842 create table t3 (a int, b int);
843 insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
844 insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
845 insert into t3 values (3,3), (2,2), (1,1);
846 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;
847 drop table t1,t2,t3;
850 # aggregate functions in HAVING test
852 create table t1 (s1 int);
853 create table t2 (s1 int);
854 insert into t1 values (1);
855 insert into t2 values (1);
856 select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
857 drop table t1,t2;
860 # update subquery with wrong field (to force name resolving
861 # in UPDATE name space)
863 create table t1 (s1 int);
864 create table t2 (s1 int);
865 insert into t1 values (1);
866 insert into t2 values (1);
867 -- error ER_BAD_FIELD_ERROR
868 update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
869 DROP TABLE t1, t2;
872 # collation test
874 CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
875                  s2 CHAR(5) COLLATE latin1_swedish_ci);
876 INSERT INTO t1 VALUES ('z','?');
877 -- error ER_CANT_AGGREGATE_2COLLATIONS
878 select * from t1 where s1 > (select max(s2) from t1);
879 -- error ER_CANT_AGGREGATE_2COLLATIONS
880 select * from t1 where s1 > any (select max(s2) from t1);
881 drop table t1;
884 # aggregate functions reinitialization
886 create table t1(toid int,rd int);
887 create table t2(userid int,pmnew int,pmtotal int);
888 insert into t2 values(1,0,0),(2,0,0);
889 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);
890 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);
891 drop table t1, t2;
894 # row union
896 create table t1 (s1 char(5));
897 -- error ER_OPERAND_COLUMNS
898 select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
899 insert into t1 values ('tttt');
900 select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
901 explain extended (select * from t1);
902 (select * from t1);
903 drop table t1;
906 # IN optimisation test results
908 create table t1 (s1 char(5), index s1(s1));
909 create table t2 (s1 char(5), index s1(s1));
910 insert into t1 values ('a1'),('a2'),('a3');
911 insert into t2 values ('a1'),('a2');
912 select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
913 select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
914 select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
915 select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
916 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
917 explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
918 explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
919 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
920 drop table t1,t2;
923 # correct ALL optimisation
925 create table t2 (a int, b int);
926 create table t3 (a int);
927 insert into t3 values (6),(7),(3);
928 select * from t3 where a >= all (select b from t2);
929 explain extended select * from t3 where a >= all (select b from t2);
930 select * from t3 where a >= some (select b from t2);
931 explain extended select * from t3 where a >= some (select b from t2);
932 select * from t3 where a >= all (select b from t2 group by 1);
933 explain extended select * from t3 where a >= all (select b from t2 group by 1);
934 select * from t3 where a >= some (select b from t2 group by 1);
935 explain extended select * from t3 where a >= some (select b from t2 group by 1);
936 select * from t3 where NULL >= any (select b from t2);
937 explain extended select * from t3 where NULL >= any (select b from t2);
938 select * from t3 where NULL >= any (select b from t2 group by 1);
939 explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
940 select * from t3 where NULL >= some (select b from t2);
941 explain extended select * from t3 where NULL >= some (select b from t2);
942 select * from t3 where NULL >= some (select b from t2 group by 1);
943 explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
945 # optimized static ALL/ANY with grouping
947 insert into t2 values (2,2), (2,1), (3,3), (3,1);
948 select * from t3 where a > all (select max(b) from t2 group by a);
949 explain extended select * from t3 where a > all (select max(b) from t2 group by a);
950 drop table t2, t3;
953 # correct used_tables()
956 CREATE TABLE `t1` ( `id` mediumint(9) NOT NULL auto_increment, `taskid` bigint(20) NOT NULL default '0', `dbid` int(11) 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 ;
957 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());
958 CREATE TABLE `t2` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY  (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;
959 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);
960 CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY  (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;
961 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);
962 CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
963 INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
964 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;
965 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;
966 drop table t1,t2,t3,t4;
969 # cardinality check
971 CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
972 INSERT INTO t1 VALUES (1),(5);
973 CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
974 INSERT INTO t2 VALUES (2),(6);
975 -- error ER_OPERAND_COLUMNS
976 select * from t1 where (1,2,6) in (select * from t2);
977 DROP TABLE t1,t2;
980 # DO and SET with errors
982 create table t1 (s1 int);
983 insert into t1 values (1);
984 insert into t1 values (2);
985 -- error ER_SUBQUERY_NO_1_ROW
986 set sort_buffer_size = (select s1 from t1);
987 do (select * from t1);
988 drop table t1;
991 # optimized ALL/ANY with union
993 create table t1 (s1 char);
994 insert into t1 values ('e');
995 select * from t1 where 'f' > any (select s1 from t1);
996 select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
997 explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
998 drop table t1;
1001 # filesort in subquery (restoring join_tab)
1003 CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1004 INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
1005 CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
1006 INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
1007 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;
1008 drop table t1, t2;
1011 # unresolved field error
1013 create table t1 (s1 int);
1014 create table t2 (s1 int);
1015 -- error ER_BAD_FIELD_ERROR
1016 select * from t1 where (select count(*) from t2 where t1.s2) = 1;
1017 -- error ER_BAD_FIELD_ERROR
1018 select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
1019 -- error ER_BAD_FIELD_ERROR
1020 select count(*) from t2 group by t1.s2;
1021 drop table t1, t2;
1024 # fix_fields() in add_ref_to_table_cond()
1026 CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
1027 CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1028 INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1029 INSERT INTO t2 VALUES (100, 200, 'C');
1030 SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1031 DROP TABLE t1, t2;
1033 CREATE TABLE t1 (a int(1));
1034 INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1035 SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1036 DROP TABLE t1;
1040 # Bug#2198 SELECT INTO OUTFILE (with Sub-Select) Problem
1043 create table t1 (a int, b decimal(13, 3));
1044 insert into t1 values (1, 0.123);
1045 let $outfile_abs= $MYSQLTEST_VARDIR/tmp/subselect.out.file.1;
1046 let $outfile_rel= ../../tmp/subselect.out.file.1;
1047 --error 0,1
1048 --remove_file $outfile_abs
1049 eval select a, (select max(b) from t1) into outfile "$outfile_rel" from t1;
1050 delete from t1;
1051 eval load data infile "$outfile_rel" into table t1;
1052 --remove_file $outfile_abs
1053 select * from t1;
1054 drop table t1;
1058 # Bug#2479 dependant subquery with limit crash
1061 CREATE TABLE `t1` (
1062   `id` int(11) NOT NULL auto_increment,
1063   `id_cns` tinyint(3) unsigned NOT NULL default '0',
1064   `tipo` enum('','UNO','DUE') NOT NULL default '',
1065   `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1066   `particolare` mediumint(8) unsigned NOT NULL default '0',
1067   `generale` mediumint(8) unsigned NOT NULL default '0',
1068   `bis` tinyint(3) unsigned NOT NULL default '0',
1069   PRIMARY KEY  (`id`),
1070   UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1071   UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1073 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);
1074 CREATE TABLE `t2` (
1075   `id` tinyint(3) unsigned NOT NULL auto_increment,
1076   `max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1077   PRIMARY KEY  (`id`)
1079 INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1081 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;
1083 DROP TABLE t1, t2;
1086 # GLOBAL LIMIT
1088 create table t1 (a int);
1089 insert into t1 values (1), (2), (3);
1090 SET SQL_SELECT_LIMIT=1;
1091 select sum(a) from (select * from t1) as a;
1092 select 2 in (select * from t1);
1093 SET SQL_SELECT_LIMIT=default;
1094 drop table t1;
1098 # Bug#3118 subselect + order by
1101 CREATE TABLE t1 (a int, b int, INDEX (a));
1102 INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1103 SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1104 DROP TABLE t1;
1106 # Item_cond fix field
1108 create table t1(val varchar(10));
1109 insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1110 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%');
1111 drop table t1;
1114 # ref_or_null replacing with ref
1116 create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1117 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');
1118 select * from t1 where id not in (select id from t1 where id < 8);
1119 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);
1120 explain extended select * from t1 where id not in (select id from t1 where id < 8);
1121 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);
1122 insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1123 create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1124 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');
1125 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);
1126 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);
1127 drop table t1,t2;
1130 # Static tables & rund() in subqueries
1132 create table t1 (a int);
1133 insert into t1 values (1);
1134 explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1135 drop table t1;
1139 # Bug#3188 Ambiguous Column in Subselect crashes server
1141 create table t1(id int);
1142 create table t2(id int);
1143 create table t3(flag int);
1144 -- error ER_PARSE_ERROR
1145 select (select * from t3 where id not null) from t1, t2;
1146 drop table t1,t2,t3;
1150 # aggregate functions (Bug#3505 Wrong results on use of ORDER BY with subqueries)
1152 CREATE TABLE t1 (id INT);
1153 CREATE TABLE t2 (id INT);
1154 INSERT INTO t1 VALUES (1), (2);
1155 INSERT INTO t2 VALUES (1);
1156 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);
1157 SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1158 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;
1159 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;
1160 DROP TABLE t1,t2;
1163 # ALL/ANY test
1165 CREATE TABLE t1 ( a int, b int );
1166 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1167 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1168 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1169 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1170 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1171 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1172 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1173 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1174 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1175 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1176 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1177 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1178 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1179 # with index
1180 ALTER TABLE t1 ADD INDEX (a);
1181 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1182 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1183 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1184 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1185 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1186 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1187 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1188 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1189 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1190 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1191 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1192 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1193 # having clause test
1194 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
1195 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
1196 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
1197 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
1198 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
1199 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
1200 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
1201 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
1202 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
1203 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
1204 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
1205 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
1206 # union test
1207 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1208 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1209 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1210 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1211 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1212 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1213 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1214 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1215 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1216 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1217 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1218 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1219 # union + having test
1220 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1221 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1222 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1223 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1224 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1225 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1226 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1227 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1228 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1229 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1230 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1231 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1232 # row tests
1233 # < > >= <= and = ALL/ <> ANY do not support row operation
1234 -- error ER_OPERAND_COLUMNS
1235 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
1236 -- error ER_OPERAND_COLUMNS
1237 SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1238 -- error ER_OPERAND_COLUMNS
1239 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1240 -- error ER_OPERAND_COLUMNS
1241 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
1242 -- error ER_OPERAND_COLUMNS
1243 SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1244 -- error ER_OPERAND_COLUMNS
1245 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1246 -- error ER_OPERAND_COLUMNS
1247 SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
1248 -- error ER_OPERAND_COLUMNS
1249 SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
1250 # following should be converted to IN
1251 -- error ER_OPERAND_COLUMNS
1252 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
1253 -- error ER_OPERAND_COLUMNS
1254 SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1255 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1256 -- error ER_OPERAND_COLUMNS
1257 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
1258 -- error ER_OPERAND_COLUMNS
1259 SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1260 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1261 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
1262 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
1263 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
1264 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
1265 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);
1266 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);
1267 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);
1268 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);
1269 # without optimisation
1270 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1271 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1272 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1273 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1274 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1275 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
1276 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1277 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1278 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1279 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1280 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1281 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
1282 # without optimisation + having
1283 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
1284 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
1285 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
1286 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1287 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
1288 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
1289 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
1290 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
1291 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
1292 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1293 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
1294 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
1295 # EXISTS in string contence
1296 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
1297 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
1298 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
1299 DROP TABLE t1;
1300 CREATE TABLE t1 ( a double, b double );
1301 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1302 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
1303 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
1304 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
1305 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
1306 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
1307 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
1308 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
1309 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
1310 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
1311 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
1312 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
1313 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
1314 DROP TABLE t1;
1315 CREATE TABLE t1 ( a char(1), b char(1));
1316 INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
1317 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
1318 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
1319 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
1320 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
1321 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
1322 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
1323 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
1324 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
1325 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
1326 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
1327 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
1328 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
1329 DROP TABLE t1;
1333 # SELECT(EXISTS * ...)optimisation
1335 create table t1 (a int, b int);
1336 insert into t1 values (1,2),(3,4);
1337 select * from t1 up where exists (select * from t1 where t1.a=up.a);
1338 explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
1339 drop table t1;
1343 # Bug#4102 subselect in HAVING
1346 CREATE TABLE t1 (t1_a int);
1347 INSERT INTO t1 VALUES (1);
1348 CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
1349 INSERT INTO t2 VALUES (1, 1), (1, 2);
1350 SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
1351   HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
1352 DROP TABLE t1, t2;
1356 # Test problem with NULL and derived tables
1357 # (Bug#4097 JOIN with subquery causes entire column to report NULL)
1360 CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
1361 INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
1362 CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
1363 INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
1364 SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
1365 drop table t1,t2;
1369 # outer fields resolving in INSERT/REPLACE and CRETE with SELECT
1371 CREATE TABLE t1 ( a int, b int );
1372 CREATE TABLE t2 ( c int, d int );
1373 INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
1374 SELECT a AS abc, b FROM t1 outr WHERE b =
1375   (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1376 INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
1377   (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1378 select * from t2;
1379 CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
1380   (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1381 select * from t3;
1382 prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
1383 execute stmt1;
1384 deallocate prepare stmt1;
1385 select * from t2;
1386 drop table t3;
1387 prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
1388 execute stmt1;
1389 select * from t3;
1390 deallocate prepare stmt1;
1391 DROP TABLE t1, t2, t3;
1394 # Aggregate function comparation with ALL/ANY/SOME subselect
1396 CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1397 insert into t1 values (1);
1398 CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1399 insert into t2 values (1,2);
1400 select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
1401 drop table t1,t2;
1405 # Bug#4769 - fulltext in subselect
1407 create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
1408 insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
1409 create table t2 (a int);
1410 insert into t2 values (1),(3),(2),(7);
1411 select a,b from t1 where match(b) against ('Ball') > 0;
1412 select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
1413 drop table t1,t2;
1417 # Bug#5003 - like in subselect
1419 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);
1420 CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
1421 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
1422 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
1423 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
1424 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
1425 SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
1426 drop table t1;
1429 # Optimized IN with compound index
1431 CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
1432 CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
1433 insert into t1 values (1,1),(1,2),(2,1),(2,2);
1434 insert into t2 values (1,2),(2,2);
1435 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1436 alter table t2 drop primary key;
1437 alter table t2 add key KEY1 (aid, bid);
1438 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1439 alter table t2 drop key KEY1;
1440 alter table t2 add primary key (bid, aid);
1441 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1442 drop table t1,t2;
1445 # resolving fields of grouped outer SELECT
1447 CREATE TABLE t1 (howmanyvalues bigint, avalue int);
1448 INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
1449 SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
1450 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1451 CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
1452 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
1453 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
1454 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
1455 drop table t1;
1457 create table t1 (x int);
1458 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;
1459 drop table t1;
1462 # Test of correct maybe_null flag returning by subquwery for temporary table
1463 # creation
1465 CREATE TABLE `t1` ( `master` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `slave` int(10) unsigned NOT NULL default '0', `access` int(10) unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
1466 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);
1467 CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL default '0', `pid` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `level` tinyint(4) unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY  (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
1468 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');
1469 -- error ER_BAD_FIELD_ERROR
1470 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;
1471 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;
1472 drop tables t1,t2;
1476 # Test for Bug#6462 Same request on same data returns different results
1477 # a.k.a. "Proper cleanup of subqueries is missing for SET and DO statements".
1479 create table t1 (a int not null, b int not null, c int, primary key (a,b));
1480 insert into t1 values (1,1,1), (2,2,2), (3,3,3);
1481 set @b:= 0;
1482 # Let us check that subquery will use covering index
1483 explain select sum(a) from t1 where b > @b;
1484 # This should not crash -debug server due to failing assertion
1485 set @a:= (select sum(a) from t1 where b > @b);
1486 # And this should not falsely report index usage
1487 explain select a from t1 where c=2;
1488 # Same for DO statement
1489 do @a:= (select sum(a) from t1 where b > @b);
1490 explain select a from t1 where c=2;
1491 drop table t1;
1494 # Subselect in non-select command just after connection
1496 connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
1497 connection root;
1498 set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
1499 connection default;
1500 disconnect root;
1503 # primary query with temporary table and subquery with grouping
1505 create table t1 (a int, b int);
1506 create table t2 (a int, b int);
1507 insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
1508 insert into t2 values (1,3),(2,1);
1509 select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
1510 drop table t1, t2;
1513 # subqueries with full text search
1515 create table t1 (id int);
1516 create table t2 (id int, body text, fulltext (body));
1517 insert into t1 values(1),(2),(3);
1518 insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
1519 select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
1520 drop table t2,t1;
1523 # Equal operation under row and empty subquery
1525 create table t1 (s1 int,s2 int);
1526 insert into t1 values (20,15);
1527 select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
1528 drop table t1;
1531 # ALL/ANY with NULL
1533 create table t1 (s1 int);
1534 insert into t1 values (1),(null);
1535 select * from t1 where s1 < all (select s1 from t1);
1536 select s1, s1 < all (select s1 from t1) from t1;
1537 drop table t1;
1540 # reference on changable fields from subquery
1542 CREATE TABLE t1 (
1543   Code char(3) NOT NULL default '',
1544   Name char(52) NOT NULL default '',
1545   Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1546   Region char(26) NOT NULL default '',
1547   SurfaceArea float(10,2) NOT NULL default '0.00',
1548   IndepYear smallint(6) default NULL,
1549   Population int(11) NOT NULL default '0',
1550   LifeExpectancy float(3,1) default NULL,
1551   GNP float(10,2) default NULL,
1552   GNPOld float(10,2) default NULL,
1553   LocalName char(45) NOT NULL default '',
1554   GovernmentForm char(45) NOT NULL default '',
1555   HeadOfState char(60) default NULL,
1556   Capital int(11) default NULL,
1557   Code2 char(2) NOT NULL default ''
1558 ) ENGINE=MyISAM;
1559 INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
1560 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');
1561 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');
1562 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');
1563 /*!40000 ALTER TABLE t1 ENABLE KEYS */;
1564 SELECT DISTINCT Continent AS c FROM t1 outr WHERE
1565   Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
1566   Population < 200);
1567 drop table t1;
1571 # Test for Bug#7885 Server crash when 'any' subselect compared to
1572 #                   non-existant field.
1574 create table t1 (a1 int);
1575 create table t2 (b1 int);
1576 --error ER_BAD_FIELD_ERROR
1577 select * from t1 where a2 > any(select b1 from t2);
1578 select * from t1 where a1 > any(select b1 from t2);
1579 drop table t1,t2;
1583 # Comparison subquery with * and row
1585 create table t1 (a integer, b integer);
1586 select (select * from t1) = (select 1,2);
1587 select (select 1,2) = (select * from t1);
1588 # queries whih can be converted to IN
1589 select  row(1,2) = ANY (select * from t1);
1590 select  row(1,2) != ALL (select * from t1);
1591 drop table t1;
1594 # Comparison subquery and row with nested rows
1596 create table t1 (a integer, b integer);
1597 -- error ER_OPERAND_COLUMNS
1598 select row(1,(2,2)) in (select * from t1 );
1599 -- error ER_OPERAND_COLUMNS
1600 select row(1,(2,2)) = (select * from t1 );
1601 -- error ER_OPERAND_COLUMNS
1602 select (select * from t1) = row(1,(2,2));
1603 drop table t1;
1606 # Forward reference detection
1608 create  table t1 (a integer);
1609 insert into t1 values (1);
1610 -- error ER_ILLEGAL_REFERENCE
1611 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1612 -- error ER_ILLEGAL_REFERENCE
1613 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1614 select 1 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
1615 -- error ER_ILLEGAL_REFERENCE
1616 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1617 -- error ER_ILLEGAL_REFERENCE
1618 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
1619 drop table t1;
1623 # Test for Bug#8218 Join does not pass string from right table
1625 CREATE TABLE t1 (
1626   categoryId int(11) NOT NULL,
1627   courseId int(11) NOT NULL,
1628   startDate datetime NOT NULL,
1629   endDate datetime NOT NULL,
1630   createDate datetime NOT NULL,
1631   modifyDate timestamp NOT NULL,
1632   attributes text NOT NULL
1634 INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1635 (1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1636 (1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1637 (2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
1638 (2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
1639 (2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1640 (2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1641 (3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1642 (5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
1644 CREATE TABLE t2 (
1645   userId int(11) NOT NULL,
1646   courseId int(11) NOT NULL,
1647   date datetime NOT NULL
1649 INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
1650 (5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
1651 (5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
1652 (5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
1653 (5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
1654 (5141,89,'2004-10-22'),(5141,51,'2004-10-26');
1657 CREATE TABLE t3 (
1658   groupId int(11) NOT NULL,
1659   parentId int(11) NOT NULL,
1660   startDate datetime NOT NULL,
1661   endDate datetime NOT NULL,
1662   createDate datetime NOT NULL,
1663   modifyDate timestamp NOT NULL,
1664   ordering int(11)
1666 INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
1668 CREATE TABLE t4 (
1669   id int(11) NOT NULL,
1670   groupTypeId int(11) NOT NULL,
1671   groupKey varchar(50) NOT NULL,
1672   name text,
1673   ordering int(11),
1674   description text,
1675   createDate datetime NOT NULL,
1676   modifyDate timestamp NOT NULL
1678 INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
1679 (12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
1681 CREATE TABLE t5 (
1682   userId int(11) NOT NULL,
1683   groupId int(11) NOT NULL,
1684   createDate datetime NOT NULL,
1685   modifyDate timestamp NOT NULL
1687 INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
1689 select
1690   count(distinct t2.userid) pass,
1691   groupstuff.*,
1692   count(t2.courseid) crse,
1693   t1.categoryid,
1694   t2.courseid,
1695   date_format(date, '%b%y') as colhead
1696 from t2
1697 join t1 on t2.courseid=t1.courseid
1698 join
1700   select
1701     t5.userid,
1702     parentid,
1703     parentgroup,
1704     childid,
1705     groupname,
1706     grouptypeid
1707   from t5
1708   join
1709   (
1710      select t4.id as parentid,
1711        t4.name as parentgroup,
1712        t4.id as childid,
1713        t4.name as groupname,
1714        t4.grouptypeid
1715      from t4
1716   ) as gin on t5.groupid=gin.childid
1717 ) as groupstuff on t2.userid = groupstuff.userid
1718 group by
1719   groupstuff.groupname, colhead , t2.courseid;
1721 drop table t1, t2, t3, t4, t5;
1725 # Transformation in left expression of subquery (Bug#8888)
1727 create table t1 (a int);
1728 insert into t1 values (1), (2), (3);
1729 SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
1730 drop table t1;
1733 # subselect into HAVING clause (code covarage improvement)
1735 create table t1 (a int);
1736 create table t2 (a int);
1737 insert into t1 values (1),(2);
1738 insert into t2 values (0),(1),(2),(3);
1739 select a from t2 where a in (select a from t1);
1740 select a from t2 having a in (select a from t1);
1741 prepare stmt1 from "select a from t2 where a in (select a from t1)";
1742 execute stmt1;
1743 execute stmt1;
1744 deallocate prepare stmt1;
1745 prepare stmt1 from "select a from t2 having a in (select a from t1)";
1746 execute stmt1;
1747 execute stmt1;
1748 deallocate prepare stmt1;
1749 drop table t1, t2;
1752 # single row subqueries and row operations (code covarage improvement)
1754 create table t1 (a int, b int);
1755 insert into t1 values (1,2);
1756 -- error ER_OPERAND_COLUMNS
1757 select 1 = (select * from t1);
1758 -- error ER_OPERAND_COLUMNS
1759 select (select * from t1) = 1;
1760 -- error ER_OPERAND_COLUMNS
1761 select (1,2) = (select a from t1);
1762 -- error ER_OPERAND_COLUMNS
1763 select (select a from t1) = (1,2);
1764 -- error ER_OPERAND_COLUMNS
1765 select (1,2,3) = (select * from t1);
1766 -- error ER_OPERAND_COLUMNS
1767 select (select * from t1) = (1,2,3);
1768 drop table t1;
1772 # Item_int_with_ref check (Bug#10020)
1774 CREATE TABLE `t1` (
1775   `itemid` bigint(20) unsigned NOT NULL auto_increment,
1776   `sessionid` bigint(20) unsigned default NULL,
1777   `time` int(10) unsigned NOT NULL default '0',
1778   `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
1779 NULL default '',
1780   `data` text collate latin1_general_ci NOT NULL,
1781   PRIMARY KEY  (`itemid`)
1782 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1783 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
1784 CREATE TABLE `t2` (
1785   `sessionid` bigint(20) unsigned NOT NULL auto_increment,
1786   `pid` int(10) unsigned NOT NULL default '0',
1787   `date` int(10) unsigned NOT NULL default '0',
1788   `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1789   PRIMARY KEY  (`sessionid`)
1790 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1791 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1792 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;
1793 drop tables t1,t2;
1796 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
1797 create table t1 (fld enum('0','1'));
1798 insert into t1 values ('1');
1799 select * from (select max(fld) from t1) as foo;
1800 drop table t1;
1804 # Bug#11867 queries with ROW(,elems>) IN (SELECT DISTINCT <cols> FROM ...)
1807 CREATE TABLE t1 (one int, two int, flag char(1));
1808 CREATE TABLE t2 (one int, two int, flag char(1));
1809 INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
1810 INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
1812 SELECT * FROM t1
1813   WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
1814 SELECT * FROM t1
1815   WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
1817 insert into t2 values (null,null,'N');
1818 insert into t2 values (null,3,'0');
1819 insert into t2 values (null,5,'0');
1820 insert into t2 values (10,null,'0');
1821 insert into t1 values (10,3,'0');
1822 insert into t1 values (10,5,'0');
1823 insert into t1 values (10,10,'0');
1824 SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
1825 SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
1826 SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
1827 SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
1828 SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
1829 explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
1830 explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
1831 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;
1832 DROP TABLE t1,t2;
1836 # Bug#12392 where cond with IN predicate for rows and NULL values in table
1839 CREATE TABLE t1 (a char(5), b char(5));
1840 INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
1842 SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
1844 DROP TABLE t1;
1848 # Bug#11479 subquery over left join with an empty inner table
1851 CREATE TABLE t1 (a int);
1852 CREATE TABLE t2 (a int, b int);
1853 CREATE TABLE t3 (b int NOT NULL);
1854 INSERT INTO t1 VALUES (1), (2), (3), (4);
1855 INSERT INTO t2 VALUES (1,10), (3,30);
1857 SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1858   WHERE t3.b IS NOT NULL OR t2.a > 10;
1859 SELECT * FROM t1
1860   WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
1861                        WHERE t3.b IS NOT NULL OR t2.a > 10);
1863 DROP TABLE t1,t2,t3;
1867 # Bug#18503 Queries with a quantified subquery returning empty set may
1868 #           return a wrong result.
1870 CREATE TABLE t1 (f1 INT);
1871 CREATE TABLE t2 (f2 INT);
1872 INSERT INTO t1 VALUES (1);
1873 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
1874 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
1875 INSERT INTO t2 VALUES (1);
1876 INSERT INTO t2 VALUES (2);
1877 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
1878 DROP TABLE t1, t2;
1882 # Bug#16302 Quantified subquery without any tables gives wrong results
1884 select 1 from dual where 1 < any (select 2);
1885 select 1 from dual where 1 < all (select 2);
1886 select 1 from dual where 2 > any (select 1);
1887 select 1 from dual where 2 > all (select 1);
1888 select 1 from dual where 1 < any (select 2 from dual);
1889 select 1 from dual where 1 < all (select 2 from dual where 1!=1);
1892 # Bug#20975 Wrong query results for subqueries within NOT
1893 create table t1 (s1 char);
1894 insert into t1 values (1),(2);
1896 select * from t1 where (s1 < any (select s1 from t1));
1897 select * from t1 where not (s1 < any (select s1 from t1));
1899 select * from t1 where (s1 < ALL (select s1+1 from t1));
1900 select * from t1 where not(s1 < ALL (select s1+1 from t1));
1902 select * from t1 where (s1+1 = ANY (select s1 from t1));
1903 select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
1905 select * from t1 where (s1 = ALL (select s1/s1 from t1));
1906 select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
1907 drop table t1;
1911 # Bug#16255 Subquery in where
1913 create table t1 (
1914   retailerID varchar(8) NOT NULL,
1915   statusID   int(10) unsigned NOT NULL,
1916   changed    datetime NOT NULL,
1917   UNIQUE KEY retailerID (retailerID, statusID, changed)
1920 INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
1921 INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
1922 INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
1923 INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
1924 INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
1925 INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
1927 select * from t1 r1
1928   where (r1.retailerID,(r1.changed)) in
1929          (SELECT r2.retailerId,(max(changed)) from t1 r2
1930           group by r2.retailerId);
1931 drop table t1;
1935 # Bug#21180 Subselect with index for both WHERE and ORDER BY
1936 #           produces empty result
1938 create table t1(a int, primary key (a));
1939 insert into t1 values (10);
1941 create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
1942 insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
1944 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1945   ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1946              ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1947 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1948   ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1949             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1951 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1952   ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1953             ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1954 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
1955   ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1956             ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1958 drop table t1,t2;
1962 # Bug#21853 assert failure for a grouping query with
1963 #           an ALL/ANY quantified subquery in HAVING
1966 CREATE TABLE t1 (
1967   field1 int NOT NULL,
1968   field2 int NOT NULL,
1969   field3 int NOT NULL,
1970   PRIMARY KEY  (field1,field2,field3)
1972 CREATE TABLE t2 (
1973   fieldA int NOT NULL,
1974   fieldB int NOT NULL,
1975   PRIMARY KEY  (fieldA,fieldB)
1978 INSERT INTO t1 VALUES
1979   (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
1980 INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
1982 SELECT field1, field2, COUNT(*)
1983   FROM t1 GROUP BY field1, field2;
1985 SELECT field1, field2
1986   FROM  t1
1987     GROUP BY field1, field2
1988       HAVING COUNT(*) >= ALL (SELECT fieldB
1989                                 FROM t2 WHERE fieldA = field1);
1990 SELECT field1, field2
1991   FROM  t1
1992     GROUP BY field1, field2
1993       HAVING COUNT(*) < ANY (SELECT fieldB
1994                                FROM t2 WHERE fieldA = field1);
1996 DROP TABLE t1, t2;
2000 # Bug#23478 not top-level IN subquery returning a non-empty result set
2001 #           with possible NULL values by index access from the outer query
2004 CREATE TABLE t1(a int, INDEX (a));
2005 INSERT INTO t1 VALUES (1), (3), (5), (7);
2006 INSERT INTO t1 VALUES (NULL);
2008 CREATE TABLE t2(a int);
2009 INSERT INTO t2 VALUES (1),(2),(3);
2011 EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
2012 SELECT a, a IN (SELECT a FROM t1) FROM t2;
2014 DROP TABLE t1,t2;
2018 # Bug#11302 getObject() returns a String for a sub-query of type datetime
2020 CREATE TABLE t1 (a DATETIME);
2021 INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
2023 CREATE TABLE t2 AS SELECT
2024   (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
2025    FROM t1 WHERE a > '2000-01-01';
2026 SHOW CREATE TABLE t2;
2028 CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
2029 SHOW CREATE TABLE t3;
2031 DROP TABLE t1,t2,t3;
2035 # Bug#24670 subquery witout tables but with a WHERE clause
2038 CREATE TABLE t1 (a int);
2039 INSERT INTO t1 VALUES (1), (2);
2041 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
2042 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
2043 EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
2045 DROP TABLE t1;
2049 # Bug#24653 sorting by expressions containing subselects
2050 #           that return more than one row
2053 CREATE TABLE t1 (a int);
2054 INSERT INTO t1 VALUES (2), (4), (1), (3);
2056 CREATE TABLE t2 (b int, c int);
2057 INSERT INTO t2 VALUES
2058   (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
2060 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
2061 --error ER_SUBQUERY_NO_1_ROW
2062 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
2063 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
2064 --error ER_SUBQUERY_NO_1_ROW
2065 SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
2067 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
2068 --error ER_SUBQUERY_NO_1_ROW
2069 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
2072 SELECT a FROM t1 GROUP BY a
2073   HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
2074                 (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2075 --error ER_SUBQUERY_NO_1_ROW
2076 SELECT a FROM t1 GROUP BY a
2077   HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
2078                 (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2080 SELECT a FROM t1 GROUP BY a
2081   HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2082                 (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
2083 --error ER_SUBQUERY_NO_1_ROW
2084 SELECT a FROM t1 GROUP BY a
2085   HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2086                 (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
2088 SELECT a FROM t1
2089   ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
2090                   (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
2091 --error ER_SUBQUERY_NO_1_ROW
2092 SELECT a FROM t1
2093   ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
2094                   (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
2096 SELECT a FROM t1
2097   ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2098                   (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
2099 --error ER_SUBQUERY_NO_1_ROW
2100 SELECT a FROM t1
2101   ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2102                   (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
2104 DROP TABLE t1,t2;
2106 # End of 4.1 tests
2109 #decimal-related tests
2111 create table t1 (df decimal(5,1));
2112 insert into t1 values(1.1);
2113 insert into t1 values(2.2);
2115 select * from t1 where df <= all (select avg(df) from t1 group by df);
2116 select * from t1 where df >= all (select avg(df) from t1 group by df);
2117 drop table t1;
2119 create table t1 (df decimal(5,1));
2120 insert into t1 values(1.1);
2121 select 1.1 * exists(select * from t1);
2122 drop table t1;
2124 CREATE TABLE t1 (
2125   grp int(11) default NULL,
2126   a decimal(10,2) default NULL);
2128 insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
2129 select * from t1;
2130 select min(a) from t1 group by grp;
2131 drop table t1;
2135 # Test for Bug#9338 lame substitution of c1 instead of c2
2138 CREATE table t1 ( c1 integer );
2139 INSERT INTO t1 VALUES ( 1 );
2140 INSERT INTO t1 VALUES ( 2 );
2141 INSERT INTO t1 VALUES ( 3 );
2143 CREATE TABLE t2 ( c2 integer );
2144 INSERT INTO t2 VALUES ( 1 );
2145 INSERT INTO t2 VALUES ( 4 );
2146 INSERT INTO t2 VALUES ( 5 );
2148 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
2150 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
2151   WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
2153 DROP TABLE t1,t2;
2157 # Test for Bug#9516 wrong evaluation of not_null_tables attribute in SQ
2159 CREATE TABLE t1 ( c1 integer );
2160 INSERT INTO t1 VALUES ( 1 );
2161 INSERT INTO t1 VALUES ( 2 );
2162 INSERT INTO t1 VALUES ( 3 );
2163 INSERT INTO t1 VALUES ( 6 );
2165 CREATE TABLE t2 ( c2 integer );
2166 INSERT INTO t2 VALUES ( 1 );
2167 INSERT INTO t2 VALUES ( 4 );
2168 INSERT INTO t2 VALUES ( 5 );
2169 INSERT INTO t2 VALUES ( 6 );
2171 CREATE TABLE t3 ( c3 integer );
2172 INSERT INTO t3 VALUES ( 7 );
2173 INSERT INTO t3 VALUES ( 8 );
2175 SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
2176   WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
2178 DROP TABLE t1,t2,t3;
2182 # Item_int_with_ref check (Bug#10020)
2184 CREATE TABLE `t1` (
2185   `itemid` bigint(20) unsigned NOT NULL auto_increment,
2186   `sessionid` bigint(20) unsigned default NULL,
2187   `time` int(10) unsigned NOT NULL default '0',
2188   `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2189 NULL default '',
2190   `data` text collate latin1_general_ci NOT NULL,
2191   PRIMARY KEY  (`itemid`)
2192 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2193 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2194 CREATE TABLE `t2` (
2195   `sessionid` bigint(20) unsigned NOT NULL auto_increment,
2196   `pid` int(10) unsigned NOT NULL default '0',
2197   `date` int(10) unsigned NOT NULL default '0',
2198   `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2199   PRIMARY KEY  (`sessionid`)
2200 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2201 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2202 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;
2203 drop tables t1,t2;
2207 # Correct building of equal fields list (do not include outer
2208 # fields) (Bug#6384)
2210 CREATE TABLE t1 (EMPNUM   CHAR(3));
2211 CREATE TABLE t2 (EMPNUM   CHAR(3) );
2212 INSERT INTO t1 VALUES ('E1'),('E2');
2213 INSERT INTO t2 VALUES ('E1');
2214 DELETE FROM t1
2215 WHERE t1.EMPNUM NOT IN
2216       (SELECT t2.EMPNUM
2217        FROM t2
2218        WHERE t1.EMPNUM = t2.EMPNUM);
2219 select * from t1;
2220 DROP TABLE t1,t2;
2224 # Test for Bug#11487 range access in a subquery
2227 CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
2228 INSERT INTO t1 VALUES (1, 1);
2229 CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
2230                  PRIMARY KEY(select_id,values_id));
2231 INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
2233 SELECT values_id FROM t1
2234 WHERE values_id IN (SELECT values_id FROM t2
2235                     WHERE select_id IN (1, 0));
2236 SELECT values_id FROM t1
2237 WHERE values_id IN (SELECT values_id FROM t2
2238                     WHERE select_id BETWEEN 0 AND 1);
2239 SELECT values_id FROM t1
2240 WHERE values_id IN (SELECT values_id FROM t2
2241                     WHERE select_id = 0 OR select_id = 1);
2243 DROP TABLE t1, t2;
2246 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
2247 create table t1 (fld enum('0','1'));
2248 insert into t1 values ('1');
2249 select * from (select max(fld) from t1) as foo;
2250 drop table t1;
2254 # Test for Bug#11762 subquery with an aggregate function in HAVING
2257 CREATE TABLE t1 (a int, b int);
2258 CREATE TABLE t2 (c int, d int);
2259 CREATE TABLE t3 (e int);
2261 INSERT INTO t1 VALUES
2262   (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
2263 INSERT INTO t2 VALUES
2264   (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
2265 INSERT INTO t3 VALUES (10), (30), (10), (20) ;
2267 SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
2268 SELECT * FROM t2;
2269 SELECT * FROM t3;
2271 SELECT a FROM t1 GROUP BY a
2272   HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
2273 SELECT a FROM t1 GROUP BY a
2274   HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
2275 SELECT a FROM t1 GROUP BY a
2276   HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
2277 SELECT a FROM t1 GROUP BY a
2278   HAVING a IN (SELECT c FROM t2
2279                  WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2280 SELECT a FROM t1 GROUP BY a
2281   HAVING a IN (SELECT c FROM t2
2282                  WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2283 SELECT a FROM t1 GROUP BY a
2284   HAVING a IN (SELECT c FROM t2
2285                  WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2286 SELECT a FROM t1 GROUP BY a
2287   HAVING a IN (SELECT c FROM t2
2288                  WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
2289 SELECT a FROM t1 GROUP BY a
2290   HAVING a IN (SELECT c FROM t2
2291                  WHERE MIN(b) < d AND
2292                        EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2294 SELECT a, SUM(a) FROM t1 GROUP BY a;
2296 SELECT a FROM t1
2297    WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
2298 SELECT a FROM t1 GROUP BY a
2299    HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
2301 SELECT a FROM t1
2302    WHERE a < 3 AND
2303          EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
2304 SELECT a FROM t1
2305    WHERE a < 3 AND
2306          EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
2308 SELECT t1.a FROM t1 GROUP BY t1.a
2309   HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
2310                        HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2311                                        HAVING SUM(t1.a+t2.c) < t3.e/4));
2312 SELECT t1.a FROM t1 GROUP BY t1.a
2313        HAVING t1.a > ALL(SELECT t2.c FROM t2
2314                            WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2315                                           HAVING SUM(t1.a+t2.c) < t3.e/4));
2316 -- error ER_INVALID_GROUP_FUNC_USE
2317 SELECT t1.a FROM t1 GROUP BY t1.a
2318        HAVING t1.a > ALL(SELECT t2.c FROM t2
2319                            WHERE EXISTS(SELECT t3.e FROM t3
2320                                           WHERE SUM(t1.a+t2.c) < t3.e/4));
2321 -- error ER_INVALID_GROUP_FUNC_USE
2322 SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
2324 SELECT t1.a FROM t1 GROUP BY t1.a
2325   HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2326                     HAVING AVG(t2.c+SUM(t1.b)) > 20);
2327 SELECT t1.a FROM t1 GROUP BY t1.a
2328   HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2329                     HAVING AVG(SUM(t1.b)) > 20);
2331 SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
2332   HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2333                     HAVING t2.c+sum > 20);
2335 DROP TABLE t1,t2,t3;
2339 # Test for Bug#16603 GROUP BY in a row subquery with a quantifier
2340 #                    when an index is defined on the grouping field
2342 CREATE TABLE t1 (a varchar(5), b varchar(10));
2343 INSERT INTO t1 VALUES
2344   ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
2345   ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
2347 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2348 EXPLAIN
2349 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2351 ALTER TABLE t1 ADD INDEX(a);
2353 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2354 EXPLAIN
2355 SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2357 DROP TABLE t1;
2361 # Bug#17366 Unchecked Item_int results in server crash
2363 create table t1( f1 int,f2 int);
2364 insert into t1 values (1,1),(2,2);
2365 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';
2366 drop table t1;
2370 # Bug#18306 server crash on delete using subquery.
2373 create table t1 (c int, key(c));
2374 insert into t1 values (1142477582), (1142455969);
2375 create table t2 (a int, b int);
2376 insert into t2 values (2, 1), (1, 0);
2377 delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
2378 drop table t1, t2;
2382 # Bug#7549 Missing error message for invalid view selection with subquery
2385 CREATE TABLE t1 (a INT);
2387 --error ER_BAD_FIELD_ERROR
2388 CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2389 --error ER_BAD_FIELD_ERROR
2390 CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
2391 --error ER_BAD_FIELD_ERROR
2392 SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2394 DROP TABLE t1;
2398 # Bug#19077 A nested materialized derived table is used before being populated.
2400 create table t1 (i int, j bigint);
2401 insert into t1 values (1, 2), (2, 2), (3, 2);
2402 select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
2403 drop table t1;
2407 # Bug#19700 subselect returning BIGINT always returned it as SIGNED
2409 CREATE TABLE t1 (i BIGINT UNSIGNED);
2410 INSERT INTO t1 VALUES (10000000000000000000); # > MAX SIGNED BIGINT 9323372036854775807
2411 INSERT INTO t1 VALUES (1);
2413 CREATE TABLE t2 (i BIGINT UNSIGNED);
2414 INSERT INTO t2 VALUES (10000000000000000000); # same as first table
2415 INSERT INTO t2 VALUES (1);
2417 /* simple test */
2418 SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
2420 /* subquery test */
2421 SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
2423 /* subquery test with cast*/
2424 SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
2426 DROP TABLE t1;
2427 DROP TABLE t2;
2431 # Bug#20519 subselect with LIMIT M, N
2434 CREATE TABLE t1 (
2435   id bigint(20) unsigned NOT NULL auto_increment,
2436   name varchar(255) NOT NULL,
2437   PRIMARY KEY  (id)
2439 INSERT INTO t1 VALUES
2440   (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
2442 CREATE TABLE t2 (
2443   id bigint(20) unsigned NOT NULL auto_increment,
2444   mid bigint(20) unsigned NOT NULL,
2445   date date NOT NULL,
2446   PRIMARY KEY  (id)
2448 INSERT INTO t2 VALUES
2449   (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
2450   (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
2452 SELECT *,
2453       (SELECT date FROM t2 WHERE mid = t1.id
2454          ORDER BY date DESC LIMIT 0, 1) AS date_last,
2455       (SELECT date FROM t2 WHERE mid = t1.id
2456          ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
2457   FROM t1;
2458 SELECT *,
2459       (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
2460          ORDER BY date DESC LIMIT 1, 1) AS date_count
2461   FROM t1;
2462 SELECT *,
2463       (SELECT date FROM t2 WHERE mid = t1.id
2464          ORDER BY date DESC LIMIT 0, 1) AS date_last,
2465       (SELECT date FROM t2 WHERE mid = t1.id
2466          ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
2467   FROM t1;
2468 DROP TABLE t1,t2;
2472 # Bug#20869 subselect with range access by DESC
2475 CREATE TABLE t1 (
2476   i1 int(11) NOT NULL default '0',
2477   i2 int(11) NOT NULL default '0',
2478   t datetime NOT NULL default '0000-00-00 00:00:00',
2479   PRIMARY KEY  (i1,i2,t)
2481 INSERT INTO t1 VALUES
2482 (24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
2483 (24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
2484 (24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
2485 (24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
2486 (24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
2487 (24,2,'2005-05-27 12:40:06');
2489 CREATE TABLE t2 (
2490   i1 int(11) NOT NULL default '0',
2491   i2 int(11) NOT NULL default '0',
2492   t datetime default NULL,
2493   PRIMARY KEY  (i1)
2495 INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
2497 EXPLAIN
2498 SELECT * FROM t1,t2
2499   WHERE t1.t = (SELECT t1.t FROM t1
2500                   WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
2501                     ORDER BY t1.t DESC LIMIT 1);
2502 SELECT * FROM t1,t2
2503   WHERE t1.t = (SELECT t1.t FROM t1
2504                   WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
2505                     ORDER BY t1.t DESC LIMIT 1);
2507 DROP TABLE t1, t2;
2511 # Bug#14654 Cannot select from the same table twice within a UNION statement
2513 CREATE TABLE t1 (i INT);
2515 (SELECT i FROM t1) UNION (SELECT i FROM t1);
2516 #TODO:not supported
2517 --error ER_PARSE_ERROR
2518 SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
2519   (
2520    (SELECT i FROM t1) UNION
2521    (SELECT i FROM t1)
2522   );
2524 #TODO:not supported
2525 --error ER_PARSE_ERROR
2526 SELECT * FROM t1
2527 WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
2529 #TODO:not supported
2530 --error ER_PARSE_ERROR
2531 explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
2532   from t1;
2534 #TODO:not supported
2535 --error ER_PARSE_ERROR
2536 explain select * from t1 where not exists
2537   ((select t11.i from t1 t11) union (select t12.i from t1 t12));
2539 DROP TABLE t1;
2543 # Bug#21798 memory leak during query execution with subquery in column
2544 #           list using a function
2546 CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
2547 insert into t1 (a) values (FLOOR(rand() * 100));
2548 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2549 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2550 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2551 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2552 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2553 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2554 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2555 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2556 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2557 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2558 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2559 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2560 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2562 SELECT a,
2563        (SELECT REPEAT(' ',250) FROM t1 i1
2564         WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
2565 FROM t1 ORDER BY a LIMIT 5;
2566 DROP TABLE t1;
2570 # Bug#21540 Subqueries with no from and aggregate functions return
2571 #           wrong results
2572 CREATE TABLE t1 (a INT, b INT);
2573 CREATE TABLE t2 (a INT);
2574 INSERT INTO t2 values (1);
2575 INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
2576 SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2577 SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
2578   FROM t1 GROUP BY t1.a;
2579 SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
2580 SELECT COUNT(DISTINCT t1.b),
2581        (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
2582   FROM t1 GROUP BY t1.a;
2583 SELECT (
2584     SELECT (
2585       SELECT COUNT(DISTINCT t1.b)
2586     )
2588 FROM t1 GROUP BY t1.a;
2589 SELECT (
2590   SELECT (
2591       SELECT (
2592         SELECT COUNT(DISTINCT t1.b)
2593       )
2594   )
2595   FROM t1 GROUP BY t1.a LIMIT 1)
2596 FROM t1 t2
2597 GROUP BY t2.a;
2598 DROP TABLE t1,t2;
2602 # Bug#21727 Correlated subquery that requires filesort:
2603 #           slow with big sort_buffer_size
2606 CREATE TABLE t1 (a int, b int auto_increment, PRIMARY KEY (b));
2607 CREATE TABLE t2 (x int auto_increment, y int, z int,
2608                  PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
2610 disable_query_log;
2611 let $1=3000;
2612 while ($1)
2614   eval INSERT INTO t1(a) VALUES(RAND()*1000);
2615   eval SELECT MAX(b) FROM t1 INTO @id;
2616   let $2=10;
2617   while ($2)
2618   {
2619     eval INSERT INTO t2(y,z) VALUES(@id,RAND()*1000);
2620     dec $2;
2621   }
2622   dec $1;
2624 enable_query_log;
2626 SET SESSION sort_buffer_size = 32 * 1024;
2627 SELECT SQL_NO_CACHE COUNT(*)
2628   FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2629           FROM t1) t;
2631 SET SESSION sort_buffer_size = 8 * 1024 * 1024;
2632 SELECT SQL_NO_CACHE COUNT(*)
2633   FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
2634           FROM t1) t;
2636 DROP TABLE t1,t2;
2640 # Bug#25219 EXIST subquery with UNION over a mix of
2641 #           correlated and uncorrelated selects
2644 CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
2645 CREATE TABLE t2 (c int);
2647 INSERT INTO t1 VALUES ('aa', 1);
2648 INSERT INTO t2 VALUES (1);
2650 SELECT * FROM t1
2651   WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2652                 UNION
2653                 SELECT c from t2 WHERE c=t1.c);
2655 INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
2657 SELECT * FROM t1
2658   WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2659                 UNION
2660                 SELECT c from t2 WHERE c=t1.c);
2662 INSERT INTO t2 VALUES (2);
2663 CREATE TABLE t3 (c int);
2664 INSERT INTO t3 VALUES (1);
2666 SELECT * FROM t1
2667   WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
2668                 UNION
2669                 SELECT c from t2 WHERE c=t1.c);
2671 DROP TABLE t1,t2,t3;
2675 # Bug#23800 Outer fields in correlated subqueries is used in a temporary
2676 #           table created for sorting.
2678 CREATE TABLE t1(f1 int);
2679 CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
2680 INSERT INTO t1 VALUES (1),(1),(2),(2);
2681 INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
2682 SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
2683 SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
2684 PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
2685 EXECUTE stmt1;
2686 EXECUTE stmt1;
2687 DEALLOCATE PREPARE stmt1;
2688 SELECT f2, AVG(f21),
2689       (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
2690   FROM t2 GROUP BY f2;
2691 DROP TABLE t1,t2;
2692 CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
2693 INSERT INTO t1 VALUES
2694   (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
2695   (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
2696   (3,2,'k'), (3,1,'l'), (1,9,'m');
2697 SELECT a, MAX(b),
2698       (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
2699   FROM t1 GROUP BY a;
2700 DROP TABLE t1;
2704 # Bug#21904 (parser problem when using IN with a double "(())")
2707 --disable_warnings
2708 DROP TABLE IF EXISTS t1;
2709 DROP TABLE IF EXISTS t2;
2710 DROP TABLE IF EXISTS t1xt2;
2711 --enable_warnings
2713 CREATE TABLE t1 (
2714   id_1 int(5) NOT NULL,
2715   t varchar(4) DEFAULT NULL
2718 CREATE TABLE t2 (
2719   id_2 int(5) NOT NULL,
2720   t varchar(4) DEFAULT NULL
2723 CREATE TABLE t1xt2 (
2724   id_1 int(5) NOT NULL,
2725   id_2 int(5) NOT NULL
2728 INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
2730 INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
2732 INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
2734 # subselect returns 0 rows
2736 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2737 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2739 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2740 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2742 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2743 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2745 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2746 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2748 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2749 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
2751 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2752 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
2754 insert INTO t1xt2 VALUES (1, 12);
2756 # subselect returns 1 row
2758 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2759 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2761 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2762 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2764 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2765 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2767 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2768 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2770 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2771 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2773 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2774 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2776 insert INTO t1xt2 VALUES (2, 12);
2778 # subselect returns more than 1 row
2780 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2781 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2783 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2784 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2786 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2787 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2789 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2790 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2792 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2793 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2795 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2796 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2798 DROP TABLE t1;
2799 DROP TABLE t2;
2800 DROP TABLE t1xt2;
2804 # Bug#26728 derived table with concatanation of literals in select list
2807 CREATE TABLE t1 (a int);
2808 INSERT INTO t1 VALUES (3), (1), (2);
2810 SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
2811 SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
2813 DROP table t1;
2817 # Bug#27257 COUNT(*) aggregated in outer query
2820 CREATE TABLE t1 (a int, b int);
2821 CREATE TABLE t2 (m int, n int);
2822 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2823 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2825 SELECT COUNT(*), a,
2826        (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
2827   FROM t1 GROUP BY a;
2829 SELECT COUNT(*), a,
2830        (SELECT MIN(m) FROM t2 WHERE m = count(*))
2831   FROM t1 GROUP BY a;
2833 SELECT COUNT(*), a
2834   FROM t1 GROUP BY a
2835     HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
2837 DROP TABLE t1,t2;
2841 # Bug#27229 GROUP_CONCAT in subselect with COUNT() as an argument
2844 CREATE TABLE t1 (a int, b int);
2845 CREATE TABLE t2 (m int, n int);
2846 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2847 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2849 SELECT COUNT(*) c, a,
2850        (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
2851   FROM t1 GROUP BY a;
2853 SELECT COUNT(*) c, a,
2854        (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
2855   FROM t1 GROUP BY a;
2857 DROP table t1,t2;
2861 # Bug#27321 Wrong subquery result in a grouping select
2863 CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
2864 INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
2865 (1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
2866 (3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
2868 SELECT a, MAX(b),
2869   (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
2870   FROM t1 GROUP BY a;
2871 SELECT a x, MAX(b),
2872   (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
2873   FROM t1 GROUP BY a;
2874 SELECT a, AVG(b),
2875   (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
2876   FROM t1 WHERE t1.d=0 GROUP BY a;
2878 SELECT tt.a,
2879  (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2880   LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2881   FROM t1 as tt;
2883 SELECT tt.a,
2884  (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2885   LIMIT 1)
2886   FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2887   FROM t1 as tt GROUP BY tt.a;
2889 SELECT tt.a, MAX(
2890  (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2891   LIMIT 1)
2892   FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
2893   FROM t1 as tt GROUP BY tt.a;
2895 DROP TABLE t1;
2899 # Bug#27348 SET FUNCTION used in a subquery from WHERE condition
2902 CREATE TABLE t1 (a int, b int);
2903 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
2905 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2906 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2908 SELECT a FROM t1 t0
2909   WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2911 SET @@sql_mode='ansi';
2912 --error ER_INVALID_GROUP_FUNC_USE
2913 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2914 --error ER_INVALID_GROUP_FUNC_USE
2915 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2917 --error ER_INVALID_GROUP_FUNC_USE
2918 SELECT a FROM t1 t0
2919   WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2921 SET @@sql_mode=default;
2923 DROP TABLE t1;
2927 # Bug#27363 nested aggregates in outer, subquery / sum(select count(outer))
2929 CREATE TABLE t1 (a INT); INSERT INTO t1 values (1),(1),(1),(1);
2930 CREATE TABLE t2 (x INT); INSERT INTO t1 values (1000),(1001),(1002);
2932 --error ER_INVALID_GROUP_FUNC_USE
2933 SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
2934 --error ER_INVALID_GROUP_FUNC_USE
2935 SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
2936 SELECT COUNT(1) FROM DUAL;
2938 --error ER_INVALID_GROUP_FUNC_USE
2939 SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
2941 --error ER_INVALID_GROUP_FUNC_USE
2942 SELECT
2943   SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
2944 FROM t1;
2946 --error ER_INVALID_GROUP_FUNC_USE
2947 SELECT t1.a as XXA,
2948    SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
2949 FROM t1;
2951 DROP TABLE t1,t2;
2955 # Bug#27807 Server crash when executing subquery with EXPLAIN
2957 CREATE TABLE t1 (a int, b int, KEY (a));
2958 INSERT INTO t1 VALUES (1,1),(2,1);
2959 EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
2960 DROP TABLE t1;
2964 # Bug#28377 grouping query with a correlated subquery in WHERE condition
2967 CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
2968 INSERT INTO t1 VALUES
2969   (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
2970 CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
2971 INSERT INTO t2 VALUES (7), (5), (1), (3);
2973 SELECT id, st FROM t1
2974   WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2975 SELECT id, st FROM t1
2976   WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2977     GROUP BY id;
2979 SELECT id, st FROM t1
2980   WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
2981 SELECT id, st FROM t1
2982   WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
2983     GROUP BY id;
2985 DROP TABLE t1,t2;
2989 # Bug#28728 crash with EXPLAIN EXTENDED for a query with a derived table
2990 #           over a grouping subselect
2993 CREATE TABLE t1 (a int);
2995 INSERT INTO t1 VALUES (1), (2);
2997 EXPLAIN EXTENDED
2998 SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
3000 DROP TABLE t1;
3004 # Bug#28811 crash for query containing subquery with ORDER BY and LIMIT 1
3007 CREATE TABLE t1 (
3008   a varchar(255) default NULL,
3009   b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
3010   INDEX idx(a,b)
3012 CREATE TABLE t2 (
3013   a varchar(255) default NULL
3016 INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
3017 INSERT INTO t1 SELECT * FROM t1;
3018 INSERT INTO t1 SELECT * FROM t1;
3019 INSERT INTO t1 SELECT * FROM t1;
3020 INSERT INTO t1 SELECT * FROM t1;
3021 INSERT INTO t1 SELECT * FROM t1;
3022 INSERT INTO t1 SELECT * FROM t1;
3023 INSERT INTO t1 SELECT * FROM t1;
3024 INSERT INTO t1 SELECT * FROM t1;
3025 INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
3026 INSERT INTO `t2` VALUES ('abcdefghijk');
3027 INSERT INTO `t2` VALUES ('asdf');
3029 SET session sort_buffer_size=8192;
3031 SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
3033 DROP TABLE t1,t2;
3037 # Bug#27333 subquery grouped for aggregate of outer query / no aggregate
3038 #           of subquery
3040 CREATE TABLE t1 (a INTEGER, b INTEGER);
3041 CREATE TABLE t2 (x INTEGER);
3042 INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
3043 INSERT INTO t2 VALUES (1), (2);
3045 # wasn't failing, but should
3046 --error ER_SUBQUERY_NO_1_ROW
3047 SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
3049 # fails as it should
3050 --error ER_SUBQUERY_NO_1_ROW
3051 SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
3053 SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
3054 DROP TABLE t1,t2;
3056 # second test case from 27333
3057 CREATE TABLE t1 (a INT, b INT);
3058 INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
3060 # returns no rows, when it should
3061 SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
3062 AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
3063 GROUP BY a1.a;
3064 DROP TABLE t1;
3066 #test cases from 29297
3067 CREATE TABLE t1 (a INT);
3068 CREATE TABLE t2 (a INT);
3069 INSERT INTO t1 VALUES (1),(2);
3070 INSERT INTO t2 VALUES (1),(2);
3071 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
3072 --error ER_SUBQUERY_NO_1_ROW
3073 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
3074 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
3075 DROP TABLE t1,t2;
3078 # Bug#31884 Assertion + crash in subquery in the SELECT clause.
3081 CREATE TABLE t1 (a1 INT, a2 INT);
3082 CREATE TABLE t2 (b1 INT, b2 INT);
3084 INSERT INTO t1 VALUES (100, 200);
3085 INSERT INTO t1 VALUES (101, 201);
3086 INSERT INTO t2 VALUES (101, 201);
3087 INSERT INTO t2 VALUES (103, 203);
3089 SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
3090 DROP TABLE t1, t2;
3093 # Bug#28076 inconsistent binary/varbinary comparison
3096 CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
3097 INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
3099 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
3100 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3102 CREATE INDEX I1 ON t1 (s1);
3103 CREATE INDEX I2 ON t1 (s2);
3105 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
3106 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3108 TRUNCATE t1;
3109 INSERT INTO t1 VALUES (0x41,0x41);
3110 SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
3112 DROP TABLE t1;
3114 CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
3115 CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
3116 CREATE TABLE t3 (a3 BINARY(2) default '0');
3117 INSERT INTO t1 VALUES (1),(2),(3),(4);
3118 INSERT INTO t2 VALUES (1),(2),(3);
3119 INSERT INTO t3 VALUES (1),(2),(3);
3120 SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
3121 SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
3122 DROP TABLE t1,t2,t3;
3124 CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
3125 CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
3126 CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
3127 INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
3128 INSERT INTO t2 VALUES (2), (3), (4), (5);
3129 INSERT INTO t3 VALUES (10), (20), (30);
3130 SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
3131 SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
3132 DROP TABLE t1, t2, t3;
3136 # Bug#30788 Inconsistent retrieval of char/varchar
3139 CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
3140 INSERT INTO t1 VALUES ('a', 'aa');
3141 INSERT INTO t1 VALUES ('a', 'aaa');
3142 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3143 CREATE INDEX I1 ON t1 (a);
3144 CREATE INDEX I2 ON t1 (b);
3145 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3146 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3148 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
3149 INSERT INTO t2 SELECT * FROM t1;
3150 CREATE INDEX I1 ON t2 (a);
3151 CREATE INDEX I2 ON t2 (b);
3152 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
3153 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
3154 EXPLAIN
3155 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
3156 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
3158 DROP TABLE t1,t2;
3162 # Bug#32400 Complex SELECT query returns correct result only on some occasions
3165 CREATE TABLE t1(a INT, b INT);
3166 INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
3168 --error ER_BAD_FIELD_ERROR
3169 EXPLAIN
3170 SELECT a AS out_a, MIN(b) FROM t1
3171 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3172 GROUP BY a;
3174 --error ER_BAD_FIELD_ERROR
3175 SELECT a AS out_a, MIN(b) FROM t1
3176 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3177 GROUP BY a;
3179 EXPLAIN
3180 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3181 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3182 GROUP BY a;
3184 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3185 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3186 GROUP BY a;
3188 DROP TABLE t1;
3192 # Bug#32036 EXISTS within a WHERE clause with a UNION crashes MySQL 5.122
3195 CREATE TABLE t1 (a INT);
3196 CREATE TABLE t2 (a INT);
3198 INSERT INTO t1 VALUES (1),(2);
3199 INSERT INTO t2 VALUES (1),(2);
3201 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3202 EXPLAIN EXTENDED
3203 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3206 #TODO:not supported
3207 --error ER_PARSE_ERROR
3208 EXPLAIN EXTENDED
3209 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
3210                                (SELECT 1 FROM t2 WHERE t1.a = t2.a));
3212 DROP TABLE t1,t2;
3216 # Bug#33675 Usage of an uninitialized memory by filesort in a subquery
3217 #           caused server crash.
3219 create table t1(f11 int, f12 int);
3220 create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
3221 insert into t1 values(1,1),(2,2), (3, 3);
3222 let $i=10000;
3223 --disable_query_log
3224 --disable_warnings
3225 while ($i)
3227   eval insert into t2 values (-1 , $i/5000 + 1, '$i');
3228   dec $i;
3230 --enable_warnings
3231 --enable_query_log
3232 set session sort_buffer_size= 33*1024;
3233 select count(*) from t1 where f12 =
3234 (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
3236 drop table t1,t2;
3240 # Bug#33794 "MySQL crashes executing specific query on specific dump"
3242 CREATE TABLE t4 (
3243   f7 varchar(32) collate utf8_bin NOT NULL default '',
3244   f10 varchar(32) collate utf8_bin default NULL,
3245   PRIMARY KEY  (f7)
3247 INSERT INTO t4 VALUES(1,1), (2,null);
3249 CREATE TABLE t2 (
3250   f4 varchar(32) collate utf8_bin NOT NULL default '',
3251   f2 varchar(50) collate utf8_bin default NULL,
3252   f3 varchar(10) collate utf8_bin default NULL,
3253   PRIMARY KEY  (f4),
3254   UNIQUE KEY uk1 (f2)
3256 INSERT INTO t2 VALUES(1,1,null), (2,2,null);
3258 CREATE TABLE t1 (
3259   f8 varchar(32) collate utf8_bin NOT NULL default '',
3260   f1 varchar(10) collate utf8_bin default NULL,
3261   f9 varchar(32) collate utf8_bin default NULL,
3262   PRIMARY KEY  (f8)
3264 INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
3266 CREATE TABLE t3 (
3267   f6 varchar(32) collate utf8_bin NOT NULL default '',
3268   f5 varchar(50) collate utf8_bin default NULL,
3269   PRIMARY KEY (f6)
3271 INSERT INTO t3 VALUES (1,null), (2,null);
3273 SELECT
3274   IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
3275   IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
3276   SUM(
3277     IF(
3278       (SELECT VPC.f2
3279        FROM t2 VPC, t4 a2, t2 a3
3280        WHERE
3281          VPC.f4 = a2.f10 AND a3.f2 = a4
3282        LIMIT 1) IS NULL,
3283        0,
3284        t3.f5
3285     )
3286   ) AS a6
3287 FROM
3288   t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
3289 GROUP BY a4;
3291 DROP TABLE t1, t2, t3, t4;
3295 # Bug#36139 "float, zerofill, crash with subquery"
3297 create table t1 (a float(5,4) zerofill);
3298 create table t2 (a float(5,4),b float(2,0));
3300 select t1.a from t1 where
3301   t1.a= (select b from t2 limit 1) and not
3302   t1.a= (select a from t2 limit 1) ;
3304 drop table t1, t2;
3308 # Bug#36011 Server crash with explain extended on query with dependent
3309 #           subqueries
3312 CREATE TABLE t1 (a INT);
3313 INSERT INTO t1 VALUES (1),(2);
3314 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 GROUP BY a);
3315 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT 1 FROM t1 WHERE a > 3 GROUP BY a);
3316 DROP TABLE t1;
3318 --echo #
3319 --echo # Bug#45061: Incorrectly market field caused wrong result.
3320 --echo #
3321 CREATE TABLE `C` (
3322   `int_nokey` int(11) NOT NULL,
3323   `int_key` int(11) NOT NULL,
3324   KEY `int_key` (`int_key`)
3327 INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
3328 (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
3329 (5,2), (1,8), (7,0), (0,9), (9,5);
3331 --disable_warnings
3332 SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3333 EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3334 --enable_warnings
3336 DROP TABLE C;
3337 --echo # End of test for bug#45061.
3340 --echo #
3341 --echo # Bug #46749: Segfault in add_key_fields() with outer subquery level 
3342 --echo #   field references
3343 --echo #
3345 CREATE TABLE t1 (
3346   a int,
3347   b int,
3348   UNIQUE (a), KEY (b)
3350 INSERT INTO t1 VALUES (1,1), (2,1);
3352 CREATE TABLE st1 like t1;
3353 INSERT INTO st1 VALUES (1,1), (2,1);
3355 CREATE TABLE st2 like t1;
3356 INSERT INTO st2 VALUES (1,1), (2,1);
3358 # should have "impossible where"
3359 EXPLAIN
3360 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3361 FROM t1 
3362 WHERE a = 230;
3364 # should not crash
3365 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3366 FROM t1 
3367 WHERE a = 230;
3369 DROP TABLE t1, st1, st2;
3371 --echo #
3372 --echo # Bug #48709: Assertion failed in sql_select.cc:11782: 
3373 --echo #   int join_read_key(JOIN_TAB*)
3374 --echo #
3376 CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
3377 INSERT INTO t1 VALUES (10,1), (14,1);
3379 CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
3380 INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
3382 --echo # should have eq_ref for t1
3383 --replace_column 1 x 2 x 5 x 6 x 7 x 8 x 9 x 10 x
3384 EXPLAIN
3385 SELECT * FROM t2 outr
3386 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)  
3387 ORDER BY outr.pk;
3389 --echo # should not crash on debug binaries
3390 SELECT * FROM t2 outr
3391 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)  
3392 ORDER BY outr.pk;
3394 DROP TABLE t1,t2;
3396 --echo #
3397 --echo # Bug#12329653 
3398 --echo # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
3399 --echo #
3401 CREATE TABLE t1(a1 int);
3402 INSERT INTO t1 VALUES (1),(2);
3404 SELECT @@session.sql_mode INTO @old_sql_mode;
3405 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
3407 ## First a simpler query, illustrating the transformation
3408 ## '1 < some (...)' => '1 < max(...)'
3409 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
3411 ## The query which made the server crash.
3412 PREPARE stmt FROM 
3413 'SELECT 1 UNION ALL 
3414 SELECT 1 FROM t1
3415 ORDER BY
3416 (SELECT 1 FROM t1 AS t1_0  
3417   WHERE 1 < SOME (SELECT a1 FROM t1)
3418 )' ;
3420 --error ER_SUBQUERY_NO_1_ROW
3421 EXECUTE stmt ;
3422 --error ER_SUBQUERY_NO_1_ROW
3423 EXECUTE stmt ;
3425 SET SESSION sql_mode=@old_sql_mode;
3427 DEALLOCATE PREPARE stmt;
3428 DROP TABLE t1;
3430 --echo #
3431 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
3432 --echo #
3434 CREATE TABLE t1(a1 int);
3435 INSERT INTO t1 VALUES (1),(2);
3437 CREATE TABLE t2(a1 int);
3438 INSERT INTO t2 VALUES (3);
3440 SELECT @@session.sql_mode INTO @old_sql_mode;
3441 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
3443 ## All these are subject to the transformation
3444 ## '1 < some (...)' => '1 < max(...)'
3445 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
3446 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
3447 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
3448 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
3450 SET SESSION sql_mode=@old_sql_mode;
3452 DROP TABLE t1, t2;
3454 --echo #
3455 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
3456 --echo #
3458 create table t2(i int);
3459 insert into t2 values(0);
3461 SELECT @@session.sql_mode INTO @old_sql_mode;
3462 SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
3464 CREATE VIEW v1 AS  
3465 SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
3468 CREATE TABLE t1 (
3469   pk int NOT NULL,
3470   col_varchar_key varchar(1) DEFAULT NULL,
3471   PRIMARY KEY (pk),
3472   KEY col_varchar_key (col_varchar_key)
3475 SELECT t1.pk
3476 FROM t1
3477 WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
3480 SET SESSION sql_mode=@old_sql_mode;
3482 drop table t2, t1;
3483 drop view v1;
3485 --echo End of 5.0 tests.
3488 # Bug#27348 SET FUNCTION used in  a subquery from WHERE condition
3491 CREATE TABLE t1 (a INT, b INT);
3492 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
3494 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
3495 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
3497 SELECT a FROM t1 t0
3498   WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
3500 SET @@sql_mode='ansi';
3501 --error ER_INVALID_GROUP_FUNC_USE
3502 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
3503 --error ER_INVALID_GROUP_FUNC_USE
3504 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
3506 --error ER_INVALID_GROUP_FUNC_USE
3507 SELECT a FROM t1 t0
3508   WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
3510 SET @@sql_mode=default;
3511 DROP TABLE t1;
3514 # Bug#20835 (literal string with =any values)
3516 CREATE TABLE t1 (s1 CHAR(1));
3517 INSERT INTO t1 VALUES ('a');
3518 SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
3519 DROP TABLE t1;
3522 # Bug#37004 NOT IN subquery with MAX over an empty set
3525 CREATE TABLE t1(c INT, KEY(c));
3526 CREATE TABLE t2(a INT, b INT);
3527 INSERT INTO t2 VALUES (1, 10), (2, NULL);
3528 INSERT INTO t1 VALUES (1), (3);
3530 SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
3532 DROP TABLE t1,t2;
3535 # Bug#38191 Server crash with subquery containing DISTINCT and ORDER BY
3538 CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
3539 INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
3540 CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
3541 INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
3542 SELECT * FROM t1
3543    WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
3544 DROP TABLE t1,t2;
3547 # Bug#37548 result value erronously reported being NULL in certain subqueries
3550 CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
3552 INSERT INTO t1 VALUES (1,NULL), (9,NULL);
3554 CREATE TABLE t2 (
3555   a INT,
3556   b INT,
3557   c INT,
3558   d INT,
3559   PRIMARY KEY (a),
3560   UNIQUE KEY b (b,c,d),
3561   KEY b_2 (b),
3562   KEY c (c),
3563   KEY d (d)
3566 INSERT INTO t2 VALUES
3567   (43, 2, 11 ,30),
3568   (44, 2, 12 ,30),
3569   (45, 1, 1  ,10000),
3570   (46, 1, 2  ,10000),
3571   (556,1, 32 ,10000);
3573 CREATE TABLE t3 (
3574   a INT,
3575   b INT,
3576   c INT,
3577   PRIMARY KEY (a),
3578   UNIQUE KEY b (b,c),
3579   KEY c (c),
3580   KEY b_2 (b)
3583 INSERT INTO t3 VALUES (1,1,1), (2,32,1);
3585 explain
3586 SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
3587 SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
3589 DROP TABLE t1,t2,t3;
3592 # Bug#37460 Assertion failed:
3593 # !table->file || table->file->inited == handler::NONE
3595 CREATE TABLE t1 (id int);
3596 CREATE TABLE t2 (id int, c int);
3598 INSERT INTO t1 (id) VALUES (1);
3599 INSERT INTO t2 (id) VALUES (1);
3600 INSERT INTO t1 (id) VALUES (1);
3601 INSERT INTO t2 (id) VALUES (1);
3603 CREATE VIEW v1 AS
3604 SELECT t2.c AS c FROM t1, t2
3605 WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
3606 UPDATE v1 SET c=1;
3608 CREATE VIEW v2 (a,b) AS
3609 SELECT t2.id, t2.c AS c FROM t1, t2
3610 WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
3612 --error ER_VIEW_CHECK_FAILED
3613 INSERT INTO v2(a,b) VALUES (2,2);
3614 INSERT INTO v2(a,b) VALUES (1,2);
3615 SELECT * FROM v1;
3617 CREATE VIEW v3 AS
3618 SELECT t2.c AS c FROM t2
3619 WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
3621 DELETE FROM v3;
3623 DROP VIEW v1,v2,v3;
3624 DROP TABLE t1,t2;
3626 --echo #
3627 --echo # Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
3628 --echo #
3629 create table t1(id integer primary key, g integer, v integer, s char(1));
3630 create table t2(id integer primary key, g integer, v integer, s char(1));
3631 insert into t1 values
3632   (10, 10, 10,   'l'),
3633   (20, 20, 20,   'l'),
3634   (40, 40, 40,   'l'),
3635   (41, 40, null, 'l'),
3636   (50, 50, 50,   'l'),
3637   (51, 50, null, 'l'),
3638   (60, 60, 60,   'l'),
3639   (61, 60, null, 'l'),
3640   (70, 70, 70,   'l'),
3641   (90, 90, null, 'l');
3642 insert into t2 values
3643   (10, 10, 10,   'r'),
3644   (30, 30, 30,   'r'),
3645   (50, 50, 50,   'r'),
3646   (60, 60, 60,   'r'),
3647   (61, 60, null, 'r'),
3648   (70, 70, 70,   'r'),
3649   (71, 70, null, 'r'),
3650   (80, 80, 80,   'r'),
3651   (81, 80, null, 'r'),
3652   (100,100,null, 'r');
3654 select *
3655 from t1
3656 where v in(select v
3657            from t2
3658            where t1.g=t2.g) is unknown;
3659 drop table t1, t2;
3662 # Bug #31157: Crash when select+order by the avg of some field within the
3663 # group by
3665 CREATE TABLE t1 (a ENUM('rainbow'));
3666 INSERT INTO t1 VALUES (),(),(),(),();
3667 SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
3668 DROP TABLE t1;
3669 CREATE TABLE t1 (a LONGBLOB);
3670 INSERT INTO t1 SET a = 'aaaa';
3671 INSERT INTO t1 SET a = 'aaaa';
3672 SELECT 1 FROM t1 GROUP BY
3673   (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
3674 DROP TABLE t1;
3676 --echo #
3677 --echo # Bug #49512 : subquery with aggregate function crash 
3678 --echo #   subselect_single_select_engine::exec()
3680 CREATE TABLE t1(a INT);
3681 INSERT INTO t1 VALUES();
3683 --echo # should not crash
3684 SELECT 1 FROM t1 WHERE a <> SOME
3686  SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
3687  FROM t1,t1 a
3689 DROP TABLE t1;
3691 --echo #
3692 --echo # Bug #45989 take 2 : memory leak after explain encounters an 
3693 --echo # error in the query
3694 --echo #
3696 CREATE TABLE t1(a LONGTEXT);
3697 INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
3698 INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
3700 --error ER_BAD_FIELD_ERROR
3701 EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
3702 (SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1  
3703 WHERE t1.a = d1.a;
3705 DROP TABLE t1;
3708 --echo End of 5.1 tests.
3711 # Bug #47904 Incorrect results w/ table subquery, derived SQs, and LEFT JOIN on index
3714 --echo Set up test tables.
3715 CREATE TABLE t1 (
3716         t1_id INT UNSIGNED,
3718         PRIMARY KEY(t1_id)
3719 ) Engine=MyISAM;
3721 INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
3723 CREATE TABLE t2 SELECT * FROM t1;
3724         
3725 CREATE TABLE t3 (
3726         t3_id INT UNSIGNED AUTO_INCREMENT,
3727         t1_id INT UNSIGNED,
3728         amount DECIMAL(16,2),
3730         PRIMARY KEY(t3_id),
3731         KEY(t1_id)
3732 ) Engine=MyISAM;
3734 INSERT INTO t3 (t1_id, t3_id, amount) 
3735         VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
3737 --echo This is the 'inner query' running by itself.
3738 --echo Produces correct results.
3739 SELECT
3740         t1.t1_id,
3741         IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
3742 FROM
3743         t1
3744         LEFT JOIN t2 ON t2.t1_id=t1.t1_id
3745 GROUP BY
3746         t1.t1_id
3749 --echo SELECT * FROM (the same inner query)
3750 --echo Produces correct results.
3751 SELECT * FROM (
3752 SELECT
3753         t1.t1_id,
3754         IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
3755 FROM
3756         t1
3757         LEFT JOIN t2 ON t2.t1_id=t1.t1_id
3758 GROUP BY
3759         t1.t1_id
3760 ) AS t;
3762 --echo Now make t2.t1_id part of a key.
3763 ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
3765 --echo Same inner query by itself.
3766 --echo Still correct results.
3767 SELECT
3768         t1.t1_id,
3769         IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
3770 FROM
3771         t1
3772         LEFT JOIN t2 ON t2.t1_id=t1.t1_id
3773 GROUP BY
3774         t1.t1_id;
3776 --echo SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
3777 SELECT * FROM (
3778 SELECT
3779         t1.t1_id,
3780         IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
3781 FROM
3782         t1
3783         LEFT JOIN t2 ON t2.t1_id=t1.t1_id
3784 GROUP BY
3785         t1.t1_id
3786 ) AS t;
3789 DROP TABLE t3;
3790 DROP TABLE t2;
3791 DROP TABLE t1;
3794 --echo #
3795 --echo # Bug #52711: Segfault when doing EXPLAIN SELECT with 
3796 --echo #  union...order by (select... where...)
3797 --echo #
3799 CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
3800 INSERT INTO t1 VALUES (1),(2);
3801 CREATE TABLE t2 (b INT);
3802 INSERT INTO t2 VALUES (1),(2);
3804 --echo # Should not crash
3805 --disable_result_log
3806 EXPLAIN
3807 SELECT * FROM t2 UNION SELECT * FROM t2
3808   ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
3810 --echo # Should not crash
3811 SELECT * FROM t2 UNION SELECT * FROM t2
3812   ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
3813 DROP TABLE t1,t2;
3814 --enable_result_log
3816 --echo End of 5.1 tests
3818 --echo #
3819 --echo # Bug #11765713 58705:
3820 --echo # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
3821 --echo # CREATED BY OPT_SUM_QUERY
3822 --echo #
3824 CREATE TABLE t1(a INT NOT NULL, KEY (a));
3825 INSERT INTO t1 VALUES (0), (1);
3827 --error ER_SUBQUERY_NO_1_ROW
3828 SELECT 1 as foo FROM t1 WHERE a < SOME
3829   (SELECT a FROM t1 WHERE a <=>
3830     (SELECT a FROM t1)
3831   );
3833 SELECT 1 as foo FROM t1 WHERE a < SOME
3834   (SELECT a FROM t1 WHERE a <=>
3835     (SELECT a FROM t1 where a is null)
3836   ); 
3838 DROP TABLE t1;