1
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
2
drop view if exists v2;
3
set @subselect_tmp=@@optimizer_switch;
4
set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
5
"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
6
set join_cache_level=1;
7
set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
11
explain extended select (select 2);
12
id select_type table type possible_keys key key_len ref rows filtered Extra
13
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
15
Note 1249 Select 2 was reduced during optimization
16
Note 1003 select 2 AS `(select 2)`
17
SELECT (SELECT 1) UNION SELECT (SELECT 2);
21
explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
22
id select_type table type possible_keys key key_len ref rows filtered Extra
23
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
24
3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
25
NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
27
Note 1249 Select 2 was reduced during optimization
28
Note 1249 Select 4 was reduced during optimization
29
Note 1003 select 1 AS `(SELECT 1)` union select 2 AS `(SELECT 2)`
30
SELECT (SELECT (SELECT 0 UNION SELECT 0));
31
(SELECT (SELECT 0 UNION SELECT 0))
33
explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
34
id select_type table type possible_keys key key_len ref rows filtered Extra
35
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
36
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
37
4 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
38
NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL
40
Note 1249 Select 2 was reduced during optimization
41
Note 1003 select (select 0 union select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
42
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
43
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
44
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;
45
ERROR 42S22: Reference 'b' not supported (forward reference in item list)
46
SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
49
SELECT (SELECT a) as a;
50
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
51
EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
52
id select_type table type possible_keys key key_len ref rows filtered Extra
53
1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
54
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
55
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
57
Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
58
Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
59
Note 1003 select 1 AS `1` from dual having ((select 1) = 1)
60
SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
64
ERROR 42S22: Unknown column 'a' in 'field list'
65
SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
68
SELECT 1 FROM (SELECT (SELECT a) b) c;
69
ERROR 42S22: Unknown column 'a' in 'field list'
70
SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
73
SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
74
ERROR 21000: Operand should contain 1 column(s)
75
SELECT 1 IN (SELECT 1);
78
SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
81
select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
82
ERROR HY000: Incorrect usage of PROCEDURE and subquery
83
SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
84
ERROR HY000: Incorrect parameters to procedure 'ANALYSE'
85
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
86
ERROR 42S22: Unknown column 'a' in 'field list'
87
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
88
ERROR 42S22: Unknown column 'a' in 'field list'
89
SELECT (SELECT 1,2,3) = ROW(1,2,3);
90
(SELECT 1,2,3) = ROW(1,2,3)
92
SELECT (SELECT 1,2,3) = ROW(1,2,1);
93
(SELECT 1,2,3) = ROW(1,2,1)
95
SELECT (SELECT 1,2,3) < ROW(1,2,1);
96
(SELECT 1,2,3) < ROW(1,2,1)
98
SELECT (SELECT 1,2,3) > ROW(1,2,1);
99
(SELECT 1,2,3) > ROW(1,2,1)
101
SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
102
(SELECT 1,2,3) = ROW(1,2,NULL)
104
SELECT ROW(1,2,3) = (SELECT 1,2,3);
105
ROW(1,2,3) = (SELECT 1,2,3)
107
SELECT ROW(1,2,3) = (SELECT 1,2,1);
108
ROW(1,2,3) = (SELECT 1,2,1)
110
SELECT ROW(1,2,3) < (SELECT 1,2,1);
111
ROW(1,2,3) < (SELECT 1,2,1)
113
SELECT ROW(1,2,3) > (SELECT 1,2,1);
114
ROW(1,2,3) > (SELECT 1,2,1)
116
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
117
ROW(1,2,3) = (SELECT 1,2,NULL)
119
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
120
(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
122
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
123
(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
125
SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
126
(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
129
Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
130
SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
131
(SELECT 'b',2,'a') = ROW(1.5,2,'a')
133
SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
134
(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
136
SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
137
(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
139
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
140
ERROR 21000: Operand should contain 1 column(s)
141
SELECT 1 as a,(SELECT a+a) b,(SELECT b);
144
create table t1 (a int);
145
create table t2 (a int, b int);
146
create table t3 (a int);
147
create table t4 (a int not null, b int not null);
148
insert into t1 values (2);
149
insert into t2 values (1,7),(2,7);
150
insert into t4 values (4,8),(3,8),(5,9);
151
select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
152
ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
153
select (select a from t1 where t1.a=t2.a), a from t2;
154
(select a from t1 where t1.a=t2.a) a
157
select (select a from t1 where t1.a=t2.b), a from t2;
158
(select a from t1 where t1.a=t2.b) a
161
select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
162
(select a from t1) a (select 1 union select 2 limit 1)
165
select (select a from t3), a from t2;
169
select * from t2 where t2.a=(select a from t1);
172
insert into t3 values (6),(7),(3);
173
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
177
(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;
182
(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);
188
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);
189
id select_type table type possible_keys key key_len ref rows filtered Extra
190
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
191
2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort
192
3 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where
193
4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
194
NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
196
Note 1003 (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`b` = (select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1))) union (select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where (`test`.`t4`.`b` = (select (max(`test`.`t2`.`a`) * 4) from `test`.`t2`)) order by `a`)
197
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
198
(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a
201
select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
202
(select * from t2 where a>1) as tt;
203
(select t3.a from t3 where a<8 order by 1 desc limit 1) a
205
set @tmp_optimizer_switch=@@optimizer_switch;
206
set optimizer_switch='derived_merge=off,derived_with_keys=off';
207
explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
208
(select * from t2 where a>1) as tt;
209
id select_type table type possible_keys key key_len ref rows filtered Extra
210
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00
211
3 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where
212
2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort
214
Note 1003 select (select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`a` < 8) order by 1 desc limit 1) AS `(select t3.a from t3 where a<8 order by 1 desc limit 1)`,`tt`.`a` AS `a` from (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` > 1)) `tt`
215
set optimizer_switch=@tmp_optimizer_switch;
216
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);
219
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);
222
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);
224
select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
225
b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
229
explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
230
id select_type table type possible_keys key key_len ref rows filtered Extra
231
1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
232
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
233
3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
235
Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
236
Note 1003 select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((select avg((`test`.`t2`.`a` + (select min(`test`.`t3`.`a`) from `test`.`t3` where (`test`.`t3`.`a` >= `test`.`t4`.`a`)))) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
237
select * from t3 where exists (select * from t2 where t2.b=t3.a);
240
select * from t3 where not exists (select * from t2 where t2.b=t3.a);
244
select * from t3 where a in (select b from t2);
247
select * from t3 where a not in (select b from t2);
251
select * from t3 where a = some (select b from t2);
254
select * from t3 where a <> any (select b from t2);
258
select * from t3 where a = all (select b from t2);
261
select * from t3 where a <> all (select b from t2);
265
insert into t2 values (100, 5);
266
select * from t3 where a < any (select b from t2);
270
select * from t3 where a < all (select b from t2);
273
select * from t3 where a >= any (select b from t2);
277
explain extended select * from t3 where a >= any (select b from t2);
278
id select_type table type possible_keys key key_len ref rows filtered Extra
279
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
280
2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00
282
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
283
select * from t3 where a >= all (select b from t2);
286
delete from t2 where a=100;
287
select * from t3 where a in (select a,b from t2);
288
ERROR 21000: Operand should contain 1 column(s)
289
select * from t3 where a in (select * from t2);
290
ERROR 21000: Operand should contain 1 column(s)
291
insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
292
select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
294
insert into t2 values (2,10);
295
select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
298
delete from t2 where a=2 and b=10;
299
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
302
create table t5 (a int);
303
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
304
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
307
insert into t5 values (5);
308
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
309
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
312
insert into t5 values (2);
313
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
314
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
317
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;
318
id select_type table type possible_keys key key_len ref rows filtered Extra
319
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
320
2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
321
3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where
322
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
324
Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
325
Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
326
Note 1003 select <expr_cache><`test`.`t2`.`a`>((select 2 from dual where (2 = `test`.`t2`.`a`) union select `test`.`t5`.`a` from `test`.`t5` where (`test`.`t5`.`a` = `test`.`t2`.`a`))) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
327
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
328
ERROR 21000: Subquery returns more than 1 row
329
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
330
create table t7( uq int primary key, name char(25));
331
insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
332
insert into t6 values (1,1),(1,2),(2,2),(1,3);
333
select * from t6 where exists (select * from t7 where uq = clinic_uq);
338
explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
339
id select_type table type possible_keys key key_len ref rows filtered Extra
340
1 PRIMARY t6 ALL NULL NULL NULL NULL 4 100.00 Using where
341
2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
343
Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
344
Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)))
345
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
346
ERROR 23000: Column 'a' in field list is ambiguous
348
CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
349
INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
350
CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
351
INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
352
CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
353
INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
354
SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
357
SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
360
SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
364
`pseudo` varchar(35) character set latin1 NOT NULL default '',
365
`email` varchar(60) character set latin1 NOT NULL default '',
366
PRIMARY KEY (`pseudo`),
367
UNIQUE KEY `email` (`email`)
368
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
369
INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
370
INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
371
INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
372
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');
373
id select_type table type possible_keys key key_len ref rows filtered Extra
374
1 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
375
4 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
376
2 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00
377
3 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
379
Note 1003 select 'joce' AS `pseudo`,(select 'test' from `test`.`t8` where 1) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where 1
380
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
381
t8 WHERE pseudo='joce');
382
ERROR 21000: Operand should contain 1 column(s)
383
SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
385
ERROR 21000: Operand should contain 1 column(s)
386
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
389
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
390
ERROR 21000: Subquery returns more than 1 row
391
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
393
`topic` mediumint(8) unsigned NOT NULL default '0',
394
`date` date NOT NULL default '0000-00-00',
395
`pseudo` varchar(35) character set latin1 NOT NULL default '',
396
PRIMARY KEY (`pseudo`,`date`,`topic`),
397
KEY `topic` (`topic`)
398
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
399
INSERT INTO t1 (topic,date,pseudo) VALUES
400
('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
401
EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
402
id select_type table type possible_keys key key_len ref rows filtered Extra
403
1 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
405
Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')
406
EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
407
id select_type table type possible_keys key key_len ref rows filtered Extra
408
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
409
2 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
411
Note 1003 select (select distinct `test`.`t1`.`date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')) AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')`
412
SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
415
SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
416
(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
418
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
423
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
424
ERROR 21000: Subquery returns more than 1 row
425
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
426
id select_type table type possible_keys key key_len ref rows filtered Extra
427
1 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index
428
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
429
3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
430
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
432
Note 1003 select 1 AS `1` from `test`.`t1` where 1
435
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
436
`maxnumrep` int(10) unsigned NOT NULL default '0',
437
PRIMARY KEY (`numeropost`),
438
UNIQUE KEY `maxnumrep` (`maxnumrep`)
439
) ENGINE=MyISAM ROW_FORMAT=FIXED;
440
INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
442
`mot` varchar(30) NOT NULL default '',
443
`topic` mediumint(8) unsigned NOT NULL default '0',
444
`date` date NOT NULL default '0000-00-00',
445
`pseudo` varchar(35) NOT NULL default '',
446
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
447
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
448
INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
449
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
452
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;
456
SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
457
ERROR 42S22: Unknown column 'a' in 'having clause'
458
SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
459
ERROR 42S22: Unknown column 'a' in 'having clause'
460
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
461
mot topic date pseudo
462
joce 40143 2002-10-22 joce
463
joce 43506 2002-10-22 joce
464
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
465
mot topic date pseudo
466
SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
467
mot topic date pseudo
468
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
469
mot topic date pseudo
470
joce 40143 2002-10-22 joce
471
joce 43506 2002-10-22 joce
472
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
473
mot topic date pseudo
474
SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
475
mot topic date pseudo
476
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
477
mot topic date pseudo
478
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
479
mot topic date pseudo
480
joce 40143 2002-10-22 joce
481
joce 43506 2002-10-22 joce
482
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
483
mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
484
joce 40143 2002-10-22 joce 1
485
joce 43506 2002-10-22 joce 1
486
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
487
mot topic date pseudo
488
SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
489
mot topic date pseudo
490
joce 40143 2002-10-22 joce
491
joce 43506 2002-10-22 joce
492
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
493
mot topic date pseudo
494
joce 40143 2002-10-22 joce
495
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
496
mot topic date pseudo
497
joce 40143 2002-10-22 joce
498
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
499
mot topic date pseudo
500
joce 40143 2002-10-22 joce
501
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
502
mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
503
joce 40143 2002-10-22 joce 1
504
joce 43506 2002-10-22 joce 0
507
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
508
`maxnumrep` int(10) unsigned NOT NULL default '0',
509
PRIMARY KEY (`numeropost`),
510
UNIQUE KEY `maxnumrep` (`maxnumrep`)
511
) ENGINE=MyISAM ROW_FORMAT=FIXED;
512
INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
513
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
514
ERROR 21000: Subquery returns more than 1 row
515
select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
516
ERROR 21000: Subquery returns more than 1 row
519
Error 1242 Subquery returns more than 1 row
520
Error 1028 Sort aborted: Subquery returns more than 1 row
522
create table t1 (a int);
523
insert into t1 values (1),(2),(3);
524
(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
530
CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
531
INSERT INTO t1 VALUES ();
532
SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
533
ERROR 21000: Subquery returns more than 1 row
536
`numeropost` mediumint(8) unsigned NOT NULL default '0',
537
`numreponse` int(10) unsigned NOT NULL auto_increment,
538
`pseudo` varchar(35) NOT NULL default '',
539
PRIMARY KEY (`numeropost`,`numreponse`),
540
UNIQUE KEY `numreponse` (`numreponse`),
541
KEY `pseudo` (`pseudo`,`numeropost`)
543
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
544
ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
545
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
546
ERROR 42S22: Unknown column 'a' in 'having clause'
547
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
548
numreponse (SELECT numeropost FROM t1 HAVING numreponse=1)
549
INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
550
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
551
ERROR 21000: Subquery returns more than 1 row
552
SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
553
ERROR 21000: Subquery returns more than 1 row
554
EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
555
id select_type table type possible_keys key key_len ref rows filtered Extra
556
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
558
Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where (`test`.`t1`.`numeropost` = '1')
559
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
560
id select_type table type possible_keys key key_len ref rows filtered Extra
561
1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using index
562
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
564
Note 1003 select 3 AS `numreponse` from `test`.`t1` where ((1 = '1'))
566
CREATE TABLE t1 (a int(1));
567
INSERT INTO t1 VALUES (1);
568
SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
572
create table t1 (a int NOT NULL, b int, primary key (a));
573
create table t2 (a int NOT NULL, b int, primary key (a));
574
insert into t1 values (0, 10),(1, 11),(2, 12);
575
insert into t2 values (1, 21),(2, 22),(3, 23);
581
update t1 set b= (select b from t1);
582
ERROR HY000: You can't specify target table 't1' for update in FROM clause
583
update t1 set b= (select b from t2);
584
ERROR 21000: Subquery returns more than 1 row
585
update t1 set b= (select b from t2 where t1.a = t2.a);
592
create table t1 (a int NOT NULL, b int, primary key (a));
593
create table t2 (a int NOT NULL, b int, primary key (a));
594
insert into t1 values (0, 10),(1, 11),(2, 12);
595
insert into t2 values (1, 21),(2, 12),(3, 23);
601
select * from t1 where b = (select b from t2 where t1.a = t2.a);
604
delete from t1 where b in (select b from t1);
605
ERROR HY000: You can't specify target table 't1' for update in FROM clause
606
delete from t1 where b = (select b from t2);
607
ERROR 21000: Subquery returns more than 1 row
608
delete from t1 where b = (select b from t2 where t1.a = t2.a);
614
create table t11 (a int NOT NULL, b int, primary key (a));
615
create table t12 (a int NOT NULL, b int, primary key (a));
616
create table t2 (a int NOT NULL, b int, primary key (a));
617
insert into t11 values (0, 10),(1, 11),(2, 12);
618
insert into t12 values (33, 10),(22, 11),(2, 12);
619
insert into t2 values (1, 21),(2, 12),(3, 23);
630
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
631
ERROR HY000: You can't specify target table 't12' for update in FROM clause
632
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
633
ERROR 21000: Subquery returns more than 1 row
634
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
643
drop table t11, t12, t2;
644
CREATE TABLE t1 (x int) ENGINE=MyISAM;
645
create table t2 (a int) ENGINE=MyISAM;
646
create table t3 (b int);
647
insert into t2 values (1);
648
insert into t3 values (1),(2);
649
INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
650
ERROR HY000: You can't specify target table 't1' for update in FROM clause
651
INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
652
ERROR 21000: Subquery returns more than 1 row
653
INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
657
insert into t2 values (1);
658
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
663
INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
670
INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
679
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
680
ERROR 42S22: Unknown column 'x' in 'field list'
681
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
691
drop table t1, t2, t3;
692
CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
693
create table t2 (a int);
694
create table t3 (a int);
695
insert into t2 values (1);
696
insert into t3 values (1),(2);
699
replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
700
ERROR HY000: You can't specify target table 't1' for update in FROM clause
701
replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
702
ERROR 21000: Subquery returns more than 1 row
703
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
707
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
711
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
716
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
721
replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
727
drop table t1, t2, t3;
728
SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
729
ERROR HY000: No tables used
730
CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
731
INSERT INTO t2 VALUES (1),(2);
732
SELECT * FROM t2 WHERE id IN (SELECT 1);
735
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
736
id select_type table type possible_keys key key_len ref rows filtered Extra
737
1 PRIMARY t2 ref id id 5 const 1 100.00 Using index
739
Note 1249 Select 2 was reduced during optimization
740
Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1)
741
SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
744
SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
747
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
748
id select_type table type possible_keys key key_len ref rows filtered Extra
749
1 PRIMARY t2 ref id id 5 const 1 100.00 Using where; Using index
751
Note 1249 Select 3 was reduced during optimization
752
Note 1249 Select 2 was reduced during optimization
753
Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = <cache>((1 + 1)))
754
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
755
id select_type table type possible_keys key key_len ref rows filtered Extra
756
1 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index
757
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
758
3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
759
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
761
Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1)) union select 3 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))))
762
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
764
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
767
INSERT INTO t2 VALUES ((SELECT * FROM t2));
768
ERROR HY000: You can't specify target table 't2' for update in FROM clause
769
INSERT INTO t2 VALUES ((SELECT id FROM t2));
770
ERROR HY000: You can't specify target table 't2' for update in FROM clause
775
CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
776
INSERT INTO t1 values (1),(1);
777
UPDATE t2 SET id=(SELECT * FROM t1);
778
ERROR 21000: Subquery returns more than 1 row
780
create table t1 (a int);
781
insert into t1 values (1),(2),(3);
782
select 1 IN (SELECT * from t1);
783
1 IN (SELECT * from t1)
785
select 10 IN (SELECT * from t1);
786
10 IN (SELECT * from t1)
788
select NULL IN (SELECT * from t1);
789
NULL IN (SELECT * from t1)
791
update t1 set a=NULL where a=2;
792
select 1 IN (SELECT * from t1);
793
1 IN (SELECT * from t1)
795
select 3 IN (SELECT * from t1);
796
3 IN (SELECT * from t1)
798
select 10 IN (SELECT * from t1);
799
10 IN (SELECT * from t1)
801
select 1 > ALL (SELECT * from t1);
802
1 > ALL (SELECT * from t1)
804
select 10 > ALL (SELECT * from t1);
805
10 > ALL (SELECT * from t1)
807
select 1 > ANY (SELECT * from t1);
808
1 > ANY (SELECT * from t1)
810
select 10 > ANY (SELECT * from t1);
811
10 > ANY (SELECT * from t1)
814
create table t1 (a varchar(20));
815
insert into t1 values ('A'),('BC'),('DEF');
816
select 'A' IN (SELECT * from t1);
817
'A' IN (SELECT * from t1)
819
select 'XYZS' IN (SELECT * from t1);
820
'XYZS' IN (SELECT * from t1)
822
select NULL IN (SELECT * from t1);
823
NULL IN (SELECT * from t1)
825
update t1 set a=NULL where a='BC';
826
select 'A' IN (SELECT * from t1);
827
'A' IN (SELECT * from t1)
829
select 'DEF' IN (SELECT * from t1);
830
'DEF' IN (SELECT * from t1)
832
select 'XYZS' IN (SELECT * from t1);
833
'XYZS' IN (SELECT * from t1)
835
select 'A' > ALL (SELECT * from t1);
836
'A' > ALL (SELECT * from t1)
838
select 'XYZS' > ALL (SELECT * from t1);
839
'XYZS' > ALL (SELECT * from t1)
841
select 'A' > ANY (SELECT * from t1);
842
'A' > ANY (SELECT * from t1)
844
select 'XYZS' > ANY (SELECT * from t1);
845
'XYZS' > ANY (SELECT * from t1)
848
create table t1 (a float);
849
insert into t1 values (1.5),(2.5),(3.5);
850
select 1.5 IN (SELECT * from t1);
851
1.5 IN (SELECT * from t1)
853
select 10.5 IN (SELECT * from t1);
854
10.5 IN (SELECT * from t1)
856
select NULL IN (SELECT * from t1);
857
NULL IN (SELECT * from t1)
859
update t1 set a=NULL where a=2.5;
860
select 1.5 IN (SELECT * from t1);
861
1.5 IN (SELECT * from t1)
863
select 3.5 IN (SELECT * from t1);
864
3.5 IN (SELECT * from t1)
866
select 10.5 IN (SELECT * from t1);
867
10.5 IN (SELECT * from t1)
869
select 1.5 > ALL (SELECT * from t1);
870
1.5 > ALL (SELECT * from t1)
872
select 10.5 > ALL (SELECT * from t1);
873
10.5 > ALL (SELECT * from t1)
875
select 1.5 > ANY (SELECT * from t1);
876
1.5 > ANY (SELECT * from t1)
878
select 10.5 > ANY (SELECT * from t1);
879
10.5 > ANY (SELECT * from t1)
881
explain extended select (select a+1) from t1;
882
id select_type table type possible_keys key key_len ref rows filtered Extra
883
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
885
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
886
Note 1249 Select 2 was reduced during optimization
887
Note 1003 select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1`
888
select (select a+1) from t1;
894
CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));
895
CREATE TABLE t2 (a int(11) default '0', INDEX (a));
896
INSERT INTO t1 VALUES (1),(2),(3),(4);
897
INSERT INTO t2 VALUES (1),(2),(3);
898
SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
899
a t1.a in (select t2.a from t2)
904
explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
905
id select_type table type possible_keys key key_len ref rows filtered Extra
906
1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
907
2 SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index
909
Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having <is_not_null_test>(`test`.`t2`.`a`))))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
910
CREATE TABLE t3 (a int(11) default '0');
911
INSERT INTO t3 VALUES (1),(2),(3);
912
SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
913
a t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
918
explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
919
id select_type table type possible_keys key key_len ref rows filtered Extra
920
1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
921
2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index
922
2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
924
Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`))) having <is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
926
# check correct NULL Processing for normal IN/ALL/ANY
927
# and 2 ways of max/min optimization
928
create table t1 (a int);
929
insert into t1 values (1), (100), (NULL), (1000);
930
create table t2 (a int not null);
931
# subselect returns empty set (for NULL and non-NULL left part)
932
select a, a in (select * from t2) from t1;
933
a a in (select * from t2)
938
select a, a > any (select * from t2) from t1;
939
a a > any (select * from t2)
944
select a, a > all (select * from t2) from t1;
945
a a > all (select * from t2)
950
select a from t1 where a in (select * from t2);
952
select a from t1 where a > any (select * from t2);
954
select a from t1 where a > all (select * from t2);
960
select a from t1 where a in (select * from t2 group by a);
962
select a from t1 where a > any (select * from t2 group by a);
964
select a from t1 where a > all (select * from t2 group by a);
970
insert into t2 values (1),(200);
971
# sebselect returns non-empty set without NULLs
972
select a, a in (select * from t2) from t1;
973
a a in (select * from t2)
978
select a, a > any (select * from t2) from t1;
979
a a > any (select * from t2)
984
select a, a > all (select * from t2) from t1;
985
a a > all (select * from t2)
990
select a from t1 where a in (select * from t2);
993
select a from t1 where a > any (select * from t2);
997
select a from t1 where a > all (select * from t2);
1000
select a from t1 where a in (select * from t2 group by a);
1003
select a from t1 where a > any (select * from t2 group by a);
1007
select a from t1 where a > all (select * from t2 group by a);
1011
create table t2 (a int);
1012
insert into t2 values (1),(NULL),(200);
1013
# sebselect returns non-empty set with NULLs
1014
select a, a in (select * from t2) from t1;
1015
a a in (select * from t2)
1020
select a, a > any (select * from t2) from t1;
1021
a a > any (select * from t2)
1026
select a, a > all (select * from t2) from t1;
1027
a a > all (select * from t2)
1032
select a from t1 where a in (select * from t2);
1035
select a from t1 where a > any (select * from t2);
1039
select a from t1 where a > all (select * from t2);
1041
select a from t1 where a in (select * from t2 group by a);
1044
select a from t1 where a > any (select * from t2 group by a);
1048
select a from t1 where a > all (select * from t2 group by a);
1051
create table t1 (a float);
1052
select 10.5 IN (SELECT * from t1 LIMIT 1);
1053
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1054
select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
1055
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1057
create table t1 (a int, b int, c varchar(10));
1058
create table t2 (a int);
1059
insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
1060
insert into t2 values (1),(2),(NULL);
1061
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;
1062
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)
1066
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;
1067
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)
1071
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;
1072
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)
1077
create table t1 (a int, b real, c varchar(10));
1078
insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
1079
select ROW(1, 1, 'a') IN (select a,b,c from t1);
1080
ROW(1, 1, 'a') IN (select a,b,c from t1)
1082
select ROW(1, 2, 'a') IN (select a,b,c from t1);
1083
ROW(1, 2, 'a') IN (select a,b,c from t1)
1085
select ROW(1, 1, 'a') IN (select b,a,c from t1);
1086
ROW(1, 1, 'a') IN (select b,a,c from t1)
1088
select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
1089
ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
1091
select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
1092
ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
1094
select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
1095
ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
1097
select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1098
ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
1100
select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1101
ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
1103
select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
1104
ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
1106
select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
1107
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1109
create table t1 (a int);
1110
insert into t1 values (1);
1111
do @a:=(SELECT a from t1);
1116
set @a:=(SELECT a from t1);
1121
do (SELECT a from t1);
1122
ERROR 42S02: Table 'test.t1' doesn't exist
1123
set @a:=(SELECT a from t1);
1124
ERROR 42S02: Table 'test.t1' doesn't exist
1125
CREATE TABLE t1 (a int, KEY(a));
1127
HANDLER t1 READ a=((SELECT 1));
1128
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1))' at line 1
1131
create table t1 (a int);
1132
create table t2 (b int);
1133
insert into t1 values (1),(2);
1134
insert into t2 values (1);
1135
select a from t1 where a in (select a from t1 where a in (select b from t2));
1139
create table t1 (a int, b int);
1140
create table t2 like t1;
1141
insert into t1 values (1,2),(1,3),(1,4),(1,5);
1142
insert into t2 values (1,2),(1,3);
1143
select * from t1 where row(a,b) in (select a,b from t2);
1148
CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1;
1149
INSERT INTO t1 VALUES (1);
1150
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1155
CREATE TABLE t1 (a int(1));
1156
EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
1157
id select_type table type possible_keys key key_len ref rows filtered Extra
1158
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1159
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1161
Note 1003 select (select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
1162
EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
1163
id select_type table type possible_keys key key_len ref rows filtered Extra
1164
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1165
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1167
Note 1003 select (select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
1168
EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
1169
id select_type table type possible_keys key key_len ref rows filtered Extra
1170
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1171
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
1173
Note 1003 select (select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
1176
`mot` varchar(30) character set latin1 NOT NULL default '',
1177
`topic` mediumint(8) unsigned NOT NULL default '0',
1178
`date` date NOT NULL default '0000-00-00',
1179
`pseudo` varchar(35) character set latin1 NOT NULL default '',
1180
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
1181
KEY `pseudo` (`pseudo`,`date`,`topic`),
1182
KEY `topic` (`topic`)
1183
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1185
`mot` varchar(30) character set latin1 NOT NULL default '',
1186
`topic` mediumint(8) unsigned NOT NULL default '0',
1187
`date` date NOT NULL default '0000-00-00',
1188
`pseudo` varchar(35) character set latin1 NOT NULL default '',
1189
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
1190
KEY `pseudo` (`pseudo`,`date`,`topic`),
1191
KEY `topic` (`topic`)
1192
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1194
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
1195
`maxnumrep` int(10) unsigned NOT NULL default '0',
1196
PRIMARY KEY (`numeropost`),
1197
UNIQUE KEY `maxnumrep` (`maxnumrep`)
1198
) ENGINE=MyISAM CHARSET=latin1;
1199
INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
1201
Warning 1265 Data truncated for column 'date' at row 1
1202
Warning 1265 Data truncated for column 'date' at row 2
1203
INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
1205
Warning 1265 Data truncated for column 'date' at row 1
1206
Warning 1265 Data truncated for column 'date' at row 2
1207
INSERT INTO t3 VALUES (1,1);
1208
SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
1213
mot topic date pseudo
1214
joce 1 0000-00-00 joce
1215
test 2 0000-00-00 test
1216
DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
1217
EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
1219
mot topic date pseudo
1220
joce 1 0000-00-00 joce
1221
drop table t1, t2, t3;
1222
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1225
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
1226
SHOW CREATE TABLE t1;
1228
t1 CREATE TABLE `t1` (
1229
`a` int(1) NOT NULL DEFAULT '0',
1230
`(SELECT 1)` int(1) NOT NULL DEFAULT '0'
1231
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1233
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1234
SHOW CREATE TABLE t1;
1236
t1 CREATE TABLE `t1` (
1237
`a` int(1) NOT NULL DEFAULT '0',
1238
`(SELECT a)` int(1) NOT NULL DEFAULT '0'
1239
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1241
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1242
SHOW CREATE TABLE t1;
1244
t1 CREATE TABLE `t1` (
1245
`a` int(1) NOT NULL DEFAULT '0',
1246
`(SELECT a+0)` int(3) NOT NULL DEFAULT '0'
1247
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1249
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
1253
SHOW CREATE TABLE t1;
1255
t1 CREATE TABLE `t1` (
1256
`a` bigint(20) NOT NULL DEFAULT '0'
1257
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1259
create table t1 (a int);
1260
insert into t1 values (1), (2), (3);
1261
explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
1263
id select_type table type possible_keys key key_len ref rows filtered Extra
1264
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
1265
2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
1266
3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
1268
Note 1003 select `test`.`t1`.`a` AS `a`,(select (select rand() from `test`.`t1` limit 1) from `test`.`t1` limit 1) AS `(select (select rand() from t1 limit 1) from t1 limit 1)` from `test`.`t1`
1270
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);
1271
ERROR 42S02: Table 'test.t1' doesn't exist
1273
ID int(11) NOT NULL auto_increment,
1274
name char(35) NOT NULL default '',
1275
t2 char(3) NOT NULL default '',
1276
District char(20) NOT NULL default '',
1277
Population int(11) NOT NULL default '0',
1280
INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
1281
INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
1282
INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
1284
Code char(3) NOT NULL default '',
1285
Name char(52) NOT NULL default '',
1286
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1287
Region char(26) NOT NULL default '',
1288
SurfaceArea float(10,2) NOT NULL default '0.00',
1289
IndepYear smallint(6) default NULL,
1290
Population int(11) NOT NULL default '0',
1291
LifeExpectancy float(3,1) default NULL,
1292
GNP float(10,2) default NULL,
1293
GNPOld float(10,2) default NULL,
1294
LocalName char(45) NOT NULL default '',
1295
GovernmentForm char(45) NOT NULL default '',
1296
HeadOfState char(60) default NULL,
1297
Capital int(11) default NULL,
1298
Code2 char(2) NOT NULL default '',
1301
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');
1302
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');
1303
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);
1304
Continent Name Population
1305
Oceania Sydney 3276207
1308
`id` mediumint(8) unsigned NOT NULL auto_increment,
1309
`pseudo` varchar(35) character set latin1 NOT NULL default '',
1311
UNIQUE KEY `pseudo` (`pseudo`)
1312
) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1313
INSERT INTO t1 (pseudo) VALUES ('test');
1314
SELECT 0 IN (SELECT 1 FROM t1 a);
1315
0 IN (SELECT 1 FROM t1 a)
1317
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1318
id select_type table type possible_keys key key_len ref rows filtered Extra
1319
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
1320
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
1322
Note 1003 select <in_optimizer>(0,<exists>(select 1 from dual where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
1323
INSERT INTO t1 (pseudo) VALUES ('test1');
1324
SELECT 0 IN (SELECT 1 FROM t1 a);
1325
0 IN (SELECT 1 FROM t1 a)
1327
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1328
id select_type table type possible_keys key key_len ref rows filtered Extra
1329
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
1330
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
1332
Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
1335
`i` int(11) NOT NULL default '0',
1337
) ENGINE=MyISAM CHARSET=latin1;
1338
INSERT INTO t1 VALUES (1);
1339
UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
1340
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1341
UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
1342
ERROR 42S22: Unknown column 't.i' in 'field list'
1348
id int(11) default NULL
1349
) ENGINE=MyISAM CHARSET=latin1;
1350
INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
1352
id int(11) default NULL,
1353
name varchar(15) default NULL
1354
) ENGINE=MyISAM CHARSET=latin1;
1355
INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
1356
update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
1364
create table t1 (a int, unique index indexa (a));
1365
insert into t1 values (-1), (-4), (-2), (NULL);
1366
select -10 IN (select a from t1 FORCE INDEX (indexa));
1367
-10 IN (select a from t1 FORCE INDEX (indexa))
1370
create table t1 (id int not null auto_increment primary key, salary int, key(salary));
1371
insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
1372
explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
1373
id select_type table type possible_keys key key_len ref rows filtered Extra
1374
1 PRIMARY t1 ref salary salary 5 const 1 100.00 Using where
1375
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
1377
Note 1003 select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) from `test`.`t1`))
1380
ID int(10) unsigned NOT NULL auto_increment,
1381
SUB_ID int(3) unsigned NOT NULL default '0',
1382
REF_ID int(10) unsigned default NULL,
1383
REF_SUB int(3) unsigned default '0',
1384
PRIMARY KEY (ID,SUB_ID),
1385
UNIQUE KEY t1_PK (ID,SUB_ID),
1386
KEY t1_FK (REF_ID,REF_SUB),
1387
KEY t1_REFID (REF_ID)
1388
) ENGINE=MyISAM CHARSET=cp1251;
1389
INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
1390
SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
1393
create table t1 (a int, b int);
1394
create table t2 (a int, b int);
1395
insert into t1 values (1,0), (2,0), (3,0);
1396
insert into t2 values (1,1), (2,1), (3,1), (2,2);
1397
update ignore t1 set b=(select b from t2 where t1.a=t2.a);
1399
Warning 1242 Subquery returns more than 1 row
1407
`id` mediumint(8) unsigned NOT NULL auto_increment,
1408
`pseudo` varchar(35) NOT NULL default '',
1409
`email` varchar(60) NOT NULL default '',
1411
UNIQUE KEY `email` (`email`),
1412
UNIQUE KEY `pseudo` (`pseudo`)
1413
) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1414
INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
1415
SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
1419
drop table if exists t1;
1420
(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
1423
create table t1 (a int not null, b int, primary key (a));
1424
create table t2 (a int not null, primary key (a));
1425
create table t3 (a int not null, b int, primary key (a));
1426
insert into t1 values (1,10), (2,20), (3,30), (4,40);
1427
insert into t2 values (2), (3), (4), (5);
1428
insert into t3 values (10,3), (20,4), (30,5);
1429
select * from t2 where t2.a in (select a from t1);
1434
explain extended select * from t2 where t2.a in (select a from t1);
1435
id select_type table type possible_keys key key_len ref rows filtered Extra
1436
1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
1437
1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 4 75.00 Using where; Using index; Using join buffer (flat, BNL join)
1439
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)
1440
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1444
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1445
id select_type table type possible_keys key key_len ref rows filtered Extra
1446
1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
1447
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
1449
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
1450
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1454
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1455
id select_type table type possible_keys key key_len ref rows filtered Extra
1456
1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
1457
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
1458
1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 100.00 Using index
1460
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t3` join `test`.`t2` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (`test`.`t1`.`a` = `test`.`t2`.`a`))
1461
drop table t1, t2, t3;
1462
create table t1 (a int, b int, index a (a,b));
1463
create table t2 (a int, index a (a));
1464
create table t3 (a int, b int, index a (a));
1465
insert into t1 values (1,10), (2,20), (3,30), (4,40);
1466
create table t0(a int);
1467
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1469
select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
1470
insert into t2 values (2), (3), (4), (5);
1471
insert into t3 values (10,3), (20,4), (30,5);
1472
select * from t2 where t2.a in (select a from t1);
1477
explain extended select * from t2 where t2.a in (select a from t1);
1478
id select_type table type possible_keys key key_len ref rows filtered Extra
1479
1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
1480
1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using index; FirstMatch(t2)
1482
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
1483
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1487
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1488
id select_type table type possible_keys key key_len ref rows filtered Extra
1489
1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
1490
1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2)
1492
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
1493
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1497
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1498
id select_type table type possible_keys key key_len ref rows filtered Extra
1499
1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
1500
1 PRIMARY t3 index a a 5 NULL 3 100.00 Using where; Using index
1501
1 PRIMARY t1 ref a a 10 test.t2.a,test.t3.a 116 100.00 Using index; FirstMatch(t2)
1503
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1` join `test`.`t3`) where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (`test`.`t1`.`a` = `test`.`t2`.`a`))
1504
insert into t1 values (3,31);
1505
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1510
select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
1514
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1515
id select_type table type possible_keys key key_len ref rows filtered Extra
1516
1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
1517
1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2)
1519
Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
1520
drop table t0, t1, t2, t3;
1521
create table t1 (a int, b int);
1522
create table t2 (a int, b int);
1523
create table t3 (a int, b int);
1524
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
1525
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
1526
insert into t3 values (3,3), (2,2), (1,1);
1527
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;
1528
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)
1532
drop table t1,t2,t3;
1533
create table t1 (s1 int);
1534
create table t2 (s1 int);
1535
insert into t1 values (1);
1536
insert into t2 values (1);
1537
select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
1541
create table t1 (s1 int);
1542
create table t2 (s1 int);
1543
insert into t1 values (1);
1544
insert into t2 values (1);
1545
update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
1546
ERROR 42S22: Unknown column 'x.s1' in 'field list'
1548
CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
1549
s2 CHAR(5) COLLATE latin1_swedish_ci);
1550
INSERT INTO t1 VALUES ('z','?');
1551
select * from t1 where s1 > (select max(s2) from t1);
1552
ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
1553
select * from t1 where s1 > any (select max(s2) from t1);
1554
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
1556
create table t1(toid int,rd int);
1557
create table t2(userid int,pmnew int,pmtotal int);
1558
insert into t2 values(1,0,0),(2,0,0);
1559
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);
1560
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);
1561
userid pmtotal pmnew calc_total calc_new
1565
create table t1 (s1 char(5));
1566
select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
1567
ERROR 21000: Operand should contain 1 column(s)
1568
insert into t1 values ('tttt');
1569
select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
1572
explain extended (select * from t1);
1573
id select_type table type possible_keys key key_len ref rows filtered Extra
1574
1 SIMPLE t1 system NULL NULL NULL NULL 1 100.00
1576
Note 1003 (select 'tttt' AS `s1` from dual)
1581
create table t1 (s1 char(5), index s1(s1));
1582
create table t2 (s1 char(5), index s1(s1));
1583
insert into t1 values ('a1'),('a2'),('a3');
1584
insert into t2 values ('a1'),('a2');
1585
select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1586
s1 s1 NOT IN (SELECT s1 FROM t2)
1590
select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1591
s1 s1 = ANY (SELECT s1 FROM t2)
1595
select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1596
s1 s1 <> ALL (SELECT s1 FROM t2)
1600
select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1601
s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
1605
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1606
id select_type table type possible_keys key key_len ref rows filtered Extra
1607
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1608
2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1610
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
1611
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1612
id select_type table type possible_keys key key_len ref rows filtered Extra
1613
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1614
2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1616
Note 1003 select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
1617
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1618
id select_type table type possible_keys key key_len ref rows filtered Extra
1619
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1620
2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1622
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
1623
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1624
id select_type table type possible_keys key key_len ref rows filtered Extra
1625
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1626
2 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Using where; Full scan on NULL key
1628
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < 'a2') having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
1630
create table t2 (a int, b int not null);
1631
create table t3 (a int);
1632
insert into t3 values (6),(7),(3);
1633
select * from t3 where a >= all (select b from t2);
1638
explain extended select * from t3 where a >= all (select b from t2);
1639
id select_type table type possible_keys key key_len ref rows filtered Extra
1640
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1641
2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
1643
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
1644
select * from t3 where a >= some (select b from t2);
1646
explain extended select * from t3 where a >= some (select b from t2);
1647
id select_type table type possible_keys key key_len ref rows filtered Extra
1648
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1649
2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
1651
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
1652
select * from t3 where a >= all (select b from t2 group by 1);
1657
explain extended select * from t3 where a >= all (select b from t2 group by 1);
1658
id select_type table type possible_keys key key_len ref rows filtered Extra
1659
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1660
2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
1662
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
1663
select * from t3 where a >= some (select b from t2 group by 1);
1665
explain extended select * from t3 where a >= some (select b from t2 group by 1);
1666
id select_type table type possible_keys key key_len ref rows filtered Extra
1667
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1668
2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
1670
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
1671
select * from t3 where NULL >= any (select b from t2);
1673
explain extended select * from t3 where NULL >= any (select b from t2);
1674
id select_type table type possible_keys key key_len ref rows filtered Extra
1675
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
1676
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
1678
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1679
select * from t3 where NULL >= any (select b from t2 group by 1);
1681
explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
1682
id select_type table type possible_keys key key_len ref rows filtered Extra
1683
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
1684
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
1686
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1687
select * from t3 where NULL >= some (select b from t2);
1689
explain extended select * from t3 where NULL >= some (select b from t2);
1690
id select_type table type possible_keys key key_len ref rows filtered Extra
1691
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
1692
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
1694
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1695
select * from t3 where NULL >= some (select b from t2 group by 1);
1697
explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
1698
id select_type table type possible_keys key key_len ref rows filtered Extra
1699
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
1700
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
1702
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1703
insert into t2 values (2,2), (2,1), (3,3), (3,1);
1704
select * from t3 where a > all (select max(b) from t2 group by a);
1708
explain extended select * from t3 where a > all (select max(b) from t2 group by a);
1709
id select_type table type possible_keys key key_len ref rows filtered Extra
1710
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1711
2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary
1713
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(<max>(select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`))))
1715
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 ;
1716
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());
1717
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;
1718
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);
1719
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 ;
1720
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);
1721
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1722
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
1723
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;
1724
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')
1727
-1 Should Not Return 0
1728
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;
1732
drop table t1,t2,t3,t4;
1733
CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1734
INSERT INTO t1 VALUES (1),(5);
1735
CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1736
INSERT INTO t2 VALUES (2),(6);
1737
select * from t1 where (1,2,6) in (select * from t2);
1738
ERROR 21000: Operand should contain 3 column(s)
1740
create table t1 (s1 int);
1741
insert into t1 values (1);
1742
insert into t1 values (2);
1743
set sort_buffer_size = (select s1 from t1);
1744
ERROR 21000: Subquery returns more than 1 row
1745
do (select * from t1);
1747
Error 1242 Subquery returns more than 1 row
1749
create table t1 (s1 char);
1750
insert into t1 values ('e');
1751
select * from t1 where 'f' > any (select s1 from t1);
1754
select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1757
explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1758
id select_type table type possible_keys key key_len ref rows filtered Extra
1759
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
1760
2 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
1761
3 UNION t1 system NULL NULL NULL NULL 1 100.00
1762
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
1764
Note 1003 select 'e' AS `s1` from dual where 1
1766
CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1767
INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
1768
CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
1769
INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
1770
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;
1777
create table t1 (s1 int);
1778
create table t2 (s1 int);
1779
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
1780
ERROR 42S22: Unknown column 't1.s2' in 'where clause'
1781
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
1782
ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1783
select count(*) from t2 group by t1.s2;
1784
ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1786
CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
1787
CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1788
INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1789
INSERT INTO t2 VALUES (100, 200, 'C');
1790
SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1793
CREATE TABLE t1 (a int(1));
1794
INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1795
SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1803
create table t1 (a int, b decimal(13, 3));
1804
insert into t1 values (1, 0.123);
1805
select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
1807
load data infile "../../tmp/subselect.out.file.1" into table t1;
1813
`id` int(11) NOT NULL auto_increment,
1814
`id_cns` tinyint(3) unsigned NOT NULL default '0',
1815
`tipo` enum('','UNO','DUE') NOT NULL default '',
1816
`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1817
`particolare` mediumint(8) unsigned NOT NULL default '0',
1818
`generale` mediumint(8) unsigned NOT NULL default '0',
1819
`bis` tinyint(3) unsigned NOT NULL default '0',
1821
UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1822
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1824
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);
1826
`id` tinyint(3) unsigned NOT NULL auto_increment,
1827
`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1830
INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1831
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;
1832
id max_anno_dep PIPPO
1837
create table t1 (a int);
1838
insert into t1 values (1), (2), (3);
1839
SET SQL_SELECT_LIMIT=1;
1840
select sum(a) from (select * from t1) as a;
1843
select 2 in (select * from t1);
1844
2 in (select * from t1)
1846
SET SQL_SELECT_LIMIT=default;
1848
CREATE TABLE t1 (a int, b int, INDEX (a));
1849
INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1850
SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1856
create table t1(val varchar(10));
1857
insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1858
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%');
1862
create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1863
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');
1864
select * from t1 where id not in (select id from t1 where id < 8);
1871
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);
1878
explain extended select * from t1 where id not in (select id from t1 where id < 8);
1879
id select_type table type possible_keys key key_len ref rows filtered Extra
1880
1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where
1881
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where
1883
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where ((`test`.`t1`.`id` < 8) and (<cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`))))))))
1884
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);
1885
id select_type table type possible_keys key key_len ref rows filtered Extra
1886
1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where
1887
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
1889
Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1890
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))
1891
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1892
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1893
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');
1894
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);
1895
id text id text id text
1896
1 text1 1 text1 1 text1
1897
2 text2 2 text2 2 text2
1898
3 text3 3 text3 3 text3
1899
4 text4 4 text4 4 text4
1900
5 text5 5 text5 5 text5
1901
6 text6 6 text6 6 text6
1902
7 text7 7 text7 7 text7
1903
8 text8 8 text8 8 text8
1904
9 text9 9 text9 9 text9
1905
10 text10 10 text10 10 text10
1906
11 text11 11 text1 11 text11
1907
12 text12 12 text2 12 text12
1908
1000 text1000 NULL NULL 1000 text1000
1909
1001 text1001 NULL NULL 1000 text1000
1910
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);
1911
id select_type table type possible_keys key key_len ref rows filtered Extra
1912
1 SIMPLE a ALL NULL NULL NULL NULL 14 100.00
1913
1 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00
1914
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition
1916
Note 1003 select `test`.`a`.`id` AS `id`,`test`.`a`.`text` AS `text`,`test`.`b`.`id` AS `id`,`test`.`b`.`text` AS `text`,`test`.`c`.`id` AS `id`,`test`.`c`.`text` AS `text` from `test`.`t1` `a` left join `test`.`t2` `b` on(((`test`.`b`.`id` = `test`.`a`.`id`) or isnull(`test`.`b`.`id`))) join `test`.`t1` `c` where (if(isnull(`test`.`b`.`id`),1000,`test`.`b`.`id`) = `test`.`c`.`id`)
1918
create table t1 (a int);
1919
insert into t1 values (1);
1920
explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1921
id select_type table type possible_keys key key_len ref rows Extra
1922
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
1923
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1
1925
create table t1(id int);
1926
create table t2(id int);
1927
create table t3(flag int);
1928
select (select * from t3 where id not null) from t1, t2;
1929
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'null) from t1, t2' at line 1
1930
drop table t1,t2,t3;
1931
CREATE TABLE t1 (id INT);
1932
CREATE TABLE t2 (id INT);
1933
INSERT INTO t1 VALUES (1), (2);
1934
INSERT INTO t2 VALUES (1);
1935
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);
1939
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1943
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;
1947
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;
1952
CREATE TABLE t1 ( a int, b int );
1953
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1954
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1957
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1960
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1963
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1967
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1971
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1975
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1978
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1981
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1984
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1988
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1992
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
1996
ALTER TABLE t1 ADD INDEX (a);
1997
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
2000
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
2003
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
2006
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
2010
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
2014
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
2018
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
2021
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
2024
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
2027
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
2031
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2035
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2039
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
2042
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
2045
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
2048
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
2052
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
2056
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
2060
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
2063
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
2066
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
2069
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
2073
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
2077
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
2081
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2084
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2087
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2090
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2094
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2098
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2102
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2105
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2108
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2111
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2115
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2119
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2123
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2126
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2129
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2132
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2136
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2140
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2144
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2147
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2150
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2153
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2157
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2161
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2165
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
2166
ERROR 21000: Operand should contain 1 column(s)
2167
SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2168
ERROR 21000: Operand should contain 1 column(s)
2169
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2170
ERROR 21000: Operand should contain 1 column(s)
2171
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
2172
ERROR 21000: Operand should contain 1 column(s)
2173
SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2174
ERROR 21000: Operand should contain 1 column(s)
2175
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2176
ERROR 21000: Operand should contain 1 column(s)
2177
SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
2178
ERROR 21000: Operand should contain 1 column(s)
2179
SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
2180
ERROR 21000: Operand should contain 1 column(s)
2181
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
2182
ERROR 21000: Operand should contain 2 column(s)
2183
SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2184
ERROR 21000: Operand should contain 1 column(s)
2185
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2187
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
2188
ERROR 21000: Operand should contain 2 column(s)
2189
SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2190
ERROR 21000: Operand should contain 1 column(s)
2191
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2196
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
2199
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
2203
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
2206
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
2210
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);
2213
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);
2217
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);
2220
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);
2224
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2227
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2230
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2233
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2237
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2241
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2245
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2248
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2251
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2254
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2258
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2262
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2266
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
2269
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
2272
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
2275
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2279
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2283
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
2287
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
2290
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
2293
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
2296
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2300
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2304
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
2308
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
2309
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
2313
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
2314
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
2318
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
2319
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
2324
CREATE TABLE t1 ( a double, b double );
2325
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
2326
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
2329
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
2332
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
2335
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
2339
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
2343
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
2347
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
2350
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
2353
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
2356
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
2360
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
2364
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
2369
CREATE TABLE t1 ( a char(1), b char(1));
2370
INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
2371
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
2374
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
2377
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
2380
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
2384
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
2388
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
2392
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
2395
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
2398
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
2401
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
2405
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
2409
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
2414
create table t1 (a int, b int);
2415
insert into t1 values (1,2),(3,4);
2416
select * from t1 up where exists (select * from t1 where t1.a=up.a);
2420
explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
2421
id select_type table type possible_keys key key_len ref rows filtered Extra
2422
1 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 Using where
2423
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
2425
Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2426
Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)))
2428
CREATE TABLE t1 (t1_a int);
2429
INSERT INTO t1 VALUES (1);
2430
CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
2431
INSERT INTO t2 VALUES (1, 1), (1, 2);
2432
SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
2433
HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
2437
CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
2438
INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
2439
CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
2440
INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
2441
SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
2447
CREATE TABLE t1 ( a int, b int );
2448
CREATE TABLE t2 ( c int, d int );
2449
INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
2450
SELECT a AS abc, b FROM t1 outr WHERE b =
2451
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2456
INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
2457
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2463
CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
2464
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2470
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);";
2472
deallocate prepare stmt1;
2482
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);";
2489
deallocate prepare stmt1;
2490
DROP TABLE t1, t2, t3;
2491
CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2492
insert into t1 values (1);
2493
CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2494
insert into t2 values (1,2);
2495
select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
2499
create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
2500
insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
2501
create table t2 (a int);
2502
insert into t2 values (1),(3),(2),(7);
2503
select a,b from t1 where match(b) against ('Ball') > 0;
2507
select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
2512
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);
2513
CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
2514
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
2515
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
2516
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
2517
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
2518
SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
2522
CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
2523
CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
2524
insert into t1 values (1,1),(1,2),(2,1),(2,2);
2525
insert into t2 values (1,2),(2,2);
2526
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2530
alter table t2 drop primary key;
2531
alter table t2 add key KEY1 (aid, bid);
2532
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2536
alter table t2 drop key KEY1;
2537
alter table t2 add primary key (bid, aid);
2538
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2543
CREATE TABLE t1 (howmanyvalues bigint, avalue int);
2544
INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
2545
SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
2546
howmanyvalues count(*)
2551
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2552
howmanyvalues mycount
2557
CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
2558
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
2559
howmanyvalues mycount
2564
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2565
howmanyvalues mycount
2570
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
2571
howmanyvalues mycount
2577
create table t1 (x int);
2578
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;
2579
(select b.x from t1 as b where b.x=a.x)
2581
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`));
2582
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);
2583
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`)) ;
2584
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');
2585
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;
2586
ERROR 42S22: Unknown column 'b.sc' in 'field list'
2587
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;
2592
create table t1 (a int not null, b int not null, c int, primary key (a,b));
2593
insert into t1 values (1,1,1), (2,2,2), (3,3,3);
2595
explain select sum(a) from t1 where b > @b;
2596
id select_type table type possible_keys key key_len ref rows Extra
2597
1 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index
2598
set @a:= (select sum(a) from t1 where b > @b);
2599
explain select a from t1 where c=2;
2600
id select_type table type possible_keys key key_len ref rows Extra
2601
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
2602
do @a:= (select sum(a) from t1 where b > @b);
2603
explain select a from t1 where c=2;
2604
id select_type table type possible_keys key key_len ref rows Extra
2605
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
2607
set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
2608
create table t1 (a int, b int);
2609
create table t2 (a int, b int);
2610
insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
2611
insert into t2 values (1,3),(2,1);
2612
select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
2613
a b (select max(b) from t2 where t1.b=t2.a)
2620
create table t1 (id int);
2621
create table t2 (id int, body text, fulltext (body));
2622
insert into t1 values(1),(2),(3);
2623
insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
2624
select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
2628
create table t1 (s1 int,s2 int);
2629
insert into t1 values (20,15);
2630
select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
2633
create table t1 (s1 int);
2634
insert into t1 values (1),(null);
2635
select * from t1 where s1 < all (select s1 from t1);
2637
select s1, s1 < all (select s1 from t1) from t1;
2638
s1 s1 < all (select s1 from t1)
2643
Code char(3) NOT NULL default '',
2644
Name char(52) NOT NULL default '',
2645
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
2646
Region char(26) NOT NULL default '',
2647
SurfaceArea float(10,2) NOT NULL default '0.00',
2648
IndepYear smallint(6) default NULL,
2649
Population int(11) NOT NULL default '0',
2650
LifeExpectancy float(3,1) default NULL,
2651
GNP float(10,2) default NULL,
2652
GNPOld float(10,2) default NULL,
2653
LocalName char(45) NOT NULL default '',
2654
GovernmentForm char(45) NOT NULL default '',
2655
HeadOfState char(60) default NULL,
2656
Capital int(11) default NULL,
2657
Code2 char(2) NOT NULL default ''
2659
INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
2660
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');
2661
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');
2662
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');
2663
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
2664
SELECT DISTINCT Continent AS c FROM t1 outr WHERE
2665
Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
2670
create table t1 (a1 int);
2671
create table t2 (b1 int);
2672
select * from t1 where a2 > any(select b1 from t2);
2673
ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
2674
select * from t1 where a1 > any(select b1 from t2);
2677
create table t1 (a integer, b integer);
2678
select (select * from t1) = (select 1,2);
2679
(select * from t1) = (select 1,2)
2681
select (select 1,2) = (select * from t1);
2682
(select 1,2) = (select * from t1)
2684
select row(1,2) = ANY (select * from t1);
2685
row(1,2) = ANY (select * from t1)
2687
select row(1,2) != ALL (select * from t1);
2688
row(1,2) != ALL (select * from t1)
2691
create table t1 (a integer, b integer);
2692
select row(1,(2,2)) in (select * from t1 );
2693
ERROR 21000: Operand should contain 2 column(s)
2694
select row(1,(2,2)) = (select * from t1 );
2695
ERROR 21000: Operand should contain 2 column(s)
2696
select (select * from t1) = row(1,(2,2));
2697
ERROR 21000: Operand should contain 1 column(s)
2699
create table t1 (a integer);
2700
insert into t1 values (1);
2701
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
2702
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2703
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2704
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2705
select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
2706
xx 1 = ALL ( select 1 from t1 where 1 = xx )
2708
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2709
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2710
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
2711
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2714
categoryId int(11) NOT NULL,
2715
courseId int(11) NOT NULL,
2716
startDate datetime NOT NULL,
2717
endDate datetime NOT NULL,
2718
createDate datetime NOT NULL,
2719
modifyDate timestamp NOT NULL,
2720
attributes text NOT NULL
2722
INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2723
(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2724
(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2725
(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
2726
(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
2727
(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2728
(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2729
(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2730
(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
2732
userId int(11) NOT NULL,
2733
courseId int(11) NOT NULL,
2734
date datetime NOT NULL
2736
INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
2737
(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
2738
(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
2739
(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
2740
(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
2741
(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
2743
groupId int(11) NOT NULL,
2744
parentId int(11) NOT NULL,
2745
startDate datetime NOT NULL,
2746
endDate datetime NOT NULL,
2747
createDate datetime NOT NULL,
2748
modifyDate timestamp NOT NULL,
2751
INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
2753
id int(11) NOT NULL,
2754
groupTypeId int(11) NOT NULL,
2755
groupKey varchar(50) NOT NULL,
2759
createDate datetime NOT NULL,
2760
modifyDate timestamp NOT NULL
2762
INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
2763
(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
2765
userId int(11) NOT NULL,
2766
groupId int(11) NOT NULL,
2767
createDate datetime NOT NULL,
2768
modifyDate timestamp NOT NULL
2770
INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
2772
count(distinct t2.userid) pass,
2774
count(t2.courseid) crse,
2777
date_format(date, '%b%y') as colhead
2779
join t1 on t2.courseid=t1.courseid
2792
select t4.id as parentid,
2793
t4.name as parentgroup,
2795
t4.name as groupname,
2798
) as gin on t5.groupid=gin.childid
2799
) as groupstuff on t2.userid = groupstuff.userid
2801
groupstuff.groupname, colhead , t2.courseid;
2802
pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead
2803
1 5141 12 group2 12 group2 5 1 5 12 Aug04
2804
1 5141 12 group2 12 group2 5 1 1 41 Aug04
2805
1 5141 12 group2 12 group2 5 1 2 52 Aug04
2806
1 5141 12 group2 12 group2 5 1 2 53 Aug04
2807
1 5141 12 group2 12 group2 5 1 3 51 Oct04
2808
1 5141 12 group2 12 group2 5 1 1 86 Oct04
2809
1 5141 12 group2 12 group2 5 1 1 87 Oct04
2810
1 5141 12 group2 12 group2 5 1 2 88 Oct04
2811
1 5141 12 group2 12 group2 5 1 2 89 Oct04
2812
drop table t1, t2, t3, t4, t5;
2813
create table t1 (a int);
2814
insert into t1 values (1), (2), (3);
2815
SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
2821
create table t1 (a int);
2822
create table t2 (a int);
2823
insert into t1 values (1),(2);
2824
insert into t2 values (0),(1),(2),(3);
2825
select a from t2 where a in (select a from t1);
2829
select a from t2 having a in (select a from t1);
2833
prepare stmt1 from "select a from t2 where a in (select a from t1)";
2842
deallocate prepare stmt1;
2843
prepare stmt1 from "select a from t2 having a in (select a from t1)";
2852
deallocate prepare stmt1;
2854
create table t1 (a int, b int);
2855
insert into t1 values (1,2);
2856
select 1 = (select * from t1);
2857
ERROR 21000: Operand should contain 1 column(s)
2858
select (select * from t1) = 1;
2859
ERROR 21000: Operand should contain 2 column(s)
2860
select (1,2) = (select a from t1);
2861
ERROR 21000: Operand should contain 2 column(s)
2862
select (select a from t1) = (1,2);
2863
ERROR 21000: Operand should contain 1 column(s)
2864
select (1,2,3) = (select * from t1);
2865
ERROR 21000: Operand should contain 3 column(s)
2866
select (select * from t1) = (1,2,3);
2867
ERROR 21000: Operand should contain 2 column(s)
2870
`itemid` bigint(20) unsigned NOT NULL auto_increment,
2871
`sessionid` bigint(20) unsigned default NULL,
2872
`time` int(10) unsigned NOT NULL default '0',
2873
`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2875
`data` text collate latin1_general_ci NOT NULL,
2876
PRIMARY KEY (`itemid`)
2877
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2878
INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2880
`sessionid` bigint(20) unsigned NOT NULL auto_increment,
2881
`pid` int(10) unsigned NOT NULL default '0',
2882
`date` int(10) unsigned NOT NULL default '0',
2883
`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2884
PRIMARY KEY (`sessionid`)
2885
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2886
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2887
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;
2888
ip count( e.itemid )
2891
create table t1 (fld enum('0','1'));
2892
insert into t1 values ('1');
2893
select * from (select max(fld) from t1) as foo;
2897
set @tmp11867_optimizer_switch=@@optimizer_switch;
2898
set optimizer_switch='semijoin_with_cache=off';
2899
CREATE TABLE t1 (one int, two int, flag char(1));
2900
CREATE TABLE t2 (one int, two int, flag char(1));
2901
INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2902
INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2904
WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
2909
WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
2913
insert into t2 values (null,null,'N');
2914
insert into t2 values (null,3,'0');
2915
insert into t2 values (null,5,'0');
2916
insert into t2 values (10,null,'0');
2917
insert into t1 values (10,3,'0');
2918
insert into t1 values (10,5,'0');
2919
insert into t1 values (10,10,'0');
2920
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
2930
SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2934
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
2944
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2954
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2964
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2965
id select_type table type possible_keys key key_len ref rows filtered Extra
2966
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
2967
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
2969
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
2970
explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2971
id select_type table type possible_keys key key_len ref rows filtered Extra
2972
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
2973
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00
2974
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
2976
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where (`test`.`t2`.`flag` = 'N')
2977
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2978
id select_type table type possible_keys key key_len ref rows filtered Extra
2979
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
2980
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
2982
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
2984
set optimizer_switch=@tmp11867_optimizer_switch;
2985
CREATE TABLE t1 (a char(5), b char(5));
2986
INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
2987
SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
2991
CREATE TABLE t1 (a int);
2992
CREATE TABLE t2 (a int, b int);
2993
CREATE TABLE t3 (b int NOT NULL);
2994
INSERT INTO t1 VALUES (1), (2), (3), (4);
2995
INSERT INTO t2 VALUES (1,10), (3,30);
2996
SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
2997
WHERE t3.b IS NOT NULL OR t2.a > 10;
3000
WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3001
WHERE t3.b IS NOT NULL OR t2.a > 10);
3007
DROP TABLE t1,t2,t3;
3008
CREATE TABLE t1 (f1 INT);
3009
CREATE TABLE t2 (f2 INT);
3010
INSERT INTO t1 VALUES (1);
3011
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
3014
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
3017
INSERT INTO t2 VALUES (1);
3018
INSERT INTO t2 VALUES (2);
3019
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
3023
select 1 from dual where 1 < any (select 2);
3026
select 1 from dual where 1 < all (select 2);
3029
select 1 from dual where 2 > any (select 1);
3032
select 1 from dual where 2 > all (select 1);
3035
select 1 from dual where 1 < any (select 2 from dual);
3038
select 1 from dual where 1 < all (select 2 from dual where 1!=1);
3041
create table t1 (s1 char);
3042
insert into t1 values (1),(2);
3043
select * from t1 where (s1 < any (select s1 from t1));
3046
select * from t1 where not (s1 < any (select s1 from t1));
3049
select * from t1 where (s1 < ALL (select s1+1 from t1));
3052
select * from t1 where not(s1 < ALL (select s1+1 from t1));
3055
select * from t1 where (s1+1 = ANY (select s1 from t1));
3058
select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
3061
select * from t1 where (s1 = ALL (select s1/s1 from t1));
3064
select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
3069
retailerID varchar(8) NOT NULL,
3070
statusID int(10) unsigned NOT NULL,
3071
changed datetime NOT NULL,
3072
UNIQUE KEY retailerID (retailerID, statusID, changed)
3074
INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
3075
INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
3076
INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
3077
INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
3078
INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
3079
INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
3081
where (r1.retailerID,(r1.changed)) in
3082
(SELECT r2.retailerId,(max(changed)) from t1 r2
3083
group by r2.retailerId);
3084
retailerID statusID changed
3085
0026 2 2006-01-06 12:25:53
3086
0037 2 2006-01-06 12:25:53
3087
0048 1 2006-01-06 12:37:50
3088
0059 1 2006-01-06 12:37:50
3090
create table t1(a int, primary key (a));
3091
insert into t1 values (10);
3092
create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
3093
insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
3094
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3095
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3096
ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3097
id select_type table type possible_keys key key_len ref rows Extra
3098
1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
3099
1 PRIMARY r const PRIMARY PRIMARY 4 const 1
3100
2 SUBQUERY t2 range b b 40 NULL 2 Using where
3101
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3102
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3103
ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3106
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3107
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3108
ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3109
id select_type table type possible_keys key key_len ref rows Extra
3110
1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
3111
1 PRIMARY r const PRIMARY PRIMARY 4 const 1
3112
2 SUBQUERY t2 range b b 40 NULL 2 Using index condition
3113
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3114
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3115
ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3120
field1 int NOT NULL,
3121
field2 int NOT NULL,
3122
field3 int NOT NULL,
3123
PRIMARY KEY (field1,field2,field3)
3126
fieldA int NOT NULL,
3127
fieldB int NOT NULL,
3128
PRIMARY KEY (fieldA,fieldB)
3130
INSERT INTO t1 VALUES
3131
(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
3132
INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
3133
SELECT field1, field2, COUNT(*)
3134
FROM t1 GROUP BY field1, field2;
3135
field1 field2 COUNT(*)
3139
SELECT field1, field2
3141
GROUP BY field1, field2
3142
HAVING COUNT(*) >= ALL (SELECT fieldB
3143
FROM t2 WHERE fieldA = field1);
3146
SELECT field1, field2
3148
GROUP BY field1, field2
3149
HAVING COUNT(*) < ANY (SELECT fieldB
3150
FROM t2 WHERE fieldA = field1);
3155
CREATE TABLE t1(a int, INDEX (a));
3156
INSERT INTO t1 VALUES (1), (3), (5), (7);
3157
INSERT INTO t1 VALUES (NULL);
3158
CREATE TABLE t2(a int);
3159
INSERT INTO t2 VALUES (1),(2),(3);
3160
EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
3161
id select_type table type possible_keys key key_len ref rows Extra
3162
1 PRIMARY t2 ALL NULL NULL NULL NULL 3
3163
2 SUBQUERY t1 index_subquery a a 5 func 2 Using index; Full scan on NULL key
3164
SELECT a, a IN (SELECT a FROM t1) FROM t2;
3165
a a IN (SELECT a FROM t1)
3170
CREATE TABLE t1 (a DATETIME);
3171
INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
3172
CREATE TABLE t2 AS SELECT
3173
(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
3174
FROM t1 WHERE a > '2000-01-01';
3175
SHOW CREATE TABLE t2;
3177
t2 CREATE TABLE `t2` (
3178
`sub_a` datetime DEFAULT NULL
3179
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3180
CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
3181
SHOW CREATE TABLE t3;
3183
t3 CREATE TABLE `t3` (
3184
`a` datetime DEFAULT NULL
3185
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3186
DROP TABLE t1,t2,t3;
3187
CREATE TABLE t1 (a int);
3188
INSERT INTO t1 VALUES (1), (2);
3189
SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
3191
SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3195
EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3196
id select_type table type possible_keys key key_len ref rows Extra
3197
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
3198
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
3200
CREATE TABLE t1 (a int);
3201
INSERT INTO t1 VALUES (2), (4), (1), (3);
3202
CREATE TABLE t2 (b int, c int);
3203
INSERT INTO t2 VALUES
3204
(2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
3205
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
3211
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
3212
ERROR 21000: Subquery returns more than 1 row
3213
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
3219
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
3220
ERROR 21000: Subquery returns more than 1 row
3221
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
3226
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
3227
ERROR 21000: Subquery returns more than 1 row
3228
SELECT a FROM t1 GROUP BY a
3229
HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
3230
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3236
SELECT a FROM t1 GROUP BY a
3237
HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
3238
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3239
ERROR 21000: Subquery returns more than 1 row
3240
SELECT a FROM t1 GROUP BY a
3241
HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3242
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3245
SELECT a FROM t1 GROUP BY a
3246
HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3247
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
3248
ERROR 21000: Subquery returns more than 1 row
3250
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
3251
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3258
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
3259
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3260
ERROR 21000: Subquery returns more than 1 row
3262
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3263
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3270
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3271
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3272
ERROR 21000: Subquery returns more than 1 row
3274
create table t1 (df decimal(5,1));
3275
insert into t1 values(1.1);
3276
insert into t1 values(2.2);
3277
select * from t1 where df <= all (select avg(df) from t1 group by df);
3280
select * from t1 where df >= all (select avg(df) from t1 group by df);
3284
create table t1 (df decimal(5,1));
3285
insert into t1 values(1.1);
3286
select 1.1 * exists(select * from t1);
3287
1.1 * exists(select * from t1)
3291
grp int(11) default NULL,
3292
a decimal(10,2) default NULL);
3293
insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
3303
select min(a) from t1 group by grp;
3310
CREATE table t1 ( c1 integer );
3311
INSERT INTO t1 VALUES ( 1 );
3312
INSERT INTO t1 VALUES ( 2 );
3313
INSERT INTO t1 VALUES ( 3 );
3314
CREATE TABLE t2 ( c2 integer );
3315
INSERT INTO t2 VALUES ( 1 );
3316
INSERT INTO t2 VALUES ( 4 );
3317
INSERT INTO t2 VALUES ( 5 );
3318
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
3321
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
3322
WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
3326
CREATE TABLE t1 ( c1 integer );
3327
INSERT INTO t1 VALUES ( 1 );
3328
INSERT INTO t1 VALUES ( 2 );
3329
INSERT INTO t1 VALUES ( 3 );
3330
INSERT INTO t1 VALUES ( 6 );
3331
CREATE TABLE t2 ( c2 integer );
3332
INSERT INTO t2 VALUES ( 1 );
3333
INSERT INTO t2 VALUES ( 4 );
3334
INSERT INTO t2 VALUES ( 5 );
3335
INSERT INTO t2 VALUES ( 6 );
3336
CREATE TABLE t3 ( c3 integer );
3337
INSERT INTO t3 VALUES ( 7 );
3338
INSERT INTO t3 VALUES ( 8 );
3339
SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
3340
WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
3344
DROP TABLE t1,t2,t3;
3346
`itemid` bigint(20) unsigned NOT NULL auto_increment,
3347
`sessionid` bigint(20) unsigned default NULL,
3348
`time` int(10) unsigned NOT NULL default '0',
3349
`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
3351
`data` text collate latin1_general_ci NOT NULL,
3352
PRIMARY KEY (`itemid`)
3353
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3354
INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
3356
`sessionid` bigint(20) unsigned NOT NULL auto_increment,
3357
`pid` int(10) unsigned NOT NULL default '0',
3358
`date` int(10) unsigned NOT NULL default '0',
3359
`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
3360
PRIMARY KEY (`sessionid`)
3361
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3362
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
3363
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;
3364
ip count( e.itemid )
3367
CREATE TABLE t1 (EMPNUM CHAR(3));
3368
CREATE TABLE t2 (EMPNUM CHAR(3) );
3369
INSERT INTO t1 VALUES ('E1'),('E2');
3370
INSERT INTO t2 VALUES ('E1');
3372
WHERE t1.EMPNUM NOT IN
3375
WHERE t1.EMPNUM = t2.EMPNUM);
3380
CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
3381
INSERT INTO t1 VALUES (1, 1);
3382
CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
3383
PRIMARY KEY(select_id,values_id));
3384
INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
3385
SELECT values_id FROM t1
3386
WHERE values_id IN (SELECT values_id FROM t2
3387
WHERE select_id IN (1, 0));
3390
SELECT values_id FROM t1
3391
WHERE values_id IN (SELECT values_id FROM t2
3392
WHERE select_id BETWEEN 0 AND 1);
3395
SELECT values_id FROM t1
3396
WHERE values_id IN (SELECT values_id FROM t2
3397
WHERE select_id = 0 OR select_id = 1);
3401
create table t1 (fld enum('0','1'));
3402
insert into t1 values ('1');
3403
select * from (select max(fld) from t1) as foo;
3407
CREATE TABLE t1 (a int, b int);
3408
CREATE TABLE t2 (c int, d int);
3409
CREATE TABLE t3 (e int);
3410
INSERT INTO t1 VALUES
3411
(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
3412
INSERT INTO t2 VALUES
3413
(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
3414
INSERT INTO t3 VALUES (10), (30), (10), (20) ;
3415
SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
3435
SELECT a FROM t1 GROUP BY a
3436
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
3440
SELECT a FROM t1 GROUP BY a
3441
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
3444
SELECT a FROM t1 GROUP BY a
3445
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
3449
SELECT a FROM t1 GROUP BY a
3450
HAVING a IN (SELECT c FROM t2
3451
WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3455
SELECT a FROM t1 GROUP BY a
3456
HAVING a IN (SELECT c FROM t2
3457
WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3461
SELECT a FROM t1 GROUP BY a
3462
HAVING a IN (SELECT c FROM t2
3463
WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3466
SELECT a FROM t1 GROUP BY a
3467
HAVING a IN (SELECT c FROM t2
3468
WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
3471
SELECT a FROM t1 GROUP BY a
3472
HAVING a IN (SELECT c FROM t2
3473
WHERE MIN(b) < d AND
3474
EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3477
SELECT a, SUM(a) FROM t1 GROUP BY a;
3484
WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
3488
SELECT a FROM t1 GROUP BY a
3489
HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
3496
EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
3502
EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
3509
SELECT t1.a FROM t1 GROUP BY t1.a
3510
HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
3511
HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3512
HAVING SUM(t1.a+t2.c) < t3.e/4));
3516
SELECT t1.a FROM t1 GROUP BY t1.a
3517
HAVING t1.a > ALL(SELECT t2.c FROM t2
3518
WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3519
HAVING SUM(t1.a+t2.c) < t3.e/4));
3522
SELECT t1.a FROM t1 GROUP BY t1.a
3523
HAVING t1.a > ALL(SELECT t2.c FROM t2
3524
WHERE EXISTS(SELECT t3.e FROM t3
3525
WHERE SUM(t1.a+t2.c) < t3.e/4));
3526
ERROR HY000: Invalid use of group function
3527
SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
3528
ERROR HY000: Invalid use of group function
3529
SELECT t1.a FROM t1 GROUP BY t1.a
3530
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3531
HAVING AVG(t2.c+SUM(t1.b)) > 20);
3536
SELECT t1.a FROM t1 GROUP BY t1.a
3537
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3538
HAVING AVG(SUM(t1.b)) > 20);
3542
SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
3543
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3544
HAVING t2.c+sum > 20);
3549
DROP TABLE t1,t2,t3;
3550
CREATE TABLE t1 (a varchar(5), b varchar(10));
3551
INSERT INTO t1 VALUES
3552
('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
3553
('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
3554
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3560
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3561
id select_type table type possible_keys key key_len ref rows Extra
3562
1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where
3563
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
3564
2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
3565
ALTER TABLE t1 ADD INDEX(a);
3566
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3572
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3573
id select_type table type possible_keys key key_len ref rows Extra
3574
1 PRIMARY t1 ALL a NULL NULL NULL 9 Using where
3575
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
3576
2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
3578
create table t1( f1 int,f2 int);
3579
insert into t1 values (1,1),(2,2);
3580
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';
3585
create table t1 (c int, key(c));
3586
insert into t1 values (1142477582), (1142455969);
3587
create table t2 (a int, b int);
3588
insert into t2 values (2, 1), (1, 0);
3589
delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
3591
CREATE TABLE t1 (a INT);
3592
CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3593
ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
3594
CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
3595
ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
3596
SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3597
ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3599
create table t1 (i int, j bigint);
3600
insert into t1 values (1, 2), (2, 2), (3, 2);
3601
select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
3605
CREATE TABLE t1 (i BIGINT UNSIGNED);
3606
INSERT INTO t1 VALUES (10000000000000000000);
3607
INSERT INTO t1 VALUES (1);
3608
CREATE TABLE t2 (i BIGINT UNSIGNED);
3609
INSERT INTO t2 VALUES (10000000000000000000);
3610
INSERT INTO t2 VALUES (1);
3612
SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
3614
10000000000000000000
3617
SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3619
10000000000000000000
3620
/* subquery test with cast*/
3621
SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
3623
10000000000000000000
3627
id bigint(20) unsigned NOT NULL auto_increment,
3628
name varchar(255) NOT NULL,
3631
INSERT INTO t1 VALUES
3632
(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
3634
id bigint(20) unsigned NOT NULL auto_increment,
3635
mid bigint(20) unsigned NOT NULL,
3639
INSERT INTO t2 VALUES
3640
(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
3641
(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
3643
(SELECT date FROM t2 WHERE mid = t1.id
3644
ORDER BY date DESC LIMIT 0, 1) AS date_last,
3645
(SELECT date FROM t2 WHERE mid = t1.id
3646
ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
3648
id name date_last date_next_to_last
3649
1 Balazs 2006-05-01 NULL
3650
2 Joe 2006-04-20 NULL
3651
3 Frank 2006-04-13 NULL
3653
(SELECT COUNT(*) FROM t2 WHERE mid = t1.id
3654
ORDER BY date DESC LIMIT 1, 1) AS date_count
3661
(SELECT date FROM t2 WHERE mid = t1.id
3662
ORDER BY date DESC LIMIT 0, 1) AS date_last,
3663
(SELECT date FROM t2 WHERE mid = t1.id
3664
ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
3666
id name date_last date_next_to_last
3667
1 Balazs 2006-05-01 2006-03-30
3668
2 Joe 2006-04-20 2006-04-06
3669
3 Frank 2006-04-13 NULL
3672
i1 int(11) NOT NULL default '0',
3673
i2 int(11) NOT NULL default '0',
3674
t datetime NOT NULL default '0000-00-00 00:00:00',
3675
PRIMARY KEY (i1,i2,t)
3677
INSERT INTO t1 VALUES
3678
(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
3679
(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
3680
(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
3681
(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
3682
(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
3683
(24,2,'2005-05-27 12:40:06');
3685
i1 int(11) NOT NULL default '0',
3686
i2 int(11) NOT NULL default '0',
3687
t datetime default NULL,
3690
INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
3693
WHERE t1.t = (SELECT t1.t FROM t1
3694
WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
3695
ORDER BY t1.t DESC LIMIT 1);
3696
id select_type table type possible_keys key key_len ref rows Extra
3697
1 PRIMARY t2 system NULL NULL NULL NULL 1
3698
1 PRIMARY t1 index NULL PRIMARY 16 NULL 11 Using where; Using index
3699
2 SUBQUERY t1 range PRIMARY PRIMARY 16 NULL 5 Using where; Using index
3701
WHERE t1.t = (SELECT t1.t FROM t1
3702
WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
3703
ORDER BY t1.t DESC LIMIT 1);
3705
24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40
3707
CREATE TABLE t1 (i INT);
3708
(SELECT i FROM t1) UNION (SELECT i FROM t1);
3710
SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
3712
(SELECT i FROM t1) UNION
3717
WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
3718
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION (SELECT i FROM t1)))' at line 2
3719
explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
3721
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union (select t12.i from t1 t12))
3723
explain select * from t1 where not exists
3724
((select t11.i from t1 t11) union (select t12.i from t1 t12));
3725
id select_type table type possible_keys key key_len ref rows Extra
3726
1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
3727
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
3728
3 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table
3729
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
3731
CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
3732
insert into t1 (a) values (FLOOR(rand() * 100));
3733
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3734
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3735
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3736
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3737
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3738
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3739
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3740
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3741
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3742
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3743
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3744
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3745
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3747
(SELECT REPEAT(' ',250) FROM t1 i1
3748
WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
3749
FROM t1 ORDER BY a LIMIT 5;
3757
CREATE TABLE t1 (a INT, b INT);
3758
CREATE TABLE t2 (a INT);
3759
INSERT INTO t2 values (1);
3760
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
3761
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
3762
(SELECT COUNT(DISTINCT t1.b) from t2)
3766
SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
3767
FROM t1 GROUP BY t1.a;
3768
(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
3772
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
3773
COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
3777
SELECT COUNT(DISTINCT t1.b),
3778
(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
3779
FROM t1 GROUP BY t1.a;
3780
COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
3786
SELECT COUNT(DISTINCT t1.b)
3789
FROM t1 GROUP BY t1.a;
3792
SELECT COUNT(DISTINCT t1.b)
3801
SELECT COUNT(DISTINCT t1.b)
3804
FROM t1 GROUP BY t1.a LIMIT 1)
3810
SELECT COUNT(DISTINCT t1.b)
3813
FROM t1 GROUP BY t1.a LIMIT 1)
3818
CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
3819
CREATE TABLE t2 (x int auto_increment, y int, z int,
3820
PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
3821
create table t3 (a int);
3822
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3823
insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
3824
from t3 A, t3 B, t3 C, t3 D where D.a<3;
3825
insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
3826
SET SESSION sort_buffer_size = 32 * 1024;
3827
SELECT SQL_NO_CACHE COUNT(*)
3828
FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3832
SET SESSION sort_buffer_size = 8 * 1024 * 1024;
3833
SELECT SQL_NO_CACHE COUNT(*)
3834
FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3838
DROP TABLE t1,t2,t3;
3839
CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
3840
CREATE TABLE t2 (c int);
3841
INSERT INTO t1 VALUES ('aa', 1);
3842
INSERT INTO t2 VALUES (1);
3844
WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3846
SELECT c from t2 WHERE c=t1.c);
3849
INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
3851
WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3853
SELECT c from t2 WHERE c=t1.c);
3859
INSERT INTO t2 VALUES (2);
3860
CREATE TABLE t3 (c int);
3861
INSERT INTO t3 VALUES (1);
3863
WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
3865
SELECT c from t2 WHERE c=t1.c);
3871
DROP TABLE t1,t2,t3;
3872
CREATE TABLE t1(f1 int);
3873
CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
3874
INSERT INTO t1 VALUES (1),(1),(2),(2);
3875
INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
3876
SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
3880
SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
3884
PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
3893
DEALLOCATE PREPARE stmt1;
3894
SELECT f2, AVG(f21),
3895
(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
3896
FROM t2 GROUP BY f2;
3898
1 1.0000 2004-02-29 11:11:11
3899
2 2.0000 2004-02-29 11:11:11
3901
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
3902
INSERT INTO t1 VALUES
3903
(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
3904
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
3905
(3,2,'k'), (3,1,'l'), (1,9,'m');
3907
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
3914
DROP TABLE IF EXISTS t1;
3915
DROP TABLE IF EXISTS t2;
3916
DROP TABLE IF EXISTS t1xt2;
3918
id_1 int(5) NOT NULL,
3919
t varchar(4) DEFAULT NULL
3922
id_2 int(5) NOT NULL,
3923
t varchar(4) DEFAULT NULL
3925
CREATE TABLE t1xt2 (
3926
id_1 int(5) NOT NULL,
3927
id_2 int(5) NOT NULL
3929
INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
3930
INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
3931
INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
3932
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3933
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3935
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3936
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3938
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3939
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3941
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3942
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3948
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3949
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
3955
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3956
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
3962
insert INTO t1xt2 VALUES (1, 12);
3963
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3964
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3967
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3968
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3971
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3972
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3975
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3976
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3981
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3982
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3987
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3988
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3993
insert INTO t1xt2 VALUES (2, 12);
3994
SELECT DISTINCT t1.id_1 FROM t1 WHERE
3995
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3999
SELECT DISTINCT t1.id_1 FROM t1 WHERE
4000
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4004
SELECT DISTINCT t1.id_1 FROM t1 WHERE
4005
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4009
SELECT DISTINCT t1.id_1 FROM t1 WHERE
4010
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4014
SELECT DISTINCT t1.id_1 FROM t1 WHERE
4015
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4019
SELECT DISTINCT t1.id_1 FROM t1 WHERE
4020
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4027
CREATE TABLE t1 (a int);
4028
INSERT INTO t1 VALUES (3), (1), (2);
4029
SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
4034
SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
4040
CREATE TABLE t1 (a int, b int);
4041
CREATE TABLE t2 (m int, n int);
4042
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4043
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4045
(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
4047
COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
4052
(SELECT MIN(m) FROM t2 WHERE m = count(*))
4054
COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*))
4060
HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
4065
CREATE TABLE t1 (a int, b int);
4066
CREATE TABLE t2 (m int, n int);
4067
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4068
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4069
SELECT COUNT(*) c, a,
4070
(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
4072
c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
4076
SELECT COUNT(*) c, a,
4077
(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
4079
c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
4084
CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
4085
INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
4086
(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
4087
(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
4089
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
4096
(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
4103
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
4104
FROM t1 WHERE t1.d=0 GROUP BY a;
4110
(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4111
LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4131
(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4133
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4134
FROM t1 as tt GROUP BY tt.a;
4140
(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4142
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
4143
FROM t1 as tt GROUP BY tt.a;
4149
CREATE TABLE t1 (a int, b int);
4150
INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4151
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4155
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4158
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4162
SET @@sql_mode='ansi';
4163
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4164
ERROR HY000: Invalid use of group function
4165
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4166
ERROR HY000: Invalid use of group function
4168
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4169
ERROR HY000: Invalid use of group function
4170
SET @@sql_mode=default;
4172
CREATE TABLE t1 (a INT);
4173
INSERT INTO t1 values (1),(1),(1),(1);
4174
CREATE TABLE t2 (x INT);
4175
INSERT INTO t1 values (1000),(1001),(1002);
4176
SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
4177
ERROR HY000: Invalid use of group function
4178
SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
4179
ERROR HY000: Invalid use of group function
4180
SELECT COUNT(1) FROM DUAL;
4183
SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
4184
ERROR HY000: Invalid use of group function
4186
SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
4188
ERROR HY000: Invalid use of group function
4190
SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
4192
ERROR HY000: Invalid use of group function
4194
CREATE TABLE t1 (a int, b int, KEY (a));
4195
INSERT INTO t1 VALUES (1,1),(2,1);
4196
EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
4197
id select_type table type possible_keys key key_len ref rows Extra
4198
1 PRIMARY t1 ref a a 5 const 1 Using where; Using index
4199
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
4201
CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
4202
INSERT INTO t1 VALUES
4203
(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
4204
CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
4205
INSERT INTO t2 VALUES (7), (5), (1), (3);
4206
SELECT id, st FROM t1
4207
WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4212
SELECT id, st FROM t1
4213
WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4219
SELECT id, st FROM t1
4220
WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4224
SELECT id, st FROM t1
4225
WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4231
CREATE TABLE t1 (a int);
4232
INSERT INTO t1 VALUES (1), (2);
4234
SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
4235
id select_type table type possible_keys key key_len ref rows filtered Extra
4236
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00
4237
2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
4239
Note 1003 select `res`.`count(*)` AS `count(*)` from (select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
4242
a varchar(255) default NULL,
4243
b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
4247
a varchar(255) default NULL
4249
INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
4250
INSERT INTO t1 SELECT * FROM t1;
4251
INSERT INTO t1 SELECT * FROM t1;
4252
INSERT INTO t1 SELECT * FROM t1;
4253
INSERT INTO t1 SELECT * FROM t1;
4254
INSERT INTO t1 SELECT * FROM t1;
4255
INSERT INTO t1 SELECT * FROM t1;
4256
INSERT INTO t1 SELECT * FROM t1;
4257
INSERT INTO t1 SELECT * FROM t1;
4258
INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
4259
INSERT INTO `t2` VALUES ('abcdefghijk');
4260
INSERT INTO `t2` VALUES ('asdf');
4261
SET session sort_buffer_size=8192;
4262
SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
4267
CREATE TABLE t1 (a INTEGER, b INTEGER);
4268
CREATE TABLE t2 (x INTEGER);
4269
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
4270
INSERT INTO t2 VALUES (1), (2);
4271
SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
4272
ERROR 21000: Subquery returns more than 1 row
4273
SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
4274
ERROR 21000: Subquery returns more than 1 row
4275
SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
4276
(SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
4279
CREATE TABLE t1 (a INT, b INT);
4280
INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
4281
SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
4282
AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
4287
CREATE TABLE t1 (a INT);
4288
CREATE TABLE t2 (a INT);
4289
INSERT INTO t1 VALUES (1),(2);
4290
INSERT INTO t2 VALUES (1),(2);
4291
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
4292
(SELECT SUM(t1.a) FROM t2 WHERE a=0)
4294
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
4295
ERROR 21000: Subquery returns more than 1 row
4296
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
4297
(SELECT SUM(t1.a) FROM t2 WHERE a=1)
4300
CREATE TABLE t1 (a1 INT, a2 INT);
4301
CREATE TABLE t2 (b1 INT, b2 INT);
4302
INSERT INTO t1 VALUES (100, 200);
4303
INSERT INTO t1 VALUES (101, 201);
4304
INSERT INTO t2 VALUES (101, 201);
4305
INSERT INTO t2 VALUES (103, 203);
4306
SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
4307
((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
4311
CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
4312
INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
4313
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4315
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4317
CREATE INDEX I1 ON t1 (s1);
4318
CREATE INDEX I2 ON t1 (s2);
4319
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4321
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4324
INSERT INTO t1 VALUES (0x41,0x41);
4325
SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
4328
CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
4329
CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
4330
CREATE TABLE t3 (a3 BINARY(2) default '0');
4331
INSERT INTO t1 VALUES (1),(2),(3),(4);
4332
INSERT INTO t2 VALUES (1),(2),(3);
4333
INSERT INTO t3 VALUES (1),(2),(3);
4334
SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
4339
SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
4340
a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
4345
DROP TABLE t1,t2,t3;
4346
CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
4347
CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
4348
CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
4349
INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
4350
INSERT INTO t2 VALUES (2), (3), (4), (5);
4351
INSERT INTO t3 VALUES (10), (20), (30);
4352
SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
4357
SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
4359
DROP TABLE t1, t2, t3;
4360
SET @save_optimizer_switch=@@optimizer_switch;
4361
SET optimizer_switch='semijoin_with_cache=off';
4362
SET optimizer_switch='materialization=off';
4363
CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
4364
INSERT INTO t1 VALUES ('a', 'aa');
4365
INSERT INTO t1 VALUES ('a', 'aaa');
4366
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4368
CREATE INDEX I1 ON t1 (a);
4369
CREATE INDEX I2 ON t1 (b);
4370
EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4371
id select_type table type possible_keys key key_len ref rows Extra
4372
1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
4373
1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1)
4374
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4376
CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
4377
INSERT INTO t2 SELECT * FROM t1;
4378
CREATE INDEX I1 ON t2 (a);
4379
CREATE INDEX I2 ON t2 (b);
4380
EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4381
id select_type table type possible_keys key key_len ref rows Extra
4382
1 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where
4383
1 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2)
4384
SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4387
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4388
id select_type table type possible_keys key key_len ref rows Extra
4389
1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
4390
1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1)
4391
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4394
SET optimizer_switch= @save_optimizer_switch;
4395
CREATE TABLE t1(a INT, b INT);
4396
INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
4398
SELECT a AS out_a, MIN(b) FROM t1
4399
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4401
ERROR 42S22: Unknown column 'out_a' in 'where clause'
4402
SELECT a AS out_a, MIN(b) FROM t1
4403
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4405
ERROR 42S22: Unknown column 'out_a' in 'where clause'
4407
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4408
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4410
id select_type table type possible_keys key key_len ref rows Extra
4411
1 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort
4412
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where
4413
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4414
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4420
CREATE TABLE t1 (a INT);
4421
CREATE TABLE t2 (a INT);
4422
INSERT INTO t1 VALUES (1),(2);
4423
INSERT INTO t2 VALUES (1),(2);
4424
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
4429
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
4430
id select_type table type possible_keys key key_len ref rows filtered Extra
4431
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
4432
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4434
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4435
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
4437
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
4438
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
4439
id select_type table type possible_keys key key_len ref rows filtered Extra
4440
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
4441
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4442
3 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4443
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
4445
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4446
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4447
Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))))
4449
create table t0(a int);
4450
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4451
create table t1(f11 int, f12 int);
4452
create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
4453
insert into t1 values(1,1),(2,2), (3, 3);
4455
select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
4456
from t0 A, t0 B, t0 C, t0 D;
4457
set session sort_buffer_size= 33*1024;
4458
select count(*) from t1 where f12 =
4459
(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
4462
drop table t0,t1,t2;
4464
f7 varchar(32) collate utf8_bin NOT NULL default '',
4465
f10 varchar(32) collate utf8_bin default NULL,
4468
INSERT INTO t4 VALUES(1,1), (2,null);
4470
f4 varchar(32) collate utf8_bin NOT NULL default '',
4471
f2 varchar(50) collate utf8_bin default NULL,
4472
f3 varchar(10) collate utf8_bin default NULL,
4476
INSERT INTO t2 VALUES(1,1,null), (2,2,null);
4478
f8 varchar(32) collate utf8_bin NOT NULL default '',
4479
f1 varchar(10) collate utf8_bin default NULL,
4480
f9 varchar(32) collate utf8_bin default NULL,
4483
INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
4485
f6 varchar(32) collate utf8_bin NOT NULL default '',
4486
f5 varchar(50) collate utf8_bin default NULL,
4489
INSERT INTO t3 VALUES (1,null), (2,null);
4491
IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
4492
IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
4496
FROM t2 VPC, t4 a2, t2 a3
4498
VPC.f4 = a2.f10 AND a3.f2 = a4
4505
t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
4510
DROP TABLE t1, t2, t3, t4;
4511
create table t1 (a float(5,4) zerofill);
4512
create table t2 (a float(5,4),b float(2,0));
4513
select t1.a from t1 where
4514
t1.a= (select b from t2 limit 1) and not
4515
t1.a= (select a from t2 limit 1) ;
4518
CREATE TABLE t1 (a INT);
4519
INSERT INTO t1 VALUES (1),(2);
4520
SET @save_join_cache_level=@@join_cache_level;
4521
SET join_cache_level=0;
4522
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
4523
id select_type table type possible_keys key key_len ref rows filtered Extra
4524
1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00
4525
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
4526
2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary
4528
Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
4529
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
4530
id select_type table type possible_keys key key_len ref rows filtered Extra
4531
1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00
4532
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
4533
2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary
4535
Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
4536
SET join_cache_level=@save_join_cache_level;
4539
# Bug#45061: Incorrectly market field caused wrong result.
4542
`int_nokey` int(11) NOT NULL,
4543
`int_key` int(11) NOT NULL,
4544
KEY `int_key` (`int_key`)
4546
INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
4547
(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
4548
(5,2), (1,8), (7,0), (0,9), (9,5);
4549
SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4555
EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4556
id select_type table type possible_keys key key_len ref rows filtered Extra
4557
1 PRIMARY C ALL NULL NULL NULL NULL 20 100.00 Using where
4559
# End of test for bug#45061.
4561
# Bug #46749: Segfault in add_key_fields() with outer subquery level
4569
INSERT INTO t1 VALUES (1,1), (2,1);
4570
CREATE TABLE st1 like t1;
4571
INSERT INTO st1 VALUES (1,1), (2,1);
4572
CREATE TABLE st2 like t1;
4573
INSERT INTO st2 VALUES (1,1), (2,1);
4575
SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4578
id select_type table type possible_keys key key_len ref rows Extra
4579
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
4580
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
4581
SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4584
MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4586
DROP TABLE t1, st1, st2;
4588
# Bug #48709: Assertion failed in sql_select.cc:11782:
4589
# int join_read_key(JOIN_TAB*)
4591
CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
4592
INSERT INTO t1 VALUES (10,1), (14,1);
4593
CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
4594
INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
4595
# should have eq_ref for t1
4597
SELECT * FROM t2 outr
4598
WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4600
id select_type table type possible_keys key key_len ref rows Extra
4601
x x outr ALL x x x x x x
4602
x x t1 eq_ref x x x x x x
4603
x x t2 index x x x x x x
4604
# should not crash on debug binaries
4605
SELECT * FROM t2 outr
4606
WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4614
# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4616
CREATE TABLE t1(a1 int);
4617
INSERT INTO t1 VALUES (1),(2);
4618
SELECT @@session.sql_mode INTO @old_sql_mode;
4619
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4621
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4622
id select_type table type possible_keys key key_len ref rows filtered Extra
4623
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
4624
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00
4626
Note 1003 select 1 AS `1` from `test`.`t1` where 1
4627
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4635
(SELECT 1 FROM t1 AS t1_0
4636
WHERE 1 < SOME (SELECT a1 FROM t1)
4639
ERROR 21000: Subquery returns more than 1 row
4641
ERROR 21000: Subquery returns more than 1 row
4642
SET SESSION sql_mode=@old_sql_mode;
4643
DEALLOCATE PREPARE stmt;
4646
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4648
CREATE TABLE t1(a1 int);
4649
INSERT INTO t1 VALUES (1),(2);
4650
CREATE TABLE t2(a1 int);
4651
INSERT INTO t2 VALUES (3);
4652
SELECT @@session.sql_mode INTO @old_sql_mode;
4653
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4654
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
4658
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
4662
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
4664
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
4668
SET SESSION sql_mode=@old_sql_mode;
4671
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4673
create table t2(i int);
4674
insert into t2 values(0);
4675
SELECT @@session.sql_mode INTO @old_sql_mode;
4676
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4678
SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
4682
col_varchar_key varchar(1) DEFAULT NULL,
4684
KEY col_varchar_key (col_varchar_key)
4688
WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4691
SET SESSION sql_mode=@old_sql_mode;
4695
create table t_out (subcase char(3),
4696
a1 char(2), b1 char(2), c1 char(2));
4697
create table t_in (a2 char(2), b2 char(2), c2 char(2));
4698
insert into t_out values ('A.1','2a', NULL, '2a');
4699
insert into t_out values ('A.3', '2a', NULL, '2a');
4700
insert into t_out values ('A.4', '2a', NULL, 'xx');
4701
insert into t_out values ('B.1', '2a', '2a', '2a');
4702
insert into t_out values ('B.2', '2a', '2a', '2a');
4703
insert into t_out values ('B.3', '3a', 'xx', '3a');
4704
insert into t_out values ('B.4', 'xx', '3a', '3a');
4705
insert into t_in values ('1a', '1a', '1a');
4706
insert into t_in values ('2a', '2a', '2a');
4707
insert into t_in values (NULL, '2a', '2a');
4708
insert into t_in values ('3a', NULL, '3a');
4710
Test general IN semantics (not top-level)
4714
(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
4715
(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4716
from t_out where subcase = 'A.1';
4717
subcase pred_in pred_not_in
4719
case A.2 - impossible
4722
(a1, b1, c1) IN (select * from t_in) pred_in,
4723
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4724
from t_out where subcase = 'A.3';
4725
subcase pred_in pred_not_in
4729
(a1, b1, c1) IN (select * from t_in) pred_in,
4730
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4731
from t_out where subcase = 'A.4';
4732
subcase pred_in pred_not_in
4736
(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
4737
(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4738
from t_out where subcase = 'B.1';
4739
subcase pred_in pred_not_in
4743
(a1, b1, c1) IN (select * from t_in) pred_in,
4744
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4745
from t_out where subcase = 'B.2';
4746
subcase pred_in pred_not_in
4750
(a1, b1, c1) IN (select * from t_in) pred_in,
4751
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4752
from t_out where subcase = 'B.3';
4753
subcase pred_in pred_not_in
4757
(a1, b1, c1) IN (select * from t_in) pred_in,
4758
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4759
from t_out where subcase = 'B.4';
4760
subcase pred_in pred_not_in
4763
Test IN as top-level predicate, and
4764
as non-top level for cases A.3, B.3 (the only cases with NULL result).
4767
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4768
where subcase = 'A.1' and
4769
(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4772
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4773
where subcase = 'A.1' and
4774
(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4777
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4778
where subcase = 'A.1' and
4779
NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4783
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4784
where subcase = 'A.3' and
4785
(a1, b1, c1) IN (select * from t_in);
4788
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4789
where subcase = 'A.3' and
4790
(a1, b1, c1) NOT IN (select * from t_in);
4793
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4794
where subcase = 'A.3' and
4795
NOT((a1, b1, c1) IN (select * from t_in));
4798
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4799
where subcase = 'A.3' and
4800
((a1, b1, c1) IN (select * from t_in)) is NULL and
4801
((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4805
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4806
where subcase = 'A.4' and
4807
(a1, b1, c1) IN (select * from t_in);
4810
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4811
where subcase = 'A.4' and
4812
(a1, b1, c1) NOT IN (select * from t_in);
4815
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4816
where subcase = 'A.4' and
4817
NOT((a1, b1, c1) IN (select * from t_in));
4821
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4822
where subcase = 'B.1' and
4823
(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4826
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4827
where subcase = 'B.1' and
4828
(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4831
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4832
where subcase = 'B.1' and
4833
NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4837
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4838
where subcase = 'B.2' and
4839
(a1, b1, c1) IN (select * from t_in);
4842
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4843
where subcase = 'B.2' and
4844
(a1, b1, c1) NOT IN (select * from t_in);
4847
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4848
where subcase = 'B.2' and
4849
NOT((a1, b1, c1) IN (select * from t_in));
4853
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4854
where subcase = 'B.3' and
4855
(a1, b1, c1) IN (select * from t_in);
4858
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4859
where subcase = 'B.3' and
4860
(a1, b1, c1) NOT IN (select * from t_in);
4863
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4864
where subcase = 'B.3' and
4865
NOT((a1, b1, c1) IN (select * from t_in));
4868
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4869
where subcase = 'B.3' and
4870
((a1, b1, c1) IN (select * from t_in)) is NULL and
4871
((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4875
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4876
where subcase = 'B.4' and
4877
(a1, b1, c1) IN (select * from t_in);
4880
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4881
where subcase = 'B.4' and
4882
(a1, b1, c1) NOT IN (select * from t_in);
4885
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4886
where subcase = 'B.4' and
4887
NOT((a1, b1, c1) IN (select * from t_in));
4892
CREATE TABLE t1 (a INT, b INT);
4893
INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4894
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4898
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4901
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4905
SET @@sql_mode='ansi';
4906
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4907
ERROR HY000: Invalid use of group function
4908
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4909
ERROR HY000: Invalid use of group function
4911
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4912
ERROR HY000: Invalid use of group function
4913
SET @@sql_mode=default;
4915
CREATE TABLE t1 (s1 CHAR(1));
4916
INSERT INTO t1 VALUES ('a');
4917
SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
4921
CREATE TABLE t1(c INT, KEY(c));
4922
CREATE TABLE t2(a INT, b INT);
4923
INSERT INTO t2 VALUES (1, 10), (2, NULL);
4924
INSERT INTO t1 VALUES (1), (3);
4925
SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4928
CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4929
INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4930
CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4931
INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4933
WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
4937
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4938
INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4945
UNIQUE KEY b (b,c,d),
4950
INSERT INTO t2 VALUES
4965
INSERT INTO t3 VALUES (1,1,1), (2,32,1);
4967
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;
4968
id select_type table type possible_keys key key_len ref rows Extra
4969
1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index
4970
1 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index
4971
2 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index; Using filesort
4972
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;
4975
DROP TABLE t1,t2,t3;
4976
CREATE TABLE t1 (id int);
4977
CREATE TABLE t2 (id int, c int);
4978
INSERT INTO t1 (id) VALUES (1);
4979
INSERT INTO t2 (id) VALUES (1);
4980
INSERT INTO t1 (id) VALUES (1);
4981
INSERT INTO t2 (id) VALUES (1);
4983
SELECT t2.c AS c FROM t1, t2
4984
WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
4986
CREATE VIEW v2 (a,b) AS
4987
SELECT t2.id, t2.c AS c FROM t1, t2
4988
WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
4989
INSERT INTO v2(a,b) VALUES (2,2);
4990
ERROR HY000: CHECK OPTION failed 'test.v2'
4998
SELECT t2.c AS c FROM t2
4999
WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5004
# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5006
create table t1(id integer primary key, g integer, v integer, s char(1));
5007
create table t2(id integer primary key, g integer, v integer, s char(1));
5008
insert into t1 values
5012
(41, 40, null, 'l'),
5014
(51, 50, null, 'l'),
5016
(61, 60, null, 'l'),
5018
(90, 90, null, 'l');
5019
insert into t2 values
5024
(61, 60, null, 'r'),
5026
(71, 70, null, 'r'),
5028
(81, 80, null, 'r'),
5029
(100,100,null, 'r');
5034
where t1.g=t2.g) is unknown;
5040
# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5042
create table t1(id integer primary key, g integer, v integer, s char(1));
5043
create table t2(id integer primary key, g integer, v integer, s char(1));
5044
insert into t1 values
5048
(41, 40, null, 'l'),
5050
(51, 50, null, 'l'),
5052
(61, 60, null, 'l'),
5054
(90, 90, null, 'l');
5055
insert into t2 values
5060
(61, 60, null, 'r'),
5062
(71, 70, null, 'r'),
5064
(81, 80, null, 'r'),
5065
(100,100,null, 'r');
5070
where t1.g=t2.g) is unknown;
5076
# Bug#33204: INTO is allowed in subselect, causing inconsistent results
5078
CREATE TABLE t1( a INT );
5079
INSERT INTO t1 VALUES (1),(2);
5080
CREATE TABLE t2( a INT, b INT );
5082
FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
5083
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2
5085
FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
5086
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
5088
FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
5089
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
5093
SELECT a INTO @var FROM t1 WHERE a = 2
5095
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2
5100
SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
5102
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2
5107
SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
5109
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2
5111
SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
5115
SELECT a FROM t1 WHERE a = 2
5117
SELECT a FROM t1 WHERE a = 2
5124
SELECT a FROM t1 WHERE a = 2
5126
SELECT a FROM t1 WHERE a = 2
5131
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
5132
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
5133
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
5136
SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
5139
SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
5140
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
5141
SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
5142
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
5143
SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5144
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
5145
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
5146
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
5147
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5148
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
5149
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
5150
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
5151
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
5152
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a))) t1a' at line 1
5153
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
5154
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' ))) t1a' at line 1
5155
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
5156
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' ))) t1a' at line 1
5157
SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
5160
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
5163
SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
5166
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
5169
SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1;
5173
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
5174
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
5175
SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
5176
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
5177
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
5178
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
5179
SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
5180
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
5181
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
5182
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
5183
SELECT * FROM t1 JOIN (t1 t1a) ON 1;
5189
SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
5195
SELECT * FROM (t1 t1a);
5199
SELECT * FROM ((t1 t1a));
5203
SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1;
5207
SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
5211
SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1;
5215
SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
5219
SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
5220
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a2' at line 1
5221
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
5224
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
5227
SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
5230
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
5231
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
5232
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5233
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
5234
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5235
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
5236
SELECT * FROM t1 WHERE a = ( SELECT 1 );
5239
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
5242
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
5243
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
5244
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
5245
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
5246
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
5247
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
5248
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
5249
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
5250
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5251
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
5252
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5253
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
5254
SELECT ( SELECT 1 INTO @v );
5255
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5256
SELECT ( SELECT 1 INTO OUTFILE 'file' );
5257
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
5258
SELECT ( SELECT 1 INTO DUMPFILE 'file' );
5259
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
5260
SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
5261
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5262
SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5263
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
5264
SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5265
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
5266
SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
5267
( SELECT a FROM t1 WHERE a = 1 ) a
5270
SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
5271
( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a
5274
SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
5276
SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
5277
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
5278
( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5279
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) UNION SELECT 1' at line 1
5280
SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5281
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5282
SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5283
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1' at line 1
5284
SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5285
( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
5287
SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
5288
((SELECT 1 UNION SELECT 1 UNION SELECT 1))
5290
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5291
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5292
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5293
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') UNION SELECT 1 )' at line 1
5294
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
5297
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5298
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5299
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5300
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5301
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5302
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5303
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5304
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
5305
SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5306
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
5307
SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5308
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) UNION SELECT 1 )' at line 1
5309
SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5310
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) UNION SELECT 1 )' at line 1
5311
SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5312
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
5313
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5316
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5319
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5322
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5325
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
5326
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5327
SELECT EXISTS(SELECT 1+1);
5330
SELECT EXISTS(SELECT 1+1 INTO @test);
5331
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @test)' at line 1
5332
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
5333
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5334
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
5335
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5336
SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
5337
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
5339
CREATE TABLE t1 (a ENUM('rainbow'));
5340
INSERT INTO t1 VALUES (),(),(),(),();
5341
SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
5345
CREATE TABLE t1 (a LONGBLOB);
5346
INSERT INTO t1 SET a = 'aaaa';
5347
INSERT INTO t1 SET a = 'aaaa';
5348
SELECT 1 FROM t1 GROUP BY
5349
(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
5354
# Bug #49512 : subquery with aggregate function crash
5355
# subselect_single_select_engine::exec()
5356
CREATE TABLE t1(a INT);
5357
INSERT INTO t1 VALUES();
5359
SELECT 1 FROM t1 WHERE a <> SOME
5361
SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
5367
# Bug #45989 take 2 : memory leak after explain encounters an
5368
# error in the query
5370
CREATE TABLE t1(a LONGTEXT);
5371
INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
5372
INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
5373
EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
5374
(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
5376
ERROR 42S22: Unknown column 'd1.a' in 'where clause'
5383
INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
5384
CREATE TABLE t2 SELECT * FROM t1;
5386
t3_id INT UNSIGNED AUTO_INCREMENT,
5388
amount DECIMAL(16,2),
5392
INSERT INTO t3 (t1_id, t3_id, amount)
5393
VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
5394
This is the 'inner query' running by itself.
5395
Produces correct results.
5398
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5401
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5411
SELECT * FROM (the same inner query)
5412
Produces correct results.
5416
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5419
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5429
Now make t2.t1_id part of a key.
5430
ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
5431
Same inner query by itself.
5432
Still correct results.
5435
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5438
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5447
SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
5451
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5454
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5468
# Bug #52711: Segfault when doing EXPLAIN SELECT with
5469
# union...order by (select... where...)
5471
CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5472
INSERT INTO t1 VALUES (1),(2);
5473
CREATE TABLE t2 (b INT);
5474
INSERT INTO t2 VALUES (1),(2);
5477
SELECT * FROM t2 UNION SELECT * FROM t2
5478
ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5480
SELECT * FROM t2 UNION SELECT * FROM t2
5481
ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5484
# Bug #58818: Incorrect result for IN/ANY subquery
5485
# with HAVING condition
5487
CREATE TABLE t1(i INT);
5488
INSERT INTO t1 VALUES (1), (2), (3);
5489
CREATE TABLE t1s(i INT);
5490
INSERT INTO t1s VALUES (10), (20), (30);
5491
CREATE TABLE t2s(i INT);
5492
INSERT INTO t2s VALUES (100), (200), (300);
5496
SELECT STRAIGHT_JOIN t2s.i
5498
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5508
SELECT STRAIGHT_JOIN t2s.i
5510
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5515
WHERE NOT t1.I = ANY
5517
SELECT STRAIGHT_JOIN t2s.i
5519
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5528
SELECT STRAIGHT_JOIN t2s.i
5530
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5534
DROP TABLE t1,t1s,t2s;
5535
# LP BUG#675248 - select->prep_where references on freed memory
5536
CREATE TABLE t1 (a int, b int);
5537
insert into t1 values (1,1),(0,0);
5538
CREATE TABLE t2 (c int);
5539
insert into t2 values (1),(2);
5540
prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
5541
from t1 as table1 group by sub";
5546
deallocate prepare stmt1;
5547
prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
5552
deallocate prepare stmt1;
5555
# Bug LP#693935/#58727: Assertion failure with
5556
# a single row subquery returning more than one row
5558
create table t1 (a char(1) charset utf8);
5559
insert into t1 values ('a'), ('b');
5560
create table t2 (a binary(1));
5561
insert into t2 values ('x'), ('y');
5562
select * from t2 where a=(select a from t1) and a='x';
5563
ERROR 21000: Subquery returns more than 1 row
5567
# Bug #11765713 58705:
5568
# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5569
# CREATED BY OPT_SUM_QUERY
5571
CREATE TABLE t1(a INT NOT NULL, KEY (a));
5572
INSERT INTO t1 VALUES (0), (1);
5573
SELECT 1 as foo FROM t1 WHERE a < SOME
5574
(SELECT a FROM t1 WHERE a <=>
5577
ERROR 21000: Subquery returns more than 1 row
5578
SELECT 1 as foo FROM t1 WHERE a < SOME
5579
(SELECT a FROM t1 WHERE a <=>
5580
(SELECT a FROM t1 where a is null)
5585
# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
5586
# Assertion `file' failed.
5588
CREATE TABLE t1 (a INT);
5591
(SELECT 1 FROM t1 RIGHT JOIN
5592
(SELECT 1 FROM t1, t1 t2) AS d ON 1),
5594
ERROR 21000: Operand should contain 1 column(s)
5597
# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5599
CREATE TABLE t1(a TIME NOT NULL);
5600
INSERT INTO t1 VALUES ('00:00:32');
5601
SELECT 1 FROM t1 WHERE a >
5602
(SELECT timestamp(a) AS a FROM t1);
5606
# No BUG#, a case brought from 5.2's innodb_mysql_lock.test
5608
create table t1 (i int not null primary key);
5609
insert into t1 values (1),(2),(3),(4),(5);
5610
create table t2 (j int not null primary key);
5611
insert into t2 values (1),(2),(3),(4),(5);
5612
create table t3 (k int not null primary key);
5613
insert into t3 values (1),(2),(3);
5614
create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
5615
select * from t3 where k in (select j from v2);
5620
drop table t1,t2,t3;
5623
# Bug#52068: Optimizer generates invalid semijoin materialization plan
5625
drop table if exists ot1, ot2, it1, it2;
5626
CREATE TABLE ot1(a INTEGER);
5627
INSERT INTO ot1 VALUES(5), (8);
5628
CREATE TABLE it2(a INTEGER);
5629
INSERT INTO it2 VALUES(9), (5), (1), (8);
5630
CREATE TABLE it3(a INTEGER);
5631
INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
5632
CREATE TABLE ot4(a INTEGER);
5633
INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
5634
SELECT * FROM ot1,ot4
5635
WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5648
explain SELECT * FROM ot1,ot4
5649
WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5651
id select_type table type possible_keys key key_len ref rows Extra
5652
1 PRIMARY ot1 ALL NULL NULL NULL NULL 2
5653
1 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using join buffer (flat, BNL join)
5654
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1
5655
2 MATERIALIZED it2 ALL NULL NULL NULL NULL 4
5656
2 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join)
5657
DROP TABLE IF EXISTS ot1, ot4, it2, it3;
5659
# Bug#729039: NULL keys used to evaluate subquery
5661
CREATE TABLE t1 (a int) ;
5662
INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
5663
CREATE TABLE t2 (a int, INDEX idx(a)) ;
5664
INSERT INTO t2 VALUES (NULL), (1), (NULL);
5666
WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
5671
WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
5672
id select_type table type possible_keys key key_len ref rows Extra
5673
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
5674
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 Using where
5676
WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5681
WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5682
id select_type table type possible_keys key key_len ref rows Extra
5683
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
5684
2 DEPENDENT SUBQUERY t2 ref idx idx 5 test.t1.a 2 Using index
5687
# BUG#752992: Wrong results for a subquery with 'semijoin=on'
5689
CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5690
INSERT INTO t1 VALUES (11,0);
5691
INSERT INTO t1 VALUES (12,5);
5692
INSERT INTO t1 VALUES (15,0);
5693
CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5694
INSERT INTO t2 VALUES (11,1);
5695
INSERT INTO t2 VALUES (12,2);
5696
INSERT INTO t2 VALUES (15,4);
5697
SET @save_join_cache_level=@@join_cache_level;
5698
SET join_cache_level=0;
5699
EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5700
id select_type table type possible_keys key key_len ref rows Extra
5701
1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 3
5702
1 PRIMARY it eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 Using index
5703
1 PRIMARY t2 index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(it)
5704
SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5709
SET join_cache_level=@save_join_cache_level;
5712
# Bug#751350: crash with pushed condition for outer references when
5713
# there should be none of such conditions
5715
CREATE TABLE t1 (a int, b int) ;
5716
INSERT INTO t1 VALUES (0,0),(0,0);
5717
set @optimizer_switch_save=@@optimizer_switch;
5718
set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
5721
WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
5723
id select_type table type possible_keys key key_len ref rows Extra
5724
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
5725
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
5727
WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
5731
set @@optimizer_switch=@optimizer_switch_save;
5734
# Bug #11765713 58705:
5735
# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5736
# CREATED BY OPT_SUM_QUERY
5738
CREATE TABLE t1(a INT NOT NULL, KEY (a));
5739
INSERT INTO t1 VALUES (0), (1);
5740
SELECT 1 as foo FROM t1 WHERE a < SOME
5741
(SELECT a FROM t1 WHERE a <=>
5744
ERROR 21000: Subquery returns more than 1 row
5745
SELECT 1 as foo FROM t1 WHERE a < SOME
5746
(SELECT a FROM t1 WHERE a <=>
5747
(SELECT a FROM t1 where a is null)
5751
CREATE TABLE t1 (a int(11), b varchar(1));
5752
INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5753
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5756
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5759
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5762
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5765
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5769
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5773
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5777
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5781
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5785
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5789
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5793
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5797
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5799
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5801
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5803
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5805
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5807
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5809
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5811
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5813
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5815
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5817
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5819
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5822
INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5823
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5826
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5829
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5832
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5835
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5839
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5843
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5847
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5851
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5855
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5859
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5863
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5867
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5869
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5871
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5873
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5875
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5877
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5879
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5881
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5883
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5885
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5887
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5889
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5893
# Fix of lp:780386 (NULL left part with empty ALL subquery).
5895
CREATE TABLE t1 ( f11 int) ;
5896
INSERT IGNORE INTO t1 VALUES (0),(0);
5897
CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
5898
INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
5899
DROP TABLE IF EXISTS t3;
5901
Note 1051 Unknown table 't3'
5902
CREATE TABLE t3 ( f3 int) ;
5903
INSERT INTO t3 VALUES (0),(0);
5904
SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
5910
DROP TABLE t1, t2, t3;
5912
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
5914
CREATE TABLE t1(a1 int);
5915
INSERT INTO t1 VALUES (1),(2);
5916
CREATE TABLE t2(a1 int);
5917
INSERT INTO t2 VALUES (3);
5918
SELECT @@session.sql_mode INTO @old_sql_mode;
5919
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5920
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
5924
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
5928
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
5930
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
5934
SET SESSION sql_mode=@old_sql_mode;
5936
create table t2(i int);
5937
insert into t2 values(0);
5938
SELECT @@session.sql_mode INTO @old_sql_mode;
5939
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5941
SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
5945
col_varchar_key varchar(1) DEFAULT NULL,
5947
KEY col_varchar_key (col_varchar_key)
5951
WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
5954
SET SESSION sql_mode=@old_sql_mode;
5958
# BUG#50257: Missing info in REF column of the EXPLAIN
5959
# lines for subselects
5961
CREATE TABLE t1 (a INT, b INT, INDEX (a));
5962
INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5963
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5964
id select_type table type possible_keys key key_len ref rows Extra
5965
1 SIMPLE t1 ref a a 5 const 1
5966
EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5967
id select_type table type possible_keys key key_len ref rows Extra
5968
1 PRIMARY t1 ALL NULL NULL NULL NULL 4
5969
2 SUBQUERY t1 ref a a 5 const 1 Using index
5972
# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
5973
# (duplicate of LP bug #888456)
5975
CREATE TABLE t1 (f1 varchar(1));
5976
INSERT INTO t1 VALUES ('v'),('s');
5977
CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
5978
INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
5979
('d'),('y'),('t'),('d'),('s');
5981
SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
5982
WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
5983
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
5984
id select_type table type possible_keys key key_len ref rows Extra
5985
1 PRIMARY table1 ALL NULL NULL NULL NULL 2
5986
1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join)
5987
2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index
5988
SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
5989
WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
5990
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
6014
# LP bug 919427: EXPLAIN for a query over a single-row table
6015
# with IN subquery in WHERE condition
6018
col_int_nokey int(11),
6019
col_varchar_nokey varchar(1)
6021
INSERT INTO ot VALUES (1,'x');
6023
col_int_key int(11),
6024
col_varchar_key varchar(1),
6025
KEY idx_cvk_cik (col_varchar_key,col_int_key)
6027
INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
6029
col_int_key int(11),
6030
col_varchar_key varchar(1),
6031
col_varchar_key2 varchar(1),
6032
KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
6033
KEY idx_cvk_cik (col_varchar_key, col_int_key)
6035
INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
6037
SELECT col_int_nokey FROM ot
6038
WHERE col_varchar_nokey IN
6039
(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6040
id select_type table type possible_keys key key_len ref rows Extra
6041
1 PRIMARY ot system NULL NULL NULL NULL 1
6042
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
6043
2 MATERIALIZED it1 ref idx_cvk_cik idx_cvk_cik 9 const,const 1 Using where; Using index
6044
SELECT col_int_nokey FROM ot
6045
WHERE col_varchar_nokey IN
6046
(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6050
SELECT col_int_nokey FROM ot
6051
WHERE (col_varchar_nokey, 'x') IN
6052
(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6053
id select_type table type possible_keys key key_len ref rows Extra
6054
1 PRIMARY ot system NULL NULL NULL NULL 1
6055
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1
6056
2 MATERIALIZED it2 ref idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 const,const 1 Using where; Using index
6057
SELECT col_int_nokey FROM ot
6058
WHERE (col_varchar_nokey, 'x') IN
6059
(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6062
DROP TABLE ot,it1,it2;
6065
# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
6066
# HAS AN EMPTY RESULT
6070
col_int_nokey int NOT NULL,
6071
col_int_key int NOT NULL,
6072
col_time_key time NOT NULL,
6073
col_varchar_key varchar(1) NOT NULL,
6074
col_varchar_nokey varchar(1) NOT NULL,
6076
KEY col_int_key (col_int_key),
6077
KEY col_time_key (col_time_key),
6078
KEY col_varchar_key (col_varchar_key,col_int_key)
6081
pk int NOT NULL AUTO_INCREMENT,
6082
col_int_nokey int NOT NULL,
6083
col_int_key int NOT NULL,
6084
col_time_key time NOT NULL,
6085
col_varchar_key varchar(1) NOT NULL,
6086
col_varchar_nokey varchar(1) NOT NULL,
6088
KEY col_int_key (col_int_key),
6089
KEY col_time_key (col_time_key),
6090
KEY col_varchar_key (col_varchar_key,col_int_key)
6092
INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
6093
SET @var2:=4, @var3:=8;
6095
Testcase without inner subquery
6096
EXPLAIN SELECT @var3:=12, sq4_alias1.*
6097
FROM t1 AS sq4_alias1
6098
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6099
sq4_alias1.col_varchar_key = @var3;
6100
id select_type table type possible_keys key key_len ref rows Extra
6101
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6102
SELECT @var3:=12, sq4_alias1.*
6103
FROM t1 AS sq4_alias1
6104
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6105
sq4_alias1.col_varchar_key = @var3;
6106
@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
6110
EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6111
FROM t1 AS sq4_alias1
6112
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6113
sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6114
id select_type table type possible_keys key key_len ref rows Extra
6115
1 SIMPLE <derived2> system NULL NULL NULL NULL 0 const row not found
6116
2 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
6117
SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6118
FROM t1 AS sq4_alias1
6119
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6120
sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6121
@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
6126
Testcase with inner subquery; crashed WL#6095
6128
EXPLAIN SELECT sq4_alias1.*
6129
FROM t1 AS sq4_alias1
6130
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6132
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6133
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6134
FROM t2 AS c_sq1_alias1
6135
WHERE (c_sq1_alias1.col_int_nokey != @var2
6136
OR c_sq1_alias1.pk != @var3));
6137
id select_type table type possible_keys key key_len ref rows Extra
6138
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6139
2 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1
6141
FROM t1 AS sq4_alias1
6142
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6144
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6145
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6146
FROM t2 AS c_sq1_alias1
6147
WHERE (c_sq1_alias1.col_int_nokey != @var2
6148
OR c_sq1_alias1.pk != @var3));
6149
pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
6150
EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
6151
FROM t1 AS sq4_alias1
6152
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6154
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6155
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6156
FROM t2 AS c_sq1_alias1
6157
WHERE (c_sq1_alias1.col_int_nokey != @var2
6158
OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6159
id select_type table type possible_keys key key_len ref rows Extra
6160
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6161
3 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1
6162
SELECT * FROM ( SELECT sq4_alias1.*
6163
FROM t1 AS sq4_alias1
6164
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6166
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6167
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6168
FROM t2 AS c_sq1_alias1
6169
WHERE (c_sq1_alias1.col_int_nokey != @var2
6170
OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6171
pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
6175
# BUG#779885: Crash in eliminate_item_equal with materialization=on in
6177
CREATE TABLE t1 ( f1 int );
6178
INSERT INTO t1 VALUES (19), (20);
6179
CREATE TABLE t2 ( f10 varchar(32) );
6180
INSERT INTO t2 VALUES ('c'),('d');
6181
CREATE TABLE t3 ( f10 varchar(32) );
6182
INSERT INTO t3 VALUES ('a'),('b');
6193
DROP TABLE t1,t2,t3;
6195
# BUG lp:813473: Wrong result with outer join + NOT IN subquery
6196
# This bug is a duplicate of Bug#11764086 whose test case is added below
6198
CREATE TABLE t1 (c int) ;
6199
INSERT INTO t1 VALUES (5),(6);
6200
CREATE TABLE t2 (a int, b int) ;
6201
INSERT INTO t2 VALUES (20,9),(20,9);
6202
create table t3 (d int, e int);
6203
insert into t3 values (2, 9), (3,10);
6204
SET @save_optimizer_switch=@@optimizer_switch;
6205
SET optimizer_switch='outer_join_with_cache=off';
6208
FROM t2 LEFT JOIN t1 ON t1.c < 3
6209
WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
6210
id select_type table type possible_keys key key_len ref rows Extra
6211
1 PRIMARY t2 ALL NULL NULL NULL NULL 2
6212
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
6213
2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
6215
FROM t2 LEFT JOIN t1 ON t1.c < 3
6216
WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
6220
SET optimizer_switch=@save_optimizer_switch;
6221
drop table t1, t2, t3;
6223
# BUG#50257: Missing info in REF column of the EXPLAIN
6224
# lines for subselects
6226
CREATE TABLE t1 (a INT, b INT, INDEX (a));
6227
INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
6229
set @tmp_optimizer_switch=@@optimizer_switch;
6230
set optimizer_switch='derived_merge=off,derived_with_keys=off';
6231
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6232
id select_type table type possible_keys key key_len ref rows Extra
6233
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
6234
2 DERIVED t1 ref a a 5 const 1
6235
set optimizer_switch=@tmp_optimizer_switch;
6237
EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6238
id select_type table type possible_keys key key_len ref rows Extra
6239
1 PRIMARY t1 ALL NULL NULL NULL NULL 4
6240
2 SUBQUERY t1 ref a a 5 const 1 Using index
6244
# Bug#11764086: Null left operand to NOT IN in WHERE clause
6245
# behaves differently than real NULL
6247
CREATE TABLE parent (id int);
6248
INSERT INTO parent VALUES (1), (2);
6249
CREATE TABLE child (parent_id int, other int);
6250
INSERT INTO child VALUES (1,NULL);
6251
# Offending query (c.parent_id is NULL for null-complemented rows only)
6252
SELECT p.id, c.parent_id
6255
ON p.id = c.parent_id
6256
WHERE c.parent_id NOT IN (
6264
# Some syntactic variations with IS FALSE and IS NOT TRUE
6265
SELECT p.id, c.parent_id
6268
ON p.id = c.parent_id
6269
WHERE c.parent_id IN (
6277
SELECT p.id, c.parent_id
6280
ON p.id = c.parent_id
6281
WHERE c.parent_id IN (
6289
DROP TABLE parent, child;
6290
# End of test for bug#11764086.
6292
# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
6293
# BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
6295
CREATE TABLE t1(a INT);
6296
INSERT INTO t1 VALUES (0), (1);
6302
INSERT INTO t2 VALUES ('a', 2), ('b', 3);
6303
SELECT 1 FROM t1 WHERE a =
6304
(SELECT 1 FROM t2 WHERE b =
6305
(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6309
SELECT 1 FROM t1 WHERE a =
6310
(SELECT 1 FROM t2 WHERE b =
6311
(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6317
# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6319
CREATE TABLE t1 (f1 varchar(1));
6320
INSERT INTO t1 VALUES ('v'),('s');
6321
CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6322
INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6323
('d'),('y'),('t'),('d'),('s');
6324
SELECT table1.f1, table2.f1_key
6325
FROM t1 AS table1, t2 AS table2
6328
SELECT DISTINCT f1_key
6330
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6352
explain SELECT table1.f1, table2.f1_key
6353
FROM t1 AS table1, t2 AS table2
6356
SELECT DISTINCT f1_key
6358
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6359
id select_type table type possible_keys key key_len ref rows Extra
6360
1 PRIMARY table1 ALL NULL NULL NULL NULL 2
6361
1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join)
6362
2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index
6365
# lp:826279: assertion failure with GROUP BY a result of subquery
6367
CREATE TABLE t1 (a int);
6368
INSERT INTO t1 VALUES (0), (0);
6369
CREATE TABLE t2 (a int, b int, c int);
6370
INSERT INTO t2 VALUES (10,7,0), (0,7,0);
6371
CREATE TABLE t3 (a int, b int);
6372
INSERT INTO t3 VALUES (10,7), (0,7);
6373
SELECT SUM(DISTINCT b),
6374
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6375
WHERE t.a != 0 AND t2.a != 0)
6376
FROM (SELECT * FROM t3) AS t
6378
SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6379
WHERE t.a != 0 AND t2.a != 0)
6381
SELECT SUM(DISTINCT b),
6382
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
6383
FROM (SELECT * FROM t3) AS t
6385
SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
6388
DROP TABLE t1,t2,t3;
6391
# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
6393
CREATE TABLE t1(a1 int);
6394
INSERT INTO t1 VALUES (1),(2);
6395
SELECT @@session.sql_mode INTO @old_sql_mode;
6396
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
6397
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
6405
(SELECT 1 FROM t1 AS t1_0
6406
WHERE 1 < SOME (SELECT a1 FROM t1)
6409
ERROR 21000: Subquery returns more than 1 row
6411
ERROR 21000: Subquery returns more than 1 row
6412
SET SESSION sql_mode=@old_sql_mode;
6413
DEALLOCATE PREPARE stmt;
6416
# LP BUG#833777 Performance regression with deeply nested subqueries
6418
create table t1 (a int not null, b char(10) not null);
6419
insert into t1 values (1, 'a');
6420
set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
6421
select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1)))))))))))))))))))))))))))));
6424
set @@optimizer_switch=@subselect_tmp;
6427
# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
6429
CREATE TABLE t1 (a varchar(3));
6430
INSERT INTO t1 VALUES ('AAA'),('BBB');
6431
CREATE TABLE t2 (a varchar(3));
6432
INSERT INTO t2 VALUES ('CCC');
6433
set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6434
SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
6436
set @@optimizer_switch=@subselect_tmp;
6439
# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
6440
# with view , UNION and prepared statement (rewriting fake_select
6443
CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
6444
INSERT INTO t1 VALUES (6,'d'),(7,'y');
6445
CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
6446
INSERT INTO t2 VALUES (10,7);
6447
CREATE VIEW v2 AS SELECT * FROM t2;
6451
LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
6452
WHERE v2.f1 NOT IN (
6460
deallocate prepare st1;
6464
# LP bug #887458 Crash in subselect_union_engine::no_rows with
6465
# double UNION and join_cache_level=3,8
6466
# (IN/ALL/ANY optimizations should not be applied to fake_select)
6467
CREATE TABLE t2 ( a int, b varchar(1)) ;
6468
INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
6469
CREATE TABLE t1 ( b varchar(1)) ;
6470
INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
6471
set @save_join_cache_level=@@join_cache_level;
6472
SET SESSION join_cache_level=3;
6483
set @@join_cache_level= @save_join_cache_level;
6486
# LP bug #885162 Got error 124 from storage engine with UNION inside
6487
# subquery and join_cache_level=3..8
6488
# (IN/ALL/ANY optimizations should not be applied to fake_select)
6491
f1 varchar(1) DEFAULT NULL
6493
INSERT INTO t1 VALUES ('c');
6494
set @save_join_cache_level=@@join_cache_level;
6495
SET SESSION join_cache_level=8;
6496
SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
6498
set @@join_cache_level= @save_join_cache_level;
6501
# LP BUG#747278 incorrect values of the NULL (no rows) single
6502
# row subquery requested via element_index() interface
6504
CREATE TABLE t1 (f1a int, f1b int) ;
6505
INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
6506
CREATE TABLE t2 ( f2 int);
6507
INSERT IGNORE INTO t2 VALUES (3),(4);
6508
CREATE TABLE t3 (f3a int default 1, f3b int default 2);
6509
INSERT INTO t3 VALUES (1,1),(2,2);
6510
set @old_optimizer_switch = @@session.optimizer_switch;
6511
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
6512
SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6513
(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6516
SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6517
(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6520
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6521
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6524
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6525
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6527
SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6528
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6531
SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6532
(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6535
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6536
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6539
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6540
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6542
set @@session.optimizer_switch=@old_optimizer_switch;
6543
SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6544
(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6547
SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6548
(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6551
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6552
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6555
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6556
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6558
SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6559
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6562
SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6563
(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6566
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6567
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6570
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6571
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6573
select (null, null) = (null, null);
6574
(null, null) = (null, null)
6576
SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0);
6577
(SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0)
6579
drop tables t1,t2,t3;
6581
# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
6583
CREATE TABLE t1 (a date, KEY (a)) ;
6584
INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
6585
set @old_optimizer_switch = @@optimizer_switch;
6586
SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6587
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6588
id select_type table type possible_keys key key_len ref rows Extra
6589
1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
6590
2 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index
6591
SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6595
SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
6596
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6597
id select_type table type possible_keys key key_len ref rows Extra
6598
1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
6599
2 MATERIALIZED t1 index NULL a 4 NULL 2 Using index
6600
SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6604
set @@optimizer_switch=@old_optimizer_switch;
6607
# LP BUG#908269 incorrect condition in case of subqueries depending
6608
# on constant tables
6610
CREATE TABLE t1 ( a INT );
6611
INSERT INTO t1 VALUES (1),(5);
6612
CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
6613
INSERT INTO t2 VALUES (1);
6614
CREATE TABLE t3 ( c INT );
6615
INSERT INTO t3 VALUES (4),(5);
6616
SET optimizer_switch='subquery_cache=off';
6617
SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
6618
( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
6621
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
6622
( SELECT b FROM t2 WHERE b = a OR b * 0)
6625
SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
6626
( SELECT b FROM t2 WHERE b = a OR rand() * 0)
6629
drop table t1,t2,t3;
6630
set optimizer_switch=@subselect_tmp;
6632
# LP BUG#905353 Wrong non-empty result with a constant table,
6633
# aggregate function in subquery, MyISAM or Aria
6635
CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
6636
INSERT INTO t1 VALUES (1);
6637
SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
6641
# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in
6642
# main query and implicit grouping
6644
CREATE TABLE t1 (f1 int) engine=MyISAM;
6645
INSERT INTO t1 VALUES (7),(8);
6646
CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
6647
INSERT INTO t2 VALUES (3,'f');
6649
SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6650
id select_type table type possible_keys key key_len ref rows Extra
6651
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6652
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
6653
SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6657
SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6658
id select_type table type possible_keys key key_len ref rows Extra
6659
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6660
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
6661
SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6665
SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6666
id select_type table type possible_keys key key_len ref rows Extra
6667
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6668
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
6669
SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6673
SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6674
id select_type table type possible_keys key key_len ref rows Extra
6675
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
6676
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
6677
SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6682
# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
6683
# HAVING, UNION in subquery
6685
CREATE TABLE t1 (a INT);
6686
INSERT INTO t1 VALUES (7),(0);
6687
CREATE TABLE t2 (b INT);
6689
SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6690
id select_type table type possible_keys key key_len ref rows Extra
6691
1 PRIMARY t2 system NULL NULL NULL NULL 0 const row not found
6692
1 PRIMARY t1 ALL NULL NULL NULL NULL 2
6693
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
6694
3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
6695
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
6696
SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6700
# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
6701
# WHERE and UNION in HAVING
6703
CREATE TABLE t1 (a INT);
6704
INSERT INTO t1 VALUES (1),(7);
6706
SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6707
id select_type table type possible_keys key key_len ref rows Extra
6708
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
6709
2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
6710
3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
6711
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
6712
SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6715
SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
6716
id select_type table type possible_keys key key_len ref rows Extra
6717
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
6718
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
6719
3 DEPENDENT UNION t1 ALL NULL NULL NULL NULL 2 Using where
6720
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
6721
SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
6725
# MDEV-367: Different results with and without subquery_cache on
6726
# a query with a constant NOT IN condition
6728
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
6729
INSERT INTO t1 VALUES (1),(2),(3);
6730
set @mdev367_optimizer_switch = @@optimizer_switch;
6731
set optimizer_switch = 'subquery_cache=on';
6732
SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6734
SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6735
a ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
6739
set optimizer_switch=@mdev367_optimizer_switch;
6740
set optimizer_switch = 'subquery_cache=off';
6741
SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6743
SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6744
a ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
6748
set optimizer_switch=@mdev367_optimizer_switch;
6751
# MDEV-521 single value subselect transformation problem
6753
CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
6754
INSERT INTO t1 VALUES ('u1'),('u2');
6755
SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6760
SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6765
# return optimizer switch changed in the beginning of this test
6766
set optimizer_switch=@subselect_tmp;
6768
# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
6769
# precomputed and thus not part of optimization
6771
CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
6772
INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
6773
('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
6775
SELECT MAX( alias2.a ) AS field
6776
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6777
WHERE alias1.a = alias2.a OR alias1.a = 'y'
6778
HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6779
id select_type table type possible_keys key key_len ref rows Extra
6780
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
6781
2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where
6782
SELECT MAX( alias2.a ) AS field
6783
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6784
WHERE alias1.a = alias2.a OR alias1.a = 'y'
6785
HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6788
SELECT MAX( alias2.a )
6789
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6790
WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6791
id select_type table type possible_keys key key_len ref rows Extra
6792
1 PRIMARY alias1 index a a 19 NULL 11 Using where; Using index
6793
1 PRIMARY alias2 ref a a 19 test.alias1.a 2 Using index
6794
1 PRIMARY alias3 index NULL a 19 NULL 11 Using index; Using join buffer (flat, BNL join)
6795
2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where
6796
SELECT MAX( alias2.a )
6797
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6798
WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6803
# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
6804
# with semijoin+materialization, IN and = subqueries
6806
CREATE TABLE t1 (a1 INT);
6807
INSERT INTO t1 VALUES (4),(6);
6808
CREATE TABLE t2 (b1 INT);
6809
INSERT INTO t2 VALUES (1),(7);
6812
WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6813
id select_type table type possible_keys key key_len ref rows Extra
6814
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
6815
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
6816
2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1
6817
3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
6818
3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
6820
WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6824
# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
6825
# inner joins takes hundreds times longer
6827
CREATE TABLE t1 (a INT);
6828
INSERT INTO t1 VALUES (1),(7);
6829
CREATE TABLE t2 (b INT);
6830
INSERT INTO t2 VALUES (4),(5);
6831
CREATE TABLE t3 (c INT);
6832
INSERT INTO t3 VALUES (8),(3);
6833
set @@expensive_subquery_limit= 0;
6835
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6836
FROM t2 alias1, t1 alias2, t1 alias3;
6837
id select_type table type possible_keys key key_len ref rows Extra
6838
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
6839
1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
6840
1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
6841
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
6842
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
6843
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
6845
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6846
FROM t2 alias1, t1 alias2, t1 alias3;
6847
(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6856
show status like "subquery_cache%";
6858
Subquery_cache_hit 6
6859
Subquery_cache_miss 2
6860
show status like '%Handler_read%';
6862
Handler_read_first 0
6868
Handler_read_rnd_deleted 0
6869
Handler_read_rnd_next 22
6870
set @@expensive_subquery_limit= default;
6872
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6873
FROM t2 alias1, t1 alias2, t1 alias3;
6874
id select_type table type possible_keys key key_len ref rows Extra
6875
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
6876
1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
6877
1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
6878
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
6879
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
6880
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
6882
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6883
FROM t2 alias1, t1 alias2, t1 alias3;
6884
(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6893
show status like "subquery_cache%";
6895
Subquery_cache_hit 0
6896
Subquery_cache_miss 0
6897
show status like '%Handler_read%';
6899
Handler_read_first 0
6905
Handler_read_rnd_deleted 0
6906
Handler_read_rnd_next 16
6907
drop table t1, t2, t3;
6909
# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
6911
CREATE TABLE t1 (a INT);
6912
INSERT INTO t1 VALUES (0),(8);
6913
CREATE TABLE t2 (b INT PRIMARY KEY);
6914
INSERT INTO t2 VALUES (1),(2);
6916
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6917
id select_type table type possible_keys key key_len ref rows Extra
6918
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
6919
2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
6920
3 SUBQUERY t1 ALL NULL NULL NULL NULL 2
6921
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6925
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
6927
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
6928
INSERT INTO t1 VALUES ('USA');
6929
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
6930
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
6931
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
6932
INSERT INTO t3 VALUES (12),(22),(9),(45);
6933
create table t4 like t3;
6934
insert into t4 select * from t3;
6935
# This should not show range access for table t2
6937
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6938
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6939
id select_type table type possible_keys key key_len ref rows Extra
6940
1 PRIMARY t1 system NULL NULL NULL NULL 1
6941
1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
6942
1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
6943
3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
6944
set @tmp_mdev410=@@global.userstat;
6945
set global userstat=on;
6946
flush table_statistics;
6947
flush index_statistics;
6948
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6949
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6952
# The following shows that t2 was indeed scanned with a full scan.
6953
show table_statistics;
6954
Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
6957
show index_statistics;
6958
Table_schema Table_name Index_name Rows_read
6960
set global userstat=@tmp_mdev410;
6961
DROP TABLE t1,t2,t3,t4;
6963
# MDEV-430: Server crashes in select_describe on EXPLAIN with
6964
# materialization+semijoin, 2 nested subqueries, aggregate functions
6966
CREATE TABLE t1 (a INT, KEY(a));
6967
INSERT INTO t1 VALUES (1),(8);
6968
CREATE TABLE t2 (b INT, KEY(b));
6969
INSERT INTO t2 VALUES (45),(17),(20);
6970
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
6971
id select_type table type possible_keys key key_len ref rows Extra
6972
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
6973
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
6974
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
6977
# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
6979
CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
6980
INSERT INTO t1 VALUES (8),(0);
6981
CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
6982
INSERT INTO t2 VALUES (4,'j'),(6,'v');
6983
CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
6984
INSERT INTO t3 VALUES ('b'),('c');
6987
WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
6988
id select_type table type possible_keys key key_len ref rows Extra
6989
1 PRIMARY t1 index a a 5 NULL 2 Using where; Using index
6990
2 SUBQUERY <subquery3> ALL distinct_key NULL NULL NULL 1
6991
2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
6992
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2
6994
WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
6996
drop table t1, t2, t3;
6998
# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
7000
CREATE TABLE t1 (a INT, KEY(a));
7001
INSERT INTO t1 VALUES (1),(8);
7002
CREATE TABLE t2 (b INT, KEY(b));
7003
INSERT INTO t2 VALUES (45),(17),(20);
7004
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7005
id select_type table type possible_keys key key_len ref rows Extra
7006
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
7007
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
7008
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index