3 # NOTE. Please do not switch connection inside this test.
4 # subquery.inc is included from several other test cases which set
5 # explicit session properties that must be preserved throughout the test.
6 # If you need to use a dedicated connection for a test case,
7 # close the new connection and switch back to "default" as soon
11 # This portion of the file vas developed when subquery materialization
12 # was rule-based; to preserve the intended test scenarios, we switch
13 # off cost-based choice for them.
14 set @old_opt_switch=@@optimizer_switch;
15 set optimizer_switch=
'subquery_materialization_cost_based=off';
19 drop
table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
22 explain extended select (select 2);
23 SELECT (SELECT 1) UNION SELECT (SELECT 2);
24 explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
25 SELECT (SELECT (SELECT 0 UNION SELECT 0));
26 explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
27 -- error ER_ILLEGAL_REFERENCE
28 SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
29 -- error ER_ILLEGAL_REFERENCE
30 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;
31 SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
32 -- error ER_ILLEGAL_REFERENCE
33 SELECT (SELECT a) as a;
34 EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
35 SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
36 -- error ER_BAD_FIELD_ERROR
38 SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
39 -- error ER_BAD_FIELD_ERROR
40 SELECT 1 FROM (SELECT (SELECT a) b) c;
41 SELECT * FROM (SELECT 1 as
id) b WHERE
id IN (SELECT * FROM (SELECT 1 as
id) c
ORDER BY
id);
42 -- error ER_OPERAND_COLUMNS
43 SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
44 SELECT 1 IN (SELECT 1);
45 SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
46 -- error ER_WRONG_USAGE
47 select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
48 -- error ER_PARSE_ERROR
49 SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
50 -- error ER_BAD_FIELD_ERROR
51 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
52 -- error ER_BAD_FIELD_ERROR
53 SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
54 SELECT (SELECT 1,2,3) = ROW(1,2,3);
55 SELECT (SELECT 1,2,3) = ROW(1,2,1);
56 SELECT (SELECT 1,2,3) < ROW(1,2,1);
57 SELECT (SELECT 1,2,3) > ROW(1,2,1);
58 SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
59 SELECT ROW(1,2,3) = (SELECT 1,2,3);
60 SELECT ROW(1,2,3) = (SELECT 1,2,1);
61 SELECT ROW(1,2,3) < (SELECT 1,2,1);
62 SELECT ROW(1,2,3) > (SELECT 1,2,1);
63 SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
64 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
65 SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
66 SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
67 SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
68 SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
69 SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
71 -- error ER_OPERAND_COLUMNS
72 SELECT (SELECT * FROM (SELECT '
test' a,'
test' b) a);
74 SELECT 1 as a,(SELECT a+a) b,(SELECT b);
76 create
table t1 (a
int);
77 create
table t2 (a
int, b
int);
78 create
table t3 (a
int);
79 create
table t4 (a
int not null, b
int not null);
80 insert into t1 values (2);
81 insert into t2 values (1,7),(2,7);
82 insert into t4 values (4,8),(3,8),(5,9);
83 -- error ER_ILLEGAL_REFERENCE
84 select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
85 select (select a from t1 where t1.a=t2.a), a from t2;
86 select (select a from t1 where t1.a=t2.b), a from t2;
87 select (select a from t1), a, (select 1 union select 2
limit 1) from t2;
88 select (select a from t3), a from t2;
89 select * from t2 where t2.a=(select a from t1);
90 insert into t3 values (6),(7),(3);
91 select * from t2 where t2.b=(select a from t3 order by 1 desc
limit 1);
92 (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;
93 (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);
94 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);
95 select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
96 select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
97 (select * from t2 where a>1) as tt;
98 explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
99 (select * from t2 where a>1) as tt;
100 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);
101 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);
102 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);
103 select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
104 explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
105 select * from t3 where exists (select * from t2 where t2.b=t3.a);
106 select * from t3 where not exists (select * from t2 where t2.b=t3.a);
107 select * from t3 where a in (select b from t2);
108 select * from t3 where a not in (select b from t2);
109 select * from t3 where a = some (select b from t2);
110 select * from t3 where a <> any (select b from t2);
112 # Rewrite: select * from t3 where not exists (select b from t2 where a <> b);
113 select * from t3 where a = all (select b from t2);
115 select * from t3 where a <> all (select b from t2);
116 insert into t2 values (100, 5);
117 select * from t3 where a < any (select b from t2);
118 select * from t3 where a < all (select b from t2);
119 select * from t3 where a >= any (select b from t2);
120 explain extended select * from t3 where a >= any (select b from t2);
121 select * from t3 where a >= all (select b from t2);
122 delete from t2 where a=100;
123 -- error ER_OPERAND_COLUMNS
124 select * from t3 where a in (select a,b from t2);
125 -- error ER_OPERAND_COLUMNS
126 select * from t3 where a in (select * from t2);
127 insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
129 select b,max(a) as ma from t4
group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
130 insert into t2 values (2,10);
131 select b,max(a) as ma from t4
group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
132 delete from t2 where a=2 and b=10;
133 select b,max(a) as ma from t4
group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
134 create
table t5 (a
int);
135 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
136 insert into t5 values (5);
137 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
138 insert into t5 values (2);
139 select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
140 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;
141 -- error ER_SUBQUERY_NO_1_ROW
142 select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
143 create
table t6 (patient_uq
int, clinic_uq
int,
index i1 (clinic_uq));
144 create
table t7( uq
int primary key,
name char(25));
145 insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
146 insert into t6 values (1,1),(1,2),(2,2),(1,3);
147 select * from t6 where exists (select * from t7 where uq = clinic_uq);
148 explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
151 -- error ER_NON_UNIQ_ERROR
152 select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
154 # different tipes & group functions
157 CREATE
TABLE t3 (a varchar(20),b
char(1) NOT NULL
default '0');
158 INSERT INTO t3 VALUES (
'W',
'a'),(
'A',
'c'),(
'J',
'b');
159 CREATE
TABLE t2 (a varchar(20),b
int NOT NULL
default '0');
160 INSERT INTO t2 VALUES (
'W',
'1'),(
'A',
'3'),(
'J',
'2');
161 CREATE
TABLE t1 (a varchar(20),b date NOT NULL
default '0000-00-00');
162 INSERT INTO t1 VALUES (
'W',
'1732-02-22'),(
'A',
'1735-10-30'),(
'J',
'1743-04-13');
163 SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
164 SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
165 SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
168 `pseudo` varchar(35) character set latin1 NOT NULL default '',
169 `email` varchar(60) character set latin1 NOT NULL default '',
170 PRIMARY
KEY (`pseudo`),
171 UNIQUE
KEY `email` (`email`)
172 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
174 INSERT INTO t8 (pseudo,email) VALUES ('joce','
test');
175 INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
176 INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
177 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');
178 -- error ER_OPERAND_COLUMNS
179 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
180 t8 WHERE pseudo='joce');
181 -- error ER_OPERAND_COLUMNS
182 SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
184 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
185 -- error ER_SUBQUERY_NO_1_ROW
186 SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
188 drop
table if exists t1,t2,t3,t4,t5,t6,t7,t8;
190 #searchconthardwarefr3 forumconthardwarefr7
192 `topic` mediumint(8) unsigned NOT NULL default '0',
193 `date` date NOT NULL default '0000-00-00',
194 `pseudo` varchar(35) character set latin1 NOT NULL default '',
195 PRIMARY
KEY (`pseudo`,`date`,`topic`),
196 KEY `topic` (`topic`)
197 ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
198 INSERT INTO t1 (topic,date,pseudo) VALUES
199 ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
200 EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
201 EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
202 SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
203 SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
204 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
205 -- error ER_SUBQUERY_NO_1_ROW
206 SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
207 EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
210 #forumconthardwarefr7 searchconthardwarefr7
212 `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
213 `maxnumrep`
int(10)
unsigned NOT NULL default '0',
214 PRIMARY
KEY (`numeropost`),
215 UNIQUE
KEY `maxnumrep` (`maxnumrep`)
216 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
218 INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
221 `mot` varchar(30) NOT NULL default '',
222 `topic` mediumint(8)
unsigned NOT NULL default '0',
223 `date` date NOT NULL default '0000-00-00',
224 `pseudo` varchar(35) NOT NULL default '',
225 PRIMARY
KEY (`mot`,`pseudo`,`date`,`topic`)
226 ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
228 INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
229 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
230 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;
231 -- error ER_BAD_FIELD_ERROR
232 SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
233 -- error ER_BAD_FIELD_ERROR
234 SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
236 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
237 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
238 SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
239 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
240 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
241 SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
242 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
243 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
244 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
245 SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
246 SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
247 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
248 SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
249 SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
250 SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
253 #forumconthardwarefr7
255 `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
256 `maxnumrep`
int(10)
unsigned NOT NULL default '0',
257 PRIMARY
KEY (`numeropost`),
258 UNIQUE
KEY `maxnumrep` (`maxnumrep`)
259 ) ENGINE=MyISAM ROW_FORMAT=FIXED;
261 INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
262 -- error ER_SUBQUERY_NO_1_ROW
263 select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
264 -- error ER_SUBQUERY_NO_1_ROW
265 select numeropost as a FROM t1
ORDER BY (SELECT 1 FROM t1 HAVING a=1);
268 create
table t1 (a
int);
269 insert into t1 values (1),(2),(3);
270 (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
274 CREATE
TABLE t1 (field
char(1) NOT NULL DEFAULT
'b');
275 INSERT INTO t1 VALUES ();
276 -- error ER_SUBQUERY_NO_1_ROW
277 SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
282 `numeropost` mediumint(8) unsigned NOT NULL default '0',
283 `numreponse`
int(10)
unsigned NOT NULL auto_increment,
284 `pseudo` varchar(35) NOT NULL default '',
285 PRIMARY
KEY (`numeropost`,`numreponse`),
286 UNIQUE
KEY `numreponse` (`numreponse`),
287 KEY `pseudo` (`pseudo`,`numeropost`)
289 -- error ER_ILLEGAL_REFERENCE
290 SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
291 -- error ER_BAD_FIELD_ERROR
292 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
293 SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
294 INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
295 -- error ER_SUBQUERY_NO_1_ROW
296 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
297 EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
298 EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
301 CREATE
TABLE t1 (a
int(1));
302 INSERT INTO t1 VALUES (1);
303 SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
306 #update with subselects
307 create
table t1 (a
int NOT NULL, b
int, primary key (a));
308 create
table t2 (a
int NOT NULL, b
int, primary key (a));
309 insert into t1 values (0, 10),(1, 11),(2, 12);
310 insert into t2 values (1, 21),(2, 22),(3, 23);
312 -- error ER_UPDATE_TABLE_USED
313 update t1
set b= (select b from t1);
314 -- error ER_SUBQUERY_NO_1_ROW
315 update t1
set b= (select b from t2);
316 update t1
set b= (select b from t2 where t1.a = t2.a);
320 #delete with subselects
321 create
table t1 (a
int NOT NULL, b
int, primary key (a));
322 create
table t2 (a
int NOT NULL, b
int, primary key (a));
323 insert into t1 values (0, 10),(1, 11),(2, 12);
324 insert into t2 values (1, 21),(2, 12),(3, 23);
326 select * from t1 where b = (select b from t2 where t1.a = t2.a);
327 -- error ER_UPDATE_TABLE_USED
328 delete from t1 where b = (select b from t1);
329 -- error ER_SUBQUERY_NO_1_ROW
330 delete from t1 where b = (select b from t2);
331 delete from t1 where b = (select b from t2 where t1.a = t2.a);
335 #multi-delete with subselects
337 create
table t11 (a
int NOT NULL, b
int, primary key (a));
338 create
table t12 (a
int NOT NULL, b
int, primary key (a));
339 create
table t2 (a
int NOT NULL, b
int, primary key (a));
340 insert into t11 values (0, 10),(1, 11),(2, 12);
341 insert into t12 values (33, 10),(22, 11),(2, 12);
342 insert into t2 values (1, 21),(2, 12),(3, 23);
345 -- error ER_UPDATE_TABLE_USED
346 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
347 -- error ER_SUBQUERY_NO_1_ROW
348 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
349 delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
352 drop
table t11, t12, t2;
354 #insert with subselects
355 CREATE
TABLE t1 (x
int);
356 create
table t2 (a
int);
357 create
table t3 (b
int);
358 insert into t2 values (1);
359 insert into t3 values (1),(2);
360 -- error ER_UPDATE_TABLE_USED
361 INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
362 -- error ER_SUBQUERY_NO_1_ROW
363 INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
364 INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
366 insert into t2 values (1);
367 let $row_count_before= `SELECT COUNT(*) FROM t1`;
368 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
369 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
370 --source include/wait_condition.inc
372 INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
374 # After this, only data based on old t1 records should have been added.
375 INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
377 -- error ER_BAD_FIELD_ERROR
378 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
379 let $row_count_before= `SELECT COUNT(*) FROM t1`;
380 INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
381 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
382 --source include/wait_condition.inc
385 #TODO: should be uncommented after Bug#380 fix pushed
386 #INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
388 drop
table t1, t2, t3;
390 #replace with subselects
391 CREATE
TABLE t1 (x
int not null, y
int, primary key (x));
392 create
table t2 (a
int);
393 create
table t3 (a
int);
394 insert into t2 values (1);
395 insert into t3 values (1),(2);
397 -- error ER_UPDATE_TABLE_USED
398 replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
399 -- error ER_SUBQUERY_NO_1_ROW
400 replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
401 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
403 replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
405 let $row_count_before= `SELECT COUNT(*) FROM t1`;
406 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
407 # We get one additional row
408 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1;
409 --source include/wait_condition.inc
411 let $row_count_before= `SELECT COUNT(*) FROM t1 WHERE y = 2`;
412 replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
413 let $wait_condition= SELECT COUNT(*) <> $row_count_before FROM t1 WHERE y = 2;
414 --source include/wait_condition.inc
416 replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
418 drop
table t1, t2, t3;
420 -- error ER_NO_TABLES_USED
421 SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
423 CREATE
TABLE t2 (
id int(11) default NULL,
KEY id (
id)) ENGINE=MyISAM CHARSET=latin1;
424 INSERT INTO t2 VALUES (1),(2);
425 SELECT * FROM t2 WHERE
id IN (SELECT 1);
426 EXPLAIN EXTENDED SELECT * FROM t2 WHERE
id IN (SELECT 1);
427 SELECT * FROM t2 WHERE
id IN (SELECT 1 UNION SELECT 3);
428 SELECT * FROM t2 WHERE
id IN (SELECT 1+(select 1));
429 EXPLAIN EXTENDED SELECT * FROM t2 WHERE
id IN (SELECT 1+(select 1));
430 EXPLAIN EXTENDED SELECT * FROM t2 WHERE
id IN (SELECT 1 UNION SELECT 3);
431 SELECT * FROM t2 WHERE
id IN (SELECT 5 UNION SELECT 3);
432 SELECT * FROM t2 WHERE
id IN (SELECT 5 UNION SELECT 2);
433 -- error ER_UPDATE_TABLE_USED
434 INSERT INTO t2 VALUES ((SELECT * FROM t2));
435 -- error ER_UPDATE_TABLE_USED
436 INSERT INTO t2 VALUES ((SELECT
id FROM t2));
438 CREATE
TABLE t1 (
id int(11) default NULL,
KEY id (
id)) ENGINE=MyISAM CHARSET=latin1;
439 INSERT INTO t1 values (1),(1);
440 -- error ER_SUBQUERY_NO_1_ROW
441 UPDATE t2 SET
id=(SELECT * FROM t1);
445 create
table t1 (a
int);
446 insert into t1 values (1),(2),(3);
447 select 1 IN (SELECT * from t1);
448 select 10 IN (SELECT * from t1);
449 select NULL IN (SELECT * from t1);
450 update t1
set a=NULL where a=2;
451 select 1 IN (SELECT * from t1);
452 select 3 IN (SELECT * from t1);
453 select 10 IN (SELECT * from t1);
454 select 1 > ALL (SELECT * from t1);
455 select 10 > ALL (SELECT * from t1);
456 select 1 > ANY (SELECT * from t1);
457 select 10 > ANY (SELECT * from t1);
459 create
table t1 (a varchar(20));
460 insert into t1 values (
'A'),(
'BC'),(
'DEF');
461 select
'A' IN (SELECT * from t1);
462 select
'XYZS' IN (SELECT * from t1);
463 select NULL IN (SELECT * from t1);
464 update t1
set a=NULL where a=
'BC';
465 select
'A' IN (SELECT * from t1);
466 select
'DEF' IN (SELECT * from t1);
467 select
'XYZS' IN (SELECT * from t1);
468 select
'A' > ALL (SELECT * from t1);
469 select
'XYZS' > ALL (SELECT * from t1);
470 select
'A' > ANY (SELECT * from t1);
471 select
'XYZS' > ANY (SELECT * from t1);
473 create
table t1 (a
float);
474 insert into t1 values (1.5),(2.5),(3.5);
475 select 1.5 IN (SELECT * from t1);
476 select 10.5 IN (SELECT * from t1);
477 select NULL IN (SELECT * from t1);
478 update t1
set a=NULL where a=2.5;
479 select 1.5 IN (SELECT * from t1);
480 select 3.5 IN (SELECT * from t1);
481 select 10.5 IN (SELECT * from t1);
482 select 1.5 > ALL (SELECT * from t1);
483 select 10.5 > ALL (SELECT * from t1);
484 select 1.5 > ANY (SELECT * from t1);
485 select 10.5 > ANY (SELECT * from t1);
486 explain extended select (select a+1) from t1;
487 select (select a+1) from t1;
494 CREATE
TABLE t1 (a
int(11) NOT NULL
default '0', PRIMARY
KEY (a));
495 CREATE
TABLE t2 (a
int(11)
default '0', INDEX (a));
496 INSERT INTO t1 VALUES (1),(2),(3),(4);
497 INSERT INTO t2 VALUES (1),(2),(3);
498 SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
499 explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
500 CREATE
TABLE t3 (a
int(11)
default '0');
501 INSERT INTO t3 VALUES (1),(2),(3);
502 SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
503 explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
506 #LIMIT is not supported now
507 create
table t1 (a
float);
508 -- error ER_NOT_SUPPORTED_YET
509 select 10.5 IN (SELECT * from t1 LIMIT 1);
510 -- error ER_NOT_SUPPORTED_YET
511 select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
514 create
table t1 (a
int, b
int, c varchar(10));
515 create
table t2 (a
int);
516 insert into t1 values (1,2,
'a'),(2,3,
'b'),(3,4,
'c');
517 insert into t2 values (1),(2),(NULL);
518 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;
519 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;
520 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;
523 create
table t1 (a
int, b real, c varchar(10));
524 insert into t1 values (1, 1,
'a'), (2,2,
'b'), (NULL, 2,
'b');
525 select ROW(1, 1,
'a') IN (select a,b,c from t1);
526 select ROW(1, 2, 'a') IN (select a,b,c from t1);
527 select ROW(1, 1, 'a') IN (select b,a,c from t1);
528 select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
529 select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
530 select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
531 select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
532 select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
533 select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
534 -- error ER_NOT_SUPPORTED_YET
535 select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
541 create
table t1 (a
int);
542 insert into t1 values (1);
543 do @a:=(SELECT a from t1);
546 set @a:=(SELECT a from t1);
549 -- error ER_NO_SUCH_TABLE
550 do (SELECT a from t1);
551 -- error ER_NO_SUCH_TABLE
552 set @a:=(SELECT a from t1);
556 -- error ER_PARSE_ERROR
557 HANDLER t1 READ a=((SELECT 1));
561 create
table t1 (a
int);
562 create
table t2 (b
int);
563 insert into t1 values (1),(2);
564 insert into t2 values (1);
565 select a from t1 where a in (select a from t1 where a in (select b from t2));
568 create
table t1 (a
int, b
int);
569 create
table t2 like t1;
570 insert into t1 values (1,2),(1,3),(1,4),(1,5);
571 insert into t2 values (1,2),(1,3);
572 select * from t1 where row(a,b) in (select a,b from t2);
575 CREATE
TABLE `t1` (`
i`
int(11) NOT NULL default '0',PRIMARY
KEY (`
i`)) ENGINE=MyISAM CHARSET=latin1;
576 INSERT INTO t1 VALUES (1);
577 UPDATE t1 SET
i=
i+1 WHERE
i=(SELECT MAX(
i));
581 #test of uncacheable subqueries
582 CREATE
TABLE t1 (a
int(1));
583 EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
584 EXPLAIN EXTENDED SELECT (SELECT ENCRYPT(
'test') FROM t1) FROM t1;
585 EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
590 `mot` varchar(30) character set latin1 NOT NULL default '',
591 `topic` mediumint(8)
unsigned NOT NULL default '0',
592 `date` date NOT NULL default '0000-00-00',
593 `pseudo` varchar(35) character set latin1 NOT NULL default '',
594 PRIMARY
KEY (`mot`,`pseudo`,`date`,`topic`),
595 KEY `pseudo` (`pseudo`,`date`,`topic`),
596 KEY `topic` (`topic`)
597 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
600 `mot` varchar(30) character set latin1 NOT NULL default '',
601 `topic` mediumint(8)
unsigned NOT NULL default '0',
602 `date` date NOT NULL default '0000-00-00',
603 `pseudo` varchar(35) character set latin1 NOT NULL default '',
604 PRIMARY
KEY (`mot`,`pseudo`,`date`,`topic`),
605 KEY `pseudo` (`pseudo`,`date`,`topic`),
606 KEY `topic` (`topic`)
607 ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
610 `numeropost` mediumint(8)
unsigned NOT NULL auto_increment,
611 `maxnumrep`
int(10)
unsigned NOT NULL default '0',
612 PRIMARY
KEY (`numeropost`),
613 UNIQUE
KEY `maxnumrep` (`maxnumrep`)
614 ) ENGINE=MyISAM CHARSET=latin1;
615 INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
617 INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
619 INSERT INTO t3 VALUES (1,1);
621 SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
624 DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
625 EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
628 drop
table t1, t2, t3;
630 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
631 CREATE
TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
632 SHOW CREATE
TABLE t1;
634 CREATE
TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
635 SHOW CREATE
TABLE t1;
637 CREATE
TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
638 SHOW CREATE
TABLE t1;
640 CREATE
TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
642 SHOW CREATE
TABLE t1;
645 create
table t1 (a
int);
646 insert into t1 values (1), (2), (3);
647 explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
654 -- error ER_NO_SUCH_TABLE
655 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);
662 ID
int(11) NOT NULL auto_increment,
663 name char(35) NOT NULL
default '',
664 t2
char(3) NOT NULL
default '',
665 District
char(20) NOT NULL
default '',
666 Population
int(11) NOT NULL
default '0',
670 INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
671 INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
672 INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
675 Code
char(3) NOT NULL default '',
676 Name
char(52) NOT NULL default '',
677 Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
678 Region
char(26) NOT NULL default '',
679 SurfaceArea
float(10,2) NOT NULL default '0.00',
680 IndepYear smallint(6) default NULL,
681 Population
int(11) NOT NULL default '0',
682 LifeExpectancy
float(3,1) default NULL,
683 GNP
float(10,2) default NULL,
684 GNPOld
float(10,2) default NULL,
685 LocalName
char(45) NOT NULL default '',
686 GovernmentForm
char(45) NOT NULL default '',
687 HeadOfState
char(60) default NULL,
688 Capital
int(11) default NULL,
689 Code2
char(2) NOT NULL default '',
693 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');
694 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');
696 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);
704 `
id` mediumint(8) unsigned NOT NULL auto_increment,
705 `pseudo` varchar(35) character set latin1 NOT NULL default '',
707 UNIQUE
KEY `pseudo` (`pseudo`)
708 ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
709 INSERT INTO t1 (pseudo) VALUES ('test');
710 SELECT 0 IN (SELECT 1 FROM t1 a);
711 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
712 INSERT INTO t1 (pseudo) VALUES ('test1');
713 SELECT 0 IN (SELECT 1 FROM t1 a);
714 EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
718 `i`
int(11) NOT NULL default '0',
720 ) ENGINE=MyISAM CHARSET=latin1;
722 INSERT INTO t1 VALUES (1);
723 UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
724 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
725 -- error ER_BAD_FIELD_ERROR
726 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
734 id int(11)
default NULL
735 ) ENGINE=MyISAM CHARSET=latin1;
736 INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
738 id int(11)
default NULL,
739 name varchar(15)
default NULL
740 ) ENGINE=MyISAM CHARSET=latin1;
742 INSERT INTO t2 VALUES (4,
'vita'), (1,
'vita'), (2,
'vita'), (1,
'vita');
743 update t1, t2
set t2.name=
'lenka' where t2.id in (select
id from t1);
748 # correct NULL in <CONSTANT> IN (SELECT ...)
750 create
table t1 (a
int, unique
index indexa (a));
751 insert into t1 values (-1), (-4), (-2), (NULL);
752 select -10 IN (select a from t1 FORCE INDEX (indexa));
756 # Test optimization for sub selects
758 create
table t1 (
id int not null auto_increment primary key, salary
int, key(salary));
759 insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
760 explain extended SELECT
id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
764 ID
int(10)
unsigned NOT NULL auto_increment,
765 SUB_ID
int(3)
unsigned NOT NULL default '0',
766 REF_ID
int(10)
unsigned default NULL,
767 REF_SUB
int(3)
unsigned default '0',
768 PRIMARY
KEY (ID,SUB_ID),
769 UNIQUE
KEY t1_PK (ID,SUB_ID),
770 KEY t1_FK (REF_ID,REF_SUB),
771 KEY t1_REFID (REF_ID)
772 ) ENGINE=MyISAM CHARSET=cp1251;
773 INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
774 SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
778 # uninterruptable update
780 create
table t1 (a
int, b
int);
781 create
table t2 (a
int, b
int);
783 insert into t1 values (1,0), (2,0), (3,0);
784 insert into t2 values (1,1), (2,1), (3,1), (2,2);
786 update ignore t1
set b=(select b from t2 where t1.a=t2.a);
792 # reduced subselect in ORDER BY & GROUP BY clauses
796 `
id` mediumint(8) unsigned NOT NULL auto_increment,
797 `pseudo` varchar(35) NOT NULL default '',
798 `email` varchar(60) NOT NULL default '',
800 UNIQUE
KEY `email` (`email`),
801 UNIQUE
KEY `pseudo` (`pseudo`)
802 ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
803 INSERT INTO t1 (
id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
804 SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a)
ORDER BY (SELECT
id*1);
805 drop
table if exists t1;
807 (SELECT 1 as a) UNION (SELECT 1)
ORDER BY (SELECT a+0);
810 # alloc_group_fields() working
812 create
table t1 (a
int, b
int);
813 create
table t2 (a
int, b
int);
814 create
table t3 (a
int, b
int);
815 insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
816 insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
817 insert into t3 values (3,3), (2,2), (1,1);
818 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;
822 # aggregate functions in HAVING test
824 create
table t1 (s1
int);
825 create
table t2 (s1
int);
826 insert into t1 values (1);
827 insert into t2 values (1);
828 select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
832 # update subquery with wrong field (to force name resolving
833 # in UPDATE name space)
835 create
table t1 (s1
int);
836 create
table t2 (s1
int);
837 insert into t1 values (1);
838 insert into t2 values (1);
839 -- error ER_BAD_FIELD_ERROR
840 update t1
set s1 = s1 + 1 where 1 = (select x.s1 as
A from t2 WHERE t2.s1 > t1.s1 order by
A);
846 CREATE
TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
847 s2 CHAR(5) COLLATE latin1_swedish_ci);
848 INSERT INTO t1 VALUES (
'z',
'?');
849 -- error ER_CANT_AGGREGATE_2COLLATIONS
850 select * from t1 where s1 > (select max(s2) from t1);
851 -- error ER_CANT_AGGREGATE_2COLLATIONS
852 select * from t1 where s1 > any (select max(s2) from t1);
856 # aggregate functions reinitialization
858 create
table t1(toid
int,rd
int);
859 create
table t2(userid
int,pmnew
int,pmtotal
int);
860 insert into t2 values(1,0,0),(2,0,0);
861 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);
862 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);
868 create
table t1 (s1
char(5));
869 -- error ER_OPERAND_COLUMNS
870 select (select
'a',
'b' from t1
union select
'a',
'b' from t1) from t1;
871 insert into t1 values (
'tttt');
872 select * from t1 where (
'a',
'b')=(select
'a',
'b' from t1
union select 'a','b' from t1);
873 explain extended (select * from t1);
878 # IN optimisation test results
882 insert into t1 values (
'a1'),(
'a2'),(
'a3');
883 insert into t2 values (
'a1'),(
'a2');
884 select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
885 select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
886 select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
887 select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 <
'a2') from t1;
888 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
889 explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
890 explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
891 explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
895 # correct ALL optimisation
897 create
table t2 (a
int, b
int);
898 create
table t3 (a
int);
899 insert into t3 values (6),(7),(3);
900 select * from t3 where a >= all (select b from t2);
901 explain extended select * from t3 where a >= all (select b from t2);
902 select * from t3 where a >= some (select b from t2);
903 explain extended select * from t3 where a >= some (select b from t2);
904 select * from t3 where a >= all (select b from t2
group by 1);
905 explain extended select * from t3 where a >= all (select b from t2
group by 1);
906 select * from t3 where a >= some (select b from t2
group by 1);
907 explain extended select * from t3 where a >= some (select b from t2
group by 1);
908 select * from t3 where NULL >= any (select b from t2);
909 explain extended select * from t3 where NULL >= any (select b from t2);
910 select * from t3 where NULL >= any (select b from t2
group by 1);
911 explain extended select * from t3 where NULL >= any (select b from t2
group by 1);
912 select * from t3 where NULL >= some (select b from t2);
913 explain extended select * from t3 where NULL >= some (select b from t2);
914 select * from t3 where NULL >= some (select b from t2
group by 1);
915 explain extended select * from t3 where NULL >= some (select b from t2
group by 1);
917 # optimized static ALL/ANY with grouping
919 insert into t2 values (2,2), (2,1), (3,3), (3,1);
920 select * from t3 where a > all (select max(b) from t2
group by a);
921 explain extended select * from t3 where a > all (select max(b) from t2
group by a);
925 # correct used_tables()
928 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 ;
929 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());
930 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;
931 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);
932 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 ;
933 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);
934 CREATE
TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
935 INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily
Check List'),(2, 'Weekly Status');
936 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;
937 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;
938 drop
table t1,t2,t3,t4;
943 CREATE
TABLE t1 (
id int(11)
default NULL) ENGINE=MyISAM CHARSET=latin1;
944 INSERT INTO t1 VALUES (1),(5);
945 CREATE
TABLE t2 (
id int(11)
default NULL) ENGINE=MyISAM CHARSET=latin1;
946 INSERT INTO t2 VALUES (2),(6);
947 -- error ER_OPERAND_COLUMNS
948 select * from t1 where (1,2,6) in (select * from t2);
952 # DO and SET with errors
954 create
table t1 (s1
int);
955 insert into t1 values (1);
956 insert into t1 values (2);
957 -- error ER_SUBQUERY_NO_1_ROW
958 set sort_buffer_size = (select s1 from t1);
959 do (select * from t1);
963 # optimized ALL/ANY with union
965 create
table t1 (s1
char);
966 insert into t1 values (
'e');
967 select * from t1 where
'f' > any (select s1 from t1);
968 select * from t1 where
'f' > any (select s1 from t1
union select s1 from t1);
969 explain extended select * from t1 where
'f' > any (select s1 from t1
union select s1 from t1);
973 # filesort in subquery (restoring join_tab)
975 CREATE
TABLE t1 (number
char(11) NOT NULL
default '') ENGINE=MyISAM CHARSET=latin1;
976 INSERT INTO t1 VALUES (
'69294728265'),(
'18621828126'),(
'89356874041'),(
'95895001874');
977 CREATE
TABLE t2 (
code char(5) NOT NULL
default '',UNIQUE
KEY code (
code)) ENGINE=MyISAM CHARSET=latin1;
978 INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
979 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;
983 # unresolved field error
985 create
table t1 (s1
int);
986 create
table t2 (s1
int);
987 -- error ER_BAD_FIELD_ERROR
988 select * from t1 where (select count(*) from t2 where t1.s2) = 1;
989 -- error ER_BAD_FIELD_ERROR
990 select * from t1 where (select count(*) from t2
group by t1.s2) = 1;
991 -- error ER_BAD_FIELD_ERROR
992 select count(*) from t2
group by t1.s2;
996 # fix_fields() in add_ref_to_table_cond()
998 CREATE
TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY
KEY (COLA, COLB));
999 CREATE
TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY
KEY (COLA));
1000 INSERT INTO t1 VALUES (1,1,
'1A3240'), (1,2,
'4W2365');
1001 INSERT INTO t2 VALUES (100, 200,
'C');
1002 SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC =
'C' LIMIT 1);
1005 CREATE
TABLE t1 (a
int(1));
1006 INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1007 SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1012 # Bug#2198 SELECT INTO OUTFILE (with Sub-Select) Problem
1015 create
table t1 (a
int, b decimal(13, 3));
1016 insert into t1 values (1, 0.123);
1017 let $outfile_abs= $MYSQLTEST_VARDIR/tmp/subselect.out.file.1;
1018 let $outfile_rel= ../../tmp/subselect.out.file.1;
1020 --remove_file $outfile_abs
1021 eval select a, (select max(b) from t1) into outfile "$outfile_rel" from t1;
1023 eval load data infile "$outfile_rel" into
table t1;
1024 --remove_file $outfile_abs
1030 # Bug#2479 dependant subquery with limit crash
1034 `
id` int(11) NOT NULL auto_increment,
1035 `id_cns` tinyint(3)
unsigned NOT NULL default '0',
1036 `tipo` enum('','UNO','DUE') NOT NULL default '',
1037 `anno_dep` smallint(4)
unsigned zerofill NOT NULL default '0000',
1038 `particolare` mediumint(8)
unsigned NOT NULL default '0',
1039 `generale` mediumint(8)
unsigned NOT NULL default '0',
1040 `bis` tinyint(3)
unsigned NOT NULL default '0',
1042 UNIQUE
KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1043 UNIQUE
KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1045 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);
1047 `
id` tinyint(3)
unsigned NOT NULL auto_increment,
1048 `max_anno_dep` smallint(6)
unsigned NOT NULL default '0',
1051 INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1053 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;
1060 create
table t1 (a
int);
1061 insert into t1 values (1), (2), (3);
1062 SET SQL_SELECT_LIMIT=1;
1063 select sum(a) from (select * from t1) as a;
1064 select 2 in (select * from t1);
1065 SET SQL_SELECT_LIMIT=default;
1070 # Bug#3118 subselect + order by
1073 CREATE
TABLE t1 (a
int, b
int, INDEX (a));
1074 INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1075 SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1)
ORDER BY b;
1078 # Item_cond fix field
1080 create
table t1(val varchar(10));
1081 insert into t1 values (
'aaa'), (
'bbb'),(
'eee'),(
'mmm'),(
'ppp');
1082 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%');
1086 # ref_or_null replacing with ref
1088 create
table t1 (
id int not null, text varchar(20) not null
default '', primary key (
id));
1089 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');
1090 select * from t1 where
id not in (select
id from t1 where
id < 8);
1091 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);
1092 explain extended select * from t1 where
id not in (select
id from t1 where
id < 8);
1093 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);
1094 insert into t1 (
id, text) values (1000, 'text1000'), (1001, 'text1001');
1095 create
table t2 (
id int not null, text varchar(20) not null default '', primary key (
id));
1096 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');
1097 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);
1098 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);
1102 # Static tables & rund() in subqueries
1104 create
table t1 (a
int);
1105 insert into t1 values (1);
1106 explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1111 # Bug#3188 Ambiguous Column in Subselect crashes server
1113 create
table t1(
id int);
1114 create
table t2(
id int);
1115 create
table t3(flag
int);
1116 -- error ER_PARSE_ERROR
1117 select (select * from t3 where
id not null) from t1, t2;
1118 drop
table t1,t2,t3;
1122 # aggregate functions (Bug#3505 Wrong results on use of ORDER BY with subqueries)
1124 CREATE
TABLE t1 (
id INT);
1125 CREATE
TABLE t2 (
id INT);
1126 INSERT INTO t1 VALUES (1), (2);
1127 INSERT INTO t2 VALUES (1);
1128 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);
1129 SELECT
id, ( SELECT COUNT(t.
id) FROM t2 AS t WHERE t.
id = t1.
id ) AS c FROM t1 LEFT
JOIN t2 USING (
id);
1130 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;
1131 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;
1137 CREATE
TABLE t1 ( a
int, b
int );
1138 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1139 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1140 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1141 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1142 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1143 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1144 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1145 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1146 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1147 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1148 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1149 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1150 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1152 ALTER
TABLE t1 ADD INDEX (a);
1153 SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1154 SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1155 SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1156 SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1157 SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1158 SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1159 SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1160 SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1161 SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1162 SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1163 SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1164 SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1165 # having clause test
1166 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
1167 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
1168 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
1169 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
1170 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
1171 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
1172 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
1173 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
1174 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
1175 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
1176 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
1177 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
1179 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1180 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1181 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1182 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1183 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1184 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1185 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1186 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1187 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1188 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1189 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1190 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
1191 # union + having test
1192 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1193 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1194 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1195 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1196 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1197 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1198 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1199 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1200 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1201 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1202 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1203 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1205 # < > >= <= and = ALL/ <> ANY do not support row operation
1206 -- error ER_OPERAND_COLUMNS
1207 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
1208 -- error ER_OPERAND_COLUMNS
1209 SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1210 -- error ER_OPERAND_COLUMNS
1211 SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1212 -- error ER_OPERAND_COLUMNS
1213 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
1214 -- error ER_OPERAND_COLUMNS
1215 SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1216 -- error ER_OPERAND_COLUMNS
1217 SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1218 -- error ER_OPERAND_COLUMNS
1219 SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
1220 -- error ER_OPERAND_COLUMNS
1221 SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
1222 # following should be converted to IN
1223 -- error ER_OPERAND_COLUMNS
1224 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
1225 -- error ER_OPERAND_COLUMNS
1226 SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1227 SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1228 -- error ER_OPERAND_COLUMNS
1229 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
1230 -- error ER_OPERAND_COLUMNS
1231 SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1232 SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1233 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
1234 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
1235 SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
1236 SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
1237 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);
1238 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);
1239 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);
1240 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);
1241 # without optimisation
1242 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1243 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1244 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1245 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1246 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1247 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2
group by a);
1248 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1249 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1250 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1251 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1252 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1253 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2
group by a);
1254 # without optimisation + having
1255 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1
group by a HAVING a = 2);
1256 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1
group by a HAVING a = 2);
1257 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1
group by a HAVING a = 2);
1258 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1
group by a HAVING a = 2);
1259 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1
group by a HAVING a = 2);
1260 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1
group by a HAVING a = 2);
1261 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1
group by a HAVING a = 2);
1262 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1
group by a HAVING a = 2);
1263 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1
group by a HAVING a = 2);
1264 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1
group by a HAVING a = 2);
1265 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1
group by a HAVING a = 2);
1266 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1
group by a HAVING a = 2);
1267 # EXISTS in string contence
1268 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a),
'-') from t1 a;
1269 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
1270 SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
1272 CREATE
TABLE t1 ( a
double, b
double );
1273 INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1274 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
1275 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
1276 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
1277 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
1278 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
1279 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
1280 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
1281 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
1282 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
1283 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
1284 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
1285 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
1287 CREATE
TABLE t1 ( a
char(1), b
char(1));
1288 INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
1289 SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
1290 SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
1291 SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
1292 SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
1293 SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
1294 SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
1295 SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
1296 SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
1297 SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
1298 SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
1299 SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
1300 SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
1305 # SELECT(EXISTS * ...)optimisation
1307 create
table t1 (a
int, b
int);
1308 insert into t1 values (1,2),(3,4);
1309 select * from t1 up where exists (select * from t1 where t1.a=up.a);
1310 explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
1315 # Bug#4102 subselect in HAVING
1318 CREATE
TABLE t1 (t1_a
int);
1319 INSERT INTO t1 VALUES (1);
1320 CREATE
TABLE t2 (t2_a
int, t2_b
int, PRIMARY
KEY (t2_a, t2_b));
1321 INSERT INTO t2 VALUES (1, 1), (1, 2);
1322 SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
1323 HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
1328 # Test problem with NULL and derived tables
1329 # (Bug#4097 JOIN with subquery causes entire column to report NULL)
1332 CREATE
TABLE t1 (
id int(11)
default NULL,name varchar(10)
default NULL);
1333 INSERT INTO t1 VALUES (1,
'Tim'),(2,
'Rebecca'),(3,NULL);
1334 CREATE
TABLE t2 (
id int(11)
default NULL, pet varchar(10)
default NULL);
1335 INSERT INTO t2 VALUES (1,
'Fido'),(2,
'Spot'),(3,
'Felix');
1336 SELECT a.*, b.* FROM (SELECT * FROM t1) AS a
JOIN t2 as b on a.id=b.id;
1341 # outer fields resolving in INSERT/REPLACE and CRETE with SELECT
1343 CREATE
TABLE t1 ( a
int, b
int );
1344 CREATE
TABLE t2 ( c
int, d
int );
1345 INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
1346 SELECT a AS abc, b FROM t1 outr WHERE b =
1347 (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1348 INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
1349 (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1351 CREATE
TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
1352 (SELECT MIN(b) FROM t1 WHERE a=outr.a);
1354 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);";
1356 deallocate prepare stmt1;
1359 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);";
1362 deallocate prepare stmt1;
1363 DROP
TABLE t1, t2, t3;
1366 # Aggregate function comparation with ALL/ANY/SOME subselect
1368 CREATE
TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1369 insert into t1 values (1);
1370 CREATE
TABLE `t2` ( `b`
int(11) default NULL, `a`
int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
1371 insert into t2 values (1,2);
1372 select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
1377 # Bug#4769 - fulltext in subselect
1379 create
table t1 (a
int not null auto_increment primary key, b varchar(40), fulltext(b));
1380 insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
1381 create
table t2 (a
int);
1382 insert into t2 values (1),(3),(2),(7);
1383 select a,b from t1 where
match(b) against ('Ball') > 0;
1384 select a from t2 where a in (select a from t1 where
match(b) against ('Ball') > 0);
1389 # Bug#5003 - like in subselect
1391 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);
1392 CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
1393 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
1394 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
1395 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
1396 INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
1397 SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
1401 # Optimized IN with compound index
1403 CREATE
TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid`
int(11) NOT NULL default '0', PRIMARY
KEY (`aid`,`bid`));
1404 CREATE
TABLE `t2` ( `aid`
int(11) NOT NULL default '0', `bid`
int(11) NOT NULL default '0', PRIMARY
KEY (`aid`,`bid`));
1405 insert into t1 values (1,1),(1,2),(2,1),(2,2);
1406 insert into t2 values (1,2),(2,2);
1407 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1408 alter
table t2 drop primary key;
1409 alter
table t2 add key KEY1 (aid, bid);
1410 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1411 alter
table t2 drop key KEY1;
1412 alter
table t2 add primary key (bid, aid);
1413 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
1417 # resolving fields of grouped outer SELECT
1419 CREATE
TABLE t1 (howmanyvalues bigint, avalue
int);
1420 INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
1421 SELECT howmanyvalues, count(*) from t1
group by howmanyvalues;
1422 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a
group by a.howmanyvalues;
1423 CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
1424 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a
group by a.howmanyvalues;
1425 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a
group by a.howmanyvalues;
1426 SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a
group by a.howmanyvalues;
1429 create
table t1 (x
int);
1430 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;
1434 # Test of correct maybe_null flag returning by subquwery for temporary table
1437 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`));
1438 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);
1439 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`)) ;
1440 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');
1441 -- error ER_BAD_FIELD_ERROR
1442 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;
1443 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;
1448 # Test for Bug#6462 Same request on same data returns different results
1449 # a.k.a. "Proper cleanup of subqueries is missing for SET and DO statements".
1451 create
table t1 (a
int not null, b
int not null, c
int, primary key (a,b));
1452 insert into t1 values (1,1,1), (2,2,2), (3,3,3);
1454 # Let us check that subquery will use covering index
1455 explain select sum(a) from t1 where b > @b;
1456 # This should not crash -debug server due to failing assertion
1457 set @a:= (select sum(a) from t1 where b > @b);
1458 # And this should not falsely report index usage
1459 explain select a from t1 where c=2;
1460 # Same for DO statement
1461 do @a:= (select sum(a) from t1 where b > @b);
1462 explain select a from t1 where c=2;
1466 # Subselect in non-select command just after connection
1467 # Disconnect new connection and switch back when test is finished
1469 connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
1471 set @got_val= (SELECT 1 FROM (SELECT
'A' as my_col) as
T1 ) ;
1476 # primary query with temporary table and subquery with grouping
1478 create
table t1 (a
int, b
int);
1479 create
table t2 (a
int, b
int);
1480 insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
1481 insert into t2 values (1,3),(2,1);
1482 select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
1486 # subqueries with full text search
1488 create
table t1 (
id int);
1489 create
table t2 (
id int, body text, fulltext (body));
1490 insert into t1 values(1),(2),(3);
1491 insert into t2 values (1,
'test'), (2,
'mysql'), (3,
'test'), (4,
'test');
1492 select count(distinct
id) from t1 where
id in (select
id from t2 where
match(body) against (
'mysql' in
boolean mode));
1496 # Equal operation under row and empty subquery
1498 create
table t1 (s1
int,s2
int);
1499 insert into t1 values (20,15);
1500 select * from t1 where ((
'a',null) <=> (select
'a',s2 from t1 where s1 = 0));
1506 create
table t1 (s1
int);
1507 insert into t1 values (1),(null);
1508 select * from t1 where s1 < all (select s1 from t1);
1509 select s1, s1 < all (select s1 from t1) from t1;
1513 # reference on changable fields from subquery
1516 Code
char(3) NOT NULL
default '',
1517 Name
char(52) NOT NULL
default '',
1518 Continent
enum(
'Asia',
'Europe',
'North America',
'Africa',
'Oceania',
'Antarctica',
'South America') NOT NULL
default 'Asia',
1519 Region
char(26) NOT NULL
default '',
1520 SurfaceArea
float(10,2) NOT NULL
default '0.00',
1521 IndepYear smallint(6)
default NULL,
1522 Population
int(11) NOT NULL
default '0',
1523 LifeExpectancy
float(3,1)
default NULL,
1524 GNP
float(10,2)
default NULL,
1525 GNPOld
float(10,2)
default NULL,
1526 LocalName
char(45) NOT NULL
default '',
1527 GovernmentForm
char(45) NOT NULL
default '',
1528 HeadOfState
char(60)
default NULL,
1529 Capital
int(11)
default NULL,
1530 Code2
char(2) NOT NULL
default ''
1532 INSERT INTO t1 VALUES (
'XXX',
'Xxxxx',
'Oceania',
'Xxxxxx',26.00,0,0,0,0,0,
'Xxxxx',
'Xxxxx',
'Xxxxx',NULL,
'XX');
1533 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');
1534 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');
1535 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');;
1537 SELECT DISTINCT Continent AS c FROM t1 outr WHERE
1538 Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
1544 # Test for Bug#7885 Server crash when 'any' subselect compared to
1545 # non-existant field.
1547 create
table t1 (a1
int);
1548 create
table t2 (b1
int);
1549 --error ER_BAD_FIELD_ERROR
1550 select * from t1 where a2 > any(select b1 from t2);
1551 select * from t1 where a1 > any(select b1 from t2);
1556 # Comparison subquery with * and row
1558 create
table t1 (a integer, b integer);
1559 select (select * from t1) = (select 1,2);
1560 select (select 1,2) = (select * from t1);
1561 # queries whih can be converted to IN
1562 select row(1,2) = ANY (select * from t1);
1563 select row(1,2) != ALL (select * from t1);
1567 # Comparison subquery and row with nested rows
1569 create
table t1 (a integer, b integer);
1570 -- error ER_OPERAND_COLUMNS
1571 select row(1,(2,2)) in (select * from t1 );
1572 -- error ER_OPERAND_COLUMNS
1573 select row(1,(2,2)) = (select * from t1 );
1574 -- error ER_OPERAND_COLUMNS
1575 select (select * from t1) = row(1,(2,2));
1579 # Forward reference detection
1581 create
table t1 (a integer);
1582 insert into t1 values (1);
1583 -- error ER_ILLEGAL_REFERENCE
1584 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1585 -- error ER_ILLEGAL_REFERENCE
1586 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1587 select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
1588 -- error ER_ILLEGAL_REFERENCE
1589 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1590 -- error ER_ILLEGAL_REFERENCE
1591 select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
1596 # Test for Bug#8218 Join does not pass string from right table
1599 categoryId
int(11) NOT NULL,
1600 courseId
int(11) NOT NULL,
1601 startDate datetime NOT NULL,
1602 endDate datetime NOT NULL,
1603 createDate datetime NOT NULL,
1604 modifyDate timestamp NOT NULL,
1605 attributes text NOT NULL
1607 INSERT INTO t1 VALUES (1,41,
'2004-02-09',
'2010-01-01',
'2004-02-09',
'2004-02-09',
''),
1608 (1,86,
'2004-08-16',
'2004-08-16',
'2004-08-16',
'2004-08-16',
''),
1609 (1,87,
'2004-08-16',
'2004-08-16',
'2004-08-16',
'2004-08-16',
''),
1610 (2,52,
'2004-03-15',
'2004-10-01',
'2004-03-15',
'2004-09-17',
''),
1611 (2,53,
'2004-03-16',
'2004-10-01',
'2004-03-16',
'2004-09-17',
''),
1612 (2,88,
'2004-08-16',
'2004-08-16',
'2004-08-16',
'2004-08-16',
''),
1613 (2,89,
'2004-08-16',
'2004-08-16',
'2004-08-16',
'2004-08-16',
''),
1614 (3,51,
'2004-02-09',
'2010-01-01',
'2004-02-09',
'2004-02-09',
''),
1615 (5,12,
'2004-02-18',
'2010-01-01',
'2004-02-18',
'2004-02-18',
'');
1618 userId
int(11) NOT NULL,
1619 courseId
int(11) NOT NULL,
1620 date datetime NOT NULL
1622 INSERT INTO t2 VALUES (5141,71,
'2003-11-18'),
1623 (5141,72,
'2003-11-25'),(5141,41,
'2004-08-06'),
1624 (5141,52,
'2004-08-06'),(5141,53,
'2004-08-06'),
1625 (5141,12,
'2004-08-06'),(5141,86,
'2004-10-21'),
1626 (5141,87,
'2004-10-21'),(5141,88,
'2004-10-21'),
1627 (5141,89,
'2004-10-22'),(5141,51,
'2004-10-26');
1631 groupId
int(11) NOT NULL,
1632 parentId
int(11) NOT NULL,
1633 startDate datetime NOT NULL,
1634 endDate datetime NOT NULL,
1635 createDate datetime NOT NULL,
1636 modifyDate timestamp NOT NULL,
1639 INSERT INTO t3 VALUES (12,9,
'1000-01-01',
'3999-12-31',
'2004-01-29',
'2004-01-29',NULL);
1642 id int(11) NOT NULL,
1643 groupTypeId
int(11) NOT NULL,
1644 groupKey varchar(50) NOT NULL,
1648 createDate datetime NOT NULL,
1649 modifyDate timestamp NOT NULL
1651 INSERT INTO t4 VALUES (9,5,
'stationer',
'stationer',0,
'Stationer',
'2004-01-29',
'2004-01-29'),
1652 (12,5,
'group2',
'group2',0,
'group2',
'2004-01-29',
'2004-01-29');
1655 userId
int(11) NOT NULL,
1656 groupId
int(11) NOT NULL,
1657 createDate datetime NOT NULL,
1658 modifyDate timestamp NOT NULL
1660 INSERT INTO t5 VALUES (5141,12,
'2004-08-06',
'2004-08-06');
1663 count(distinct t2.userid) pass,
1665 count(t2.courseid) crse,
1668 date_format(date, '%b%y') as colhead
1670 join t1 on t2.courseid=t1.courseid
1683 select t4.
id as parentid,
1684 t4.name as parentgroup,
1686 t4.name as groupname,
1689 ) as gin on t5.groupid=gin.childid
1690 ) as groupstuff on t2.userid = groupstuff.userid
1692 groupstuff.groupname, colhead , t2.courseid;
1694 drop
table t1, t2, t3, t4, t5;
1698 # Transformation in left expression of subquery (Bug#8888)
1700 create
table t1 (a
int);
1701 insert into t1 values (1), (2), (3);
1702 SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
1705 # subselect into HAVING clause (code covarage improvement)
1707 create
table t1 (a
int);
1708 create
table t2 (a
int);
1709 insert into t1 values (1),(2);
1710 insert into t2 values (0),(1),(2),(3);
1711 select a from t2 where a in (select a from t1);
1712 select a from t2 having a in (select a from t1);
1713 prepare stmt1 from
"select a from t2 where a in (select a from t1)";
1716 deallocate prepare stmt1;
1717 prepare stmt1 from
"select a from t2 having a in (select a from t1)";
1720 deallocate prepare stmt1;
1724 # single row subqueries and row operations (code covarage improvement)
1726 create
table t1 (a
int, b
int);
1727 insert into t1 values (1,2);
1728 -- error ER_OPERAND_COLUMNS
1729 select 1 = (select * from t1);
1730 -- error ER_OPERAND_COLUMNS
1731 select (select * from t1) = 1;
1732 -- error ER_OPERAND_COLUMNS
1733 select (1,2) = (select a from t1);
1734 -- error ER_OPERAND_COLUMNS
1735 select (select a from t1) = (1,2);
1736 -- error ER_OPERAND_COLUMNS
1737 select (1,2,3) = (select * from t1);
1738 -- error ER_OPERAND_COLUMNS
1739 select (select * from t1) = (1,2,3);
1744 # Item_int_with_ref check (Bug#10020)
1747 `itemid` bigint(20) unsigned NOT NULL auto_increment,
1748 `sessionid` bigint(20)
unsigned default NULL,
1749 `time`
int(10)
unsigned NOT NULL default '0',
1750 `
type` set('
A','D','
E','F','G','I','L','N','
U') collate latin1_general_ci NOT
1752 `data` text collate latin1_general_ci NOT NULL,
1753 PRIMARY
KEY (`itemid`)
1754 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1755 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
1757 `sessionid` bigint(20)
unsigned NOT NULL auto_increment,
1758 `pid`
int(10)
unsigned NOT NULL default '0',
1759 `date`
int(10)
unsigned NOT NULL default '0',
1760 `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1761 PRIMARY
KEY (`sessionid`)
1762 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
1763 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1764 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;
1768 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
1769 create
table t1 (fld
enum(
'0',
'1'));
1770 insert into t1 values (
'1');
1771 select * from (select max(fld) from t1) as foo;
1776 # Bug#11479 subquery over left join with an empty inner table
1779 CREATE
TABLE t1 (a
int);
1780 CREATE
TABLE t2 (a
int, b
int);
1781 CREATE
TABLE t3 (b
int NOT NULL);
1782 INSERT INTO t1 VALUES (1), (2), (3), (4);
1783 INSERT INTO t2 VALUES (1,10), (3,30);
1785 SELECT * FROM t2 LEFT
JOIN t3 ON t2.b=t3.b
1786 WHERE t3.b IS NOT NULL OR t2.a > 10;
1788 WHERE t1.a NOT IN (SELECT a FROM t2 LEFT
JOIN t3 ON t2.b=t3.b
1789 WHERE t3.b IS NOT NULL OR t2.a > 10);
1791 DROP
TABLE t1,t2,t3;
1795 # Bug#18503 Queries with a quantified subquery returning empty set may
1796 # return a wrong result.
1798 CREATE
TABLE t1 (f1 INT);
1799 CREATE
TABLE t2 (f2 INT);
1800 INSERT INTO t1 VALUES (1);
1801 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
1802 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
1803 INSERT INTO t2 VALUES (1);
1804 INSERT INTO t2 VALUES (2);
1805 SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
1810 # Bug#16302 Quantified subquery without any tables gives wrong results
1812 select 1 from dual where 1 < any (select 2);
1813 select 1 from dual where 1 < all (select 2);
1814 select 1 from dual where 2 > any (select 1);
1815 select 1 from dual where 2 > all (select 1);
1816 select 1 from dual where 1 < any (select 2 from dual);
1817 select 1 from dual where 1 < all (select 2 from dual where 1!=1);
1820 # Bug#20975 Wrong query results for subqueries within NOT
1821 create
table t1 (s1
char);
1822 insert into t1 values (1),(2);
1824 select * from t1 where (s1 < any (select s1 from t1));
1825 select * from t1 where not (s1 < any (select s1 from t1));
1827 select * from t1 where (s1 < ALL (select s1+1 from t1));
1828 select * from t1 where not(s1 < ALL (select s1+1 from t1));
1830 select * from t1 where (s1+1 = ANY (select s1 from t1));
1831 select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
1833 select * from t1 where (s1 = ALL (select s1/s1 from t1));
1834 select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
1839 # Bug#16255 Subquery in where
1842 retailerID varchar(8) NOT NULL,
1843 statusID
int(10)
unsigned NOT NULL,
1844 changed datetime NOT NULL,
1845 UNIQUE
KEY retailerID (retailerID, statusID, changed)
1848 INSERT INTO t1 VALUES(
"0026",
"1",
"2005-12-06 12:18:56");
1849 INSERT INTO t1 VALUES(
"0026",
"2",
"2006-01-06 12:25:53");
1850 INSERT INTO t1 VALUES(
"0037",
"1",
"2005-12-06 12:18:56");
1851 INSERT INTO t1 VALUES(
"0037",
"2",
"2006-01-06 12:25:53");
1852 INSERT INTO t1 VALUES(
"0048",
"1",
"2006-01-06 12:37:50");
1853 INSERT INTO t1 VALUES(
"0059",
"1",
"2006-01-06 12:37:50");
1856 where (r1.retailerID,(r1.changed)) in
1857 (SELECT r2.retailerId,(max(changed)) from t1 r2
1858 group by r2.retailerId);
1863 # Bug#21180 Subselect with index for both WHERE and ORDER BY
1864 # produces empty result
1866 create
table t1(a
int, primary key (a));
1867 insert into t1 values (10);
1869 create
table t2 (a
int primary key, b varchar(32), c
int, unique key b(c, b));
1870 insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
1872 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT
JOIN t2 r
1873 ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1874 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1875 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT
JOIN t2 r
1876 ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1877 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
1879 explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT
JOIN t2 r
1880 ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1881 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1882 SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT
JOIN t2 r
1883 ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
1884 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
1890 # Bug#21853 assert failure for a grouping query with
1891 # an ALL/ANY quantified subquery in HAVING
1895 field1
int NOT NULL,
1896 field2
int NOT NULL,
1897 field3
int NOT NULL,
1898 PRIMARY
KEY (field1,field2,field3)
1901 fieldA
int NOT NULL,
1902 fieldB
int NOT NULL,
1903 PRIMARY
KEY (fieldA,fieldB)
1906 INSERT INTO t1 VALUES
1907 (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
1908 INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
1910 SELECT field1, field2, COUNT(*)
1911 FROM t1 GROUP BY field1, field2;
1913 SELECT field1, field2
1915 GROUP BY field1, field2
1916 HAVING COUNT(*) >= ALL (SELECT fieldB
1917 FROM t2 WHERE fieldA = field1);
1918 SELECT field1, field2
1920 GROUP BY field1, field2
1921 HAVING COUNT(*) < ANY (SELECT fieldB
1922 FROM t2 WHERE fieldA = field1);
1928 # Bug#23478 not top-level IN subquery returning a non-empty result set
1929 # with possible NULL values by index access from the outer query
1932 CREATE
TABLE t1(a
int, INDEX (a));
1933 INSERT INTO t1 VALUES (1), (3), (5), (7);
1934 INSERT INTO t1 VALUES (NULL);
1936 CREATE
TABLE t2(a
int);
1937 INSERT INTO t2 VALUES (1),(2),(3);
1939 EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
1940 SELECT a, a IN (SELECT a FROM t1) FROM t2;
1946 # Bug#11302 getObject() returns a String for a sub-query of type datetime
1948 CREATE
TABLE t1 (a DATETIME);
1949 INSERT INTO t1 VALUES (
'1998-09-23'), (
'2003-03-25');
1951 CREATE
TABLE t2 AS SELECT
1952 (SELECT a FROM t1 WHERE a <
'2000-01-01') AS sub_a
1953 FROM t1 WHERE a > '2000-01-01';
1954 SHOW CREATE
TABLE t2;
1956 CREATE
TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
1957 SHOW CREATE
TABLE t3;
1959 DROP
TABLE t1,t2,t3;
1963 # Bug#24670 subquery witout tables but with a WHERE clause
1966 CREATE
TABLE t1 (a
int);
1967 INSERT INTO t1 VALUES (1), (2);
1969 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
1970 SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
1971 EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
1977 # Bug#24653 sorting by expressions containing subselects
1978 # that return more than one row
1981 CREATE
TABLE t1 (a
int);
1982 INSERT INTO t1 VALUES (2), (4), (1), (3);
1984 CREATE
TABLE t2 (b
int, c
int);
1985 INSERT INTO t2 VALUES
1986 (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
1988 SELECT a FROM t1
ORDER BY (SELECT c FROM t2 WHERE b > 2 );
1989 --error ER_SUBQUERY_NO_1_ROW
1990 SELECT a FROM t1
ORDER BY (SELECT c FROM t2 WHERE b > 1);
1991 SELECT a FROM t1
ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
1992 --error ER_SUBQUERY_NO_1_ROW
1993 SELECT a FROM t1
ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
1995 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
1996 --error ER_SUBQUERY_NO_1_ROW
1997 SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
2000 SELECT a FROM t1 GROUP BY a
2001 HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
2002 (SELECT c FROM t2 WHERE c=a AND b > 2
ORDER BY b)) > 3;
2003 --error ER_SUBQUERY_NO_1_ROW
2004 SELECT a FROM t1 GROUP BY a
2005 HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
2006 (SELECT c FROM t2 WHERE c=a AND b > 2
ORDER BY b)) > 3;
2008 SELECT a FROM t1 GROUP BY a
2009 HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2010 (SELECT c FROM t2 WHERE c=a AND b > 2
ORDER BY b)) > 3;
2011 --error ER_SUBQUERY_NO_1_ROW
2012 SELECT a FROM t1 GROUP BY a
2013 HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
2014 (SELECT c FROM t2 WHERE c=a AND b > 1
ORDER BY b)) > 3;
2017 ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
2018 (SELECT c FROM t2 WHERE c=a AND b > 2
ORDER BY b));
2019 --error ER_SUBQUERY_NO_1_ROW
2021 ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
2022 (SELECT c FROM t2 WHERE c=a AND b > 1
ORDER BY b));
2025 ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2026 (SELECT c FROM t2 WHERE c=a AND b > 2
ORDER BY b));
2027 --error ER_SUBQUERY_NO_1_ROW
2029 ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
2030 (SELECT c FROM t2 WHERE c=a AND b > 1
ORDER BY b));
2037 #decimal-related tests
2039 create
table t1 (df decimal(5,1));
2040 insert into t1 values(1.1);
2041 insert into t1 values(2.2);
2043 select * from t1 where df <= all (select avg(df) from t1
group by df);
2044 select * from t1 where df >= all (select avg(df) from t1
group by df);
2047 create
table t1 (df decimal(5,1));
2048 insert into t1 values(1.1);
2049 select 1.1 * exists(select * from t1);
2053 grp
int(11)
default NULL,
2054 a decimal(10,2)
default NULL);
2056 insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
2058 select min(a) from t1
group by grp;
2063 # Test for Bug#9338 lame substitution of c1 instead of c2
2066 CREATE
table t1 ( c1 integer );
2067 INSERT INTO t1 VALUES ( 1 );
2068 INSERT INTO t1 VALUES ( 2 );
2069 INSERT INTO t1 VALUES ( 3 );
2071 CREATE
TABLE t2 ( c2 integer );
2072 INSERT INTO t2 VALUES ( 1 );
2073 INSERT INTO t2 VALUES ( 4 );
2074 INSERT INTO t2 VALUES ( 5 );
2076 SELECT * FROM t1 LEFT
JOIN t2 ON c1 = c2 WHERE c2 IN (1);
2078 SELECT * FROM t1 LEFT
JOIN t2 ON c1 = c2
2079 WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
2085 # Test for Bug#9516 wrong evaluation of not_null_tables attribute in SQ
2087 CREATE
TABLE t1 ( c1 integer );
2088 INSERT INTO t1 VALUES ( 1 );
2089 INSERT INTO t1 VALUES ( 2 );
2090 INSERT INTO t1 VALUES ( 3 );
2091 INSERT INTO t1 VALUES ( 6 );
2093 CREATE
TABLE t2 ( c2 integer );
2094 INSERT INTO t2 VALUES ( 1 );
2095 INSERT INTO t2 VALUES ( 4 );
2096 INSERT INTO t2 VALUES ( 5 );
2097 INSERT INTO t2 VALUES ( 6 );
2099 CREATE
TABLE t3 ( c3 integer );
2100 INSERT INTO t3 VALUES ( 7 );
2101 INSERT INTO t3 VALUES ( 8 );
2103 SELECT c1,c2 FROM t1 LEFT
JOIN t2 ON c1 = c2
2104 WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
2106 DROP
TABLE t1,t2,t3;
2110 # Item_int_with_ref check (Bug#10020)
2113 `itemid` bigint(20) unsigned NOT NULL auto_increment,
2114 `sessionid` bigint(20)
unsigned default NULL,
2115 `time`
int(10)
unsigned NOT NULL default '0',
2116 `
type` set('A','D','E','F','G','I','L','N','
U') collate latin1_general_ci NOT
2118 `data` text collate latin1_general_ci NOT NULL,
2119 PRIMARY
KEY (`itemid`)
2120 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2121 INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2123 `sessionid` bigint(20)
unsigned NOT NULL auto_increment,
2124 `pid`
int(10)
unsigned NOT NULL default '0',
2125 `date`
int(10)
unsigned NOT NULL default '0',
2126 `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2127 PRIMARY
KEY (`sessionid`)
2128 ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2129 INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2130 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;
2135 # Correct building of equal fields list (do not include outer
2136 # fields) (Bug#6384)
2138 CREATE
TABLE t1 (EMPNUM CHAR(3));
2139 CREATE
TABLE t2 (EMPNUM CHAR(3) );
2140 INSERT INTO t1 VALUES (
'E1'),(
'E2');
2141 INSERT INTO t2 VALUES (
'E1');
2143 WHERE t1.EMPNUM NOT IN
2146 WHERE t1.EMPNUM = t2.EMPNUM);
2152 # Test for Bug#11487 range access in a subquery
2155 CREATE
TABLE t1(select_id BIGINT, values_id BIGINT);
2156 INSERT INTO t1 VALUES (1, 1);
2157 CREATE
TABLE t2 (select_id BIGINT, values_id BIGINT,
2158 PRIMARY
KEY(select_id,values_id));
2159 INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
2161 SELECT values_id FROM t1
2162 WHERE values_id IN (SELECT values_id FROM t2
2163 WHERE select_id IN (1, 0));
2164 SELECT values_id FROM t1
2165 WHERE values_id IN (SELECT values_id FROM t2
2166 WHERE select_id BETWEEN 0 AND 1);
2167 SELECT values_id FROM t1
2168 WHERE values_id IN (SELECT values_id FROM t2
2169 WHERE select_id = 0 OR select_id = 1);
2174 # Bug#11821 Select from subselect using aggregate function on an enum segfaults
2175 create
table t1 (fld
enum(
'0',
'1'));
2176 insert into t1 values (
'1');
2177 select * from (select max(fld) from t1) as foo;
2182 # Test for Bug#11762 subquery with an aggregate function in HAVING
2185 CREATE
TABLE t1 (a
int, b
int);
2186 CREATE
TABLE t2 (c
int, d
int);
2187 CREATE
TABLE t3 (e
int);
2189 INSERT INTO t1 VALUES
2190 (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
2191 INSERT INTO t2 VALUES
2192 (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
2193 INSERT INTO t3 VALUES (10), (30), (10), (20) ;
2195 SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
2199 SELECT a FROM t1 GROUP BY a
2200 HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
2201 SELECT a FROM t1 GROUP BY a
2202 HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
2203 SELECT a FROM t1 GROUP BY a
2204 HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
2205 SELECT a FROM t1 GROUP BY a
2206 HAVING a IN (SELECT c FROM t2
2207 WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2208 SELECT a FROM t1 GROUP BY a
2209 HAVING a IN (SELECT c FROM t2
2210 WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2211 SELECT a FROM t1 GROUP BY a
2212 HAVING a IN (SELECT c FROM t2
2213 WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
2214 SELECT a FROM t1 GROUP BY a
2215 HAVING a IN (SELECT c FROM t2
2216 WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
2217 SELECT a FROM t1 GROUP BY a
2218 HAVING a IN (SELECT c FROM t2
2219 WHERE MIN(b) < d AND
2220 EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
2222 SELECT a, SUM(a) FROM t1 GROUP BY a;
2225 WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
2226 SELECT a FROM t1 GROUP BY a
2227 HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
2231 EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
2234 EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
2236 SELECT t1.a FROM t1 GROUP BY t1.a
2237 HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
2238 HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2239 HAVING SUM(t1.a+t2.c) < t3.e/4));
2240 SELECT t1.a FROM t1 GROUP BY t1.a
2241 HAVING t1.a > ALL(SELECT t2.c FROM t2
2242 WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
2243 HAVING SUM(t1.a+t2.c) < t3.e/4));
2244 -- error ER_INVALID_GROUP_FUNC_USE
2245 SELECT t1.a FROM t1 GROUP BY t1.a
2246 HAVING t1.a > ALL(SELECT t2.c FROM t2
2247 WHERE EXISTS(SELECT t3.e FROM t3
2248 WHERE SUM(t1.a+t2.c) < t3.e/4));
2249 -- error ER_INVALID_GROUP_FUNC_USE
2250 SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
2252 SELECT t1.a FROM t1 GROUP BY t1.a
2253 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2254 HAVING AVG(t2.c+SUM(t1.b)) > 20);
2255 SELECT t1.a FROM t1 GROUP BY t1.a
2256 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2257 HAVING AVG(SUM(t1.b)) > 20);
2259 SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
2260 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
2261 HAVING t2.c+sum > 20);
2263 DROP
TABLE t1,t2,t3;
2267 # Bug#17366 Unchecked Item_int results in server crash
2269 create
table t1( f1
int,f2
int);
2270 insert into t1 values (1,1),(2,2);
2271 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';
2276 # Bug#18306 server crash on delete using subquery.
2279 create
table t1 (c
int, key(c));
2280 insert into t1 values (1142477582), (1142455969);
2281 create
table t2 (a
int, b
int);
2282 insert into t2 values (2, 1), (1, 0);
2283 delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
2288 # Bug#7549 Missing error message for invalid view selection with subquery
2291 CREATE
TABLE t1 (a INT);
2293 --error ER_BAD_FIELD_ERROR
2294 CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2295 --error ER_BAD_FIELD_ERROR
2296 CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
2297 --error ER_BAD_FIELD_ERROR
2298 SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
2304 # Bug#19077 A nested materialized derived table is used before being populated.
2306 create
table t1 (i
int, j bigint);
2307 insert into t1 values (1, 2), (2, 2), (3, 2);
2308 select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
2313 # Bug#19700 subselect returning BIGINT always returned it as SIGNED
2315 CREATE
TABLE t1 (i BIGINT UNSIGNED);
2316 INSERT INTO t1 VALUES (10000000000000000000); # > MAX SIGNED BIGINT 9323372036854775807
2317 INSERT INTO t1 VALUES (1);
2319 CREATE
TABLE t2 (i BIGINT UNSIGNED);
2320 INSERT INTO t2 VALUES (10000000000000000000); # same as first
table
2321 INSERT INTO t2 VALUES (1);
2324 SELECT t1.i FROM t1
JOIN t2 ON t1.i = t2.i;
2327 SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
2330 SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
2337 # Bug#20519 subselect with LIMIT M, N
2341 id bigint(20)
unsigned NOT NULL auto_increment,
2342 name varchar(255) NOT NULL,
2345 INSERT INTO t1 VALUES
2346 (1,
'Balazs'), (2,
'Joe'), (3,
'Frank');
2349 id bigint(20)
unsigned NOT NULL auto_increment,
2350 mid bigint(20)
unsigned NOT NULL,
2354 INSERT INTO t2 VALUES
2355 (1, 1,
'2006-03-30'), (2, 2,
'2006-04-06'), (3, 3,
'2006-04-13'),
2356 (4, 2,
'2006-04-20'), (5, 1,
'2006-05-01');
2359 (SELECT date FROM t2 WHERE mid = t1.id
2360 ORDER BY date DESC LIMIT 0, 1) AS date_last,
2361 (SELECT date FROM t2 WHERE mid = t1.id
2362 ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
2365 (SELECT COUNT(*) FROM t2 WHERE mid = t1.
id
2366 ORDER BY date DESC LIMIT 1, 1) AS date_count
2369 (SELECT date FROM t2 WHERE mid = t1.
id
2370 ORDER BY date DESC LIMIT 0, 1) AS date_last,
2371 (SELECT date FROM t2 WHERE mid = t1.
id
2372 ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
2378 # Bug#20869 subselect with range access by DESC
2382 i1
int(11) NOT NULL
default '0',
2383 i2
int(11) NOT NULL
default '0',
2384 t datetime NOT NULL
default '0000-00-00 00:00:00',
2385 PRIMARY
KEY (i1,i2,t)
2387 INSERT INTO t1 VALUES
2388 (24,1,
'2005-03-03 16:31:31'),(24,1,
'2005-05-27 12:40:07'),
2389 (24,1,
'2005-05-27 12:40:08'),(24,1,
'2005-05-27 12:40:10'),
2390 (24,1,
'2005-05-27 12:40:25'),(24,1,
'2005-05-27 12:40:30'),
2391 (24,2,
'2005-03-03 13:43:05'),(24,2,
'2005-03-03 16:23:31'),
2392 (24,2,
'2005-03-03 16:31:30'),(24,2,
'2005-05-27 12:37:02'),
2393 (24,2,
'2005-05-27 12:40:06');
2396 i1
int(11) NOT NULL
default '0',
2397 i2
int(11) NOT NULL
default '0',
2398 t datetime
default NULL,
2401 INSERT INTO t2 VALUES (24,1,
'2006-06-20 12:29:40');
2405 WHERE t1.t = (SELECT t1.t FROM t1
2406 WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
2407 ORDER BY t1.t DESC LIMIT 1);
2409 WHERE t1.t = (SELECT t1.t FROM t1
2410 WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
2411 ORDER BY t1.t DESC LIMIT 1);
2417 # Bug#14654 Cannot select from the same table twice within a UNION statement
2419 CREATE
TABLE t1 (i INT);
2421 (SELECT i FROM t1) UNION (SELECT i FROM t1);
2422 SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
2424 (SELECT i FROM t1) UNION
2429 --error ER_PARSE_ERROR
2431 WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
2434 --error ER_PARSE_ERROR
2435 explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
2438 explain select * from t1 where not exists
2439 ((select t11.i from t1 t11) union (select t12.i from t1 t12));
2445 # Bug#21798 memory leak during query execution with subquery in column
2446 # list using a function
2448 CREATE
TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY
KEY (b));
2449 insert into t1 (a) values (FLOOR(rand() * 100));
2450 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2451 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2452 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2453 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2454 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2455 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2456 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2457 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2458 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2459 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2460 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2461 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2462 insert into t1 (a) select FLOOR(rand() * 100) from t1;
2465 (SELECT REPEAT(' ',250) FROM t1 i1
2466 WHERE i1.b=t1.a
ORDER BY RAND() LIMIT 1) AS a
2467 FROM t1
ORDER BY a LIMIT 5;
2472 # Bug#21540 Subqueries with no from and aggregate functions return
2474 CREATE
TABLE t1 (a INT, b INT);
2475 CREATE
TABLE t2 (a INT);
2476 INSERT INTO t2 values (1);
2477 INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
2478 SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
2479 SELECT (SELECT COUNT(DISTINCT t1.b) from t2
union select 1 from t2 where 12 < 3)
2480 FROM t1 GROUP BY t1.a;
2481 SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
2482 SELECT COUNT(DISTINCT t1.b),
2483 (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
2484 FROM t1 GROUP BY t1.a;
2487 SELECT COUNT(DISTINCT t1.b)
2490 FROM t1 GROUP BY t1.a;
2494 SELECT COUNT(DISTINCT t1.b)
2497 FROM t1 GROUP BY t1.a LIMIT 1)
2504 # Bug#21727 Correlated subquery that requires filesort:
2505 # slow with big sort_buffer_size
2508 CREATE
TABLE t1 (a
int, b
int, PRIMARY
KEY (b));
2509 CREATE
TABLE t2 (x
int auto_increment, y
int, z
int,
2510 PRIMARY
KEY (x), FOREIGN
KEY (y) REFERENCES t1 (b));
2511 create
table t3 (a
int);
2512 insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
2514 insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
2515 from t3 A, t3 B, t3 C, t3 D where D.a<3;
2516 insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
2519 SET SESSION sort_buffer_size = 32 * 1024;
2520 SELECT SQL_NO_CACHE COUNT(*)
2521 FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b
ORDER BY z DESC LIMIT 1) c
2524 SET SESSION sort_buffer_size = 8 * 1024 * 1024;
2525 SELECT SQL_NO_CACHE COUNT(*)
2526 FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b
ORDER BY z DESC LIMIT 1) c
2529 DROP
TABLE t1,t2,t3;
2533 # Bug#25219 EXIST subquery with UNION over a mix of
2534 # correlated and uncorrelated selects
2537 CREATE
TABLE t1 (
id char(4) PRIMARY
KEY, c
int);
2538 CREATE
TABLE t2 (c
int);
2540 INSERT INTO t1 VALUES (
'aa', 1);
2541 INSERT INTO t2 VALUES (1);
2544 WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2546 SELECT c from t2 WHERE c=t1.c);
2548 INSERT INTO t1 VALUES (
'bb', 2), (
'cc', 3), (
'dd',1);
2551 WHERE EXISTS (SELECT c FROM t2 WHERE c=1
2553 SELECT c from t2 WHERE c=t1.c);
2555 INSERT INTO t2 VALUES (2);
2556 CREATE
TABLE t3 (c
int);
2557 INSERT INTO t3 VALUES (1);
2560 WHERE EXISTS (SELECT t2.c FROM t2
JOIN t3 ON t2.c=t3.c WHERE t2.c=1
2562 SELECT c from t2 WHERE c=t1.c);
2564 DROP
TABLE t1,t2,t3;
2568 # Bug#23800 Outer fields in correlated subqueries is used in a temporary
2569 # table created for sorting.
2571 CREATE
TABLE t1(f1
int);
2572 CREATE
TABLE t2(f2
int, f21
int, f3 timestamp);
2573 INSERT INTO t1 VALUES (1),(1),(2),(2);
2574 INSERT INTO t2 VALUES (1,1,
"2004-02-29 11:11:11"), (2,2,
"2004-02-29 11:11:11");
2575 SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
2576 SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
2577 PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
2580 DEALLOCATE PREPARE stmt1;
2581 SELECT f2, AVG(f21),
2582 (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
2583 FROM t2 GROUP BY f2;
2585 CREATE
TABLE t1 (a
int, b INT, c CHAR(10) NOT NULL);
2586 INSERT INTO t1 VALUES
2587 (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
2588 (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
2589 (3,2,'k'), (3,1,'l'), (1,9,'m');
2591 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
2597 # Bug#21904 (parser problem when using IN with a double "(())")
2601 DROP
TABLE IF EXISTS t1;
2602 DROP
TABLE IF EXISTS t2;
2603 DROP
TABLE IF EXISTS t1xt2;
2607 id_1
int(5) NOT NULL,
2608 t varchar(4) DEFAULT NULL
2612 id_2
int(5) NOT NULL,
2613 t varchar(4) DEFAULT NULL
2616 CREATE
TABLE t1xt2 (
2617 id_1
int(5) NOT NULL,
2618 id_2
int(5) NOT NULL
2621 INSERT INTO t1 VALUES (1,
'a'), (2,
'b'), (3,
'c'), (4,
'd');
2623 INSERT INTO t2 VALUES (2,
'bb'), (3,
'cc'), (4,
'dd'), (12,
'aa');
2625 INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
2627 # subselect returns 0 rows
2629 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2630 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2632 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2633 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2635 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2636 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2638 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2639 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2641 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2642 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
2644 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2645 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
2647 insert INTO t1xt2 VALUES (1, 12);
2649 # subselect returns 1 row
2651 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2652 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2654 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2655 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2657 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2658 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2660 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2661 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2663 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2664 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2666 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2667 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2669 insert INTO t1xt2 VALUES (2, 12);
2671 # subselect returns more than 1 row
2673 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2674 (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2676 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2677 (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2679 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2680 (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2682 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2683 (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
2685 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2686 (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
2688 SELECT DISTINCT t1.id_1 FROM t1 WHERE
2689 (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
2697 # Bug#26728 derived table with concatanation of literals in select list
2700 CREATE
TABLE t1 (a
int);
2701 INSERT INTO t1 VALUES (3), (1), (2);
2703 SELECT
'this is ' 'a test.' AS col1, a AS col2 FROM t1;
2704 SELECT * FROM (SELECT
'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
2710 # Bug#27257 COUNT(*) aggregated in outer query
2713 CREATE
TABLE t1 (a
int, b
int);
2714 CREATE
TABLE t2 (m
int,
n int);
2715 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2716 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2719 (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
2723 (SELECT MIN(m) FROM t2 WHERE m = count(*))
2728 HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
2734 # Bug#27229 GROUP_CONCAT in subselect with COUNT() as an argument
2737 CREATE
TABLE t1 (a
int, b
int);
2738 CREATE
TABLE t2 (m
int,
n int);
2739 INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
2740 INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
2742 SELECT COUNT(*) c, a,
2743 (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
2746 SELECT COUNT(*) c, a,
2747 (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
2754 # Bug#27321 Wrong subquery result in a grouping select
2756 CREATE
TABLE t1 (a
int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY
KEY (a, b));
2757 INSERT INTO t1 VALUES (1,1,0,
'a'), (1,2,0,
'b'), (1,3,0,
'c'), (1,4,0,
'd'),
2758 (1,5,0,
'e'), (2,1,0,
'f'), (2,2,0,
'g'), (2,3,0,
'h'), (3,4,0,
'i'), (3,3,0,
'j'),
2759 (3,2,0,
'k'), (3,1,0,
'l'), (1,9,0,
'm'), (1,0,10,
'n'), (2,0,5,
'o'), (3,0,7,
'p');
2762 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
2765 (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
2768 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
2769 FROM t1 WHERE t1.d=0 GROUP BY a;
2772 (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2773 LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2777 (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2779 FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
2780 FROM t1 as tt GROUP BY tt.a;
2783 (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
2785 FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
2786 FROM t1 as tt GROUP BY tt.a;
2792 # Bug#27348 SET FUNCTION used in a subquery from WHERE condition
2795 CREATE
TABLE t1 (a
int, b
int);
2796 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
2798 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2799 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2802 WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2804 SET @@sql_mode='ansi';
2805 --error ER_INVALID_GROUP_FUNC_USE
2806 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
2807 --error ER_INVALID_GROUP_FUNC_USE
2808 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
2810 --error ER_INVALID_GROUP_FUNC_USE
2812 WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
2814 SET @@sql_mode=default;
2820 # Bug#27363 nested aggregates in outer, subquery / sum(select count(outer))
2822 CREATE
TABLE t1 (a INT); INSERT INTO t1 values (1),(1),(1),(1);
2823 CREATE
TABLE t2 (x INT); INSERT INTO t1 values (1000),(1001),(1002);
2825 --error ER_INVALID_GROUP_FUNC_USE
2826 SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
2827 --error ER_INVALID_GROUP_FUNC_USE
2828 SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
2829 SELECT COUNT(1) FROM DUAL;
2831 --error ER_INVALID_GROUP_FUNC_USE
2832 SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
2834 --error ER_INVALID_GROUP_FUNC_USE
2836 SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
2839 --error ER_INVALID_GROUP_FUNC_USE
2841 SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
2848 # Bug#27807 Server crash when executing subquery with EXPLAIN
2850 CREATE
TABLE t1 (a
int, b
int,
KEY (a));
2851 INSERT INTO t1 VALUES (1,1),(2,1);
2852 EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
2857 # Bug#28377 grouping query with a correlated subquery in WHERE condition
2860 CREATE
TABLE t1 (
id int NOT NULL, st CHAR(2), INDEX idx(
id));
2861 INSERT INTO t1 VALUES
2862 (3,
'FL'), (2,
'GA'), (4,
'FL'), (1,
'GA'), (5,
'NY'), (7,
'FL'), (6,
'NY');
2863 CREATE
TABLE t2 (
id int NOT NULL, INDEX idx(
id));
2864 INSERT INTO t2 VALUES (7), (5), (1), (3);
2866 SELECT
id, st FROM t1
2867 WHERE st IN (
'GA',
'FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.
id=t1.
id);
2868 SELECT
id, st FROM t1
2869 WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.
id=t1.
id)
2872 SELECT
id, st FROM t1
2873 WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.
id=t1.
id);
2874 SELECT
id, st FROM t1
2875 WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.
id=t1.
id)
2882 # Bug#28728 crash with EXPLAIN EXTENDED for a query with a derived table
2883 # over a grouping subselect
2886 CREATE
TABLE t1 (a
int);
2888 INSERT INTO t1 VALUES (1), (2);
2891 SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
2897 # Bug#28811 crash for query containing subquery with ORDER BY and LIMIT 1
2901 a varchar(255)
default NULL,
2902 b timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2906 a varchar(255)
default NULL
2909 INSERT INTO t1 VALUES (
'abcdefghijk',
'2007-05-07 06:00:24');
2910 INSERT INTO t1 SELECT * FROM t1;
2911 INSERT INTO t1 SELECT * FROM t1;
2912 INSERT INTO t1 SELECT * FROM t1;
2913 INSERT INTO t1 SELECT * FROM t1;
2914 INSERT INTO t1 SELECT * FROM t1;
2915 INSERT INTO t1 SELECT * FROM t1;
2916 INSERT INTO t1 SELECT * FROM t1;
2917 INSERT INTO t1 SELECT * FROM t1;
2918 INSERT INTO `t1` VALUES (
'asdf',
'2007-02-08 01:11:26');
2919 INSERT INTO `t2` VALUES (
'abcdefghijk');
2920 INSERT INTO `t2` VALUES (
'asdf');
2922 SET session sort_buffer_size=8192;
2924 SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a
ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
2930 # Bug#27333 subquery grouped for aggregate of outer query / no aggregate
2933 CREATE
TABLE t1 (a INTEGER, b INTEGER);
2934 CREATE
TABLE t2 (x INTEGER);
2935 INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
2936 INSERT INTO t2 VALUES (1), (2);
2938 # wasn't failing, but should
2939 --error ER_SUBQUERY_NO_1_ROW
2940 SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
2942 # fails as it should
2943 --error ER_SUBQUERY_NO_1_ROW
2944 SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
2946 SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
2949 # second test case from 27333
2950 CREATE
TABLE t1 (a INT, b INT);
2951 INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
2953 # returns no rows, when it should
2954 SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
2955 AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
2959 #test cases from 29297
2960 CREATE
TABLE t1 (a INT);
2961 CREATE
TABLE t2 (a INT);
2962 INSERT INTO t1 VALUES (1),(2);
2963 INSERT INTO t2 VALUES (1),(2);
2964 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
2965 --error ER_SUBQUERY_NO_1_ROW
2966 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
2967 SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
2971 # Bug#31884 Assertion + crash in subquery in the SELECT clause.
2974 CREATE
TABLE t1 (a1 INT, a2 INT);
2975 CREATE
TABLE t2 (b1 INT, b2 INT);
2977 INSERT INTO t1 VALUES (100, 200);
2978 INSERT INTO t1 VALUES (101, 201);
2979 INSERT INTO t2 VALUES (101, 201);
2980 INSERT INTO t2 VALUES (103, 203);
2982 SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
2986 # Bug#28076 inconsistent binary/varbinary comparison
2989 CREATE
TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
2990 INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
2992 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
2993 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
2995 CREATE INDEX I1 ON t1 (s1);
2996 CREATE INDEX I2 ON t1 (s2);
2998 SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
2999 SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3002 INSERT INTO t1 VALUES (0x41,0x41);
3003 SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
3007 CREATE
TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY
KEY (a1));
3008 CREATE
TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
3009 CREATE
TABLE t3 (a3 BINARY(2) default '0');
3010 INSERT INTO t1 VALUES (1),(2),(3),(4);
3011 INSERT INTO t2 VALUES (1),(2),(3);
3012 INSERT INTO t3 VALUES (1),(2),(3);
3013 SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
3014 SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
3015 DROP
TABLE t1,t2,t3;
3017 CREATE
TABLE t1 (a1 BINARY(3) PRIMARY
KEY, b1 VARBINARY(3));
3018 CREATE
TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
3019 CREATE
TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
3020 INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
3021 INSERT INTO t2 VALUES (2), (3), (4), (5);
3022 INSERT INTO t3 VALUES (10), (20), (30);
3023 SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
3024 SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
3025 DROP
TABLE t1, t2, t3;
3029 # Bug#32400 Complex SELECT query returns correct result only on some occasions
3032 CREATE
TABLE t1(a INT, b INT);
3033 INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
3035 --error ER_BAD_FIELD_ERROR
3037 SELECT a AS out_a, MIN(b) FROM t1
3038 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3041 --error ER_BAD_FIELD_ERROR
3042 SELECT a AS out_a, MIN(b) FROM t1
3043 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
3047 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3048 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3051 SELECT a AS out_a, MIN(b) FROM t1 t1_outer
3052 WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
3059 # Bug#32036 EXISTS within a WHERE clause with a UNION crashes MySQL 5.122
3062 CREATE
TABLE t1 (a INT);
3063 CREATE
TABLE t2 (a INT);
3065 INSERT INTO t1 VALUES (1),(2);
3066 INSERT INTO t2 VALUES (1),(2);
3068 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3070 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
3074 SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
3075 (SELECT 1 FROM t2 WHERE t1.a = t2.a));
3081 # Bug#33675 Usage of an uninitialized memory by filesort in a subquery
3082 # caused server crash.
3084 create
table t0(a
int);
3085 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3086 create
table t1(f11
int, f12
int);
3087 create
table t2(f21
int unsigned not null, f22
int, f23 varchar(10));
3088 insert into t1 values(1,1),(2,2), (3, 3);
3091 select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
3092 from t0 A, t0 B, t0 C, t0 D;
3094 set session sort_buffer_size= 33*1024;
3095 select count(*) from t1 where f12 =
3096 (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
3098 drop
table t0,t1,t2;
3102 # Bug#33794 "MySQL crashes executing specific query on specific dump"
3105 f7 varchar(32) collate utf8_bin NOT NULL
default '',
3106 f10 varchar(32) collate utf8_bin
default NULL,
3109 INSERT INTO t4 VALUES(1,1), (2,null);
3112 f4 varchar(32) collate utf8_bin NOT NULL
default '',
3113 f2 varchar(50) collate utf8_bin
default NULL,
3114 f3 varchar(10) collate utf8_bin
default NULL,
3118 INSERT INTO t2 VALUES(1,1,null), (2,2,null);
3121 f8 varchar(32) collate utf8_bin NOT NULL
default '',
3122 f1 varchar(10) collate utf8_bin
default NULL,
3123 f9 varchar(32) collate utf8_bin
default NULL,
3126 INSERT INTO t1 VALUES (1,
'P',1), (2,
'P',1), (3,
'R',2);
3129 f6 varchar(32) collate utf8_bin NOT NULL
default '',
3130 f5 varchar(50) collate utf8_bin
default NULL,
3133 INSERT INTO t3 VALUES (1,null), (2,null);
3136 IF(t1.f1 =
'R', a1.f2, t2.f2) AS a4,
3137 IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
3141 FROM t2 VPC, t4 a2, t2 a3
3143 VPC.f4 = a2.f10 AND a3.f2 = a4
3150 t2, t3, t1
JOIN t2 a1 ON t1.f9 = a1.f4
3153 DROP
TABLE t1, t2, t3, t4;
3157 # Bug#36139 "float, zerofill, crash with subquery"
3159 create
table t1 (a
float(5,4) zerofill);
3160 create
table t2 (a
float(5,4),b
float(2,0));
3162 select t1.a from t1 where
3163 t1.a= (select b from t2 limit 1) and not
3164 t1.a= (select a from t2 limit 1) ;
3170 --echo # Bug#45061: Incorrectly market field caused wrong result.
3173 `int_nokey` int(11) NOT NULL,
3174 `int_key`
int(11) NOT NULL,
3175 KEY `int_key` (`int_key`)
3178 INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
3179 (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
3180 (5,2), (1,8), (7,0), (0,9), (9,5);
3183 SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3184 EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
3188 --echo
# End of test for bug#45061.
3192 --echo # Bug #46749: Segfault in add_key_fields() with outer subquery level
3193 --echo
# field references
3201 INSERT INTO t1 VALUES (1,1), (2,1);
3203 CREATE
TABLE st1 like t1;
3204 INSERT INTO st1 VALUES (1,1), (2,1);
3206 CREATE
TABLE st2 like t1;
3207 INSERT INTO st2 VALUES (1,1), (2,1);
3209 # should have "impossible where"
3211 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3216 SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
3220 DROP
TABLE t1, st1, st2;
3223 --echo # Bug #48709: Assertion failed in sql_select.cc:11782:
3224 --echo #
int join_read_key(
JOIN_TAB*)
3227 CREATE
TABLE t1 (pk
int PRIMARY KEY, int_key
int);
3228 INSERT INTO t1 VALUES (10,1), (14,1);
3230 CREATE
TABLE t2 (pk
int PRIMARY KEY, int_key
int);
3231 INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
3233 --echo # should have eq_ref
for t1, unless subquery materialization is used
3234 --replace_column 1 x 2 x 5 x 6 x 7 x 8 x 9 x 10 x
3236 SELECT * FROM t2 outr
3237 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
3240 --echo
# should not crash on debug binaries
3241 SELECT * FROM t2 outr
3242 WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
3248 # 1. Subquery with GROUP/HAVING
3250 create
table t1 (oref
int, grp
int, ie
int) ;
3251 insert into t1 (oref, grp, ie) values
3262 # select max(ie) from t1 where oref=PARAM group by grp
3264 # PARAM subquery result
3265 # 1 -> {(1), (NULL)} matching + NULL
3266 # 2 -> {(3)} non-matching
3267 # 3 -> {(3), (NULL)} non-matching + NULL
3270 create
table t2 (oref
int, a
int);
3271 insert into t2 values
3278 # true, false, null, false, null
3279 select a, oref, a in (select max(ie)
3280 from t1 where oref=t2.oref
group by grp) Z from t2;
3282 # This must have a trigcond
3284 select a, oref, a in (select max(ie)
3285 from t1 where oref=t2.oref
group by grp) Z from t2;
3287 # This must not have a trigcond:
3289 select a, oref from t2
3290 where a in (select max(ie) from t1 where oref=t2.oref
group by grp);
3291 select a, oref, a in (
3292 select max(ie) from t1 where oref=t2.oref
group by grp
union
3293 select max(ie) from t1 where oref=t2.oref
group by grp
3296 # Non-correlated subquery, 2 NULL evaluations
3297 create
table t3 (a
int);
3298 insert into t3 values (NULL), (NULL);
3300 select a in (select max(ie) from t1 where oref=4
group by grp) from t3;
3301 show status like
'Handler_read_rnd_next';
3302 select
' ^ This must show 11' Z;
3304 # This must show trigcond:
3305 explain extended select a in (select max(ie) from t1 where oref=4
group by grp) from t3;
3307 drop
table t1, t2, t3;
3310 # 2. Subquery handled with 'index_subquery':
3312 create
table t1 (a
int, oref
int, key(a));
3313 insert into t1 values
3320 create
table t2 (a
int, oref
int);
3321 insert into t2 values (1, 1), (2,2), (NULL, 3), (NULL, 4);
3323 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3325 # The next explain shows "using index" but that is just incorrect display
3326 # (there is a bug filed about this).
3328 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3331 select oref, a from t2 where a in (select a from t1 where oref=t2.oref);
3332 # This will only show access to t2:
3333 show status like
'%Handler_read_rnd_next';
3335 # Check that repeated NULL-scans are not cached (subq. is not correlated):
3337 insert into t2 values (NULL, 0),(NULL, 0), (NULL, 0), (NULL, 0);
3340 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2;
3341 show status like '%Handler_read%';
3342 select 'No key lookups, seq reads: 29= 5 reads from t2 + 4 * 6 reads from t1.' Z;
3347 # 3. Subquery handled with 'unique_index_subquery':
3349 create
table t1 (a
int, b
int, primary key (a));
3350 insert into t1 values (1,1), (3,1),(100,1);
3352 create
table t2 (a
int, b
int);
3353 insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
3355 select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
3360 # 4. Subquery that is a join, with ref access
3362 create
table t1 (a
int, b
int, key(a));
3363 insert into t1 values
3364 (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
3366 create
table t2 like t1;
3367 insert into t2 select * from t1;
3370 create
table t3 (a
int, oref
int);
3371 insert into t3 values (1, 1), (NULL,1), (NULL,0);
3373 t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3376 # This must have trigcond in WHERE and HAVING:
3379 t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3382 drop
table t1, t2, t3;
3386 # BUG#24085: Wrong query result for "NULL IN (SELECT ... UNION SELECT ...)"
3389 # case 1: NULL IN (SELECT not_null_val FROM ...) w/o HAVING/GROUP-BY/etc
3390 create
table t1 (a
int NOT NULL, b
int NOT NULL, key(a));
3391 insert into t1 values
3392 (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
3394 create
table t2 like t1;
3395 insert into t2 select * from t1;
3398 create
table t3 (a
int, oref
int);
3399 insert into t3 values (1, 1), (NULL,1), (NULL,0);
3401 t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3404 --echo This must show a trig_cond:
3407 t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z
3409 drop
table t1,t2,t3;
3412 # case 2: NULL IN (SELECT not_null_val FROM) where SELECT has GROUP BY
3413 create
table t1 (oref
int, grp
int);
3414 insert into t1 (oref, grp) values
3419 # select count(*) from t1 group by grp having grp=PARAM
3421 # PARAM subuqery result
3423 # 2 -> {} - empty set
3424 create
table t2 (oref
int, a
int);
3425 insert into t2 values
3430 a in (select count(*) from t1
group by grp having grp=t2.oref) Z from t2;
3432 --echo This must show a trig_cond:
3435 a in (select count(*) from t1
group by grp having grp=t2.oref) Z from t2;
3439 create
table t1 (a
int, b
int, primary key (a));
3440 insert into t1 values (1,1), (3,1),(100,1);
3441 create
table t2 (a
int, b
int);
3442 insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
3444 select a,b, a in (select a from t1 where t1.b = t2.b union select a from
3445 t1 where t1.b = t2.b) Z from t2 ;
3446 select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
3451 # BUG#24127: Incorrect results of row-based subqueries with NULLs on the left side.
3453 create
table t3 (a
int);
3454 insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3455 create
table t2 (a
int, b
int, oref
int);
3456 insert into t2 values (NULL,1, 100), (NULL,2, 100);
3458 create
table t1 (a
int, b
int, c
int, key(a,b));
3459 insert into t1 select 2*A, 2*A, 100 from t3;
3461 # First test index subquery engine
3462 explain extended select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
3463 select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) Z from t2;
3465 # Then check that we do turn off 'ref' scans in the subquery
3466 create
table t4 (x
int);
3467 insert into t4 select A.a + 10*B.a from t1 A, t1 B;
3470 (a,b) in (select a,b from t1,t4 where c=t2.oref) Z
3473 (a,b) in (select a,b from t1,t4 where c=t2.oref) Z
3476 drop
table t1,t2,t3,t4;
3478 # More tests for tricky multi-column cases, where some of pushed-down
3479 # equalities are used for index lookups and some arent.
3480 create
table t1 (oref
char(4), grp
int, ie1
int, ie2
int);
3481 insert into t1 (oref, grp, ie1, ie2) values
3494 create
table t2 (oref
char(4), a
int, b
int);
3495 insert into t2 values
3506 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=3 and b is null ;
3507 insert into t2 values (
'new1', 10,10);
3508 insert into t1 values (
'new1', 1234, 10, NULL);
3509 # new1, 10, 10, NULL,
3510 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
3512 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10;
3515 # Now test different column types:
3516 create
table t1 (oref
char(4), grp
int, ie
int);
3517 insert into t1 (oref, grp, ie) values
3533 create
table t2 (oref
char(4), a
int);
3534 insert into t2 values
3543 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3546 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3548 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3551 select oref, a, a in (select min(ie) from t1 where oref=t2.oref
group by grp) Z from t2;
3553 select oref, a from t2 where
3554 a in (select min(ie) from t1 where oref=t2.oref
group by grp);
3556 select oref, a from t2 where
3557 a not in (select min(ie) from t1 where oref=t2.oref
group by grp);
3560 update t1
set ie=3 where oref=
'ff' and ie=1;
3562 select oref, a, a in (select min(ie) from t1 where oref=t2.oref
group by
3566 select oref, a from t2 where a in (select min(ie) from t1 where
3567 oref=t2.oref
group by grp);
3569 select oref, a from t2 where a not in (select min(ie) from t1 where
3570 oref=t2.oref
group by grp);
3572 select oref, a, a in (select min(ie) from t1 where oref=t2.oref
group by
3573 grp having min(ie) > 1) Z from t2;
3575 select oref, a from t2 where a in (select min(ie) from t1 where
3576 oref=t2.oref
group by grp having min(ie) > 1);
3578 select oref, a from t2 where a not in (select min(ie) from t1 where
3579 oref=t2.oref
group by grp having min(ie) > 1);
3584 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3586 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3589 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3591 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3597 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3599 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3601 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3603 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3607 a in (select min(ie) from t1 where oref=t2.oref
3608 group by grp having min(ie) > 1) Z
3612 a in (select min(ie) from t1 where oref=t2.oref
3613 group by grp having min(ie) > 1) Z
3616 select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref
3617 group by grp having min(ie) > 1);
3619 select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref
3620 group by grp having min(ie) > 1);
3624 create
table t1 (oref
char(4), grp
int, ie1
int, ie2
int);
3625 insert into t1 (oref, grp, ie1, ie2) values
3641 create
table t2 (oref
char(4), a
int, b
int);
3642 insert into t2 values
3651 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3654 select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
3656 select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
3659 (a,b) in (select min(ie1),max(ie2) from t1
3660 where oref=t2.oref
group by grp) Z
3663 select oref, a, b from t2 where
3664 (a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref
group by grp);
3666 select oref, a, b from t2 where
3667 (a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref
group by grp);
3671 explain select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3673 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3676 select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref);
3678 select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref);
3681 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2;
3685 create
table t1 (oref
char(4), grp
int, ie
int primary key);
3686 insert into t1 (oref, grp, ie) values
3696 create
table t2 (oref
char(4), a
int);
3697 insert into t2 values
3706 explain select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3708 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2;
3711 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref);
3713 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref);
3716 select oref, a, a in (select min(ie) from t1 where oref=t2.oref
group by grp) Z from t2;
3718 select oref, a, a in (select min(ie) from t1 where oref=t2.oref
group by grp) Z from t2;
3723 # BUG#24420: row-based IN suqueries with aggregation when the left operand
3724 # of the subquery predicate may contain NULL values
3727 create
table t1 (a
int, b
int);
3728 insert into t1 values (0,0), (2,2), (3,3);
3729 create
table t2 (a
int, b
int);
3730 insert into t2 values (1,1), (3,3);
3732 select a, b, (a,b) in (select a, min(b) from t2
group by a) Z from t1;
3734 insert into t2 values (NULL,4);
3735 select a, b, (a,b) in (select a, min(b) from t2
group by a) Z from t1;
3740 # Bug #24484: Aggregate function used in column list subquery gives erroneous
3743 CREATE
TABLE t1 (a
int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
3744 INSERT INTO t1 VALUES (1,1,
'a'), (1,2,
'b'), (1,3,
'c'), (1,4,
'd'), (1,5,
'e'),
3745 (2,1,
'f'), (2,2,
'g'), (2,3,
'h'), (3,4,
'i'),(3,3,
'j'), (3,2,
'k'), (3,1,
'l'),
3747 CREATE
TABLE t2 (a
int, b INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
3748 INSERT INTO t2 SELECT * FROM t1;
3750 # Gives error, but should work since it is (a, b) is the PK so only one
3751 # given match possible
3752 SELECT a, MAX(b), (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b))
3753 as test FROM t1 GROUP BY a;
3754 SELECT * FROM t1 GROUP by t1.a
3755 HAVING (MAX(t1.b) > (SELECT MAX(t2.b) FROM t2 WHERE t2.c < t1.c
3756 HAVING MAX(t2.b+t1.a) < 10));
3758 SELECT a,b,c FROM t1 WHERE b in (9,3,4)
ORDER BY b,c;
3761 (SELECT COUNT(DISTINCT t.c) FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
3764 (SELECT t.b FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
3766 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1)
3768 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)
ORDER BY t.c LIMIT 1)
3773 (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b) LIMIT 1) as test
3781 # Bug #27870: crash of an equijoin query with WHERE condition containing
3782 # a subquery predicate of the form <join attr> NOT IN (SELECT ...)
3785 CREATE
TABLE t1 (a
int);
3786 CREATE
TABLE t2 (b
int, PRIMARY KEY(b));
3787 INSERT INTO t1 VALUES (1), (NULL), (4);
3788 INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6);
3791 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
3792 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1));
3793 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4));
3798 # Bug #28375: crash for NOT IN subquery predicate when left operand becomes NULL
3801 CREATE
TABLE t1 (
id int);
3802 CREATE
TABLE t2 (
id int PRIMARY KEY);
3803 CREATE
TABLE t3 (
id int PRIMARY KEY, name varchar(10));
3804 INSERT INTO t1 VALUES (2), (NULL), (3), (1);
3805 INSERT INTO t2 VALUES (234), (345), (457);
3806 INSERT INTO t3 VALUES (222,
'bbb'), (333,
'ccc'), (111,
'aaa');
3810 WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
3811 WHERE t3.name=
'xxx' AND t2.id=t3.id);
3813 WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3
3814 WHERE t3.name=
'xxx' AND t2.id=t3.id);
3816 SELECT (t1.id IN (SELECT t2.id FROM t2,t3
3817 WHERE t3.name=
'xxx' AND t2.id=t3.id)) AS x
3820 DROP
TABLE t1,t2,t3;
3823 # Bug #22855: Optimizer doesn't rewrite NOT IN subselects to a correlated
3826 CREATE
TABLE t1 (a INT NOT NULL);
3827 INSERT INTO t1 VALUES (1),(-1), (65),(66);
3829 CREATE
TABLE t2 (a INT UNSIGNED NOT NULL PRIMARY KEY);
3830 INSERT INTO t2 VALUES (65),(66);
3832 SELECT a FROM t1 WHERE a NOT IN (65,66);
3833 SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
3834 EXPLAIN SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2);
3839 # Bug #34763: item_subselect.cc:1235:Item_in_subselect::row_value_transformer:
3840 # Assertion failed, unexpected error message:
3841 # ERROR 1247 (42S22): Reference '<list ref>' not supported (forward
3842 # reference in item list)
3844 CREATE
TABLE t1 (a INT);
3845 INSERT INTO t1 VALUES(1);
3847 CREATE
TABLE t2 (placeholder CHAR(11));
3848 INSERT INTO t2 VALUES(
"placeholder");
3850 SELECT ROW(1, 2) IN (SELECT t1.a, 2) FROM t1 GROUP BY t1.a;
3851 SELECT ROW(1, 2) IN (SELECT t1.a, 2 FROM t2) FROM t1 GROUP BY t1.a;
3856 # Bug #36005: crash in subselect with single row
3857 # (subselect_single_select_engine::exec)
3860 CREATE
TABLE t1 (a INT);
3861 INSERT INTO t1 VALUES (1),(2),(3);
3862 CREATE
TABLE t2 SELECT * FROM t1;
3864 SELECT 1 FROM t1 WHERE t1.a NOT IN (SELECT 1 FROM t1, t2 WHERE 0);
3872 create
table t1 (a
int, b decimal(13, 3));
3873 insert into t1 values (1, 0.123);
3874 select a, (select max(b) from t1) into outfile "subselect.out.
file.1" from t1;
3876 load data infile "subselect.out.
file.1" into
table t1;
3879 let $datadir=`select @@datadir`;
3880 --remove_file $datadir/test/subselect.out.
file.1
3883 # Bug #37894: Assertion in init_read_record_seq in handler.h line 1444
3889 varchar_key VARCHAR(5) UNIQUE,
3890 varchar_nokey VARCHAR(5)
3892 INSERT INTO t1 VALUES (9, 7,NULL,NULL), (10,8,
'p' ,
'p');
3894 SELECT varchar_nokey
3897 SELECT INNR.pk FROM t1 AS INNR2
3898 LEFT
JOIN t1 AS INNR ON ( INNR2.int_key = INNR.int_key )
3899 WHERE INNR.varchar_key >
'n{'
3905 # Bug #39069: <row constructor> IN <table-subquery> seriously messed up
3908 CREATE
TABLE t1 (a INT);
3909 INSERT INTO t1 VALUES (1), (2), (11);
3911 --echo # 2nd and 3rd columns should be same
3912 SELECT a, ROW(11, 12) = (SELECT a, 22), ROW(11, 12) IN (SELECT a, 22) FROM t1 GROUP BY t1.a;
3913 SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1 GROUP BY t1.a;
3914 SELECT a, ROW(11, 12) = (SELECT a, 22), ROW(11, 12) IN (SELECT a, 22) FROM t1;
3915 SELECT a, ROW(11, 12) = (SELECT a, 12), ROW(11, 12) IN (SELECT a, 12) FROM t1;
3917 # The x alias is used below to workaround bug #40674.
3918 # Regression tests for sum function on outer column in subselect from dual:
3919 SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 22), ROW(11, 12) IN (SELECT MAX(x), 22) FROM t1;
3920 SELECT a AS x, ROW(11, 12) = (SELECT MAX(x), 12), ROW(11, 12) IN (SELECT MAX(x), 12) FROM t1;
3924 --echo
# both columns should be same
3925 SELECT ROW(1,2) = (SELECT NULL, NULL), ROW(1,2) IN (SELECT NULL, NULL);
3926 SELECT ROW(1,2) = (SELECT 1, NULL), ROW(1,2) IN (SELECT 1, NULL);
3927 SELECT ROW(1,2) = (SELECT NULL, 2), ROW(1,2) IN (SELECT NULL, 2);
3928 SELECT ROW(1,2) = (SELECT NULL, 1), ROW(1,2) IN (SELECT NULL, 1);
3929 SELECT ROW(1,2) = (SELECT 1, 1), ROW(1,2) IN (SELECT 1, 1);
3930 SELECT ROW(1,2) = (SELECT 1, 2), ROW(1,2) IN (SELECT 1, 2);
3933 # Bug #37362 Crash in do_field_eq
3935 CREATE
TABLE t1 (a INT, b INT, c INT);
3936 INSERT INTO t1 VALUES (1,1,1), (1,1,1);
3942 (SELECT COUNT(a) FROM
3943 (SELECT COUNT(b) FROM t1) AS x GROUP BY c
3944 ) FROM t1 GROUP BY b
3951 --echo # Bug #46791: Assertion failed:(
table->key_read==0),
function unknown
3952 --echo #
function,
file sql_base.cc
3955 CREATE
TABLE t1 (a INT, b INT, KEY(a));
3956 INSERT INTO t1 VALUES (1,1),(2,2);
3957 CREATE
TABLE t2 LIKE t1;
3958 INSERT INTO t2 VALUES (1,1),(2,2);
3959 CREATE
TABLE t3 LIKE t1;
3961 --echo # should have 1 impossible where and 2 dependent subqueries
3964 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3967 --echo # should not crash the next
statement
3969 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
3972 --echo # should not crash: the crash is caused by the previous
statement
3975 DROP
TABLE t1,t2,t3;
3978 --echo # Bug #47106: Crash / segfault on adding EXPLAIN
to a non-crashing
3988 INSERT INTO t1 VALUES (1, 1), (2, 1);
3990 CREATE
TABLE t2 LIKE t1;
3991 INSERT INTO t2 SELECT * FROM t1;
3993 CREATE
TABLE t3 LIKE t1;
3994 INSERT INTO t3 SELECT * FROM t1;
3996 --echo # Should not crash.
3997 --echo # Should have 1 impossible where and 2 dependent subqs.
4000 (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
4001 FROM t3 WHERE 1 = 0 GROUP BY 1;
4003 --echo
# should return 0 rows
4005 (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
4006 FROM t3 WHERE 1 = 0 GROUP BY 1;
4008 DROP
TABLE t1,t2,t3;
4010 --echo # Bug#12329653
4011 --echo # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4014 CREATE
TABLE t1(a1
int);
4015 INSERT INTO t1 VALUES (1),(2);
4017 SELECT @@session.sql_mode INTO @old_sql_mode;
4018 SET SESSION sql_mode=
'ONLY_FULL_GROUP_BY';
4020 ## First a simpler query, illustrating the transformation
4021 ## '1 < some (...)' => '1 < max(...)'
4022 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4024 ## The query which made the server crash.
4029 (SELECT 1 FROM t1 AS t1_0
4030 WHERE 1 < SOME (SELECT a1 FROM t1)
4033 --error ER_SUBQUERY_NO_1_ROW
4035 --error ER_SUBQUERY_NO_1_ROW
4038 SET SESSION sql_mode=@old_sql_mode;
4040 DEALLOCATE PREPARE stmt;
4044 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4047 CREATE
TABLE t1(a1
int);
4048 INSERT INTO t1 VALUES (1),(2);
4050 CREATE
TABLE t2(a1
int);
4051 INSERT INTO t2 VALUES (3);
4053 SELECT @@session.sql_mode INTO @old_sql_mode;
4054 SET SESSION sql_mode=
'ONLY_FULL_GROUP_BY';
4056 ## All these are subject to the transformation
4057 ## '1 < some (...)' => '1 < max(...)'
4058 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
4059 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
4060 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT
'a' FROM t2);
4061 SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
4063 SET SESSION sql_mode=@old_sql_mode;
4068 --echo # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4071 create
table t2(i
int);
4072 insert into t2 values(0);
4074 SELECT @@session.sql_mode INTO @old_sql_mode;
4075 SET SESSION sql_mode=
'ONLY_FULL_GROUP_BY';
4078 SELECT
'f' FROM t2 UNION SELECT
'x' FROM t2
4083 col_varchar_key varchar(1) DEFAULT NULL,
4085 KEY col_varchar_key (col_varchar_key)
4090 WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4093 SET SESSION sql_mode=@old_sql_mode;
4098 --echo End of 5.0 tests.
4101 # Test [NOT] IN truth table (both as top-level and general predicate).
4104 create
table t_out (subcase
char(3),
4105 a1
char(2), b1
char(2), c1
char(2));
4106 create
table t_in (a2
char(2), b2
char(2), c2
char(2));
4108 insert into t_out values (
'A.1',
'2a', NULL,
'2a');
4109 # -------------------------- A.2 - impossible
4110 insert into t_out values (
'A.3',
'2a', NULL,
'2a');
4111 insert into t_out values (
'A.4',
'2a', NULL,
'xx');
4112 insert into t_out values (
'B.1',
'2a',
'2a',
'2a');
4113 insert into t_out values (
'B.2',
'2a',
'2a',
'2a');
4114 insert into t_out values (
'B.3',
'3a',
'xx',
'3a');
4115 insert into t_out values (
'B.4',
'xx',
'3a',
'3a');
4117 insert into t_in values (
'1a',
'1a',
'1a');
4118 insert into t_in values (
'2a',
'2a',
'2a');
4119 insert into t_in values (NULL,
'2a',
'2a');
4120 insert into t_in values (
'3a', NULL,
'3a');
4122 -- echo
Test general IN semantics (not top-level)
4126 (a1, b1, c1) IN (select * from t_in where a2 =
'no_match') pred_in,
4127 (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4128 from t_out where subcase = 'A.1';
4130 -- echo case A.2 - impossible
4134 (a1, b1, c1) IN (select * from t_in) pred_in,
4135 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4136 from t_out where subcase = 'A.3';
4140 (a1, b1, c1) IN (select * from t_in) pred_in,
4141 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4142 from t_out where subcase = 'A.4';
4146 (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
4147 (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4148 from t_out where subcase = 'B.1';
4152 (a1, b1, c1) IN (select * from t_in) pred_in,
4153 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4154 from t_out where subcase = 'B.2';
4158 (a1, b1, c1) IN (select * from t_in) pred_in,
4159 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4160 from t_out where subcase = 'B.3';
4164 (a1, b1, c1) IN (select * from t_in) pred_in,
4165 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4166 from t_out where subcase = 'B.4';
4169 -- echo
Test IN as top-level predicate, and
4170 -- echo as non-top level for cases A.3, B.3 (the only cases with NULL result).
4173 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4174 where subcase = 'A.1' and
4175 (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4177 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4178 where subcase = 'A.1' and
4179 (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4181 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4182 where subcase = 'A.1' and
4183 NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4186 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4187 where subcase = 'A.3' and
4188 (a1, b1, c1) IN (select * from t_in);
4190 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4191 where subcase = 'A.3' and
4192 (a1, b1, c1) NOT IN (select * from t_in);
4194 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4195 where subcase = 'A.3' and
4196 NOT((a1, b1, c1) IN (select * from t_in));
4197 -- echo test non-top level result indirectly
4198 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4199 where subcase = 'A.3' and
4200 ((a1, b1, c1) IN (select * from t_in)) is NULL and
4201 ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4204 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4205 where subcase = 'A.4' and
4206 (a1, b1, c1) IN (select * from t_in);
4208 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4209 where subcase = 'A.4' and
4210 (a1, b1, c1) NOT IN (select * from t_in);
4212 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4213 where subcase = 'A.4' and
4214 NOT((a1, b1, c1) IN (select * from t_in));
4217 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4218 where subcase = 'B.1' and
4219 (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4221 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4222 where subcase = 'B.1' and
4223 (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4225 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4226 where subcase = 'B.1' and
4227 NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4230 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4231 where subcase = 'B.2' and
4232 (a1, b1, c1) IN (select * from t_in);
4234 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4235 where subcase = 'B.2' and
4236 (a1, b1, c1) NOT IN (select * from t_in);
4238 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4239 where subcase = 'B.2' and
4240 NOT((a1, b1, c1) IN (select * from t_in));
4243 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4244 where subcase = 'B.3' and
4245 (a1, b1, c1) IN (select * from t_in);
4247 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4248 where subcase = 'B.3' and
4249 (a1, b1, c1) NOT IN (select * from t_in);
4251 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4252 where subcase = 'B.3' and
4253 NOT((a1, b1, c1) IN (select * from t_in));
4254 -- echo test non-top level result indirectly
4255 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4256 where subcase = 'B.3' and
4257 ((a1, b1, c1) IN (select * from t_in)) is NULL and
4258 ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4261 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4262 where subcase = 'B.4' and
4263 (a1, b1, c1) IN (select * from t_in);
4265 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4266 where subcase = 'B.4' and
4267 (a1, b1, c1) NOT IN (select * from t_in);
4269 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4270 where subcase = 'B.4' and
4271 NOT((a1, b1, c1) IN (select * from t_in));
4278 # Bug#27348 SET FUNCTION used in a subquery from WHERE condition
4281 CREATE
TABLE t1 (a INT, b INT);
4282 INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4284 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4285 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4288 WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4290 SET @@sql_mode='ansi';
4291 --error ER_INVALID_GROUP_FUNC_USE
4292 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4293 --error ER_INVALID_GROUP_FUNC_USE
4294 SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4296 --error ER_INVALID_GROUP_FUNC_USE
4298 WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4300 SET @@sql_mode=default;
4304 # Bug#20835 (literal string with =any values)
4306 CREATE
TABLE t1 (s1 CHAR(1));
4307 INSERT INTO t1 VALUES (
'a');
4308 SELECT * FROM t1 WHERE _utf8
'a' = ANY (SELECT s1 FROM t1);
4312 # Bug#37004 NOT IN subquery with MAX over an empty set
4315 CREATE
TABLE t1(c INT, KEY(c));
4316 CREATE
TABLE t2(a INT, b INT);
4317 INSERT INTO t2 VALUES (1, 10), (2, NULL);
4318 INSERT INTO t1 VALUES (1), (3);
4320 SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4325 # Bug#38191 Server crash with subquery containing DISTINCT and ORDER BY
4328 CREATE
TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4329 INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4330 CREATE
TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4331 INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4333 WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a
ORDER BY b);
4337 # Bug#37548 result value erronously reported being NULL in certain subqueries
4340 CREATE
TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4342 INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4350 UNIQUE KEY b (b,c,d),
4356 INSERT INTO t2 VALUES
4373 INSERT INTO t3 VALUES (1,1,1), (2,32,1);
4376 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;
4377 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;
4379 DROP
TABLE t1,t2,t3;
4382 # Bug#37460 Assertion failed:
4383 # !table->file || table->file->inited == handler::NONE
4385 CREATE
TABLE t1 (
id int);
4386 CREATE
TABLE t2 (
id int, c
int);
4388 INSERT INTO t1 (
id) VALUES (1);
4389 INSERT INTO t2 (
id) VALUES (1);
4390 INSERT INTO t1 (
id) VALUES (1);
4391 INSERT INTO t2 (
id) VALUES (1);
4394 SELECT t2.c AS c FROM t1, t2
4395 WHERE t1.
id=t2.
id AND 1 IN (SELECT
id FROM t1) WITH CHECK OPTION;
4398 CREATE VIEW v2 (a,b) AS
4399 SELECT t2.
id, t2.c AS c FROM t1, t2
4400 WHERE t1.
id=t2.
id AND 1 IN (SELECT
id FROM t1) WITH CHECK OPTION;
4402 --error ER_VIEW_CHECK_FAILED
4403 INSERT INTO v2(a,b) VALUES (2,2);
4404 INSERT INTO v2(a,b) VALUES (1,2);
4408 SELECT t2.c AS c FROM t2
4409 WHERE 1 IN (SELECT
id FROM t1) WITH CHECK OPTION;
4417 --echo # Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
4419 create
table t1(
id integer primary key, g integer, v integer, s
char(1));
4420 create
table t2(
id integer primary key, g integer, v integer, s
char(1));
4421 insert into t1 values
4425 (41, 40, null,
'l'),
4427 (51, 50, null,
'l'),
4429 (61, 60, null,
'l'),
4431 (90, 90, null,
'l');
4432 insert into t2 values
4437 (61, 60, null,
'r'),
4439 (71, 70, null,
'r'),
4441 (81, 80, null,
'r'),
4442 (100,100,null,
'r');
4448 where t1.g=t2.g) is unknown;
4452 -- echo # Bug#33204: INTO is allowed in subselect, causing inconsistent results
4454 CREATE
TABLE t1( a INT );
4455 INSERT INTO t1 VALUES (1),(2);
4457 CREATE
TABLE t2( a INT, b INT );
4459 --error ER_PARSE_ERROR
4461 FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
4462 --error ER_PARSE_ERROR
4464 FROM (SELECT a INTO OUTFILE '
file' FROM t1 WHERE a = 2) t1a;
4465 --error ER_PARSE_ERROR
4467 FROM (SELECT a INTO DUMPFILE '
file' FROM t1 WHERE a = 2) t1a;
4469 --error ER_PARSE_ERROR
4473 SELECT a INTO @var FROM t1 WHERE a = 2
4476 --error ER_PARSE_ERROR
4480 SELECT a INTO OUTFILE '
file' FROM t1 WHERE a = 2
4483 --error ER_PARSE_ERROR
4487 SELECT a INTO DUMPFILE '
file' FROM t1 WHERE a = 2
4490 SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
4493 SELECT a FROM t1 WHERE a = 2
4495 SELECT a FROM t1 WHERE a = 2
4501 SELECT a FROM t1 WHERE a = 2
4503 SELECT a FROM t1 WHERE a = 2
4506 # This was not allowed previously. Possibly, it should be allowed on the future.
4507 # For now, the intent is to keep the fix as non-intrusive as possible.
4508 --error ER_PARSE_ERROR
4509 SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
4510 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
4511 SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
4512 --error ER_PARSE_ERROR
4513 SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
4514 --error ER_PARSE_ERROR
4515 SELECT * FROM ((SELECT 1 a INTO OUTFILE
'file' )) t1a;
4516 --error ER_PARSE_ERROR
4517 SELECT * FROM ((SELECT 1 a INTO DUMPFILE
'file' )) t1a;
4519 --error ER_PARSE_ERROR
4520 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
4521 --error ER_PARSE_ERROR
4522 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE
'file' )) t1a;
4523 --error ER_PARSE_ERROR
4524 SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE
'file' )) t1a;
4526 --error ER_PARSE_ERROR
4527 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
4528 --error ER_PARSE_ERROR
4529 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE
'file' ))) t1a;
4530 --error ER_PARSE_ERROR
4531 SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE
'file' ))) t1a;
4533 SELECT * FROM (SELECT 1 a
ORDER BY a) t1a;
4534 SELECT * FROM (SELECT 1 a UNION SELECT 1 a
ORDER BY a) t1a;
4535 SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
4536 SELECT * FROM (SELECT 1 a UNION SELECT 1 a
ORDER BY a LIMIT 1) t1a;
4539 # table_factor: '(' get_select_lex query_expression_body ')' opt_table_alias
4540 # UNION should not be allowed inside the parentheses, nor should
4543 SELECT * FROM t1
JOIN (SELECT 1 UNION SELECT 1) alias ON 1;
4544 --error ER_PARSE_ERROR
4545 SELECT * FROM t1
JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
4546 --error ER_PARSE_ERROR
4547 SELECT * FROM t1
JOIN (t1 t1a UNION SELECT 1) ON 1;
4548 --error ER_PARSE_ERROR
4549 SELECT * FROM t1
JOIN ((t1 t1a UNION SELECT 1)) ON 1;
4550 --error ER_PARSE_ERROR
4551 SELECT * FROM t1
JOIN (t1 t1a) t1a ON 1;
4552 --error ER_PARSE_ERROR
4553 SELECT * FROM t1
JOIN ((t1 t1a)) t1a ON 1;
4555 SELECT * FROM t1
JOIN (t1 t1a) ON 1;
4556 SELECT * FROM t1
JOIN ((t1 t1a)) ON 1;
4558 SELECT * FROM (t1 t1a);
4559 SELECT * FROM ((t1 t1a));
4561 SELECT * FROM t1
JOIN (SELECT 1 t1a) alias ON 1;
4562 SELECT * FROM t1
JOIN ((SELECT 1 t1a)) alias ON 1;
4564 SELECT * FROM t1
JOIN (SELECT 1 a) a ON 1;
4565 SELECT * FROM t1
JOIN ((SELECT 1 a)) a ON 1;
4567 # For the join, TABLE_LIST::select_lex == NULL
4568 # Check that we handle this.
4569 --error ER_PARSE_ERROR
4570 SELECT * FROM (t1
JOIN (SELECT 1) t1a1 ON 1) t1a2;
4572 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
4573 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
4574 SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
4576 --error ER_PARSE_ERROR
4577 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
4578 --error ER_PARSE_ERROR
4579 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE
'file' );
4580 --error ER_PARSE_ERROR
4581 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE
'file' );
4583 SELECT * FROM t1 WHERE a = ( SELECT 1 );
4584 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
4585 --error ER_PARSE_ERROR
4586 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
4587 --error ER_PARSE_ERROR
4588 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE
'file' );
4589 --error ER_PARSE_ERROR
4590 SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE
'file' );
4592 --error ER_PARSE_ERROR
4593 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
4594 --error ER_PARSE_ERROR
4595 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE
'file' );
4596 --error ER_PARSE_ERROR
4597 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE
'file' );
4599 --error ER_PARSE_ERROR
4600 SELECT ( SELECT 1 INTO @v );
4601 --error ER_PARSE_ERROR
4602 SELECT ( SELECT 1 INTO OUTFILE
'file' );
4603 --error ER_PARSE_ERROR
4604 SELECT ( SELECT 1 INTO DUMPFILE
'file' );
4606 --error ER_PARSE_ERROR
4607 SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
4608 --error ER_PARSE_ERROR
4609 SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE
'file' );
4610 --error ER_PARSE_ERROR
4611 SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE
'file' );
4613 # Make sure context is popped when we leave the nested select
4614 SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
4615 SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
4617 # Make sure we have feature F561 (see .yy file)
4618 SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
4620 # Make sure the parser does not allow nested UNIONs anywhere
4622 --error ER_PARSE_ERROR
4623 SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
4624 --error ER_PARSE_ERROR
4625 ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
4627 --error ER_PARSE_ERROR
4628 SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4629 --error ER_PARSE_ERROR
4630 SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
4631 SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4632 SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
4634 --error ER_PARSE_ERROR
4635 SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4636 --error ER_PARSE_ERROR
4637 SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4638 SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
4640 --error ER_PARSE_ERROR
4641 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4642 --error ER_PARSE_ERROR
4643 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4644 --error ER_PARSE_ERROR
4645 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4646 --error ER_PARSE_ERROR
4647 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
4649 --error ER_PARSE_ERROR
4650 SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4651 --error ER_PARSE_ERROR
4652 SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4653 --error ER_PARSE_ERROR
4654 SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4655 --error ER_PARSE_ERROR
4656 SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
4658 SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4659 SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4660 SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4661 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
4663 --error ER_PARSE_ERROR
4664 SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
4665 SELECT EXISTS(SELECT 1+1);
4666 --error ER_PARSE_ERROR
4667 SELECT EXISTS(SELECT 1+1 INTO @test);
4668 --error ER_PARSE_ERROR
4669 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
4671 --error ER_PARSE_ERROR
4672 SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
4673 --error ER_PARSE_ERROR
4674 SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
4678 # Bug #31157: Crash when select+order by the avg of some field within the
4681 CREATE
TABLE t1 (a ENUM(
'rainbow'));
4682 INSERT INTO t1 VALUES (),(),(),(),();
4683 SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1
ORDER BY AVG(LAST_INSERT_ID()));
4685 CREATE
TABLE t1 (a LONGBLOB);
4686 INSERT INTO t1 SET a =
'aaaa';
4687 INSERT INTO t1 SET a =
'aaaa';
4688 SELECT 1 FROM t1 GROUP BY
4689 (SELECT LAST_INSERT_ID() FROM t1
ORDER BY MIN(a) ASC LIMIT 1);
4693 --echo # Bug #49512 : subquery with aggregate
function crash
4694 --echo # subselect_single_select_engine::exec()
4696 CREATE
TABLE t1(a INT);
4697 INSERT INTO t1 VALUES();
4699 --echo
# should not crash
4700 SELECT 1 FROM t1 WHERE a <> SOME
4702 SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
4708 --echo # Bug #45989 take 2 : memory leak after explain encounters an
4709 --echo # error in the
query
4712 CREATE
TABLE t1(a LONGTEXT);
4713 INSERT INTO t1 VALUES (repeat(
'a',@@global.max_allowed_packet));
4714 INSERT INTO t1 VALUES (repeat(
'b',@@global.max_allowed_packet));
4716 --error ER_BAD_FIELD_ERROR
4717 EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
4718 (SELECT a AS away FROM t1 GROUP BY a WITH
ROLLUP) AS d1
4720 --error ER_WRONG_USAGE
4721 EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
4722 (SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH
ROLLUP) AS d1
4728 # BUG#36135 "void Diagnostics_area::set_eof_status(THD*): Assertion `!is_set()' failed."
4730 create
table t0 (a
int);
4731 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4734 a
int(11)
default null,
4735 b
int(11)
default null,
4738 # produce numbers 0..999
4739 insert into t1 select A.a+10*(B.a+10*C.a),A.a+10*(B.a+10*C.a) from t0 A, t0 B, t0 C;
4741 create
table t2 (a
int(11)
default null);
4742 insert into t2 values (0),(1);
4744 create
table t3 (a
int(11)
default null);
4745 insert into t3 values (0),(1);
4747 create
table t4 (a
int(11)
default null);
4748 insert into t4 values (0),(1);
4750 create
table t5 (a
int(11)
default null);
4751 insert into t5 values (0),(1),(0),(1);
4753 # this must not fail assertion
4755 select * from t2, t3
4759 t3.a in (select t1.b from t1
4760 where t1.a+1=t1.a+1 and
4761 t1.a < (select t4.a+10
4762 from t4, t5 limit 2));
4764 drop
table t0, t1, t2, t3, t4, t5;
4767 --echo # BUG#48177 - SELECTs with NOT IN subqueries containing NULL
4768 --echo # values
return too many records
4772 i1
int DEFAULT NULL,
4776 INSERT INTO t1 VALUES (1, NULL);
4777 INSERT INTO t1 VALUES (2, 3);
4778 INSERT INTO t1 VALUES (4, NULL);
4779 INSERT INTO t1 VALUES (4, 0);
4780 INSERT INTO t1 VALUES (NULL, NULL);
4783 i1
int DEFAULT NULL,
4787 INSERT INTO t2 VALUES (4, NULL);
4788 INSERT INTO t2 VALUES (5, 0);
4792 SELECT i1, i2 FROM t1;
4795 --echo
Data in subquery (should be filtered out)
4796 SELECT i1, i2 FROM t2
ORDER BY i1;
4804 NOT IN (SELECT i1, i2 FROM t2);
4807 --echo
# Check that the subquery only has to be evaluated once
4808 --echo #
for all-NULL values even though there are two (NULL,NULL) records
4810 SHOW STATUS LIKE
'%Handler_read_rnd_next';
4813 INSERT INTO t1 VALUES (NULL, NULL);
4820 NOT IN (SELECT i1, i2 FROM t2);
4823 --echo
# Handler_read_rnd_next should be one more than baseline
4824 --echo # (read
record from t1, but
do not read from t2)
4825 SHOW STATUS LIKE
'%Handler_read_rnd_next';
4831 --echo # Bug#54568: create
view cause Assertion failed: 0,
4832 --echo #
file .\item_subselect.cc, line 836
4834 EXPLAIN SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4835 DESCRIBE SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4836 --echo # None of the below should crash
4837 CREATE VIEW v1 AS SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
4838 CREATE VIEW v2 AS SELECT 1 LIKE
'%' ESCAPE ( 1 IN ( SELECT 1 ) );
4842 # Bug #47904 Incorrect results w/ table subquery, derived SQs, and LEFT JOIN on index
4845 --echo Set up test tables.
4852 INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
4854 CREATE
TABLE t2 SELECT * FROM t1;
4857 t3_id INT UNSIGNED AUTO_INCREMENT,
4859 amount DECIMAL(16,2),
4865 INSERT INTO t3 (t1_id, t3_id, amount)
4866 VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
4868 --echo This is the 'inner
query' running by itself.
4869 --echo Produces correct results.
4872 IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4875 LEFT
JOIN t2 ON t2.t1_id=t1.t1_id
4880 --echo SELECT * FROM (the same inner
query)
4881 --echo Produces correct results.
4885 IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4888 LEFT
JOIN t2 ON t2.t1_id=t1.t1_id
4893 --echo Now make t2.t1_id part of a key.
4894 ALTER
TABLE t2 ADD PRIMARY KEY(t1_id);
4896 --echo Same inner
query by itself.
4897 --echo Still correct results.
4900 IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4903 LEFT
JOIN t2 ON t2.t1_id=t1.t1_id
4907 --echo SELECT * FROM (the same inner
query), now with indexes on the LEFT
JOIN
4911 IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
4914 LEFT
JOIN t2 ON t2.t1_id=t1.t1_id
4925 # BUG#36135 "void Diagnostics_area::set_eof_status(THD*): Assertion `!is_set()' failed."
4927 create
table t0 (a
int);
4928 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4931 a
int(11)
default null,
4932 b
int(11)
default null,
4935 # produce numbers 0..999
4936 insert into t1 select A.a+10*(B.a+10*C.a),A.a+10*(B.a+10*C.a) from t0 A, t0 B, t0 C;
4938 create
table t2 (a
int(11)
default null);
4939 insert into t2 values (0),(1);
4941 create
table t3 (a
int(11)
default null);
4942 insert into t3 values (0),(1);
4944 create
table t4 (a
int(11)
default null);
4945 insert into t4 values (0),(1);
4947 create
table t5 (a
int(11)
default null);
4948 insert into t5 values (0),(1),(0),(1);
4950 # this must not fail assertion
4952 select * from t2, t3
4956 t3.a in (select t1.b from t1
4957 where t1.a+1=t1.a+1 and
4958 t1.a < (select t4.a+10
4959 from t4, t5 limit 2));
4961 drop
table t0, t1, t2, t3, t4, t5;
4964 --echo # BUG#48177 - SELECTs with NOT IN subqueries containing NULL
4965 --echo # values
return too many records
4969 i1
int DEFAULT NULL,
4973 INSERT INTO t1 VALUES (1, NULL);
4974 INSERT INTO t1 VALUES (2, 3);
4975 INSERT INTO t1 VALUES (4, NULL);
4976 INSERT INTO t1 VALUES (4, 0);
4977 INSERT INTO t1 VALUES (NULL, NULL);
4980 i1
int DEFAULT NULL,
4984 INSERT INTO t2 VALUES (4, NULL);
4985 INSERT INTO t2 VALUES (5, 0);
4989 SELECT i1, i2 FROM t1;
4992 --echo
Data in subquery (should be filtered out)
4993 SELECT i1, i2 FROM t2
ORDER BY i1;
5001 NOT IN (SELECT i1, i2 FROM t2);
5004 --echo
# Check that the subquery only has to be evaluated once
5005 --echo #
for all-NULL values even though there are two (NULL,NULL) records
5007 SHOW STATUS LIKE
'%Handler_read_rnd_next';
5010 INSERT INTO t1 VALUES (NULL, NULL);
5017 NOT IN (SELECT i1, i2 FROM t2);
5020 --echo
# Handler_read_rnd_next should be one more than baseline
5021 --echo # (read
record from t1, but
do not read from t2)
5022 SHOW STATUS LIKE
'%Handler_read_rnd_next';
5028 --echo # Bug #52711: Segfault when doing EXPLAIN SELECT with
5029 --echo #
union...order by (select... where...)
5032 CREATE
TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5033 INSERT INTO t1 VALUES (1),(2);
5034 CREATE
TABLE t2 (b INT);
5035 INSERT INTO t2 VALUES (1),(2);
5037 --echo # Should not crash
5038 --disable_result_log
5040 SELECT * FROM t2 UNION SELECT * FROM t2
5041 ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST (
'+abc' IN BOOLEAN MODE));
5043 --echo # Should not crash
5044 SELECT * FROM t2 UNION SELECT * FROM t2
5045 ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST (
'+abc' IN BOOLEAN MODE));
5050 --echo # Bug #58818: Incorrect result
for IN/ANY subquery
5051 --echo # with HAVING condition
5054 CREATE
TABLE t1(i INT);
5055 INSERT INTO t1 VALUES (1), (2), (3);
5056 CREATE
TABLE t1s(i INT);
5057 INSERT INTO t1s VALUES (10), (20), (30);
5058 CREATE
TABLE t2s(i INT);
5059 INSERT INTO t2s VALUES (100), (200), (300);
5064 SELECT STRAIGHT_JOIN t2s.i
5066 t1s LEFT OUTER
JOIN t2s ON t2s.i = t1s.i
5073 SELECT STRAIGHT_JOIN t2s.i
5075 t1s LEFT OUTER
JOIN t2s ON t2s.i = t1s.i
5080 WHERE NOT t1.I = ANY
5082 SELECT STRAIGHT_JOIN t2s.i
5084 t1s LEFT OUTER
JOIN t2s ON t2s.i = t1s.i
5090 SELECT STRAIGHT_JOIN t2s.i
5092 t1s LEFT OUTER
JOIN t2s ON t2s.i = t1s.i
5096 DROP
TABLE t1,t1s,t2s;
5099 --echo # Bug #56690 Wrong results with subquery with
5100 --echo # GROUP BY inside < ANY clause
5104 pk INT NOT NULL PRIMARY KEY,
5106 KEY key_number (number)
5108 INSERT INTO t1 VALUES (8,8);
5111 pk INT NOT NULL PRIMARY KEY,
5113 KEY key_number (number)
5116 INSERT INTO t2 VALUES (1,2);
5117 INSERT INTO t2 VALUES (2,8);
5118 INSERT INTO t2 VALUES (3,NULL);
5119 INSERT INTO t2 VALUES (4,166);
5121 SELECT * FROM t1 WHERE t1.number < ANY(SELECT number FROM t2 GROUP BY number);
5122 SELECT * FROM t1 WHERE t1.number < ANY(SELECT number FROM t2);
5126 --echo End of 5.1 tests
5129 --echo # BUG#50257: Missing info in REF column of the EXPLAIN
5130 --echo # lines
for subselects
5133 CREATE
TABLE t1 (a INT, b INT, INDEX (a));
5134 INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5137 EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5139 EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5146 --echo # BUG#52317: Assertion failing in Field_varstring::store()
5147 --echo # at field.cc:6833
5150 CREATE
TABLE t1 (i INTEGER);
5151 INSERT INTO t1 VALUES (1);
5152 CREATE
TABLE t2 (i INTEGER, KEY k(i));
5153 INSERT INTO t2 VALUES (1), (2);
5156 SELECT i FROM t1 WHERE (1) NOT IN (SELECT i FROM t2);
5163 --echo # Bug #11765713 58705:
5164 --echo # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5165 --echo # CREATED BY OPT_SUM_QUERY
5168 CREATE
TABLE t1(a INT NOT NULL, KEY (a));
5169 INSERT INTO t1 VALUES (0), (1);
5171 --error ER_SUBQUERY_NO_1_ROW
5172 SELECT 1 as foo FROM t1 WHERE a < SOME
5173 (SELECT a FROM t1 WHERE a <=>
5177 SELECT 1 as foo FROM t1 WHERE a < SOME
5178 (SELECT a FROM t1 WHERE a <=>
5179 (SELECT a FROM t1 where a is null)
5185 --echo # Bug #57704: Cleanup code dies with
void TABLE::set_keyread(
bool):
5186 --echo # Assertion `
file' failed.
5189 CREATE TABLE t1 (a INT);
5191 --error ER_OPERAND_COLUMNS
5194 (SELECT 1 FROM t1 RIGHT JOIN
5195 (SELECT 1 FROM t1, t1 t2) AS d ON 1),
5201 --echo # Bug#11764086: Null left operand to NOT IN in WHERE clause
5202 --echo # behaves differently than real NULL
5205 CREATE TABLE parent (id int);
5206 INSERT INTO parent VALUES (1), (2);
5208 CREATE TABLE child (parent_id int, other int);
5209 INSERT INTO child VALUES (1,NULL);
5211 --echo # Offending query (c.parent_id is NULL for null-complemented rows only)
5213 SELECT p.id, c.parent_id
5216 ON p.id = c.parent_id
5217 WHERE c.parent_id NOT IN (
5223 --echo # Some syntactic variations with IS FALSE and IS NOT TRUE
5225 SELECT p.id, c.parent_id
5228 ON p.id = c.parent_id
5229 WHERE c.parent_id IN (
5235 SELECT p.id, c.parent_id
5238 ON p.id = c.parent_id
5239 WHERE c.parent_id IN (
5245 DROP TABLE parent, child;
5247 --echo # End of test for bug#11764086.
5249 --echo End of 5.5 tests.
5252 --echo # BUG#48920: COUNT DISTINCT returns 1 for NULL values when in a subquery
5253 --echo # in the select list
5258 i int(11) DEFAULT NULL,
5259 v varchar(1) DEFAULT NULL
5263 INSERT INTO t1 VALUES (8,'v
');
5264 INSERT INTO t1 VALUES (9,'r
');
5265 INSERT INTO t1 VALUES (NULL,'y
');
5269 i int(11) DEFAULT NULL,
5270 v varchar(1) DEFAULT NULL,
5275 INSERT INTO t2 VALUES (NULL,'r
');
5276 INSERT INTO t2 VALUES (0,'c
');
5277 INSERT INTO t2 VALUES (0,'o
');
5278 INSERT INTO t2 VALUES (2,'v
');
5279 INSERT INTO t2 VALUES (7,'c
');
5282 SELECT i, v, (SELECT COUNT(DISTINCT i)
5284 WHERE v = t2.v) as subsel
5289 SELECT i, v, (SELECT COUNT(DISTINCT i)
5291 WHERE v = t2.v) as subsel
5298 --echo # BUG#50257: Missing info in REF column of the EXPLAIN
5299 --echo # lines for subselects
5302 CREATE TABLE t1 (a INT, b INT, INDEX (a));
5303 INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5306 EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5308 EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5314 --echo # BUG#58561: Server Crash with correlated subquery and MyISAM tables
5320 col_varchar_key VARCHAR(1),
5322 KEY col_int_key (col_int_key),
5323 KEY col_varchar_key (col_varchar_key,col_int_key)
5325 INSERT INTO cc VALUES (10,7,'v
');
5326 INSERT INTO cc VALUES (11,1,'r
');
5332 KEY col_date_key (col_date_key)
5334 INSERT INTO bb VALUES (10,'2002-02-21
');
5339 col_varchar_key VARCHAR(1),
5341 KEY col_int_key (col_int_key),
5342 KEY col_varchar_key (col_varchar_key,col_int_key)
5344 INSERT INTO c VALUES (1,NULL,'w
');
5345 INSERT INTO c VALUES (19,NULL,'f
');
5350 col_varchar_key VARCHAR(1),
5352 KEY col_int_key (col_int_key),
5353 KEY col_varchar_key (col_varchar_key,col_int_key)
5355 INSERT INTO b VALUES (1,7,'f
');
5359 WHERE (col_int_key, col_int_key) IN (
5360 SELECT parent1.pk, parent1.pk
5361 FROM bb parent1 JOIN cc parent2
5362 ON parent2.col_varchar_key = parent2.col_varchar_key
5363 WHERE granparent1.col_varchar_key IN (
5364 SELECT col_varchar_key
5366 AND parent1.pk = granparent1.col_int_key
5367 ORDER BY parent1.col_date_key
5370 DROP TABLE bb, b, cc, c;
5372 --echo End of 5.6 tests
5375 --echo # BUG#46743 "Azalea processing correlated, aggregate SELECT
5376 --echo # subqueries incorrectly"
5378 # To see the bug, one would have to undo both the fix for BUG#46743
5379 # and the one for BUG#47123.
5380 CREATE TABLE t1 (c int);
5381 INSERT INTO t1 VALUES (NULL);
5382 CREATE TABLE t2 (d int , KEY (d)); # index is needed for bug
5383 INSERT INTO t2 VALUES (NULL),(NULL); # two rows needed for bug
5384 # we see that subquery returns 0 rows
5385 --echo 0 rows in subquery
5386 SELECT 1 AS RESULT FROM t2,t1 WHERE d = c;
5387 # so here it ends up as NULL
5389 SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
5390 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
5391 --echo first equivalent variant
5392 SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c ;
5393 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c;
5394 --echo second equivalent variant
5395 # used to fail with 1242: Subquery returns more than 1 row
5396 SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
5397 EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
5402 --echo BUG#37842: Assertion in DsMrr_impl::dsmrr_init, at handler.cc:4307
5405 `pk` int(11) NOT NULL AUTO_INCREMENT,
5406 `int_key` int(11) DEFAULT NULL,
5408 KEY `int_key` (`int_key`)
5411 INSERT INTO t1 VALUES (1,9),(2,3),(3,8),(4,6),(5,9),(6,5),(7,5),(8,9),(9,1),(10,10);
5413 SELECT `pk` FROM t1 AS OUTR WHERE `int_key` = ALL (
5414 SELECT `int_key` FROM t1 AS INNR WHERE INNR . `pk` >= 9
5419 --echo # Bug#53236 Segfault in DTCollation::set(DTCollation&)
5423 pk INTEGER AUTO_INCREMENT,
5424 col_varchar VARCHAR(1),
5429 INSERT INTO t1 (col_varchar)
5436 FROM ( t1 AS table1 JOIN t1 AS table2 ON (table1.col_varchar =
5437 table2.col_varchar) )
5438 WHERE ( 1, 2 ) IN ( SELECT SUBQUERY1_t1.pk AS SUBQUERY1_field1,
5439 SUBQUERY1_t1.pk AS SUBQUERY1_field2
5440 FROM ( t1 AS SUBQUERY1_t1 JOIN t1 AS SUBQUERY1_t2
5441 ON (SUBQUERY1_t2.col_varchar =
5442 SUBQUERY1_t1.col_varchar) ) )
5447 --echo # Bug#58207: invalid memory reads when using default column value and
5448 --echo # tmptable needed
5450 CREATE TABLE t(a VARCHAR(245) DEFAULT
5451 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
');
5452 INSERT INTO t VALUES (''),(''),(''),(''),(''),(''),(''),(''),(''),(''),('');
5453 SELECT * FROM (SELECT default(a) FROM t GROUP BY a) d;
5457 --echo # Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
5458 --echo # BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
5461 CREATE TABLE t1(a INT);
5462 INSERT INTO t1 VALUES (0), (1);
5469 INSERT INTO t2 VALUES ('a
', 2), ('b
', 3);
5471 SELECT 1 FROM t1 WHERE a =
5472 (SELECT 1 FROM t2 WHERE b =
5473 (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
5477 SELECT 1 FROM t1 WHERE a =
5478 (SELECT 1 FROM t2 WHERE b =
5479 (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
5486 --echo # BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
5489 CREATE TABLE t1 (f1 varchar(1));
5490 INSERT INTO t1 VALUES ('v
'),('s
');
5492 CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
5493 INSERT INTO t2 VALUES ('j
'),('v
'),('c
'),('m
'),('d
'),
5494 ('d
'),('y
'),('t
'),('d
'),('s
');
5496 let $query=SELECT table1.f1, table2.f1_key
5497 FROM t1 AS table1, t2 AS table2
5500 SELECT DISTINCT f1_key
5502 WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
5505 eval explain $query;
5510 --echo # BUG#12616477 - 0 VS NULL DIFFERENCES WITH OUTER JOIN, SUBQUERY
5515 col_varchar_key varchar(1),
5516 col_varchar_nokey varchar(1),
5519 INSERT INTO t1 VALUES (224,'p
','p
'),(9,'e
','e
');
5523 col_varchar_key varchar(1),
5524 KEY col_int_key (col_int_key),
5525 KEY col_varchar_key (col_varchar_key,col_int_key))
5527 INSERT INTO t3 VALUES (4,'p
'),(8,'e
'),(10,'a
');
5529 # At jcl>=1, if join buffering is on t1 bug doesn't happen, so we
5530 # force join order so that join buffering is rather on t3.
5531 # Reverse join order if you want
to see bug at jcl=0.
5533 let $query=SELECT t1f.*,t3f.*,(
5534 SELECT MIN(t3s.col_int_key)
5536 t1 AS t1s ON t1s.col_int_key = 9 and
5537 t1s.col_varchar_key =
'e'
5538 WHERE
'e' <> t1f.col_varchar_nokey )
5540 t1 AS t1f STRAIGHT_JOIN t3 AS t3f;
5543 eval explain $query;
5548 --echo # Bug#12795555: Missing rows with ALL/ANY subquery
5550 # Test the case when Item_maxmin_subselect is used
5551 CREATE
TABLE t1 (f1 INT);
5552 INSERT INTO t1 VAlUES (NULL),(1),(NULL),(2);
5553 SELECT f1 FROM t1 WHERE f1 < ALL (SELECT 1 FROM DUAL WHERE 0);
5555 # Test the case when Item_sum_[max|min] is used
5556 CREATE
TABLE t1 (k VARCHAR(1), KEY k(k));
5557 INSERT INTO t1 VALUES (
'r'), (NULL), (NULL);
5559 CREATE
TABLE t2 (c VARCHAR(1));
5560 INSERT INTO t2 VALUES (
'g'), (NULL);
5562 CREATE
TABLE t3 (c VARCHAR(1));
5571 DROP
TABLE t1, t2, t3;
5574 --echo # Bug#12838171: 51VS56: TRANSFORMED IN()+SUBQ QUERY
5575 --echo # PRODUCES EMPTY RESULT ON 5.6, 1 ROW ON 5.1
5578 col_int_nokey
int(11),
5579 col_varchar_nokey varchar(1)
5582 INSERT INTO ot VALUES (1,
'x');
5585 col_int_key
int(11),
5586 col_varchar_key varchar(1),
5587 KEY idx_cvk_cik (col_varchar_key,col_int_key)
5590 INSERT INTO it VALUES (NULL,
'x'), (NULL,
'f');
5593 SELECT col_int_nokey
5595 WHERE col_varchar_nokey IN
5596 (SELECT col_varchar_key
5598 WHERE col_int_key IS NULL);
5602 SELECT col_int_nokey
5604 WHERE col_varchar_nokey IN
5605 (SELECT col_varchar_key
5607 WHERE col_int_key IS NULL);
5610 SELECT col_int_nokey
5612 WHERE col_varchar_nokey IN
5613 (SELECT col_varchar_key
5615 WHERE coalesce(col_int_nokey, 1) );
5619 SELECT col_int_nokey
5621 WHERE col_varchar_nokey IN
5622 (SELECT col_varchar_key
5624 WHERE coalesce(col_int_nokey, 1) );
5629 col_int_key
int(11),
5630 col_varchar_key varchar(1),
5631 col_varchar_key2 varchar(1),
5632 KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
5633 KEY idx_cvk_cik (col_varchar_key, col_int_key)
5636 INSERT INTO it VALUES (NULL,
'x',
'x'), (NULL,
'f',
'f');
5638 SELECT col_int_nokey
5640 WHERE (col_varchar_nokey,
'x') IN
5641 (SELECT col_varchar_key, col_varchar_key2
5643 WHERE col_int_key IS NULL);
5647 SELECT col_int_nokey
5649 WHERE (col_varchar_nokey, 'x') IN
5650 (SELECT col_varchar_key, col_varchar_key2
5652 WHERE col_int_key IS NULL);
5658 --echo # Bug #11829691: Pure
virtual method called in Item_bool_func2::fix...()
5661 CREATE
TABLE t1(a INTEGER);
5662 CREATE
TABLE t2(b INTEGER);
5665 SELECT SUM(b) FROM t2 GROUP BY b HAVING b IN (SELECT b FROM t1)";
5670 DEALLOCATE PREPARE stmt;
5675 --echo # Bug #13595212 EXTRA ROWS RETURNED ON RIGHT
JOIN WITH VIEW AND
5676 --echo # IN-SUBQUERY IN WHERE
5680 pk
int(11) NOT NULL AUTO_INCREMENT,
5681 col_int_key
int(11) NOT NULL,
5682 col_varchar_key varchar(1) NOT NULL,
5683 col_varchar_nokey varchar(1) NOT NULL,
5685 KEY col_int_key (col_int_key),
5686 KEY col_varchar_key (col_varchar_key,col_int_key)
5689 INSERT INTO t1 VALUES (1,0,
'j',
'j'),(2,8,
'v',
'v'),
5690 (3,1,
'c',
'c'),(4,8,
'm',
'm'),(5,9,
'd',
'd');
5692 CREATE VIEW v1 AS SELECT * FROM t1;
5697 SELECT alias2.col_varchar_nokey
5699 RIGHT
JOIN t1 AS alias2 ON 1
5700 WHERE alias2.col_varchar_key IN (
5701 SELECT sq2_alias1.col_varchar_nokey
5702 FROM v1 AS sq2_alias1
5703 LEFT
JOIN t1 AS sq2_alias2
5704 ON (sq2_alias2.col_int_key = sq2_alias1.pk)
5705 WHERE sq2_alias1.pk != alias1.col_int_key
5706 AND sq2_alias1.col_varchar_key > alias1.col_varchar_key
5710 eval EXPLAIN EXTENDED $query;
5714 # Same, but in view:
5716 eval CREATE VIEW v2 AS $query;
5718 EXPLAIN EXTENDED SELECT * FROM v2;
5722 # Same, with LEFT JOIN
5724 let $query=SELECT alias2.col_varchar_nokey
5726 LEFT
JOIN v1 AS alias1 ON 1
5727 WHERE alias2.col_varchar_key IN (
5728 SELECT sq2_alias1.col_varchar_nokey
5729 FROM v1 AS sq2_alias1
5730 LEFT
JOIN t1 AS sq2_alias2
5731 ON (sq2_alias2.col_int_key = sq2_alias1.pk)
5732 WHERE sq2_alias1.pk != alias1.col_int_key
5733 AND sq2_alias1.col_varchar_key > alias1.col_varchar_key
5736 eval EXPLAIN EXTENDED $query;
5744 --echo # Bug#13651009 WRONG RESULT FROM DERIVED
TABLE IF THE SUBQUERY
5745 --echo # HAS AN EMPTY RESULT
5750 col_int_nokey
int NOT NULL,
5751 col_int_key
int NOT NULL,
5752 col_time_key time NOT NULL,
5753 col_varchar_key varchar(1) NOT NULL,
5754 col_varchar_nokey varchar(1) NOT NULL,
5756 KEY col_int_key (col_int_key),
5757 KEY col_time_key (col_time_key),
5758 KEY col_varchar_key (col_varchar_key,col_int_key)
5762 pk
int NOT NULL AUTO_INCREMENT,
5763 col_int_nokey
int NOT NULL,
5764 col_int_key
int NOT NULL,
5765 col_time_key time NOT NULL,
5766 col_varchar_key varchar(1) NOT NULL,
5767 col_varchar_nokey varchar(1) NOT NULL,
5769 KEY col_int_key (col_int_key),
5770 KEY col_time_key (col_time_key),
5771 KEY col_varchar_key (col_varchar_key,col_int_key)
5774 INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
5776 SET @var2:=4, @var3:=8;
5779 --echo Testcase without inner subquery
5782 SELECT @var3:=12, sq4_alias1.*
5783 FROM t1 AS sq4_alias1
5784 WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
5785 sq4_alias1.col_varchar_key = @var3;
5791 # Now as derived table:
5792 eval EXPLAIN SELECT * FROM ( $subq ) AS alias3;
5793 eval SELECT * FROM ( $subq ) AS alias3;
5797 --echo Testcase with inner subquery; crashed WL
#6095
5801 FROM t1 AS sq4_alias1
5802 WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
5804 (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
5805 c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
5806 FROM t2 AS c_sq1_alias1
5807 WHERE (c_sq1_alias1.col_int_nokey != @var2
5808 OR c_sq1_alias1.pk != @var3));
5812 # Now as derived table:
5813 eval EXPLAIN SELECT * FROM ( $subq ) AS alias3;
5814 eval SELECT * FROM ( $subq ) AS alias3;
5819 --echo #
Test that indexsubquery_engine only does one
lookup if
5820 --echo # the technique is unique_subquery: does not
try to read the
5821 --echo # next row
if the first row failed the subquery
's WHERE
5822 --echo # condition (here: b=3).
5825 create table t1(a int);
5826 insert into t1 values(1),(2);
5827 create table t2(a int primary key, b int);
5828 insert into t2 values(1,10),(2,10);
5829 let $query=select * from t1 where a in (select a from t2 where b=3);
5830 eval explain $query;
5833 show status like "handler_read%";
5837 --echo # Bug#13735980 Difference in number of rows when using subqueries
5840 CREATE TABLE m (c1 VARCHAR(1), c2 INTEGER, c3 INTEGER);
5841 INSERT INTO m VALUES ('',6,8), ('',75,NULL);
5843 CREATE TABLE o (c1 VARCHAR(1));
5844 INSERT INTO o VALUES ('S
'), ('S
'), ('S
');
5847 SELECT m.c1 AS c1,m.c2 AS c2,m.c3 AS c3 FROM m;
5850 SELECT o.c1 AS c1 FROM o;
5853 FROM v1 AS t1 JOIN v2 AS t2 ON t1.c1 < t2.c1
5854 WHERE t1.c3 BETWEEN 2 AND 6 OR
5855 (t1.c3 IN(4) AND t1.c3 <> 2) OR t1.c2 >= 8;
5857 CREATE TABLE integers (i1 INTEGER);
5858 INSERT IGNORE INTO integers VALUES (2),(4),(6),(8);
5861 FROM v1 AS t1 JOIN v2 AS t2 ON t1.c1 < t2.c1
5862 WHERE t1.c3 BETWEEN (SELECT i1 FROM integers WHERE i1 = 2) AND
5863 (SELECT i1 FROM integers WHERE i1 = 6) OR
5864 t1.c3 IN((SELECT i1 FROM integers WHERE i1 = 4)) AND
5865 t1.c3 <>(SELECT i1 FROM integers WHERE i1 = 2) OR
5866 t1.c2 >=(SELECT i1 FROM integers WHERE i1 = 8);
5868 # Outcome is unaffected when replacing views with tables:
5871 FROM m AS t1 JOIN o AS t2 ON t1.c1 < t2.c1
5872 WHERE t1.c3 BETWEEN (SELECT i1 FROM integers WHERE i1 = 2) AND
5873 (SELECT i1 FROM integers WHERE i1 = 6) OR
5874 t1.c3 IN((SELECT i1 FROM integers WHERE i1 = 4)) AND
5875 t1.c3 <>(SELECT i1 FROM integers WHERE i1 = 2) OR
5876 t1.c2 >=(SELECT i1 FROM integers WHERE i1 = 8);
5879 DROP TABLE m, o, integers;
5882 --echo # Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5885 CREATE TABLE t1(a TIME NOT NULL);
5886 INSERT INTO t1 VALUES ('00:00:32
');
5888 SELECT 1 FROM t1 WHERE a >
5889 (SELECT timestamp(a) AS a FROM t1);
5894 --echo # Bug #13736664 RIGHT JOIN+WHERE QUERY GIVES DIFF RESULTS WHEN USING
5895 --echo # SELECT * SUBQ FOR TABLES
5898 CREATE TABLE t1(a INT, b CHAR(1));
5899 INSERT INTO t1 VALUES (NULL, 'x
');
5901 CREATE TABLE t2(c INT, d CHAR(1));
5902 INSERT INTO t2 VALUES (NULL, 'y
'), (9, 'z
');
5905 FROM t2 AS a2 RIGHT JOIN t1 AS a1 ON c = a
5910 FROM (SELECT * FROM t2) AS a2 RIGHT JOIN (SELECT * FROM t1) AS a1 ON c = a
5917 --echo # Bug#13468414 Query shows different results when literals are selected
5918 --echo # from separate table
5922 col_varchar_key varchar(1) NOT NULL,
5923 col_varchar_nokey varchar(1) NOT NULL,
5924 KEY col_varchar_key (col_varchar_key)
5927 INSERT INTO t1 VALUES
5928 ('v
','v
'), ('s
','s
'), ('l
','l
'), ('y
','y
'), ('c
','c
'),
5929 ('i
','i
'), ('h
','h
'), ('q
','q
'), ('a
','a
'), ('v
','v
'),
5930 ('u
','u
'), ('s
','s
'), ('y
','y
'), ('z
','z
'), ('h
','h
'),
5931 ('p
','p
'), ('e
','e
'), ('i
','i
'), ('y
','y
'), ('w
','w
');
5934 col_int_nokey INT NOT NULL,
5935 col_varchar_nokey varchar(1) NOT NULL
5938 INSERT INTO t2 VALUES
5939 (4,'j
'), (6,'v
'), (3,'c
'), (5,'m
'), (3,'d
'), (246,'d
'), (2,'y
'), (9,'t
'),
5940 (3,'d
'), (8,'s
'), (1,'r
'), (8,'m
'), (8,'b
'), (5,'x
'), (7,'g
'), (5,'p
'),
5941 (1,'q
'), (6,'w
'), (2,'d
'), (9,'e
');
5944 i1 INTEGER NOT NULL PRIMARY KEY
5946 INSERT INTO t0 VALUES (7);
5948 -- disable_query_log
5949 -- disable_result_log
5953 -- enable_result_log
5956 SELECT col_varchar_nokey
5959 (SELECT it2.col_int_nokey
5960 FROM t2 AS it2 LEFT JOIN t1 AS it1
5961 ON it2.col_varchar_nokey = it1.col_varchar_key
5965 SELECT col_varchar_nokey
5967 WHERE (SELECT i1 FROM t0 WHERE i1 = 7) NOT IN
5968 (SELECT it2.col_int_nokey
5969 FROM t2 AS it2 LEFT JOIN t1 AS it1
5970 ON it2.col_varchar_nokey = it1.col_varchar_key
5974 eval explain extended $query;
5976 DROP TABLE t0, t1, t2;
5979 --echo # Bug#13735704 Memory engine + NOT IN + subquery produces different
5984 col_varchar_key varchar(1),
5985 KEY col_varchar_key(col_varchar_key)
5987 INSERT INTO t1 VALUES
5988 ('v
'), ('s
'), ('y
'), ('z
'), ('h
'), ('p
');
5993 KEY col_int_key(col_int_key)
5995 INSERT INTO t2 VALUES
5996 (4,4), (2,7), (2,5), (2,3), (197,188), (4,4), (6,2), (1,4),
5997 (156,231), (2,1), (1,2), (80,194), (3,2), (8,3), (91,98), (6,3),
5998 (7,1), (3,0), (161,189), (7,8), (7,3), (213,120), (248,25), (1,1),
5999 (6,3), (6,1), (3,3), (140,153), (0,5), (7,9), (6,1), (1,2),
6000 (1,7), (9,2), (0,1), (6,5);
6002 CREATE TABLE t0(i1 INTEGER NOT NULL PRIMARY KEY);
6003 INSERT INTO t0 VALUES(7),(2);
6005 SELECT col_varchar_key
6008 (SELECT col_int_key,col_int_nokey FROM t2);
6011 SELECT col_varchar_key
6013 WHERE ((SELECT i1 FROM t0 WHERE i1 = 7),
6014 (SELECT i1 FROM t0 WHERE i1 = 2)) NOT IN
6015 (SELECT col_int_key,col_int_nokey FROM t2);
6017 eval explain extended $query;
6020 DROP TABLE t0, t1, t2;
6024 --echo # Bug #13639204 64111: CRASH ON SELECT SUBQUERY WITH NON UNIQUE
6030 INSERT INTO t1 (id) VALUES (11);
6036 KEY t1_id_position (t1_id,position)
6040 (SELECT position FROM t2
6041 WHERE t2.t1_id = t1.id
6042 ORDER BY t2.t1_id , t2.position
6048 eval EXPLAIN $query;
6055 --echo # Bug#13731417 WRONG RESULT WITH NOT IN (SUBQUERY) AND
6056 --echo # COMPOSITE INDEX ON SUBQUERY'S
TABLE
6059 CREATE
TABLE t1 (a
int, b
int);
6060 CREATE
TABLE t2 (a
int, b
int, KEY a_b (a,b));
6061 CREATE
TABLE t4 (a
int);
6062 INSERT INTO t1 VALUES(0,1);
6063 INSERT INTO t2 VALUES(NULL,1),(NULL,1);
6064 INSERT INTO t4 VALUES(1);
6066 SELECT ((0,1) NOT IN (SELECT NULL,1)) IS NULL;
6068 let $query=SELECT * FROM t1
6069 WHERE ((a,b) NOT IN (SELECT t2.a,t2.b FROM
6070 t4 STRAIGHT_JOIN t2 WHERE t4.a=t2.b)) IS NULL;
6072 eval EXPLAIN EXTENDED $query;
6075 ALTER
TABLE t2 DROP INDEX a_b;
6077 EVAL EXPLAIN EXTENDED $query;
6080 DROP
TABLE t1,t2,t4;
6083 --echo # Bug#13725821 ASSERT NULL_REF_KEY == __NULL FAILED IN
6084 --echo # CREATE_REF_FOR_KEY() IN
SQL_SELECT.CC
6091 col_varchar_key VARCHAR(1),
6092 col_varchar_nokey VARCHAR(1),
6094 KEY col_int_key (col_int_key),
6095 KEY col_varchar_key (col_varchar_key,col_int_key)
6098 INSERT INTO t1 VALUES (17,NULL,9,NULL,NULL),
6099 (18,2,2,'o','o'),(19,NULL,9,'w','w'),(20,6,2,'m','m'),(21,7,4,'q','q'),
6100 (22,2,0,NULL,NULL),(23,5,4,'d','d'),(24,7,8,'g','g'),(25,6,NULL,'x','x'),
6101 (26,6,NULL,'f','f'),(27,2,0,'p','p'),(28,9,NULL,'j','j'),(29,6,8,'c','c');
6103 ALTER
TABLE t1 DISABLE KEYS;
6104 ALTER
TABLE t1 ENABLE KEYS;
6107 SELECT table1.col_varchar_key AS field1,
6108 table1.col_int_nokey AS field2,
6109 table2.col_varchar_key AS field3,
6110 table1.col_int_nokey AS field4,
6111 table2.col_int_nokey AS field5,
6112 table1.col_varchar_nokey AS field6
6114 INNER
JOIN t1 AS table2
6115 ON (( table2.col_int_nokey = table1.col_int_key )
6116 AND ( table2.col_int_key = table1.col_int_key )
6118 WHERE ( table1.col_varchar_key = table2.col_varchar_key OR table1.pk = 154 )
6120 AND ( table1.col_varchar_key LIKE '%a%' OR table1.col_varchar_key LIKE
6124 eval CREATE
TABLE t2 $query ;
6128 WHERE (field1, field2, field3, field4, field5, field6) NOT IN ( $query
6134 --echo # Bug #13735712 SELECT W/ SUBQUERY PRODUCES MORE ROWS WHEN USING
6138 CREATE
TABLE t1(a INT);
6139 INSERT INTO t1 VALUES(9);
6140 CREATE
TABLE t2(b INT);
6141 INSERT INTO t2 VALUES(8);
6142 CREATE
TABLE t3(c INT);
6143 INSERT INTO t3 VALUES(3);
6146 FROM t2 RIGHT
JOIN t3 ON(c = b)
6147 WHERE b < ALL(SELECT a FROM t1 WHERE a <= 7);
6150 FROM t2 RIGHT
JOIN t3 ON(c = b)
6151 WHERE b < ALL(SELECT a FROM t1 WHERE a <= @var);
6153 DROP
TABLE t1, t2, t3;
6156 --echo # Bug #13330886 TOO MANY ROWS WITH ALL|ANY
6159 CREATE
TABLE t1 (a INT, b INT);
6160 INSERT INTO t1 VALUES (NULL,8), (8,7);
6161 CREATE
TABLE t2 (c INT);
6162 INSERT INTO t2 VALUES (10);
6164 SELECT 1 FROM t2 WHERE c > ALL (SELECT a FROM t1 WHERE b >= 3);
6166 SELECT 1 FROM t2 WHERE c > ALL (SELECT a FROM t1 WHERE b >= @var);
6170 CREATE
TABLE t1 (a INT NOT NULL, b INT NOT NULL);
6171 INSERT INTO t1 VALUES (0,8), (8,7);
6172 CREATE
TABLE t2 (c INT NOT NULL);
6173 INSERT INTO t2 VALUES (10);
6176 SELECT 1 FROM t2 WHERE c > ANY (SELECT a FROM t1 WHERE b >= 3) IS TRUE;
6177 SELECT 1 FROM t2 WHERE c > ANY (SELECT a FROM t1 WHERE b >= 3) IS TRUE;
6181 set @@optimizer_switch=@old_opt_switch;
6182 # New tests go here.