1select @@optimizer_switch like '%subquery_cache=on%';
2@@optimizer_switch like '%subquery_cache=on%'
31
4set optimizer_switch='subquery_cache=off';
5set join_cache_level=1;
6set @join_cache_level_for_subselect_test=@@join_cache_level;
7drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
8drop view if exists v2;
9call mtr.add_suppression("Sort aborted.*");
10set @subselect_tmp=@@optimizer_switch;
11set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
12"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
13set join_cache_level=@join_cache_level_for_subselect_test;
14SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
15SET optimizer_use_condition_selectivity=1;
16select (select 2);
17(select 2)
182
19explain extended select (select 2);
20id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
211	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
22Warnings:
23Note	1249	Select 2 was reduced during optimization
24Note	1003	select 2 AS `(select 2)`
25SELECT (SELECT 1) UNION SELECT (SELECT 2);
26(SELECT 1)
271
282
29explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
30id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
311	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
323	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
33NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
34Warnings:
35Note	1249	Select 2 was reduced during optimization
36Note	1249	Select 4 was reduced during optimization
37Note	1003	/* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)`
38SELECT (SELECT (SELECT 0 UNION SELECT 0));
39(SELECT (SELECT 0 UNION SELECT 0))
400
41explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
42id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
431	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
443	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
454	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
46NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
47Warnings:
48Note	1249	Select 2 was reduced during optimization
49Note	1003	/* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
50SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
51ERROR 42S22: Reference 'a' not supported (forward reference in item list)
52SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
53ERROR 42S22: Reference 'b' not supported (forward reference in item list)
54SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
55(SELECT 1)	MAX(1)
561	1
57SELECT (SELECT a) as a;
58ERROR 42S22: Reference 'a' not supported (forward reference in item list)
59EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
60id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
611	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	1	100.00
623	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
632	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
64Warnings:
65Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
66Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
67Note	1003	/* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1
68SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
691
701
71SELECT (SELECT 1), a;
72ERROR 42S22: Unknown column 'a' in 'field list'
73SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
74a
751
76SELECT 1 FROM (SELECT (SELECT a) b) c;
77ERROR 42S22: Unknown column 'a' in 'field list'
78SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
79id
801
81SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
82ERROR 21000: Operand should contain 1 column(s)
83SELECT 1 IN (SELECT 1);
841 IN (SELECT 1)
851
86SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
871
881
89select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
90ERROR 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 'PROCEDURE ANALYSE(1))' at line 1
91SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
92ERROR 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
93SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
94ERROR 42S22: Unknown column 'a' in 'field list'
95SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
96ERROR 42S22: Unknown column 'a' in 'field list'
97SELECT (SELECT 1,2,3) = ROW(1,2,3);
98(SELECT 1,2,3) = ROW(1,2,3)
991
100SELECT (SELECT 1,2,3) = ROW(1,2,1);
101(SELECT 1,2,3) = ROW(1,2,1)
1020
103SELECT (SELECT 1,2,3) < ROW(1,2,1);
104(SELECT 1,2,3) < ROW(1,2,1)
1050
106SELECT (SELECT 1,2,3) > ROW(1,2,1);
107(SELECT 1,2,3) > ROW(1,2,1)
1081
109SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
110(SELECT 1,2,3) = ROW(1,2,NULL)
111NULL
112SELECT ROW(1,2,3) = (SELECT 1,2,3);
113ROW(1,2,3) = (SELECT 1,2,3)
1141
115SELECT ROW(1,2,3) = (SELECT 1,2,1);
116ROW(1,2,3) = (SELECT 1,2,1)
1170
118SELECT ROW(1,2,3) < (SELECT 1,2,1);
119ROW(1,2,3) < (SELECT 1,2,1)
1200
121SELECT ROW(1,2,3) > (SELECT 1,2,1);
122ROW(1,2,3) > (SELECT 1,2,1)
1231
124SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
125ROW(1,2,3) = (SELECT 1,2,NULL)
126NULL
127SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
128(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
1291
130SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
131(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
1320
133SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
134(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
1350
136Warnings:
137Warning	1292	Truncated incorrect DOUBLE value: '1.5b'
138SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
139(SELECT 'b',2,'a') = ROW(1.5,2,'a')
1400
141Warnings:
142Warning	1292	Truncated incorrect DOUBLE value: 'b'
143SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
144(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
1451
146SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
147(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
1480
149Warnings:
150Warning	1292	Truncated incorrect DOUBLE value: 'c'
151SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
152ERROR 21000: Operand should contain 1 column(s)
153SELECT 1 as a,(SELECT a+a) b,(SELECT b);
154a	b	(SELECT b)
1551	2	2
156create table t1 (a int);
157create table t2 (a int, b int);
158create table t3 (a int);
159create table t4 (a int not null, b int not null);
160insert into t1 values (2);
161insert into t2 values (1,7),(2,7);
162insert into t4 values (4,8),(3,8),(5,9);
163select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
164ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
165select (select a from t1 where t1.a=t2.a), a from t2;
166(select a from t1 where t1.a=t2.a)	a
167NULL	1
1682	2
169select (select a from t1 where t1.a=t2.b), a from t2;
170(select a from t1 where t1.a=t2.b)	a
171NULL	1
172NULL	2
173select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
174(select a from t1)	a	(select 1 union select 2 limit 1)
1752	1	1
1762	2	1
177select (select a from t3), a from t2;
178(select a from t3)	a
179NULL	1
180NULL	2
181select * from t2 where t2.a=(select a from t1);
182a	b
1832	7
184insert into t3 values (6),(7),(3);
185select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
186a	b
1871	7
1882	7
189(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;
190a	b
1911	7
1922	7
1933	8
194(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);
195a	b
1961	7
1972	7
1984	8
1993	8
200explain 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);
201id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2021	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
2032	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using filesort
2043	UNION	t4	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2054	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
206NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
207Warnings:
208Note	1003	(/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`b` = (/* select#2 */ select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1)) union (/* select#3 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where `test`.`t4`.`b` = (/* select#4 */ select max(`test`.`t2`.`a`) * 4 from `test`.`t2`))
209select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
210(select a from t3 where a<t2.a*4 order by 1 desc limit 1)	a
2113	1
2127	2
213select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
214(select * from t2 where a>1) as tt;
215(select t3.a from t3 where a<8 order by 1 desc limit 1)	a
2167	2
217set @tmp_optimizer_switch=@@optimizer_switch;
218set optimizer_switch='derived_merge=off,derived_with_keys=off';
219explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
220(select * from t2 where a>1) as tt;
221id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2221	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	100.00
2233	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
2242	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using filesort
225Warnings:
226Note	1003	/* select#1 */ select (/* select#2 */ 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#3 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`a` > 1) `tt`
227set optimizer_switch=@tmp_optimizer_switch;
228select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
229a
2302
231select * 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);
232a
2332
234select * 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);
235a
236select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
237b	(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
2388	7.5000
2398	4.5000
2409	7.5000
241explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
242id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2431	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	3	100.00
2442	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
2453	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
246Warnings:
247Note	1276	Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
248Note	1003	/* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ 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`
249select * from t3 where exists (select * from t2 where t2.b=t3.a);
250a
2517
252select * from t3 where not exists (select * from t2 where t2.b=t3.a);
253a
2546
2553
256select * from t3 where a in (select b from t2);
257a
2587
259select * from t3 where a not in (select b from t2);
260a
2616
2623
263select * from t3 where a = some (select b from t2);
264a
2657
266select * from t3 where a <> any (select b from t2);
267a
2686
2693
270select * from t3 where a = all (select b from t2);
271a
2727
273select * from t3 where a <> all (select b from t2);
274a
2756
2763
277insert into t2 values (100, 5);
278select * from t3 where a < any (select b from t2);
279a
2806
2813
282select * from t3 where a < all (select b from t2);
283a
2843
285select * from t3 where a >= any (select b from t2);
286a
2876
2887
289explain extended select * from t3 where a >= any (select b from t2);
290id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2911	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2922	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	3	100.00
293Warnings:
294Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
295select * from t3 where a >= all (select b from t2);
296a
2977
298delete from t2 where a=100;
299select * from t3 where a in (select a,b from t2);
300ERROR 21000: Operand should contain 1 column(s)
301select * from t3 where a in (select * from t2);
302ERROR 21000: Operand should contain 1 column(s)
303insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
304select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
305b	ma
306insert into t2 values (2,10);
307select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
308b	ma
30910	1
310delete from t2 where a=2 and b=10;
311select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
312b	ma
3137	12
314create table t5 (a int);
315select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
316(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
317NULL	1
3182	2
319insert into t5 values (5);
320select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
321(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
322NULL	1
3232	2
324insert into t5 values (2);
325select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
326(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
327NULL	1
3282	2
329explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
330id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3311	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
3322	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
3333	DEPENDENT UNION	t5	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
334NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
335Warnings:
336Note	1276	Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
337Note	1276	Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
338Note	1003	/* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ 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`
339select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
340ERROR 21000: Subquery returns more than 1 row
341create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
342create table t7( uq int primary key, name char(25));
343insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
344insert into t6 values (1,1),(1,2),(2,2),(1,3);
345select * from t6 where exists (select * from t7 where uq = clinic_uq);
346patient_uq	clinic_uq
3471	1
3481	2
3492	2
350explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
351id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3521	PRIMARY	t7	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using index
3531	PRIMARY	t6	ALL	i1	NULL	NULL	NULL	4	75.00	Using where; Using join buffer (flat, BNL join)
354Warnings:
355Note	1276	Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
356Note	1003	select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t7` join `test`.`t6` where `test`.`t6`.`clinic_uq` = `test`.`t7`.`uq`
357select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
358ERROR 23000: Column 'a' in field list is ambiguous
359drop table t1,t2,t3;
360CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
361INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
362CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
363INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
364CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
365INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
366SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
367a	b
368W	1732-02-22
369SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
370a	b
371W	1
372SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
373a	b
374W	a
375CREATE TABLE `t8` (
376`pseudo` varchar(35) character set latin1 NOT NULL default '',
377`email` varchar(60) character set latin1 NOT NULL default '',
378PRIMARY KEY  (`pseudo`),
379UNIQUE KEY `email` (`email`)
380) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
381INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
382INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
383INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
384EXPLAIN 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');
385id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3861	PRIMARY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3874	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3882	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00
3893	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
390Warnings:
391Note	1003	/* select#1 */ select 'joce' AS `pseudo`,(/* select#2 */ 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
392SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
393t8 WHERE pseudo='joce');
394ERROR HY000: Illegal parameter data types varchar and row for operation '='
395SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
396pseudo='joce');
397ERROR HY000: Illegal parameter data types varchar and row for operation '='
398SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
399pseudo
400joce
401SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
402ERROR 21000: Subquery returns more than 1 row
403drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
404CREATE TABLE `t1` (
405`topic` mediumint(8) unsigned NOT NULL default '0',
406`date` date NOT NULL default '0000-00-00',
407`pseudo` varchar(35) character set latin1 NOT NULL default '',
408PRIMARY KEY  (`pseudo`,`date`,`topic`),
409KEY `topic` (`topic`)
410) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
411INSERT INTO t1 (topic,date,pseudo) VALUES
412('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
413EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
414id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4151	SIMPLE	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
416Warnings:
417Note	1003	select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03'
418EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
419id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4201	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4212	SUBQUERY	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
422Warnings:
423Note	1003	/* select#1 */ select (/* select#2 */ select distinct `test`.`t1`.`date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03') AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')`
424SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
425date
4262002-08-03
427SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
428(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
4292002-08-03
430SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
4311
4321
4331
4341
435SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
436ERROR 21000: Subquery returns more than 1 row
437EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
438id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4391	PRIMARY	t1	index	NULL	topic	3	NULL	2	100.00	Using index
4402	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4413	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
442NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
443Warnings:
444Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
445drop table t1;
446CREATE TABLE `t1` (
447`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
448`maxnumrep` int(10) unsigned NOT NULL default '0',
449PRIMARY KEY  (`numeropost`),
450UNIQUE KEY `maxnumrep` (`maxnumrep`)
451) ENGINE=MyISAM ROW_FORMAT=FIXED;
452INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
453CREATE TABLE `t2` (
454`mot` varchar(30) NOT NULL default '',
455`topic` mediumint(8) unsigned NOT NULL default '0',
456`date` date NOT NULL default '0000-00-00',
457`pseudo` varchar(35) NOT NULL default '',
458PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
459) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
460INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
461select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
462a
46340143
464SELECT 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;
465numeropost	maxnumrep
46643506	2
46740143	1
468SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
469ERROR 42S22: Unknown column 'a' in 'having clause'
470SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
471ERROR 42S22: Unknown column 'a' in 'having clause'
472SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
473mot	topic	date	pseudo
474joce	40143	2002-10-22	joce
475joce	43506	2002-10-22	joce
476SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
477mot	topic	date	pseudo
478SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
479mot	topic	date	pseudo
480SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
481mot	topic	date	pseudo
482joce	40143	2002-10-22	joce
483joce	43506	2002-10-22	joce
484SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
485mot	topic	date	pseudo
486SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
487mot	topic	date	pseudo
488SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
489mot	topic	date	pseudo
490SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
491mot	topic	date	pseudo
492joce	40143	2002-10-22	joce
493joce	43506	2002-10-22	joce
494SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
495mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
496joce	40143	2002-10-22	joce	1
497joce	43506	2002-10-22	joce	1
498SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
499mot	topic	date	pseudo
500SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
501mot	topic	date	pseudo
502joce	40143	2002-10-22	joce
503joce	43506	2002-10-22	joce
504SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
505mot	topic	date	pseudo
506joce	40143	2002-10-22	joce
507SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
508mot	topic	date	pseudo
509joce	40143	2002-10-22	joce
510SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
511mot	topic	date	pseudo
512joce	40143	2002-10-22	joce
513SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
514mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
515joce	40143	2002-10-22	joce	1
516joce	43506	2002-10-22	joce	0
517drop table t1,t2;
518CREATE TABLE `t1` (
519`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
520`maxnumrep` int(10) unsigned NOT NULL default '0',
521PRIMARY KEY  (`numeropost`),
522UNIQUE KEY `maxnumrep` (`maxnumrep`)
523) ENGINE=MyISAM ROW_FORMAT=FIXED;
524INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
525select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
526ERROR 21000: Subquery returns more than 1 row
527select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
528ERROR 21000: Subquery returns more than 1 row
529show warnings;
530Level	Code	Message
531Error	1242	Subquery returns more than 1 row
532Error	1028	Sort aborted: Subquery returns more than 1 row
533drop table t1;
534create table t1 (a int);
535insert into t1 values (1),(2),(3);
536(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
537a
5381
5392
5403
541drop table t1;
542CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
543INSERT INTO t1 VALUES ();
544SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
545ERROR 21000: Subquery returns more than 1 row
546drop table t1;
547CREATE TABLE `t1` (
548`numeropost` mediumint(8) unsigned NOT NULL default '0',
549`numreponse` int(10) unsigned NOT NULL auto_increment,
550`pseudo` varchar(35) NOT NULL default '',
551PRIMARY KEY  (`numeropost`,`numreponse`),
552UNIQUE KEY `numreponse` (`numreponse`),
553KEY `pseudo` (`pseudo`,`numeropost`)
554) ENGINE=MyISAM;
555SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
556ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
557SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
558ERROR 42S22: Unknown column 'a' in 'having clause'
559SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
560numreponse	(SELECT numeropost FROM t1 HAVING numreponse=1)
561INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
562EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
563ERROR 21000: Subquery returns more than 1 row
564SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
565ERROR 21000: Subquery returns more than 1 row
566EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
567id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5681	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
569Warnings:
570Note	1003	select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1'
571EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
572id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5731	PRIMARY	t1	const	PRIMARY,numreponse	PRIMARY	7	const,const	1	100.00	Using index
5742	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
575Warnings:
576Note	1003	/* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1
577drop table t1;
578CREATE TABLE t1 (a int(1));
579INSERT INTO t1 VALUES (1);
580SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
5811
5821
583drop table t1;
584create table t1 (a int NOT NULL, b int, primary key (a));
585create table t2 (a int NOT NULL, b int, primary key (a));
586insert into t1 values (0, 10),(1, 11),(2, 12);
587insert into t2 values (1, 21),(2, 22),(3, 23);
588select * from t1;
589a	b
5900	10
5911	11
5922	12
593update t1 set b= (select b from t2);
594ERROR 21000: Subquery returns more than 1 row
595update t1 set b= (select b from t2 where t1.a = t2.a);
596select * from t1;
597a	b
5980	NULL
5991	21
6002	22
601drop table t1, t2;
602create table t1 (a int NOT NULL, b int, primary key (a));
603create table t2 (a int NOT NULL, b int, primary key (a));
604insert into t1 values (0, 10),(1, 11),(2, 12);
605insert into t2 values (1, 21),(2, 12),(3, 23);
606select * from t1;
607a	b
6080	10
6091	11
6102	12
611select * from t1 where b = (select b from t2 where t1.a = t2.a);
612a	b
6132	12
614delete from t1 where b in (select b from t1);
615affected rows: 3
616insert into t1 values (0, 10),(1, 11),(2, 12);
617delete from t1 where b = (select b from t2);
618ERROR 21000: Subquery returns more than 1 row
619delete from t1 where b = (select b from t2 where t1.a = t2.a);
620select * from t1 order by b;
621a	b
6220	10
6231	11
624drop table t1, t2;
625create table t11 (a int NOT NULL, b int, primary key (a));
626create table t12 (a int NOT NULL, b int, primary key (a));
627create table t2 (a int NOT NULL, b int, primary key (a));
628insert into t11 values (0, 10),(1, 11),(2, 12);
629insert into t12 values (33, 10),(22, 11),(2, 12);
630insert into t2 values (1, 21),(2, 12),(3, 23);
631select * from t11;
632a	b
6330	10
6341	11
6352	12
636select * from t12;
637a	b
63833	10
63922	11
6402	12
641delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
642ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data
643delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
644ERROR 21000: Subquery returns more than 1 row
645delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
646select * from t11;
647a	b
6480	10
6491	11
650select * from t12;
651a	b
65233	10
65322	11
654drop table t11, t12, t2;
655CREATE TABLE t1 (x int) ENGINE=MyISAM;
656create table t2 (a int) ENGINE=MyISAM;
657create table t3 (b int);
658insert into t2 values (1);
659insert into t3 values (1),(2);
660INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
661ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
662INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
663ERROR 21000: Subquery returns more than 1 row
664INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
665select * from t1;
666x
6671
668insert into t2 values (1);
669INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
670select * from t1;
671x
6721
6732
674INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
675select * from t1;
676x
6771
6782
6793
6803
681INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
682select * from t1;
683x
6841
6852
6863
6873
68811
68911
690INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
691ERROR 42S22: Unknown column 'x' in 'field list'
692INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
693select * from t1;
694x
6951
6962
6973
6983
69911
70011
7012
702drop table t1, t2, t3;
703CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
704create table t2 (a int);
705create table t3 (a int);
706insert into t2 values (1);
707insert into t3 values (1),(2);
708select * from t1;
709x	y
710replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
711ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
712replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
713ERROR 21000: Subquery returns more than 1 row
714replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
715select * from t1;
716x	y
7171	2
718replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
719select * from t1;
720x	y
7211	3
722replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
723select * from t1;
724x	y
7251	3
7264	1
727replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
728select * from t1;
729x	y
7301	3
7314	2
732replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
733select * from t1;
734x	y
7351	3
7364	2
7372	1
738drop table t1, t2, t3;
739SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
740ERROR HY000: No tables used
741CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
742INSERT INTO t2 VALUES (1),(2);
743SELECT * FROM t2 WHERE id IN (SELECT 1);
744id
7451
746EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
747id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7481	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using index
749Warnings:
750Note	1249	Select 2 was reduced during optimization
751Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1
752SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
753id
7541
755SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
756id
7572
758EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
759id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7601	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using where; Using index
761Warnings:
762Note	1249	Select 3 was reduced during optimization
763Note	1249	Select 2 was reduced during optimization
764Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1)
765EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
766id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7671	PRIMARY	t2	index	NULL	id	5	NULL	2	100.00	Using where; Using index
7682	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
7693	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
770NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
771Warnings:
772Note	1003	/* select#1 */ select `test`.`t2`.`id` AS `id` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(/* select#2 */ select 1 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(1) union /* select#3 */ select 3 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))
773SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
774id
775SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
776id
7772
778INSERT INTO t2 VALUES ((SELECT * FROM t2));
779ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
780INSERT INTO t2 VALUES ((SELECT id FROM t2));
781ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
782SELECT * FROM t2;
783id
7841
7852
786CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
787INSERT INTO t1 values (1),(1);
788UPDATE t2 SET id=(SELECT * FROM t1);
789ERROR 21000: Subquery returns more than 1 row
790drop table t2, t1;
791create table t1 (a int);
792insert into t1 values (1),(2),(3);
793select 1 IN (SELECT * from t1);
7941 IN (SELECT * from t1)
7951
796select 10 IN (SELECT * from t1);
79710 IN (SELECT * from t1)
7980
799select NULL IN (SELECT * from t1);
800NULL IN (SELECT * from t1)
801NULL
802update t1 set a=NULL where a=2;
803select 1 IN (SELECT * from t1);
8041 IN (SELECT * from t1)
8051
806select 3 IN (SELECT * from t1);
8073 IN (SELECT * from t1)
8081
809select 10 IN (SELECT * from t1);
81010 IN (SELECT * from t1)
811NULL
812select 1 > ALL (SELECT * from t1);
8131 > ALL (SELECT * from t1)
8140
815select 10 > ALL (SELECT * from t1);
81610 > ALL (SELECT * from t1)
817NULL
818select 1 > ANY (SELECT * from t1);
8191 > ANY (SELECT * from t1)
820NULL
821select 10 > ANY (SELECT * from t1);
82210 > ANY (SELECT * from t1)
8231
824drop table t1;
825create table t1 (a varchar(20));
826insert into t1 values ('A'),('BC'),('DEF');
827select 'A' IN (SELECT * from t1);
828'A' IN (SELECT * from t1)
8291
830select 'XYZS' IN (SELECT * from t1);
831'XYZS' IN (SELECT * from t1)
8320
833select NULL IN (SELECT * from t1);
834NULL IN (SELECT * from t1)
835NULL
836update t1 set a=NULL where a='BC';
837select 'A' IN (SELECT * from t1);
838'A' IN (SELECT * from t1)
8391
840select 'DEF' IN (SELECT * from t1);
841'DEF' IN (SELECT * from t1)
8421
843select 'XYZS' IN (SELECT * from t1);
844'XYZS' IN (SELECT * from t1)
845NULL
846select 'A' > ALL (SELECT * from t1);
847'A' > ALL (SELECT * from t1)
8480
849select 'XYZS' > ALL (SELECT * from t1);
850'XYZS' > ALL (SELECT * from t1)
851NULL
852select 'A' > ANY (SELECT * from t1);
853'A' > ANY (SELECT * from t1)
854NULL
855select 'XYZS' > ANY (SELECT * from t1);
856'XYZS' > ANY (SELECT * from t1)
8571
858drop table t1;
859create table t1 (a float);
860insert into t1 values (1.5),(2.5),(3.5);
861select 1.5 IN (SELECT * from t1);
8621.5 IN (SELECT * from t1)
8631
864select 10.5 IN (SELECT * from t1);
86510.5 IN (SELECT * from t1)
8660
867select NULL IN (SELECT * from t1);
868NULL IN (SELECT * from t1)
869NULL
870update t1 set a=NULL where a=2.5;
871select 1.5 IN (SELECT * from t1);
8721.5 IN (SELECT * from t1)
8731
874select 3.5 IN (SELECT * from t1);
8753.5 IN (SELECT * from t1)
8761
877select 10.5 IN (SELECT * from t1);
87810.5 IN (SELECT * from t1)
879NULL
880select 1.5 > ALL (SELECT * from t1);
8811.5 > ALL (SELECT * from t1)
8820
883select 10.5 > ALL (SELECT * from t1);
88410.5 > ALL (SELECT * from t1)
885NULL
886select 1.5 > ANY (SELECT * from t1);
8871.5 > ANY (SELECT * from t1)
888NULL
889select 10.5 > ANY (SELECT * from t1);
89010.5 > ANY (SELECT * from t1)
8911
892explain extended select (select a+1) from t1;
893id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
8941	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
895Warnings:
896Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
897Note	1249	Select 2 was reduced during optimization
898Note	1003	select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
899select (select a+1) from t1;
900(select a+1)
9012.5
902NULL
9034.5
904drop table t1;
905CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY  (a));
906CREATE TABLE t2 (a int(11) default '0', INDEX (a));
907INSERT INTO t1 VALUES (1),(2),(3),(4);
908INSERT INTO t2 VALUES (1),(2),(3);
909SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
910a	t1.a in (select t2.a from t2)
9111	1
9122	1
9133	1
9144	0
915explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
916id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9171	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9182	SUBQUERY	t2	index_subquery	a	a	5	func	2	100.00	Using index
919Warnings:
920Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having `test`.`t2`.`a` is null))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
921CREATE TABLE t3 (a int(11) default '0');
922INSERT INTO t3 VALUES (1),(2),(3);
923SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
924a	t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
9251	1
9262	1
9273	1
9284	0
929explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
930id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9311	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9322	DEPENDENT SUBQUERY	t2	ref_or_null	a	a	5	func	2	100.00	Using where; Using index
9332	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (flat, BNL join)
934Warnings:
935Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ 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 `test`.`t2`.`a` is null) having `test`.`t2`.`a` is null)) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
936drop table t1,t2,t3;
937# check correct NULL Processing for normal IN/ALL/ANY
938# and 2 ways of max/min optimization
939create table t1 (a int);
940insert into t1 values (1), (100), (NULL), (1000);
941create table t2 (a int not null);
942# subselect returns empty set (for NULL and non-NULL left part)
943select a, a in (select * from t2) from t1;
944a	a in (select * from t2)
9451	0
946100	0
947NULL	0
9481000	0
949select a, a > any (select * from t2) from t1;
950a	a > any (select * from t2)
9511	0
952100	0
953NULL	0
9541000	0
955select a, a > all (select * from t2) from t1;
956a	a > all (select * from t2)
9571	1
958100	1
959NULL	1
9601000	1
961select a from t1 where a in (select * from t2);
962a
963select a from t1 where a > any (select * from t2);
964a
965select a from t1 where a > all (select * from t2);
966a
9671
968100
969NULL
9701000
971select a from t1 where a in (select * from t2 group by a);
972a
973select a from t1 where a > any (select * from t2 group by a);
974a
975select a from t1 where a > all (select * from t2 group by a);
976a
9771
978100
979NULL
9801000
981insert into t2 values (1),(200);
982# sebselect returns non-empty set without NULLs
983select a, a in (select * from t2) from t1;
984a	a in (select * from t2)
9851	1
986100	0
987NULL	NULL
9881000	0
989select a, a > any (select * from t2) from t1;
990a	a > any (select * from t2)
9911	0
992100	1
993NULL	NULL
9941000	1
995select a, a > all (select * from t2) from t1;
996a	a > all (select * from t2)
9971	0
998100	0
999NULL	NULL
10001000	1
1001select a from t1 where a in (select * from t2);
1002a
10031
1004select a from t1 where a > any (select * from t2);
1005a
1006100
10071000
1008select a from t1 where a > all (select * from t2);
1009a
10101000
1011select a from t1 where a in (select * from t2 group by a);
1012a
10131
1014select a from t1 where a > any (select * from t2 group by a);
1015a
1016100
10171000
1018select a from t1 where a > all (select * from t2 group by a);
1019a
10201000
1021drop table t2;
1022create table t2 (a int);
1023insert into t2 values (1),(NULL),(200);
1024# sebselect returns non-empty set with NULLs
1025select a, a in (select * from t2) from t1;
1026a	a in (select * from t2)
10271	1
1028100	NULL
1029NULL	NULL
10301000	NULL
1031select a, a > any (select * from t2) from t1;
1032a	a > any (select * from t2)
10331	NULL
1034100	1
1035NULL	NULL
10361000	1
1037select a, a > all (select * from t2) from t1;
1038a	a > all (select * from t2)
10391	0
1040100	0
1041NULL	NULL
10421000	NULL
1043select a from t1 where a in (select * from t2);
1044a
10451
1046select a from t1 where a > any (select * from t2);
1047a
1048100
10491000
1050select a from t1 where a > all (select * from t2);
1051a
1052select a from t1 where a in (select * from t2 group by a);
1053a
10541
1055select a from t1 where a > any (select * from t2 group by a);
1056a
1057100
10581000
1059select a from t1 where a > all (select * from t2 group by a);
1060a
1061drop table t1, t2;
1062create table t1 (a float);
1063select 10.5 IN (SELECT * from t1 LIMIT 1);
1064ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1065select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
1066ERROR 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.5)' at line 1
1067select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1);
1068ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1069drop table t1;
1070create table t1 (a int, b int, c varchar(10));
1071create table t2 (a int);
1072insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
1073insert into t2 values (1),(2),(NULL);
1074select 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;
1075a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a')	(select c from t1 where a=t2.a)
10761	1	a
10772	0	b
1078NULL	NULL	NULL
1079select 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;
1080a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b')	(select c from t1 where a=t2.a)
10811	0	a
10822	1	b
1083NULL	NULL	NULL
1084select 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;
1085a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c')	(select c from t1 where a=t2.a)
10861	0	a
10872	0	b
1088NULL	NULL	NULL
1089drop table t1,t2;
1090create table t1 (a int, b real, c varchar(10));
1091insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
1092select ROW(1, 1, 'a') IN (select a,b,c from t1);
1093ROW(1, 1, 'a') IN (select a,b,c from t1)
10941
1095select ROW(1, 2, 'a') IN (select a,b,c from t1);
1096ROW(1, 2, 'a') IN (select a,b,c from t1)
10970
1098select ROW(1, 1, 'a') IN (select b,a,c from t1);
1099ROW(1, 1, 'a') IN (select b,a,c from t1)
11001
1101select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
1102ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
11031
1104select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
1105ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
11060
1107select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
1108ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
11091
1110select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1111ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11121
1113select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1114ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11150
1116select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
1117ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
11181
1119select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
1120ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1121drop table t1;
1122create table t1 (a int);
1123insert into t1 values (1);
1124do @a:=(SELECT a from t1);
1125select @a;
1126@a
11271
1128set @a:=2;
1129set @a:=(SELECT a from t1);
1130select @a;
1131@a
11321
1133drop table t1;
1134do (SELECT a from t1);
1135ERROR 42S02: Table 'test.t1' doesn't exist
1136set @a:=(SELECT a from t1);
1137ERROR 42S02: Table 'test.t1' doesn't exist
1138CREATE TABLE t1 (a int, KEY(a));
1139HANDLER t1 OPEN;
1140HANDLER t1 READ a=((SELECT 1));
1141ERROR 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
1142HANDLER t1 CLOSE;
1143drop table t1;
1144create table t1 (a int);
1145create table t2 (b int);
1146insert into t1 values (1),(2);
1147insert into t2 values (1);
1148select a from t1 where a in (select a from t1 where a in (select b from t2));
1149a
11501
1151drop table t1, t2;
1152create table t1 (a int, b int);
1153create table t2 like t1;
1154insert into t1 values (1,2),(1,3),(1,4),(1,5);
1155insert into t2 values (1,2),(1,3);
1156select * from t1 where row(a,b) in (select a,b from t2);
1157a	b
11581	2
11591	3
1160drop table t1, t2;
1161CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;
1162INSERT INTO t1 VALUES (1);
1163UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1164select * from t1;
1165i
11662
1167drop table t1;
1168CREATE TABLE t1 (a int(1));
1169EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
1170id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11711	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11722	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1173Warnings:
1174Note	1003	/* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
1175EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
1176id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11771	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11782	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1179Warnings:
1180Note	1003	/* select#1 */ select (/* select#2 */ select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
1181EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
1182id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11831	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11842	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1185Warnings:
1186Note	1003	/* select#1 */ select (/* select#2 */ select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
1187drop table t1;
1188CREATE TABLE `t1` (
1189`mot` varchar(30) character set latin1 NOT NULL default '',
1190`topic` mediumint(8) unsigned NOT NULL default '0',
1191`date` date NOT NULL default '0000-00-00',
1192`pseudo` varchar(35) character set latin1 NOT NULL default '',
1193PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1194KEY `pseudo` (`pseudo`,`date`,`topic`),
1195KEY `topic` (`topic`)
1196) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1197CREATE TABLE `t2` (
1198`mot` varchar(30) character set latin1 NOT NULL default '',
1199`topic` mediumint(8) unsigned NOT NULL default '0',
1200`date` date NOT NULL default '0000-00-00',
1201`pseudo` varchar(35) character set latin1 NOT NULL default '',
1202PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1203KEY `pseudo` (`pseudo`,`date`,`topic`),
1204KEY `topic` (`topic`)
1205) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1206CREATE TABLE `t3` (
1207`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
1208`maxnumrep` int(10) unsigned NOT NULL default '0',
1209PRIMARY KEY  (`numeropost`),
1210UNIQUE KEY `maxnumrep` (`maxnumrep`)
1211) ENGINE=MyISAM CHARSET=latin1;
1212INSERT IGNORE INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
1213Warnings:
1214Warning	1265	Data truncated for column 'date' at row 1
1215Warning	1265	Data truncated for column 'date' at row 2
1216INSERT IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
1217Warnings:
1218Warning	1265	Data truncated for column 'date' at row 1
1219Warning	1265	Data truncated for column 'date' at row 2
1220INSERT INTO t3 VALUES (1,1);
1221SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
1222numeropost=topic);
1223topic
12242
1225select * from t1;
1226mot	topic	date	pseudo
1227joce	1	0000-00-00	joce
1228test	2	0000-00-00	test
1229DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
1230EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
1231select * from t1;
1232mot	topic	date	pseudo
1233joce	1	0000-00-00	joce
1234drop table t1, t2, t3;
1235SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1236a	(SELECT a)
12371	1
1238CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
1239SHOW CREATE TABLE t1;
1240Table	Create Table
1241t1	CREATE TABLE `t1` (
1242  `a` int(1) NOT NULL DEFAULT 0,
1243  `(SELECT 1)` int(1) NOT NULL DEFAULT 0
1244) ENGINE=MyISAM DEFAULT CHARSET=latin1
1245drop table t1;
1246CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1247SHOW CREATE TABLE t1;
1248Table	Create Table
1249t1	CREATE TABLE `t1` (
1250  `a` int(1) NOT NULL DEFAULT 0,
1251  `(SELECT a)` int(1) NOT NULL DEFAULT 0
1252) ENGINE=MyISAM DEFAULT CHARSET=latin1
1253drop table t1;
1254CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1255SHOW CREATE TABLE t1;
1256Table	Create Table
1257t1	CREATE TABLE `t1` (
1258  `a` int(1) NOT NULL DEFAULT 0,
1259  `(SELECT a+0)` int(3) NOT NULL DEFAULT 0
1260) ENGINE=MyISAM DEFAULT CHARSET=latin1
1261drop table t1;
1262CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
1263select * from t1;
1264a
12652
1266SHOW CREATE TABLE t1;
1267Table	Create Table
1268t1	CREATE TABLE `t1` (
1269  `a` int(3) NOT NULL
1270) ENGINE=MyISAM DEFAULT CHARSET=latin1
1271drop table t1;
1272create table t1 (a int);
1273insert into t1 values (1), (2), (3);
1274explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
1275from t1;
1276id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
12771	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12782	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12793	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
1280Warnings:
1281Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,(/* select#2 */ select (/* select#3 */ 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`
1282drop table t1;
1283select 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);
1284ERROR 42S02: Table 'test.t1' doesn't exist
1285CREATE TABLE t1 (
1286ID int(11) NOT NULL auto_increment,
1287name char(35) NOT NULL default '',
1288t2 char(3) NOT NULL default '',
1289District char(20) NOT NULL default '',
1290Population int(11) NOT NULL default '0',
1291PRIMARY KEY  (ID)
1292) ENGINE=MyISAM;
1293INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
1294INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
1295INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
1296CREATE TABLE t2 (
1297Code char(3) NOT NULL default '',
1298Name char(52) NOT NULL default '',
1299Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1300Region char(26) NOT NULL default '',
1301SurfaceArea float(10,2) NOT NULL default '0.00',
1302IndepYear smallint(6) default NULL,
1303Population int(11) NOT NULL default '0',
1304LifeExpectancy float(3,1) default NULL,
1305GNP float(10,2) default NULL,
1306GNPOld float(10,2) default NULL,
1307LocalName char(45) NOT NULL default '',
1308GovernmentForm char(45) NOT NULL default '',
1309HeadOfState char(60) default NULL,
1310Capital int(11) default NULL,
1311Code2 char(2) NOT NULL default '',
1312PRIMARY KEY  (Code)
1313) ENGINE=MyISAM;
1314INSERT 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');
1315INSERT 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');
1316select 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);
1317Continent	Name	Population
1318Oceania	Sydney	3276207
1319drop table t1, t2;
1320CREATE TABLE `t1` (
1321`id` mediumint(8) unsigned NOT NULL auto_increment,
1322`pseudo` varchar(35) character set latin1 NOT NULL default '',
1323PRIMARY KEY  (`id`),
1324UNIQUE KEY `pseudo` (`pseudo`)
1325) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1326INSERT INTO t1 (pseudo) VALUES ('test');
1327SELECT 0 IN (SELECT 1 FROM t1 a);
13280 IN (SELECT 1 FROM t1 a)
13290
1330EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1331id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13321	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13332	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1334Warnings:
1335Note	1003	/* select#1 */ select <in_optimizer>(0,<exists>(/* select#2 */ select 1 from dual where 0 = 1)) AS `0 IN (SELECT 1 FROM t1 a)`
1336INSERT INTO t1 (pseudo) VALUES ('test1');
1337SELECT 0 IN (SELECT 1 FROM t1 a);
13380 IN (SELECT 1 FROM t1 a)
13390
1340EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1341id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13421	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13432	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1344Warnings:
1345Note	1003	/* select#1 */ select <in_optimizer>(0,<exists>(/* select#2 */ select 1 from `test`.`t1` `a` where 0 = 1)) AS `0 IN (SELECT 1 FROM t1 a)`
1346drop table t1;
1347CREATE TABLE `t1` (
1348`i` int(11) NOT NULL default '0',
1349PRIMARY KEY  (`i`)
1350) ENGINE=MyISAM CHARSET=latin1;
1351INSERT INTO t1 VALUES (1);
1352UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
1353UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1354UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
1355ERROR 42S22: Unknown column 't.i' in 'field list'
1356select * from t1;
1357i
13583
1359drop table t1;
1360CREATE TABLE t1 (
1361id int(11) default NULL
1362) ENGINE=MyISAM CHARSET=latin1;
1363INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
1364CREATE TABLE t2 (
1365id int(11) default NULL,
1366name varchar(15) default NULL
1367) ENGINE=MyISAM CHARSET=latin1;
1368INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
1369update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
1370select * from t2;
1371id	name
13724	vita
13731	lenka
13742	lenka
13751	lenka
1376drop table t1,t2;
1377create table t1 (a int, unique index indexa (a));
1378insert into t1 values (-1), (-4), (-2), (NULL);
1379select -10 IN (select a from t1 FORCE INDEX (indexa));
1380-10 IN (select a from t1 FORCE INDEX (indexa))
1381NULL
1382drop table t1;
1383create table t1 (id int not null auto_increment primary key, salary int, key(salary));
1384insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
1385explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
1386id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13871	PRIMARY	t1	ref	salary	salary	5	const	1	100.00	Using where
13882	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1389Warnings:
1390Note	1003	/* select#1 */ select `test`.`t1`.`id` AS `id` from `test`.`t1` where `test`.`t1`.`salary` = (/* select#2 */ select max(`test`.`t1`.`salary`) from `test`.`t1`)
1391drop table t1;
1392CREATE TABLE t1 (
1393ID int(10) unsigned NOT NULL auto_increment,
1394SUB_ID int(3) unsigned NOT NULL default '0',
1395REF_ID int(10) unsigned default NULL,
1396REF_SUB int(3) unsigned default '0',
1397PRIMARY KEY (ID,SUB_ID),
1398UNIQUE KEY t1_PK (ID,SUB_ID),
1399KEY t1_FK (REF_ID,REF_SUB),
1400KEY t1_REFID (REF_ID)
1401) ENGINE=MyISAM CHARSET=cp1251;
1402INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
1403SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
1404REF_ID
1405DROP TABLE t1;
1406create table t1 (a int, b int);
1407create table t2 (a int, b int);
1408insert into t1 values (1,0), (2,0), (3,0);
1409insert into t2 values (1,1), (2,1), (3,1), (2,2);
1410update ignore t1 set b=(select b from t2 where t1.a=t2.a);
1411Warnings:
1412Warning	1242	Subquery returns more than 1 row
1413select * from t1;
1414a	b
14151	1
14162	NULL
14173	1
1418drop table t1, t2;
1419CREATE TABLE `t1` (
1420`id` mediumint(8) unsigned NOT NULL auto_increment,
1421`pseudo` varchar(35) NOT NULL default '',
1422`email` varchar(60) NOT NULL default '',
1423PRIMARY KEY  (`id`),
1424UNIQUE KEY `email` (`email`),
1425UNIQUE KEY `pseudo` (`pseudo`)
1426) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1427INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
1428SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
1429a	b
1430test	test
1431test1	test1
1432drop table if exists t1;
1433(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
1434a
14351
1436create table t1 (a int not null, b int, primary key (a));
1437create table t2 (a int not null, primary key (a));
1438create table t3 (a int not null, b int, primary key (a));
1439insert into t1 values (1,10), (2,20), (3,30),  (4,40);
1440insert into t2 values (2), (3), (4), (5);
1441insert into t3 values (10,3), (20,4), (30,5);
1442select * from t2 where t2.a in (select a from t1);
1443a
14442
14453
14464
1447explain extended select * from t2 where t2.a in (select a from t1);
1448id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14491	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14501	PRIMARY	t1	index	PRIMARY	PRIMARY	4	NULL	4	75.00	Using where; Using index; Using join buffer (flat, BNL join)
1451Warnings:
1452Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`
1453select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1454a
14552
14564
1457explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1458id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14591	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14601	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	4	75.00	Using where; Using join buffer (flat, BNL join)
1461Warnings:
1462Note	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
1463select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1464a
14652
14663
1467explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1468id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14691	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14701	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	4	75.00	Using where; Using join buffer (flat, BNL join)
14711	PRIMARY	t3	eq_ref	PRIMARY	PRIMARY	4	test.t1.b	1	100.00	Using index
1472Warnings:
1473Note	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`
1474drop table t1, t2, t3;
1475create table t1 (a int, b int, index a (a,b));
1476create table t2 (a int, index a (a));
1477create table t3 (a int, b int, index a (a));
1478insert into t1 values (1,10), (2,20), (3,30), (4,40);
1479create table t0(a int);
1480insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1481insert into t1
1482select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
1483insert into t2 values (2), (3), (4), (5);
1484insert into t3 values (10,3), (20,4), (30,5);
1485select * from t2 where t2.a in (select a from t1);
1486a
14872
14883
14894
1490explain extended select * from t2 where t2.a in (select a from t1);
1491id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14921	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
14931	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using index; FirstMatch(t2)
1494Warnings:
1495Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a`
1496select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1497a
14982
14994
1500explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1501id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15021	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
15031	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; FirstMatch(t2)
1504Warnings:
1505Note	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
1506select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1507a
15082
15093
1510explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1511id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15121	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
15131	PRIMARY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index
15141	PRIMARY	t1	ref	a	a	10	test.t2.a,test.t3.a	116	100.00	Using index; FirstMatch(t2)
1515Warnings:
1516Note	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`
1517insert into t1 values (3,31);
1518select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1519a
15202
15213
15224
1523select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
1524a
15252
15264
1527explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1528id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15291	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
15301	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; FirstMatch(t2)
1531Warnings:
1532Note	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
1533drop table t0, t1, t2, t3;
1534create table t1 (a int, b int);
1535create table t2 (a int, b int);
1536create table t3 (a int, b int);
1537insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
1538insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
1539insert into t3 values (3,3), (2,2), (1,1);
1540select 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;
1541a	(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)
15423	1
15432	2
15441	2
1545drop table t1,t2,t3;
1546create table t1 (s1 int);
1547create table t2 (s1 int);
1548insert into t1 values (1);
1549insert into t2 values (1);
1550select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
1551s1
15521
1553drop table t1,t2;
1554create table t1 (s1 int);
1555create table t2 (s1 int);
1556insert into t1 values (1);
1557insert into t2 values (1);
1558update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
1559ERROR 42S22: Unknown column 'x.s1' in 'field list'
1560DROP TABLE t1, t2;
1561CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
1562s2 CHAR(5) COLLATE latin1_swedish_ci);
1563INSERT INTO t1 VALUES ('z','?');
1564select * from t1 where s1 > (select max(s2) from t1);
1565ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
1566select * from t1 where s1 > any (select max(s2) from t1);
1567ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
1568drop table t1;
1569create table t1(toid int,rd int);
1570create table t2(userid int,pmnew int,pmtotal int);
1571insert into t2 values(1,0,0),(2,0,0);
1572insert 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);
1573select 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);
1574userid	pmtotal	pmnew	calc_total	calc_new
15751	0	0	9	3
15762	0	0	4	2
1577drop table t1, t2;
1578create table t1 (s1 char(5));
1579select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
1580ERROR 21000: Operand should contain 1 column(s)
1581insert into t1 values ('tttt');
1582select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
1583s1
1584tttt
1585explain extended (select * from t1);
1586id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15871	SIMPLE	t1	system	NULL	NULL	NULL	NULL	1	100.00
1588Warnings:
1589Note	1003	(select 'tttt' AS `s1` from dual)
1590(select * from t1);
1591s1
1592tttt
1593drop table t1;
1594create table t1 (s1 char(5), index s1(s1));
1595create table t2 (s1 char(5), index s1(s1));
1596insert into t1 values ('a1'),('a2'),('a3');
1597insert into t2 values ('a1'),('a2');
1598select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1599s1	s1 NOT IN (SELECT s1 FROM t2)
1600a1	0
1601a2	0
1602a3	1
1603select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1604s1	s1 = ANY (SELECT s1 FROM t2)
1605a1	1
1606a2	1
1607a3	0
1608select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1609s1	s1 <> ALL (SELECT s1 FROM t2)
1610a1	0
1611a2	0
1612a3	1
1613select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1614s1	s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
1615a1	0
1616a2	1
1617a3	1
1618explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1619id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16201	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16212	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1622Warnings:
1623Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null)))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
1624explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1625id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16261	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16272	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1628Warnings:
1629Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null)))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
1630explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1631id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16321	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16332	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1634Warnings:
1635Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null)))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
1636explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1637id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16381	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16392	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Using where; Full scan on NULL key
1640Warnings:
1641Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `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(`test`.`t2`.`s1` is null)))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
1642drop table t1,t2;
1643create table t2 (a int, b int not null);
1644create table t3 (a int);
1645insert into t3 values (6),(7),(3);
1646select * from t3 where a >= all (select b from t2);
1647a
16486
16497
16503
1651explain extended select * from t3 where a >= all (select b from t2);
1652id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16531	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16542	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1655Warnings:
1656Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))
1657select * from t3 where a >= some (select b from t2);
1658a
1659explain extended select * from t3 where a >= some (select b from t2);
1660id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16611	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16622	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1663Warnings:
1664Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
1665select * from t3 where a >= all (select b from t2 group by 1);
1666a
16676
16687
16693
1670explain extended select * from t3 where a >= all (select b from t2 group by 1);
1671id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16721	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16732	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1674Warnings:
1675Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))
1676select * from t3 where a >= some (select b from t2 group by 1);
1677a
1678explain extended select * from t3 where a >= some (select b from t2 group by 1);
1679id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16801	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16812	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1682Warnings:
1683Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
1684select * from t3 where NULL >= any (select b from t2);
1685a
1686explain extended select * from t3 where NULL >= any (select b from t2);
1687id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16881	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16892	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1690Warnings:
1691Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1692select * from t3 where NULL >= any (select b from t2 group by 1);
1693a
1694explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
1695id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16961	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16972	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1698Warnings:
1699Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1700select * from t3 where NULL >= some (select b from t2);
1701a
1702explain extended select * from t3 where NULL >= some (select b from t2);
1703id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17041	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17052	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1706Warnings:
1707Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1708select * from t3 where NULL >= some (select b from t2 group by 1);
1709a
1710explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
1711id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17121	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17132	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1714Warnings:
1715Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1716insert into t2 values (2,2), (2,1), (3,3), (3,1);
1717select * from t3 where a > all (select max(b) from t2 group by a);
1718a
17196
17207
1721explain extended select * from t3 where a > all (select max(b) from t2 group by a);
1722id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17231	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
17242	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
1725Warnings:
1726Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,<max>(/* select#2 */ select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`)))
1727drop table t2, t3;
1728CREATE 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 ;
1729INSERT 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());
1730CREATE 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;
1731INSERT 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);
1732CREATE 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 ;
1733INSERT 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);
1734CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1735INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
1736select  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;
1737dbid	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')
1738-1	Valid	1
1739-1	Valid 2	1
1740-1	Should Not Return	0
1741SELECT 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;
1742dbid	name
1743-1	Valid
1744-1	Valid 2
1745drop table t1,t2,t3,t4;
1746CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1747INSERT INTO t1 VALUES (1),(5);
1748CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1749INSERT INTO t2 VALUES (2),(6);
1750select * from t1 where (1,2,6) in (select * from t2);
1751ERROR 21000: Operand should contain 3 column(s)
1752DROP TABLE t1,t2;
1753create table t1 (s1 int);
1754insert into t1 values (1);
1755insert into t1 values (2);
1756set sort_buffer_size = (select s1 from t1);
1757ERROR 21000: Subquery returns more than 1 row
1758do (select * from t1);
1759Warnings:
1760Error	1242	Subquery returns more than 1 row
1761drop table t1;
1762create table t1 (s1 char);
1763insert into t1 values ('e');
1764select * from t1 where 'f' > any (select s1 from t1);
1765s1
1766e
1767select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1768s1
1769e
1770explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1771id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17721	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17732	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17743	UNION	t1	system	NULL	NULL	NULL	NULL	1	100.00
1775NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
1776Warnings:
1777Note	1003	/* select#1 */ select 'e' AS `s1` from dual where 1
1778drop table t1;
1779CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1780INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
1781CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
1782INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
1783select 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;
1784phone	code
178569294728265	6
178618621828126	1862
178789356874041	NULL
178895895001874	NULL
1789drop table t1, t2;
1790create table t1 (s1 int);
1791create table t2 (s1 int);
1792select * from t1 where (select count(*) from t2 where t1.s2) = 1;
1793ERROR 42S22: Unknown column 't1.s2' in 'where clause'
1794select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
1795ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1796select count(*) from t2 group by t1.s2;
1797ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1798drop table t1, t2;
1799CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
1800CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1801INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1802INSERT INTO t2 VALUES (100, 200, 'C');
1803SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1804COLC
1805DROP TABLE t1, t2;
1806CREATE TABLE t1 (a int(1));
1807INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1808SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1809(SELECT a)
18101
18112
18123
18134
18145
1815DROP TABLE t1;
1816create table t1 (a int, b decimal(13, 3));
1817insert into t1 values (1, 0.123);
1818select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
1819delete from t1;
1820load data infile "../../tmp/subselect.out.file.1" into table t1;
1821select * from t1;
1822a	b
18231	0.123
1824drop table t1;
1825CREATE TABLE `t1` (
1826`id` int(11) NOT NULL auto_increment,
1827`id_cns` tinyint(3) unsigned NOT NULL default '0',
1828`tipo` enum('','UNO','DUE') NOT NULL default '',
1829`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1830`particolare` mediumint(8) unsigned NOT NULL default '0',
1831`generale` mediumint(8) unsigned NOT NULL default '0',
1832`bis` tinyint(3) unsigned NOT NULL default '0',
1833PRIMARY KEY  (`id`),
1834UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1835UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1836);
1837INSERT 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);
1838CREATE TABLE `t2` (
1839`id` tinyint(3) unsigned NOT NULL auto_increment,
1840`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1841PRIMARY KEY  (`id`)
1842);
1843INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1844SELECT 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;
1845id	max_anno_dep	PIPPO
184616	1987	1
184750	1990	0
184851	1990	NULL
1849DROP TABLE t1, t2;
1850create table t1 (a int);
1851insert into t1 values (1), (2), (3);
1852SET SQL_SELECT_LIMIT=1;
1853select sum(a) from (select * from t1) as a;
1854sum(a)
18556
1856select 2 in (select * from t1);
18572 in (select * from t1)
18581
1859SET SQL_SELECT_LIMIT=default;
1860drop table t1;
1861CREATE TABLE t1 (a int, b int, INDEX (a));
1862INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1863SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1864a	b
18651	1
18661	2
18671	3
1868DROP TABLE t1;
1869create table t1(val varchar(10));
1870insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1871select 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%');
1872count(*)
18730
1874drop table t1;
1875create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1876insert 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');
1877select * from t1 where id not in (select id from t1 where id < 8);
1878id	text
18798	text8
18809	text9
188110	text10
188211	text11
188312	text12
1884select * 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);
1885id	text
18868	text8
18879	text9
188810	text10
188911	text11
189012	text12
1891explain extended select * from t1 where id not in (select id from t1 where id < 8);
1892id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18931	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
18942	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	100.00	Using index; Using where
1895Warnings:
1896Note	1003	/* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where !<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`)))
1897explain 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);
1898id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18991	PRIMARY	tt	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
19002	DEPENDENT SUBQUERY	t1	eq_ref	PRIMARY	PRIMARY	4	test.tt.id	1	100.00	Using where; Using index
1901Warnings:
1902Note	1276	Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1903Note	1003	/* select#1 */ select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where !<in_optimizer>(1,exists(/* select#2 */ 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 limit 1))
1904insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1905create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1906insert 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');
1907select * 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);
1908id	text	id	text	id	text
19091	text1	1	text1	1	text1
19102	text2	2	text2	2	text2
19113	text3	3	text3	3	text3
19124	text4	4	text4	4	text4
19135	text5	5	text5	5	text5
19146	text6	6	text6	6	text6
19157	text7	7	text7	7	text7
19168	text8	8	text8	8	text8
19179	text9	9	text9	9	text9
191810	text10	10	text10	10	text10
191911	text11	11	text1	11	text11
192012	text12	12	text2	12	text12
19211000	text1000	NULL	NULL	1000	text1000
19221001	text1001	NULL	NULL	1000	text1000
1923explain 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);
1924id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
19251	SIMPLE	a	ALL	NULL	NULL	NULL	NULL	14	100.00
19261	SIMPLE	b	eq_ref	PRIMARY	PRIMARY	4	test.a.id	2	100.00
19271	SIMPLE	c	eq_ref	PRIMARY	PRIMARY	4	func	1	100.00	Using index condition
1928Warnings:
1929Note	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 `test`.`b`.`id` is null) join `test`.`t1` `c` where if(`test`.`b`.`id` is null,1000,`test`.`b`.`id`) = `test`.`c`.`id`
1930drop table t1,t2;
1931create table t1 (a int);
1932insert into t1 values (1);
1933explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1934id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
19351	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
19362	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1
1937drop table t1;
1938create table t1(id int);
1939create table t2(id int);
1940create table t3(flag int);
1941select (select * from t3 where id not null) from t1, t2;
1942ERROR 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
1943drop table t1,t2,t3;
1944CREATE TABLE t1 (id INT);
1945CREATE TABLE t2 (id INT);
1946INSERT INTO t1 VALUES (1), (2);
1947INSERT INTO t2 VALUES (1);
1948SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1949id	c
19501	1
19512	0
1952SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1953id	c
19541	1
19552	0
1956SELECT 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;
1957id	c
19581	1
19592	0
1960SELECT 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;
1961id	c
19621	1
19632	0
1964DROP TABLE t1,t2;
1965CREATE TABLE t1 ( a int, b int );
1966INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1967SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1968a
19693
1970SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1971a
19721
1973SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1974a
19752
1976SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1977a
19782
19793
1980SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1981a
19821
19832
1984SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1985a
19861
19873
1988SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1989a
19903
1991SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1992a
19931
1994SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1995a
19962
1997SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1998a
19992
20003
2001SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2002a
20031
20042
2005SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2006a
20071
20083
2009ALTER TABLE t1 ADD INDEX (a);
2010SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
2011a
20123
2013SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
2014a
20151
2016SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
2017a
20182
2019SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
2020a
20212
20223
2023SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
2024a
20251
20262
2027SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
2028a
20291
20303
2031SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
2032a
20333
2034SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
2035a
20361
2037SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
2038a
20392
2040SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
2041a
20422
20433
2044SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2045a
20461
20472
2048SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2049a
20501
20513
2052SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
2053a
20543
2055SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
2056a
20571
2058SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
2059a
20602
2061SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
2062a
20632
20643
2065SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
2066a
20671
20682
2069SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
2070a
20711
20723
2073SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
2074a
20753
2076SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
2077a
20781
2079SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
2080a
20812
2082SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
2083a
20842
20853
2086SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
2087a
20881
20892
2090SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
2091a
20921
20933
2094SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2095a
20963
2097SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2098a
20991
2100SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2101a
21022
2103SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2104a
21052
21063
2107SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2108a
21091
21102
2111SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2112a
21131
21143
2115SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2116a
21173
2118SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2119a
21201
2121SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2122a
21232
2124SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2125a
21262
21273
2128SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2129a
21301
21312
2132SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2133a
21341
21353
2136SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2137a
21383
2139SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2140a
21411
2142SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2143a
21442
2145SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2146a
21472
21483
2149SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2150a
21511
21522
2153SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2154a
21551
21563
2157SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2158a
21593
2160SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2161a
21621
2163SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2164a
21652
2166SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2167a
21682
21693
2170SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2171a
21721
21732
2174SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2175a
21761
21773
2178SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
2179ERROR 21000: Operand should contain 1 column(s)
2180SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2181ERROR 21000: Operand should contain 1 column(s)
2182SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2183ERROR 21000: Operand should contain 1 column(s)
2184SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
2185ERROR 21000: Operand should contain 1 column(s)
2186SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2187ERROR 21000: Operand should contain 1 column(s)
2188SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2189ERROR 21000: Operand should contain 1 column(s)
2190SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
2191ERROR 21000: Operand should contain 1 column(s)
2192SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
2193ERROR 21000: Operand should contain 1 column(s)
2194SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
2195ERROR 21000: Operand should contain 2 column(s)
2196SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2197ERROR 21000: Operand should contain 1 column(s)
2198SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2199a
2200SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
2201ERROR 21000: Operand should contain 2 column(s)
2202SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2203ERROR 21000: Operand should contain 1 column(s)
2204SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2205a
22061
22072
22083
2209SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
2210a
22112
2212SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
2213a
22141
22153
2216SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
2217a
22182
2219SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
2220a
22211
22223
2223SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2224a
22252
2226SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2227a
22281
22293
2230SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2231a
22322
2233SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2234a
22351
22363
2237SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2238a
22393
2240SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2241a
22421
2243SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2244a
22452
2246SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2247a
22482
22493
2250SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2251a
22521
22532
2254SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2255a
22561
22573
2258SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2259a
22603
2261SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2262a
22631
2264SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2265a
22662
2267SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2268a
22692
22703
2271SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2272a
22731
22742
2275SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2276a
22771
22783
2279SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
2280a
22813
2282SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
2283a
22841
2285SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
2286a
22872
2288SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2289a
22902
22913
2292SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2293a
22941
22952
2296SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
2297a
22981
22993
2300SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
2301a
23023
2303SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
2304a
23051
2306SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
2307a
23082
2309SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2310a
23112
23123
2313SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2314a
23151
23162
2317SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
2318a
23191
23203
2321SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
2322concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
23230-
23240-
23251-
2326SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
2327concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
23281-
23290-
23300-
2331SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
2332concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
23330-
23341-
23350-
2336DROP TABLE t1;
2337CREATE TABLE t1 ( a double, b double );
2338INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
2339SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
2340a
23413
2342SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
2343a
23441
2345SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
2346a
23472
2348SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
2349a
23502
23513
2352SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
2353a
23541
23552
2356SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
2357a
23581
23593
2360SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
2361a
23623
2363SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
2364a
23651
2366SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
2367a
23682
2369SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
2370a
23712
23723
2373SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
2374a
23751
23762
2377SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
2378a
23791
23803
2381DROP TABLE t1;
2382CREATE TABLE t1 ( a char(1), b char(1));
2383INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
2384SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
2385a
23863
2387SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
2388a
23891
2390SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
2391a
23922
2393SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
2394a
23952
23963
2397SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
2398a
23991
24002
2401SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
2402a
24031
24043
2405SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
2406a
24073
2408SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
2409a
24101
2411SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
2412a
24132
2414SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
2415a
24162
24173
2418SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
2419a
24201
24212
2422SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
2423a
24241
24253
2426DROP TABLE t1;
2427create table t1 (a int, b int);
2428insert into t1 values (1,2),(3,4);
2429select * from t1 up where exists (select * from t1 where t1.a=up.a);
2430a	b
24311	2
24323	4
2433explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
2434id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
24351	PRIMARY	up	ALL	NULL	NULL	NULL	NULL	2	100.00
24361	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	100.00
24372	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
2438Warnings:
2439Note	1276	Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2440Note	1003	select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` semi join (`test`.`t1`) where 1
2441drop table t1;
2442CREATE TABLE t1 (t1_a int);
2443INSERT INTO t1 VALUES (1);
2444CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
2445INSERT INTO t2 VALUES (1, 1), (1, 2);
2446SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
2447HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
2448t1_a	t2_a	t2_b
24491	1	2
2450DROP TABLE t1, t2;
2451CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
2452INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
2453CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
2454INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
2455SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
2456id	name	id	pet
24571	Tim	1	Fido
24582	Rebecca	2	Spot
24593	NULL	3	Felix
2460drop table t1,t2;
2461CREATE TABLE t1 ( a int, b int );
2462CREATE TABLE t2 ( c int, d int );
2463INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
2464SELECT a AS abc, b FROM t1 outr WHERE b =
2465(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2466abc	b
24671	2
24682	3
24693	4
2470INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
2471(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2472select * from t2;
2473c	d
24741	2
24752	3
24763	4
2477CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
2478(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2479select * from t3;
2480abc	b
24811	2
24822	3
24833	4
2484prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2485execute stmt1;
2486deallocate prepare stmt1;
2487select * from t2;
2488c	d
24891	2
24902	3
24913	4
24921	2
24932	3
24943	4
2495drop table t3;
2496prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2497execute stmt1;
2498select * from t3;
2499abc	b
25001	2
25012	3
25023	4
2503deallocate prepare stmt1;
2504DROP TABLE t1, t2, t3;
2505CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2506insert into t1 values (1);
2507CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2508insert into t2 values (1,2);
2509select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
2510a	C
25111	1
2512drop table t1,t2;
2513create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
2514insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
2515create table t2 (a int);
2516insert into t2 values (1),(3),(2),(7);
2517select a,b from t1 where match(b) against ('Ball') > 0;
2518a	b
25191	ball
25202	ball games
2521select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
2522a
25231
25242
2525drop table t1,t2;
2526CREATE 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);
2527CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
2528INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
2529INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
2530INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
2531INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
2532SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
2533IZAVORGANG_ID
2534D0000000001
2535drop table t1;
2536CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2537CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2538insert into t1 values (1,1),(1,2),(2,1),(2,2);
2539insert into t2 values (1,2),(2,2);
2540select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2541aid	bid
25421	1
25432	1
2544alter table t2 drop primary key;
2545alter table t2 add key KEY1 (aid, bid);
2546select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2547aid	bid
25481	1
25492	1
2550alter table t2 drop key KEY1;
2551alter table t2 add primary key (bid, aid);
2552select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2553aid	bid
25541	1
25552	1
2556drop table t1,t2;
2557CREATE TABLE t1 (howmanyvalues bigint, avalue int);
2558INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
2559SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
2560howmanyvalues	count(*)
25611	1
25622	2
25633	3
25644	4
2565SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2566howmanyvalues	mycount
25671	1
25682	2
25693	3
25704	4
2571CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
2572SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
2573howmanyvalues	mycount
25741	1
25752	2
25763	3
25774	4
2578SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2579howmanyvalues	mycount
25801	1
25812	2
25823	3
25834	4
2584SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
2585howmanyvalues	mycount
25861	1
25872	1
25883	1
25894	1
2590drop table t1;
2591create table t1 (x int);
2592select  (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
2593(select b.x from t1 as b where b.x=a.x)
2594drop table t1;
2595CREATE 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`));
2596INSERT 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);
2597CREATE 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`)) ;
2598INSERT 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');
2599SELECT 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;
2600ERROR 42S22: Unknown column 'b.sc' in 'field list'
2601SELECT 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;
2602ac
2603700
2604NULL
2605drop tables t1,t2;
2606create table t1 (a int not null, b int not null, c int, primary key (a,b));
2607insert into t1 values (1,1,1), (2,2,2), (3,3,3);
2608set @b:= 0;
2609explain select sum(a) from t1 where b > @b;
2610id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26111	SIMPLE	t1	index	NULL	PRIMARY	8	NULL	3	Using where; Using index
2612set @a:= (select sum(a) from t1 where b > @b);
2613explain select a from t1 where c=2;
2614id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26151	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2616do @a:= (select sum(a) from t1 where b > @b);
2617explain select a from t1 where c=2;
2618id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26191	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2620drop table t1;
2621connect  root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK;
2622connection root;
2623set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
2624disconnect root;
2625connection default;
2626create table t1 (a int, b int);
2627create table t2 (a int, b int);
2628insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
2629insert into t2 values (1,3),(2,1);
2630select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
2631a	b	(select max(b) from t2 where t1.b=t2.a)
26321	1	3
26331	2	1
26341	3	NULL
26352	4	NULL
26362	5	NULL
2637drop table t1, t2;
2638create table t1 (id int);
2639create table t2 (id int, body text, fulltext (body));
2640insert into t1 values(1),(2),(3);
2641insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
2642select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
2643count(distinct id)
26441
2645drop table t2,t1;
2646create table t1 (s1 int,s2 int);
2647insert into t1 values (20,15);
2648select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
2649s1	s2
2650drop table t1;
2651create table t1 (s1 int);
2652insert into t1 values (1),(null);
2653select * from t1 where s1 < all (select s1 from t1);
2654s1
2655select s1, s1 < all (select s1 from t1) from t1;
2656s1	s1 < all (select s1 from t1)
26571	0
2658NULL	NULL
2659drop table t1;
2660CREATE TABLE t1 (
2661Code char(3) NOT NULL default '',
2662Name char(52) NOT NULL default '',
2663Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
2664Region char(26) NOT NULL default '',
2665SurfaceArea float(10,2) NOT NULL default '0.00',
2666IndepYear smallint(6) default NULL,
2667Population int(11) NOT NULL default '0',
2668LifeExpectancy float(3,1) default NULL,
2669GNP float(10,2) default NULL,
2670GNPOld float(10,2) default NULL,
2671LocalName char(45) NOT NULL default '',
2672GovernmentForm char(45) NOT NULL default '',
2673HeadOfState char(60) default NULL,
2674Capital int(11) default NULL,
2675Code2 char(2) NOT NULL default ''
2676) ENGINE=MyISAM;
2677INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
2678INSERT 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');
2679INSERT 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');
2680INSERT 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');
2681/*!40000 ALTER TABLE t1 ENABLE KEYS */;
2682SELECT DISTINCT Continent AS c FROM t1 outr WHERE
2683Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
2684Population < 200);
2685c
2686Oceania
2687drop table t1;
2688create table t1 (a1 int);
2689create table t2 (b1 int);
2690select * from t1 where a2 > any(select b1 from t2);
2691ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
2692select * from t1 where a1 > any(select b1 from t2);
2693a1
2694drop table t1,t2;
2695create table t1 (a integer, b integer);
2696select (select * from t1) = (select 1,2);
2697(select * from t1) = (select 1,2)
2698NULL
2699select (select 1,2) = (select * from t1);
2700(select 1,2) = (select * from t1)
2701NULL
2702select  row(1,2) = ANY (select * from t1);
2703row(1,2) = ANY (select * from t1)
27040
2705select  row(1,2) != ALL (select * from t1);
2706row(1,2) != ALL (select * from t1)
27071
2708drop table t1;
2709create table t1 (a integer, b integer);
2710select row(1,(2,2)) in (select * from t1 );
2711ERROR 21000: Operand should contain 2 column(s)
2712select row(1,(2,2)) = (select * from t1 );
2713ERROR 21000: Operand should contain 2 column(s)
2714select (select * from t1) = row(1,(2,2));
2715ERROR 21000: Operand should contain 1 column(s)
2716drop table t1;
2717create  table t1 (a integer);
2718insert into t1 values (1);
2719select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
2720ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2721select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2722ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2723select 1 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
2724xx	1 = ALL (  select 1 from t1 where 1 = xx )
27251	1
2726select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2727ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2728select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
2729ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2730drop table t1;
2731CREATE TABLE t1 (
2732categoryId int(11) NOT NULL,
2733courseId int(11) NOT NULL,
2734startDate datetime NOT NULL,
2735endDate datetime NOT NULL,
2736createDate datetime NOT NULL,
2737modifyDate timestamp NOT NULL,
2738attributes text NOT NULL
2739);
2740INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2741(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2742(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2743(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
2744(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
2745(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2746(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2747(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2748(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
2749CREATE TABLE t2 (
2750userId int(11) NOT NULL,
2751courseId int(11) NOT NULL,
2752date datetime NOT NULL
2753);
2754INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
2755(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
2756(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
2757(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
2758(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
2759(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
2760CREATE TABLE t3 (
2761groupId int(11) NOT NULL,
2762parentId int(11) NOT NULL,
2763startDate datetime NOT NULL,
2764endDate datetime NOT NULL,
2765createDate datetime NOT NULL,
2766modifyDate timestamp NOT NULL,
2767ordering int(11)
2768);
2769INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
2770CREATE TABLE t4 (
2771id int(11) NOT NULL,
2772groupTypeId int(11) NOT NULL,
2773groupKey varchar(50) NOT NULL,
2774name text,
2775ordering int(11),
2776description text,
2777createDate datetime NOT NULL,
2778modifyDate timestamp NOT NULL
2779);
2780INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
2781(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
2782CREATE TABLE t5 (
2783userId int(11) NOT NULL,
2784groupId int(11) NOT NULL,
2785createDate datetime NOT NULL,
2786modifyDate timestamp NOT NULL
2787);
2788INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
2789select
2790count(distinct t2.userid) pass,
2791groupstuff.*,
2792count(t2.courseid) crse,
2793t1.categoryid,
2794t2.courseid,
2795date_format(date, '%b%y') as colhead
2796from t2
2797join t1 on t2.courseid=t1.courseid
2798join
2799(
2800select
2801t5.userid,
2802parentid,
2803parentgroup,
2804childid,
2805groupname,
2806grouptypeid
2807from t5
2808join
2809(
2810select t4.id as parentid,
2811t4.name as parentgroup,
2812t4.id as childid,
2813t4.name as groupname,
2814t4.grouptypeid
2815from t4
2816) as gin on t5.groupid=gin.childid
2817) as groupstuff on t2.userid = groupstuff.userid
2818group by
2819groupstuff.groupname, colhead , t2.courseid;
2820pass	userid	parentid	parentgroup	childid	groupname	grouptypeid	crse	categoryid	courseid	colhead
28211	5141	12	group2	12	group2	5	1	5	12	Aug04
28221	5141	12	group2	12	group2	5	1	1	41	Aug04
28231	5141	12	group2	12	group2	5	1	2	52	Aug04
28241	5141	12	group2	12	group2	5	1	2	53	Aug04
28251	5141	12	group2	12	group2	5	1	3	51	Oct04
28261	5141	12	group2	12	group2	5	1	1	86	Oct04
28271	5141	12	group2	12	group2	5	1	1	87	Oct04
28281	5141	12	group2	12	group2	5	1	2	88	Oct04
28291	5141	12	group2	12	group2	5	1	2	89	Oct04
2830drop table t1, t2, t3, t4, t5;
2831create table t1 (a int);
2832insert into t1 values (1), (2), (3);
2833SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
28341
28351
28361
28371
2838drop table t1;
2839create table t1 (a int);
2840create table t2 (a int);
2841insert into t1 values (1),(2);
2842insert into t2 values (0),(1),(2),(3);
2843select a from t2 where a in (select a from t1);
2844a
28451
28462
2847select a from t2 having a in (select a from t1);
2848a
28491
28502
2851prepare stmt1 from "select a from t2 where a in (select a from t1)";
2852execute stmt1;
2853a
28541
28552
2856execute stmt1;
2857a
28581
28592
2860deallocate prepare stmt1;
2861prepare stmt1 from "select a from t2 having a in (select a from t1)";
2862execute stmt1;
2863a
28641
28652
2866execute stmt1;
2867a
28681
28692
2870deallocate prepare stmt1;
2871drop table t1, t2;
2872create table t1 (a int, b int);
2873insert into t1 values (1,2);
2874select 1 = (select * from t1);
2875ERROR HY000: Illegal parameter data types int and row for operation '='
2876select (select * from t1) = 1;
2877ERROR HY000: Illegal parameter data types row and int for operation '='
2878select (1,2) = (select a from t1);
2879ERROR HY000: Illegal parameter data types row and int for operation '='
2880select (select a from t1) = (1,2);
2881ERROR HY000: Illegal parameter data types int and row for operation '='
2882select (1,2,3) = (select * from t1);
2883ERROR 21000: Operand should contain 3 column(s)
2884select (select * from t1) = (1,2,3);
2885ERROR 21000: Operand should contain 2 column(s)
2886drop table t1;
2887CREATE TABLE `t1` (
2888`itemid` bigint(20) unsigned NOT NULL auto_increment,
2889`sessionid` bigint(20) unsigned default NULL,
2890`time` int(10) unsigned NOT NULL default '0',
2891`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2892NULL default '',
2893`data` text collate latin1_general_ci NOT NULL,
2894PRIMARY KEY  (`itemid`)
2895) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2896INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2897CREATE TABLE `t2` (
2898`sessionid` bigint(20) unsigned NOT NULL auto_increment,
2899`pid` int(10) unsigned NOT NULL default '0',
2900`date` int(10) unsigned NOT NULL default '0',
2901`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2902PRIMARY KEY  (`sessionid`)
2903) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2904INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2905SELECT 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;
2906ip	count( e.itemid )
290710.10.10.1	1
2908drop tables t1,t2;
2909create table t1 (fld enum('0','1'));
2910insert into t1 values ('1');
2911select * from (select max(fld) from t1) as foo;
2912max(fld)
29131
2914drop table t1;
2915set @tmp11867_optimizer_switch=@@optimizer_switch;
2916set optimizer_switch='semijoin_with_cache=off';
2917CREATE TABLE t1 (one int, two int, flag char(1));
2918CREATE TABLE t2 (one int, two int, flag char(1));
2919INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2920INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2921SELECT * FROM t1
2922WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
2923one	two	flag
29245	6	N
29257	8	N
2926SELECT * FROM t1
2927WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
2928one	two	flag
29295	6	N
29307	8	N
2931insert into t2 values (null,null,'N');
2932insert into t2 values (null,3,'0');
2933insert into t2 values (null,5,'0');
2934insert into t2 values (10,null,'0');
2935insert into t1 values (10,3,'0');
2936insert into t1 values (10,5,'0');
2937insert into t1 values (10,10,'0');
2938SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
2939one	two	test
29401	2	NULL
29412	3	NULL
29423	4	NULL
29435	6	1
29447	8	1
294510	3	NULL
294610	5	NULL
294710	10	NULL
2948SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2949one	two
29505	6
29517	8
2952SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
2953one	two	test
29541	2	NULL
29552	3	NULL
29563	4	NULL
29575	6	1
29587	8	1
295910	3	NULL
296010	5	NULL
296110	10	NULL
2962SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2963one	two	test
29641	2	0
29652	3	NULL
29663	4	0
29675	6	0
29687	8	0
296910	3	NULL
297010	5	NULL
297110	10	NULL
2972SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2973one	two	test
29741	2	0
29752	3	NULL
29763	4	0
29775	6	0
29787	8	0
297910	3	NULL
298010	5	NULL
298110	10	NULL
2982explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2983id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29841	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29852	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2986Warnings:
2987Note	1003	/* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null))) AS `test` from `test`.`t1`
2988explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2989id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29901	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29911	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1	100.00
29922	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2993Warnings:
2994Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`flag` = 'N'
2995explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2996id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29971	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29982	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2999Warnings:
3000Note	1003	/* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null))) AS `test` from `test`.`t1`
3001DROP TABLE t1,t2;
3002set optimizer_switch=@tmp11867_optimizer_switch;
3003CREATE TABLE t1 (a char(5), b char(5));
3004INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
3005SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
3006a	b
3007aaa	aaa
3008DROP TABLE t1;
3009CREATE TABLE t1 (a int);
3010CREATE TABLE t2 (a int, b int);
3011CREATE TABLE t3 (b int NOT NULL);
3012INSERT INTO t1 VALUES (1), (2), (3), (4);
3013INSERT INTO t2 VALUES (1,10), (3,30);
3014SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3015WHERE t3.b IS NOT NULL OR t2.a > 10;
3016a	b	b
3017SELECT * FROM t1
3018WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3019WHERE t3.b IS NOT NULL OR t2.a > 10);
3020a
30211
30222
30233
30244
3025DROP TABLE t1,t2,t3;
3026CREATE TABLE t1 (f1 INT);
3027CREATE TABLE t2 (f2 INT);
3028INSERT INTO t1 VALUES (1);
3029SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
3030f1
30311
3032SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
3033f1
30341
3035INSERT INTO t2 VALUES (1);
3036INSERT INTO t2 VALUES (2);
3037SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
3038f1
30391
3040DROP TABLE t1, t2;
3041select 1 from dual where 1 < any (select 2);
30421
30431
3044select 1 from dual where 1 < all (select 2);
30451
30461
3047select 1 from dual where 2 > any (select 1);
30481
30491
3050select 1 from dual where 2 > all (select 1);
30511
30521
3053select 1 from dual where 1 < any (select 2 from dual);
30541
30551
3056select 1 from dual where 1 < all (select 2 from dual where 1!=1);
30571
30581
3059create table t1 (s1 char);
3060insert into t1 values (1),(2);
3061select * from t1 where (s1 < any (select s1 from t1));
3062s1
30631
3064select * from t1 where not (s1 < any (select s1 from t1));
3065s1
30662
3067select * from t1 where (s1 < ALL (select s1+1 from t1));
3068s1
30691
3070select * from t1 where not(s1 < ALL (select s1+1 from t1));
3071s1
30722
3073select * from t1 where (s1+1 = ANY (select s1 from t1));
3074s1
30751
3076select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
3077s1
30782
3079select * from t1 where (s1 = ALL (select s1/s1 from t1));
3080s1
30811
3082select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
3083s1
30842
3085drop table t1;
3086create table t1 (
3087retailerID varchar(8) NOT NULL,
3088statusID   int(10) unsigned NOT NULL,
3089changed    datetime NOT NULL,
3090UNIQUE KEY retailerID (retailerID, statusID, changed)
3091);
3092INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
3093INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
3094INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
3095INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
3096INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
3097INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
3098select * from t1 r1
3099where (r1.retailerID,(r1.changed)) in
3100(SELECT r2.retailerId,(max(changed)) from t1 r2
3101group by r2.retailerId);
3102retailerID	statusID	changed
31030026	2	2006-01-06 12:25:53
31040037	2	2006-01-06 12:25:53
31050048	1	2006-01-06 12:37:50
31060059	1	2006-01-06 12:37:50
3107drop table t1;
3108create table t1(a int, primary key (a));
3109insert into t1 values (10);
3110create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
3111insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
3112explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3113ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3114             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3115id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31161	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31171	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31182	SUBQUERY	t2	range	b	b	40	NULL	2	Using where
3119SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3120ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3121            ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3122a	a	b
312310	3	35989
3124explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3125ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3126            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3127id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31281	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31291	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31302	SUBQUERY	t2	range	b	b	40	NULL	2	Using index condition
3131SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3132ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3133            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3134a	a	b
313510	1	359
3136drop table t1,t2;
3137CREATE TABLE t1 (
3138field1 int NOT NULL,
3139field2 int NOT NULL,
3140field3 int NOT NULL,
3141PRIMARY KEY  (field1,field2,field3)
3142);
3143CREATE TABLE t2 (
3144fieldA int NOT NULL,
3145fieldB int NOT NULL,
3146PRIMARY KEY  (fieldA,fieldB)
3147);
3148INSERT INTO t1 VALUES
3149(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
3150INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
3151SELECT field1, field2, COUNT(*)
3152FROM t1 GROUP BY field1, field2;
3153field1	field2	COUNT(*)
31541	1	2
31551	2	3
31561	3	1
3157SELECT field1, field2
3158FROM  t1
3159GROUP BY field1, field2
3160HAVING COUNT(*) >= ALL (SELECT fieldB
3161FROM t2 WHERE fieldA = field1);
3162field1	field2
31631	2
3164SELECT field1, field2
3165FROM  t1
3166GROUP BY field1, field2
3167HAVING COUNT(*) < ANY (SELECT fieldB
3168FROM t2 WHERE fieldA = field1);
3169field1	field2
31701	1
31711	3
3172DROP TABLE t1, t2;
3173CREATE TABLE t1(a int, INDEX (a));
3174INSERT INTO t1 VALUES (1), (3), (5), (7);
3175INSERT INTO t1 VALUES (NULL);
3176CREATE TABLE t2(a int);
3177INSERT INTO t2 VALUES (1),(2),(3);
3178EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
3179id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31801	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3
31812	SUBQUERY	t1	index_subquery	a	a	5	func	2	Using index; Full scan on NULL key
3182SELECT a, a IN (SELECT a FROM t1) FROM t2;
3183a	a IN (SELECT a FROM t1)
31841	1
31852	NULL
31863	1
3187DROP TABLE t1,t2;
3188CREATE TABLE t1 (a DATETIME);
3189INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
3190CREATE TABLE t2 AS SELECT
3191(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
3192FROM t1 WHERE a > '2000-01-01';
3193SHOW CREATE TABLE t2;
3194Table	Create Table
3195t2	CREATE TABLE `t2` (
3196  `sub_a` datetime DEFAULT NULL
3197) ENGINE=MyISAM DEFAULT CHARSET=latin1
3198CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
3199SHOW CREATE TABLE t3;
3200Table	Create Table
3201t3	CREATE TABLE `t3` (
3202  `a` datetime DEFAULT NULL
3203) ENGINE=MyISAM DEFAULT CHARSET=latin1
3204DROP TABLE t1,t2,t3;
3205CREATE TABLE t1 (a int);
3206INSERT INTO t1 VALUES (1), (2);
3207SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
3208a
3209SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3210a
32111
32122
3213EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3214id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
32151	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
32162	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
3217DROP TABLE t1;
3218CREATE TABLE t1 (a int);
3219INSERT INTO t1 VALUES (2), (4), (1), (3);
3220CREATE TABLE t2 (b int, c int);
3221INSERT INTO t2 VALUES
3222(2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
3223SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
3224a
32252
32264
32271
32283
3229SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
3230ERROR 21000: Subquery returns more than 1 row
3231SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
3232a
32331
32342
32353
32364
3237SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
3238ERROR 21000: Subquery returns more than 1 row
3239SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
3240b	MAX(c)
32411	4
32422	2
32434	4
3244SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
3245ERROR 21000: Subquery returns more than 1 row
3246SELECT a FROM t1 GROUP BY a
3247HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
3248(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3249a
32501
32512
32523
32534
3254SELECT a FROM t1 GROUP BY a
3255HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
3256(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3257ERROR 21000: Subquery returns more than 1 row
3258SELECT a FROM t1 GROUP BY a
3259HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3260(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3261a
32624
3263SELECT a FROM t1 GROUP BY a
3264HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3265(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
3266ERROR 21000: Subquery returns more than 1 row
3267SELECT a FROM t1
3268ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
3269(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3270a
32711
32722
32733
32744
3275SELECT a FROM t1
3276ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
3277(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3278ERROR 21000: Subquery returns more than 1 row
3279SELECT a FROM t1
3280ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3281(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3282a
32831
32842
32853
32864
3287SELECT a FROM t1
3288ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3289(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3290ERROR 21000: Subquery returns more than 1 row
3291DROP TABLE t1,t2;
3292create table t1 (df decimal(5,1));
3293insert into t1 values(1.1);
3294insert into t1 values(2.2);
3295select * from t1 where df <= all (select avg(df) from t1 group by df);
3296df
32971.1
3298select * from t1 where df >= all (select avg(df) from t1 group by df);
3299df
33002.2
3301drop table t1;
3302create table t1 (df decimal(5,1));
3303insert into t1 values(1.1);
3304select 1.1 * exists(select * from t1);
33051.1 * exists(select * from t1)
33061.1
3307drop table t1;
3308CREATE TABLE t1 (
3309grp int(11) default NULL,
3310a decimal(10,2) default NULL);
3311insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
3312select * from t1;
3313grp	a
33141	1.00
33152	2.00
33162	3.00
33173	4.00
33183	5.00
33193	6.00
3320NULL	NULL
3321select min(a) from t1 group by grp;
3322min(a)
3323NULL
33241.00
33252.00
33264.00
3327drop table t1;
3328CREATE table t1 ( c1 integer );
3329INSERT INTO t1 VALUES ( 1 );
3330INSERT INTO t1 VALUES ( 2 );
3331INSERT INTO t1 VALUES ( 3 );
3332CREATE TABLE t2 ( c2 integer );
3333INSERT INTO t2 VALUES ( 1 );
3334INSERT INTO t2 VALUES ( 4 );
3335INSERT INTO t2 VALUES ( 5 );
3336SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
3337c1	c2
33381	1
3339SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
3340WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
3341c1	c2
33421	1
3343DROP TABLE t1,t2;
3344CREATE TABLE t1 ( c1 integer );
3345INSERT INTO t1 VALUES ( 1 );
3346INSERT INTO t1 VALUES ( 2 );
3347INSERT INTO t1 VALUES ( 3 );
3348INSERT INTO t1 VALUES ( 6 );
3349CREATE TABLE t2 ( c2 integer );
3350INSERT INTO t2 VALUES ( 1 );
3351INSERT INTO t2 VALUES ( 4 );
3352INSERT INTO t2 VALUES ( 5 );
3353INSERT INTO t2 VALUES ( 6 );
3354CREATE TABLE t3 ( c3 integer );
3355INSERT INTO t3 VALUES ( 7 );
3356INSERT INTO t3 VALUES ( 8 );
3357SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
3358WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
3359c1	c2
33602	NULL
33613	NULL
3362DROP TABLE t1,t2,t3;
3363CREATE TABLE `t1` (
3364`itemid` bigint(20) unsigned NOT NULL auto_increment,
3365`sessionid` bigint(20) unsigned default NULL,
3366`time` int(10) unsigned NOT NULL default '0',
3367`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
3368NULL default '',
3369`data` text collate latin1_general_ci NOT NULL,
3370PRIMARY KEY  (`itemid`)
3371) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3372INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
3373CREATE TABLE `t2` (
3374`sessionid` bigint(20) unsigned NOT NULL auto_increment,
3375`pid` int(10) unsigned NOT NULL default '0',
3376`date` int(10) unsigned NOT NULL default '0',
3377`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
3378PRIMARY KEY  (`sessionid`)
3379) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3380INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
3381SELECT 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;
3382ip	count( e.itemid )
338310.10.10.1	1
3384drop tables t1,t2;
3385CREATE TABLE t1 (EMPNUM   CHAR(3));
3386CREATE TABLE t2 (EMPNUM   CHAR(3) );
3387INSERT INTO t1 VALUES ('E1'),('E2');
3388INSERT INTO t2 VALUES ('E1');
3389DELETE FROM t1
3390WHERE t1.EMPNUM NOT IN
3391(SELECT t2.EMPNUM
3392FROM t2
3393WHERE t1.EMPNUM = t2.EMPNUM);
3394select * from t1;
3395EMPNUM
3396E1
3397DROP TABLE t1,t2;
3398CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
3399INSERT INTO t1 VALUES (1, 1);
3400CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
3401PRIMARY KEY(select_id,values_id));
3402INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
3403SELECT values_id FROM t1
3404WHERE values_id IN (SELECT values_id FROM t2
3405WHERE select_id IN (1, 0));
3406values_id
34071
3408SELECT values_id FROM t1
3409WHERE values_id IN (SELECT values_id FROM t2
3410WHERE select_id BETWEEN 0 AND 1);
3411values_id
34121
3413SELECT values_id FROM t1
3414WHERE values_id IN (SELECT values_id FROM t2
3415WHERE select_id = 0 OR select_id = 1);
3416values_id
34171
3418DROP TABLE t1, t2;
3419create table t1 (fld enum('0','1'));
3420insert into t1 values ('1');
3421select * from (select max(fld) from t1) as foo;
3422max(fld)
34231
3424drop table t1;
3425CREATE TABLE t1 (a int, b int);
3426CREATE TABLE t2 (c int, d int);
3427CREATE TABLE t3 (e int);
3428INSERT INTO t1 VALUES
3429(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
3430INSERT INTO t2 VALUES
3431(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
3432INSERT INTO t3 VALUES (10), (30), (10), (20) ;
3433SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
3434a	MAX(b)	MIN(b)
34351	20	10
34362	30	10
34373	20	20
34384	40	40
3439SELECT * FROM t2;
3440c	d
34412	10
34422	20
34434	10
34445	10
34453	20
34462	40
3447SELECT * FROM t3;
3448e
344910
345030
345110
345220
3453SELECT a FROM t1 GROUP BY a
3454HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
3455a
34562
34574
3458SELECT a FROM t1 GROUP BY a
3459HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
3460a
34612
3462SELECT a FROM t1 GROUP BY a
3463HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
3464a
34652
34664
3467SELECT a FROM t1 GROUP BY a
3468HAVING a IN (SELECT c FROM t2
3469WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3470a
34712
34723
3473SELECT a FROM t1 GROUP BY a
3474HAVING a IN (SELECT c FROM t2
3475WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3476a
34772
34783
3479SELECT a FROM t1 GROUP BY a
3480HAVING a IN (SELECT c FROM t2
3481WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3482a
34832
3484SELECT a FROM t1 GROUP BY a
3485HAVING a IN (SELECT c FROM t2
3486WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
3487a
34882
3489SELECT a FROM t1 GROUP BY a
3490HAVING a IN (SELECT c FROM t2
3491WHERE MIN(b) < d AND
3492EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3493a
34942
3495SELECT a, SUM(a) FROM t1 GROUP BY a;
3496a	SUM(a)
34971	2
34982	6
34993	3
35004	4
3501SELECT a FROM t1
3502WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
3503a
35043
35054
3506SELECT a FROM t1 GROUP BY a
3507HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
3508a
35091
35103
35114
3512SELECT a FROM t1
3513WHERE a < 3 AND
3514EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
3515a
35161
35172
3518SELECT a FROM t1
3519WHERE a < 3 AND
3520EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
3521a
35221
35232
35241
35252
35262
3527SELECT t1.a FROM t1 GROUP BY t1.a
3528HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
3529HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3530HAVING SUM(t1.a+t2.c) < t3.e/4));
3531a
35321
35332
3534SELECT t1.a FROM t1 GROUP BY t1.a
3535HAVING t1.a > ALL(SELECT t2.c FROM t2
3536WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3537HAVING SUM(t1.a+t2.c) < t3.e/4));
3538a
35394
3540SELECT t1.a FROM t1 GROUP BY t1.a
3541HAVING t1.a > ALL(SELECT t2.c FROM t2
3542WHERE EXISTS(SELECT t3.e FROM t3
3543WHERE SUM(t1.a+t2.c) < t3.e/4));
3544ERROR HY000: Invalid use of group function
3545SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
3546ERROR HY000: Invalid use of group function
3547SELECT t1.a FROM t1 GROUP BY t1.a
3548HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3549HAVING AVG(t2.c+SUM(t1.b)) > 20);
3550a
35512
35523
35534
3554SELECT t1.a FROM t1 GROUP BY t1.a
3555HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3556HAVING AVG(SUM(t1.b)) > 20);
3557a
35582
35594
3560SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
3561HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3562HAVING t2.c+sum > 20);
3563a	sum
35642	60
35653	20
35664	40
3567DROP TABLE t1,t2,t3;
3568CREATE TABLE t1 (a varchar(5), b varchar(10));
3569INSERT INTO t1 VALUES
3570('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
3571('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
3572SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3573a	b
3574BBB	4
3575CCC	7
3576AAA	8
3577EXPLAIN
3578SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3579id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
35801	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	Using where
35811	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	21	test.t1.a,test.t1.b	1
35822	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	9	Using temporary
3583ALTER TABLE t1 ADD INDEX(a);
3584SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3585a	b
3586BBB	4
3587CCC	7
3588AAA	8
3589EXPLAIN
3590SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3591id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
35921	PRIMARY	t1	ALL	a	NULL	NULL	NULL	9	Using where
35931	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	21	test.t1.a,test.t1.b	1
35942	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	9	Using temporary
3595DROP TABLE t1;
3596create table t1( f1 int,f2 int);
3597insert into t1 values (1,1),(2,2);
3598select 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';
3599t
3600crash1
3601crash1
3602drop table t1;
3603create table t1 (c int, key(c));
3604insert into t1 values (1142477582), (1142455969);
3605create table t2 (a int, b int);
3606insert into t2 values (2, 1), (1, 0);
3607delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
3608drop table t1, t2;
3609CREATE TABLE t1 (a INT);
3610CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3611ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3612CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
3613ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
3614SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3615ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3616DROP TABLE t1;
3617create table t1 (i int, j bigint);
3618insert into t1 values (1, 2), (2, 2), (3, 2);
3619select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
3620min(i)
36211
3622drop table t1;
3623CREATE TABLE t1 (i BIGINT UNSIGNED);
3624INSERT INTO t1 VALUES (10000000000000000000);
3625INSERT INTO t1 VALUES (1);
3626CREATE TABLE t2 (i BIGINT UNSIGNED);
3627INSERT INTO t2 VALUES (10000000000000000000);
3628INSERT INTO t2 VALUES (1);
3629/* simple test */
3630SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
3631i
363210000000000000000000
36331
3634/* subquery test */
3635SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3636i
363710000000000000000000
3638/* subquery test with cast*/
3639SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
3640i
364110000000000000000000
3642DROP TABLE t1;
3643DROP TABLE t2;
3644CREATE TABLE t1 (
3645id bigint(20) unsigned NOT NULL auto_increment,
3646name varchar(255) NOT NULL,
3647PRIMARY KEY  (id)
3648);
3649INSERT INTO t1 VALUES
3650(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
3651CREATE TABLE t2 (
3652id bigint(20) unsigned NOT NULL auto_increment,
3653mid bigint(20) unsigned NOT NULL,
3654date date NOT NULL,
3655PRIMARY KEY  (id)
3656);
3657INSERT INTO t2 VALUES
3658(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
3659(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
3660SELECT *,
3661(SELECT date FROM t2 WHERE mid = t1.id
3662ORDER BY date DESC LIMIT 0, 1) AS date_last,
3663(SELECT date FROM t2 WHERE mid = t1.id
3664ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
3665FROM t1;
3666id	name	date_last	date_next_to_last
36671	Balazs	2006-05-01	NULL
36682	Joe	2006-04-20	NULL
36693	Frank	2006-04-13	NULL
3670SELECT *,
3671(SELECT COUNT(*) FROM t2 WHERE mid = t1.id
3672ORDER BY date DESC LIMIT 1, 1) AS date_count
3673FROM t1;
3674id	name	date_count
36751	Balazs	NULL
36762	Joe	NULL
36773	Frank	NULL
3678SELECT *,
3679(SELECT date FROM t2 WHERE mid = t1.id
3680ORDER BY date DESC LIMIT 0, 1) AS date_last,
3681(SELECT date FROM t2 WHERE mid = t1.id
3682ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
3683FROM t1;
3684id	name	date_last	date_next_to_last
36851	Balazs	2006-05-01	2006-03-30
36862	Joe	2006-04-20	2006-04-06
36873	Frank	2006-04-13	NULL
3688DROP TABLE t1,t2;
3689CREATE TABLE t1 (
3690i1 int(11) NOT NULL default '0',
3691i2 int(11) NOT NULL default '0',
3692t datetime NOT NULL default '0000-00-00 00:00:00',
3693PRIMARY KEY  (i1,i2,t)
3694);
3695INSERT INTO t1 VALUES
3696(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
3697(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
3698(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
3699(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
3700(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
3701(24,2,'2005-05-27 12:40:06');
3702CREATE TABLE t2 (
3703i1 int(11) NOT NULL default '0',
3704i2 int(11) NOT NULL default '0',
3705t datetime default NULL,
3706PRIMARY KEY  (i1)
3707);
3708INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
3709EXPLAIN
3710SELECT * FROM t1,t2
3711WHERE t1.t = (SELECT t1.t FROM t1
3712WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3713ORDER BY t1.t DESC LIMIT 1);
3714id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37151	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1
37161	PRIMARY	t1	index	NULL	PRIMARY	13	NULL	11	Using where; Using index
37172	SUBQUERY	t1	range	PRIMARY	PRIMARY	13	NULL	5	Using where; Using index
3718SELECT * FROM t1,t2
3719WHERE t1.t = (SELECT t1.t FROM t1
3720WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3721ORDER BY t1.t DESC LIMIT 1);
3722i1	i2	t	i1	i2	t
372324	1	2005-05-27 12:40:30	24	1	2006-06-20 12:29:40
3724DROP TABLE t1, t2;
3725CREATE TABLE t1 (i INT);
3726(SELECT i FROM t1) UNION (SELECT i FROM t1);
3727i
3728SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
3729(
3730(SELECT i FROM t1) UNION
3731(SELECT i FROM t1)
3732);
3733i
3734SELECT * FROM t1
3735WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
3736ERROR 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
3737explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
3738from t1;
3739ERROR 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))
3740from t1' at line 1
3741explain select * from t1 where not exists
3742((select t11.i from t1 t11) union (select t12.i from t1 t12));
3743id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37441	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
37452	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
37463	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
3747NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
3748DROP TABLE t1;
3749CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
3750insert into t1 (a) values (FLOOR(rand() * 100));
3751insert into t1 (a) select FLOOR(rand() * 100) from t1;
3752insert into t1 (a) select FLOOR(rand() * 100) from t1;
3753insert into t1 (a) select FLOOR(rand() * 100) from t1;
3754insert into t1 (a) select FLOOR(rand() * 100) from t1;
3755insert into t1 (a) select FLOOR(rand() * 100) from t1;
3756insert into t1 (a) select FLOOR(rand() * 100) from t1;
3757insert into t1 (a) select FLOOR(rand() * 100) from t1;
3758insert into t1 (a) select FLOOR(rand() * 100) from t1;
3759insert into t1 (a) select FLOOR(rand() * 100) from t1;
3760insert into t1 (a) select FLOOR(rand() * 100) from t1;
3761insert into t1 (a) select FLOOR(rand() * 100) from t1;
3762insert into t1 (a) select FLOOR(rand() * 100) from t1;
3763insert into t1 (a) select FLOOR(rand() * 100) from t1;
3764SELECT a,
3765(SELECT REPEAT(' ',250) FROM t1 i1
3766WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
3767FROM t1 ORDER BY a LIMIT 5;
3768a	a
37690	NULL
37700	NULL
37710	NULL
37720	NULL
37730	NULL
3774DROP TABLE t1;
3775CREATE TABLE t1 (a INT, b INT);
3776CREATE TABLE t2 (a INT);
3777INSERT INTO t2 values (1);
3778INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
3779SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
3780(SELECT COUNT(DISTINCT t1.b) from t2)
37812
37821
37831
3784SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
3785FROM t1 GROUP BY t1.a;
3786(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
37872
37881
37891
3790SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
3791COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b))
37922	2
37931	1
37941	1
3795SELECT COUNT(DISTINCT t1.b),
3796(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
3797FROM t1 GROUP BY t1.a;
3798COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
37992	2
38001	1
38011	1
3802SELECT (
3803SELECT (
3804SELECT COUNT(DISTINCT t1.b)
3805)
3806)
3807FROM t1 GROUP BY t1.a;
3808(
3809SELECT (
3810SELECT COUNT(DISTINCT t1.b)
3811)
3812)
38132
38141
38151
3816SELECT (
3817SELECT (
3818SELECT (
3819SELECT COUNT(DISTINCT t1.b)
3820)
3821)
3822FROM t1 GROUP BY t1.a LIMIT 1)
3823FROM t1 t2
3824GROUP BY t2.a;
3825(
3826SELECT (
3827SELECT (
3828SELECT COUNT(DISTINCT t1.b)
3829)
3830)
3831FROM t1 GROUP BY t1.a LIMIT 1)
38322
38332
38342
3835DROP TABLE t1,t2;
3836CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
3837CREATE TABLE t2 (x int auto_increment, y int, z int,
3838PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
3839create table t3 (a int);
3840insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3841insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
3842from t3 A, t3 B, t3 C, t3 D where D.a<3;
3843insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
3844SET SESSION sort_buffer_size = 32 * 1024;
3845SELECT SQL_NO_CACHE COUNT(*)
3846FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3847FROM t1) t;
3848COUNT(*)
38493000
3850SET SESSION sort_buffer_size = 8 * 1024 * 1024;
3851SELECT SQL_NO_CACHE COUNT(*)
3852FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3853FROM t1) t;
3854COUNT(*)
38553000
3856DROP TABLE t1,t2,t3;
3857CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
3858CREATE TABLE t2 (c int);
3859INSERT INTO t1 VALUES ('aa', 1);
3860INSERT INTO t2 VALUES (1);
3861SELECT * FROM t1
3862WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3863UNION
3864SELECT c from t2 WHERE c=t1.c);
3865id	c
3866aa	1
3867INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
3868SELECT * FROM t1
3869WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3870UNION
3871SELECT c from t2 WHERE c=t1.c);
3872id	c
3873aa	1
3874bb	2
3875cc	3
3876dd	1
3877INSERT INTO t2 VALUES (2);
3878CREATE TABLE t3 (c int);
3879INSERT INTO t3 VALUES (1);
3880SELECT * FROM t1
3881WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
3882UNION
3883SELECT c from t2 WHERE c=t1.c);
3884id	c
3885aa	1
3886bb	2
3887cc	3
3888dd	1
3889DROP TABLE t1,t2,t3;
3890CREATE TABLE t1(f1 int);
3891CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
3892INSERT INTO t1 VALUES (1),(1),(2),(2);
3893INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
3894SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
3895sq
38962
38974
3898SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
3899tt
39002
39012
3902PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
3903EXECUTE stmt1;
3904sq
39052
39064
3907EXECUTE stmt1;
3908sq
39092
39104
3911DEALLOCATE PREPARE stmt1;
3912SELECT f2, AVG(f21),
3913(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
3914FROM t2 GROUP BY f2;
3915f2	AVG(f21)	test
39161	1.0000	2004-02-29 11:11:11
39172	2.0000	2004-02-29 11:11:11
3918DROP TABLE t1,t2;
3919CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
3920INSERT INTO t1 VALUES
3921(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
3922(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
3923(3,2,'k'), (3,1,'l'), (1,9,'m');
3924SELECT a, MAX(b),
3925(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
3926FROM t1 GROUP BY a;
3927a	MAX(b)	test
39281	9	m
39292	3	h
39303	4	i
3931DROP TABLE t1;
3932DROP TABLE IF EXISTS t1;
3933DROP TABLE IF EXISTS t2;
3934DROP TABLE IF EXISTS t1xt2;
3935CREATE TABLE t1 (
3936id_1 int(5) NOT NULL,
3937t varchar(4) DEFAULT NULL
3938);
3939CREATE TABLE t2 (
3940id_2 int(5) NOT NULL,
3941t varchar(4) DEFAULT NULL
3942);
3943CREATE TABLE t1xt2 (
3944id_1 int(5) NOT NULL,
3945id_2 int(5) NOT NULL
3946);
3947INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
3948INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
3949INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
3950SELECT DISTINCT t1.id_1 FROM t1 WHERE
3951(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3952id_1
3953SELECT DISTINCT t1.id_1 FROM t1 WHERE
3954(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3955id_1
3956SELECT DISTINCT t1.id_1 FROM t1 WHERE
3957(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3958id_1
3959SELECT DISTINCT t1.id_1 FROM t1 WHERE
3960(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3961id_1
39621
39632
39643
39654
3966SELECT DISTINCT t1.id_1 FROM t1 WHERE
3967(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
3968id_1
39691
39702
39713
39724
3973SELECT DISTINCT t1.id_1 FROM t1 WHERE
3974(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
3975id_1
39761
39772
39783
39794
3980insert INTO t1xt2 VALUES (1, 12);
3981SELECT DISTINCT t1.id_1 FROM t1 WHERE
3982(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3983id_1
39841
3985SELECT DISTINCT t1.id_1 FROM t1 WHERE
3986(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3987id_1
39881
3989SELECT DISTINCT t1.id_1 FROM t1 WHERE
3990(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3991id_1
39921
3993SELECT DISTINCT t1.id_1 FROM t1 WHERE
3994(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3995id_1
39962
39973
39984
3999SELECT DISTINCT t1.id_1 FROM t1 WHERE
4000(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4001id_1
40022
40033
40044
4005SELECT DISTINCT t1.id_1 FROM t1 WHERE
4006(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4007id_1
40082
40093
40104
4011insert INTO t1xt2 VALUES (2, 12);
4012SELECT DISTINCT t1.id_1 FROM t1 WHERE
4013(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4014id_1
40151
40162
4017SELECT DISTINCT t1.id_1 FROM t1 WHERE
4018(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4019id_1
40201
40212
4022SELECT DISTINCT t1.id_1 FROM t1 WHERE
4023(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4024id_1
40251
40262
4027SELECT DISTINCT t1.id_1 FROM t1 WHERE
4028(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4029id_1
40303
40314
4032SELECT DISTINCT t1.id_1 FROM t1 WHERE
4033(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4034id_1
40353
40364
4037SELECT DISTINCT t1.id_1 FROM t1 WHERE
4038(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4039id_1
40403
40414
4042DROP TABLE t1;
4043DROP TABLE t2;
4044DROP TABLE t1xt2;
4045CREATE TABLE t1 (a int);
4046INSERT INTO t1 VALUES (3), (1), (2);
4047SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
4048col1	col2
4049this is a test.	3
4050this is a test.	1
4051this is a test.	2
4052SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
4053col1	t2
4054this is a test.	3
4055this is a test.	1
4056this is a test.	2
4057DROP table t1;
4058CREATE TABLE t1 (a int, b int);
4059CREATE TABLE t2 (m int, n int);
4060INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4061INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4062SELECT COUNT(*), a,
4063(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
4064FROM t1 GROUP BY a;
4065COUNT(*)	a	(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
40662	2	2
40673	3	3
40681	4	1
4069SELECT COUNT(*), a,
4070(SELECT MIN(m) FROM t2 WHERE m = count(*))
4071FROM t1 GROUP BY a;
4072COUNT(*)	a	(SELECT MIN(m) FROM t2 WHERE m = count(*))
40732	2	2
40743	3	3
40751	4	1
4076SELECT COUNT(*), a
4077FROM t1 GROUP BY a
4078HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
4079COUNT(*)	a
40802	2
40813	3
4082DROP TABLE t1,t2;
4083CREATE TABLE t1 (a int, b int);
4084CREATE TABLE t2 (m int, n int);
4085INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4086INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4087SELECT COUNT(*) c, a,
4088(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
4089FROM t1 GROUP BY a;
4090c	a	(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
40912	2	2
40923	3	3
40931	4	1,1
4094SELECT COUNT(*) c, a,
4095(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
4096FROM t1 GROUP BY a;
4097c	a	(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
40982	2	3
40993	3	4
41001	4	2,2
4101DROP table t1,t2;
4102CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
4103INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
4104(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
4105(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
4106SELECT a, MAX(b),
4107(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
4108FROM t1 GROUP BY a;
4109a	MAX(b)	test
41101	9	m
41112	3	h
41123	4	i
4113SELECT a x, MAX(b),
4114(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
4115FROM t1 GROUP BY a;
4116x	MAX(b)	test
41171	9	m
41182	3	h
41193	4	i
4120SELECT a, AVG(b),
4121(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
4122FROM t1 WHERE t1.d=0 GROUP BY a;
4123a	AVG(b)	test
41241	4.0000	d
41252	2.0000	g
41263	2.5000	NULL
4127SELECT tt.a,
4128(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4129LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4130FROM t1 as tt;
4131a	test
41321	n
41331	n
41341	n
41351	n
41361	n
41371	n
41381	n
41392	o
41402	o
41412	o
41422	o
41433	p
41443	p
41453	p
41463	p
41473	p
4148SELECT tt.a,
4149(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4150LIMIT 1)
4151FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4152FROM t1 as tt GROUP BY tt.a;
4153a	test
41541	n
41552	o
41563	p
4157SELECT tt.a, MAX(
4158(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4159LIMIT 1)
4160FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
4161FROM t1 as tt GROUP BY tt.a;
4162a	test
41631	n
41642	o
41653	p
4166DROP TABLE t1;
4167CREATE TABLE t1 (a int, b int);
4168INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4169SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4170a
41711
41722
4173SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4174a
4175SELECT a FROM t1 t0
4176WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4177a
41781
41792
4180SET @@sql_mode='ansi';
4181SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4182ERROR HY000: Invalid use of group function
4183SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4184ERROR HY000: Invalid use of group function
4185SELECT a FROM t1 t0
4186WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4187ERROR HY000: Invalid use of group function
4188SET @@sql_mode=default;
4189DROP TABLE t1;
4190CREATE TABLE t1 (a INT);
4191INSERT INTO t1 values (1),(1),(1),(1);
4192CREATE TABLE t2 (x INT);
4193INSERT INTO t1 values (1000),(1001),(1002);
4194SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
4195ERROR HY000: Invalid use of group function
4196SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
4197ERROR HY000: Invalid use of group function
4198SELECT COUNT(1) FROM DUAL;
4199COUNT(1)
42001
4201SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
4202ERROR HY000: Invalid use of group function
4203SELECT
4204SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
4205FROM t1;
4206ERROR HY000: Invalid use of group function
4207SELECT t1.a as XXA,
4208SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
4209FROM t1;
4210ERROR HY000: Invalid use of group function
4211DROP TABLE t1,t2;
4212CREATE TABLE t1 (a int, b int, KEY (a));
4213INSERT INTO t1 VALUES (1,1),(2,1);
4214EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
4215id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
42161	PRIMARY	t1	ref	a	a	5	const	1	Using where; Using index
42172	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
4218DROP TABLE t1;
4219CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
4220INSERT INTO t1 VALUES
4221(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
4222CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
4223INSERT INTO t2 VALUES (7), (5), (1), (3);
4224SELECT id, st FROM t1
4225WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4226id	st
42273	FL
42281	GA
42297	FL
4230SELECT id, st FROM t1
4231WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4232GROUP BY id;
4233id	st
42341	GA
42353	FL
42367	FL
4237SELECT id, st FROM t1
4238WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4239id	st
42402	GA
42414	FL
4242SELECT id, st FROM t1
4243WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4244GROUP BY id;
4245id	st
42462	GA
42474	FL
4248DROP TABLE t1,t2;
4249CREATE TABLE t1 (a int);
4250INSERT INTO t1 VALUES (1), (2);
4251EXPLAIN EXTENDED
4252SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
4253id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
42541	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	100.00
42552	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
4256Warnings:
4257Note	1003	/* select#1 */ select `res`.`count(*)` AS `count(*)` from (/* select#2 */ select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
4258DROP TABLE t1;
4259CREATE TABLE t1 (
4260a varchar(255) default NULL,
4261b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
4262INDEX idx(a,b)
4263);
4264CREATE TABLE t2 (
4265a varchar(255) default NULL
4266);
4267INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
4268INSERT INTO t1 SELECT * FROM t1;
4269INSERT INTO t1 SELECT * FROM t1;
4270INSERT INTO t1 SELECT * FROM t1;
4271INSERT INTO t1 SELECT * FROM t1;
4272INSERT INTO t1 SELECT * FROM t1;
4273INSERT INTO t1 SELECT * FROM t1;
4274INSERT INTO t1 SELECT * FROM t1;
4275INSERT INTO t1 SELECT * FROM t1;
4276INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
4277INSERT INTO `t2` VALUES ('abcdefghijk');
4278INSERT INTO `t2` VALUES ('asdf');
4279SET session sort_buffer_size=8192;
4280SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
4281d1
42821
42831
4284DROP TABLE t1,t2;
4285CREATE TABLE t1 (a INTEGER, b INTEGER);
4286CREATE TABLE t2 (x INTEGER);
4287INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
4288INSERT INTO t2 VALUES (1), (2);
4289SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
4290ERROR 21000: Subquery returns more than 1 row
4291SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
4292ERROR 21000: Subquery returns more than 1 row
4293SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
4294(SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
42953.3333
4296DROP TABLE t1,t2;
4297CREATE TABLE t1 (a INT, b INT);
4298INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
4299SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
4300AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
4301GROUP BY a1.a;
4302a	COUNT(*)
43031	3
4304DROP TABLE t1;
4305CREATE TABLE t1 (a INT);
4306CREATE TABLE t2 (a INT);
4307INSERT INTO t1 VALUES (1),(2);
4308INSERT INTO t2 VALUES (1),(2);
4309SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
4310(SELECT SUM(t1.a) FROM t2 WHERE a=0)
4311NULL
4312SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
4313ERROR 21000: Subquery returns more than 1 row
4314SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
4315(SELECT SUM(t1.a) FROM t2 WHERE a=1)
43163
4317DROP TABLE t1,t2;
4318CREATE TABLE t1 (a1 INT, a2 INT);
4319CREATE TABLE t2 (b1 INT, b2 INT);
4320INSERT INTO t1 VALUES (100, 200);
4321INSERT INTO t1 VALUES (101, 201);
4322INSERT INTO t2 VALUES (101, 201);
4323INSERT INTO t2 VALUES (103, 203);
4324SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
4325((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
43260
43270
4328DROP TABLE t1, t2;
4329CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
4330INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
4331SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4332s1	s2
4333SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4334s1	s2
4335CREATE INDEX I1 ON t1 (s1);
4336CREATE INDEX I2 ON t1 (s2);
4337SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4338s1	s2
4339SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4340s1	s2
4341TRUNCATE t1;
4342INSERT INTO t1 VALUES (0x41,0x41);
4343SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
4344s1	s2
4345DROP TABLE t1;
4346CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
4347CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
4348CREATE TABLE t3 (a3 BINARY(2) default '0');
4349INSERT INTO t1 VALUES (1),(2),(3),(4);
4350INSERT INTO t2 VALUES (1),(2),(3);
4351INSERT INTO t3 VALUES (1),(2),(3);
4352SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
4353LEFT(t2.a2, 1)
43541
43552
43563
4357SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
4358a1	t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
43591	0
43602	0
43613	0
43624	0
4363DROP TABLE t1,t2,t3;
4364CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
4365CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
4366CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
4367INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
4368INSERT INTO t2 VALUES (2), (3), (4), (5);
4369INSERT INTO t3 VALUES (10), (20), (30);
4370SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
4371LEFT(t1.a1,1)
43721
43732
43743
4375SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
4376a2
4377DROP TABLE t1, t2, t3;
4378SET @save_optimizer_switch=@@optimizer_switch;
4379SET optimizer_switch='semijoin_with_cache=off';
4380SET optimizer_switch='materialization=off';
4381CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
4382INSERT INTO t1 VALUES ('a', 'aa');
4383INSERT INTO t1 VALUES ('a', 'aaa');
4384SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4385a	b
4386CREATE INDEX I1 ON t1 (a);
4387CREATE INDEX I2 ON t1 (b);
4388EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4389id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
43901	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where
43911	PRIMARY	t1	ref	I1	I1	2	test.t1.b	2	Using where; Using index; FirstMatch(t1)
4392SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4393a	b
4394CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
4395INSERT INTO t2 SELECT * FROM t1;
4396CREATE INDEX I1 ON t2 (a);
4397CREATE INDEX I2 ON t2 (b);
4398EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4399id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44001	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where
44011	PRIMARY	t2	ref	I1	I1	4	test.t2.b	2	Using where; Using index; FirstMatch(t2)
4402SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4403a	b
4404EXPLAIN
4405SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4406id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44071	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where
44081	PRIMARY	t1	ref	I1	I1	2	test.t1.b	2	Using where; Using index; FirstMatch(t1)
4409SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4410a	b
4411DROP TABLE t1,t2;
4412SET optimizer_switch= @save_optimizer_switch;
4413CREATE TABLE t1(a INT, b INT);
4414INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
4415EXPLAIN
4416SELECT a AS out_a, MIN(b) FROM t1
4417WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4418GROUP BY a;
4419ERROR 42S22: Unknown column 'out_a' in 'where clause'
4420SELECT a AS out_a, MIN(b) FROM t1
4421WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4422GROUP BY a;
4423ERROR 42S22: Unknown column 'out_a' in 'where clause'
4424EXPLAIN
4425SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4426WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4427GROUP BY a;
4428id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44291	PRIMARY	t1_outer	ALL	NULL	NULL	NULL	NULL	4	Using where; Using temporary; Using filesort
44302	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
4431SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4432WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4433GROUP BY a;
4434out_a	MIN(b)
44351	2
44362	4
4437DROP TABLE t1;
4438CREATE TABLE t1 (a INT);
4439CREATE TABLE t2 (a INT);
4440INSERT INTO t1 VALUES (1),(2);
4441INSERT INTO t2 VALUES (1),(2);
4442SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
44432
44442
44452
4446EXPLAIN EXTENDED
4447SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
4448id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44491	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
44501	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	100.00
44512	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
4452Warnings:
4453Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4454Note	1003	select 2 AS `2` from `test`.`t1` semi join (`test`.`t2`) where 1
4455EXPLAIN EXTENDED
4456SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
4457(SELECT 1 FROM t2 WHERE t1.a = t2.a));
4458id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44591	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44602	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44613	DEPENDENT UNION	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
4462NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
4463Warnings:
4464Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4465Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4466Note	1003	/* select#1 */ select 2 AS `2` from `test`.`t1` where exists((/* select#2 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) union (/* select#3 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)
4467DROP TABLE t1,t2;
4468create table t0(a int);
4469insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4470create table t1(f11 int, f12 int);
4471create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
4472insert into t1 values(1,1),(2,2), (3, 3);
4473insert ignore into t2
4474select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
4475from t0 A, t0 B, t0 C, t0 D;
4476set session sort_buffer_size= 33*1024;
4477select count(*) from t1 where f12 =
4478(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
4479count(*)
44803
4481drop table t0,t1,t2;
4482CREATE TABLE t4 (
4483f7 varchar(32) collate utf8_bin NOT NULL default '',
4484f10 varchar(32) collate utf8_bin default NULL,
4485PRIMARY KEY  (f7)
4486);
4487INSERT INTO t4 VALUES(1,1), (2,null);
4488CREATE TABLE t2 (
4489f4 varchar(32) collate utf8_bin NOT NULL default '',
4490f2 varchar(50) collate utf8_bin default NULL,
4491f3 varchar(10) collate utf8_bin default NULL,
4492PRIMARY KEY  (f4),
4493UNIQUE KEY uk1 (f2)
4494);
4495INSERT INTO t2 VALUES(1,1,null), (2,2,null);
4496CREATE TABLE t1 (
4497f8 varchar(32) collate utf8_bin NOT NULL default '',
4498f1 varchar(10) collate utf8_bin default NULL,
4499f9 varchar(32) collate utf8_bin default NULL,
4500PRIMARY KEY  (f8)
4501);
4502INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
4503CREATE TABLE t3 (
4504f6 varchar(32) collate utf8_bin NOT NULL default '',
4505f5 varchar(50) collate utf8_bin default NULL,
4506PRIMARY KEY (f6)
4507);
4508INSERT INTO t3 VALUES (1,null), (2,null);
4509SELECT
4510IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
4511IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
4512SUM(
4513IF(
4514(SELECT VPC.f2
4515FROM t2 VPC, t4 a2, t2 a3
4516WHERE
4517VPC.f4 = a2.f10 AND a3.f2 = a4
4518LIMIT 1) IS NULL,
45190,
4520t3.f5
4521)
4522) AS a6
4523FROM
4524t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
4525GROUP BY a4;
4526a4	f3	a6
45271	NULL	NULL
45282	NULL	NULL
4529DROP TABLE t1, t2, t3, t4;
4530create table t1 (a float(5,4) zerofill);
4531create table t2 (a float(5,4),b float(2,0));
4532select t1.a from t1 where
4533t1.a= (select b from t2 limit 1) and not
4534t1.a= (select a from t2 limit 1) ;
4535a
4536drop table t1, t2;
4537CREATE TABLE t1 (a INT);
4538INSERT INTO t1 VALUES (1),(2);
4539SET @save_join_cache_level=@@join_cache_level;
4540SET join_cache_level=0;
4541EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
4542id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45431	PRIMARY	<subquery2>	const	distinct_key	distinct_key	4	const	1	100.00
45441	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
45452	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary
4546Warnings:
4547Note	1003	/* select#1 */ select 1 AS `1` from  <materialize> (/* select#2 */ select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where `<subquery2>`.`min(a)` = 1
4548EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
4549id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45501	PRIMARY	<subquery2>	const	distinct_key	distinct_key	4	const	1	100.00
45511	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
45522	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using temporary
4553Warnings:
4554Note	1003	/* select#1 */ select 1 AS `1` from  <materialize> (/* select#2 */ 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
4555SET join_cache_level=@save_join_cache_level;
4556DROP TABLE t1;
4557#
4558# Bug#45061: Incorrectly market field caused wrong result.
4559#
4560CREATE TABLE `C` (
4561`int_nokey` int(11) NOT NULL,
4562`int_key` int(11) NOT NULL,
4563KEY `int_key` (`int_key`)
4564);
4565INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
4566(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
4567(5,2), (1,8), (7,0), (0,9), (9,5);
4568SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4569int_nokey	int_key
45709	9
45710	0
45725	5
45730	0
4574EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4575id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45761	SIMPLE	C	ALL	NULL	NULL	NULL	NULL	20	100.00	Using where
4577DROP TABLE C;
4578# End of test for bug#45061.
4579#
4580# Bug #46749: Segfault in add_key_fields() with outer subquery level
4581#   field references
4582#
4583CREATE TABLE t1 (
4584a int,
4585b int,
4586UNIQUE (a), KEY (b)
4587);
4588INSERT INTO t1 VALUES (1,1), (2,1);
4589CREATE TABLE st1 like t1;
4590INSERT INTO st1 VALUES (1,1), (2,1);
4591CREATE TABLE st2 like t1;
4592INSERT INTO st2 VALUES (1,1), (2,1);
4593EXPLAIN
4594SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4595FROM t1
4596WHERE a = 230;
4597id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
45981	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
45992	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
4600SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4601FROM t1
4602WHERE a = 230;
4603MAX(b)	(SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4604NULL	NULL
4605DROP TABLE t1, st1, st2;
4606#
4607# Bug #48709: Assertion failed in sql_select.cc:11782:
4608#   int join_read_key(JOIN_TAB*)
4609#
4610CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
4611INSERT INTO t1 VALUES (10,1), (14,1);
4612CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
4613INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
4614# should have eq_ref for t1
4615EXPLAIN
4616SELECT * FROM t2 outr
4617WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4618ORDER BY outr.pk;
4619id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
4620x	x	outr	ALL	x	x	x	x	x	x
4621x	x	t1	eq_ref	x	x	x	x	x	x
4622x	x	t2	index	x	x	x	x	x	x
4623# should not crash on debug binaries
4624SELECT * FROM t2 outr
4625WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4626ORDER BY outr.pk;
4627pk	int_key
46283	3
46297	3
4630DROP TABLE t1,t2;
4631#
4632# Bug#12329653
4633# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4634#
4635CREATE TABLE t1(a1 int);
4636INSERT INTO t1 VALUES (1),(2);
4637SELECT @@session.sql_mode INTO @old_sql_mode;
4638SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4639EXPLAIN EXTENDED
4640SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4641id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
46421	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
46432	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
4644Warnings:
4645Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
4646SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
46471
46481
46491
4650PREPARE stmt FROM
4651'SELECT 1 UNION ALL
4652SELECT 1 FROM t1
4653ORDER BY
4654(SELECT 1 FROM t1 AS t1_0
4655  WHERE 1 < SOME (SELECT a1 FROM t1)
4656)' ;
4657EXECUTE stmt ;
4658ERROR 21000: Subquery returns more than 1 row
4659EXECUTE stmt ;
4660ERROR 21000: Subquery returns more than 1 row
4661SET SESSION sql_mode=@old_sql_mode;
4662DEALLOCATE PREPARE stmt;
4663DROP TABLE t1;
4664#
4665# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4666#
4667CREATE TABLE t1(a1 int);
4668INSERT INTO t1 VALUES (1),(2);
4669CREATE TABLE t2(a1 int);
4670INSERT INTO t2 VALUES (3);
4671SELECT @@session.sql_mode INTO @old_sql_mode;
4672SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4673SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
46741
46751
46761
4677SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
46781
46791
46801
4681SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
46821
4683Warnings:
4684Warning	1292	Truncated incorrect DOUBLE value: 'a'
4685SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
46861
46871
46881
4689SET SESSION sql_mode=@old_sql_mode;
4690DROP TABLE t1, t2;
4691#
4692# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4693#
4694create table t2(i int);
4695insert into t2 values(0);
4696SELECT @@session.sql_mode INTO @old_sql_mode;
4697SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4698CREATE VIEW v1 AS
4699SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
4700;
4701CREATE TABLE t1 (
4702pk int NOT NULL,
4703col_varchar_key varchar(1) DEFAULT NULL,
4704PRIMARY KEY (pk),
4705KEY col_varchar_key (col_varchar_key)
4706);
4707SELECT t1.pk
4708FROM t1
4709WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4710;
4711pk
4712SET SESSION sql_mode=@old_sql_mode;
4713drop table t2, t1;
4714drop view v1;
4715# End of 5.0 tests.
4716create table t_out (subcase char(3),
4717a1 char(2), b1 char(2), c1 char(2));
4718create table t_in  (a2 char(2), b2 char(2), c2 char(2));
4719insert into t_out values ('A.1','2a', NULL, '2a');
4720insert into t_out values ('A.3', '2a', NULL, '2a');
4721insert into t_out values ('A.4', '2a', NULL, 'xx');
4722insert into t_out values ('B.1', '2a', '2a', '2a');
4723insert into t_out values ('B.2', '2a', '2a', '2a');
4724insert into t_out values ('B.3', '3a', 'xx', '3a');
4725insert into t_out values ('B.4', 'xx', '3a', '3a');
4726insert into t_in values ('1a', '1a', '1a');
4727insert into t_in values ('2a', '2a', '2a');
4728insert into t_in values (NULL, '2a', '2a');
4729insert into t_in values ('3a', NULL, '3a');
4730
4731Test general IN semantics (not top-level)
4732
4733case A.1
4734select subcase,
4735(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4736(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4737from t_out where subcase = 'A.1';
4738subcase	pred_in	pred_not_in
4739A.1	0	1
4740case A.2 - impossible
4741case A.3
4742select subcase,
4743(a1, b1, c1)     IN (select * from t_in) pred_in,
4744(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4745from t_out where subcase = 'A.3';
4746subcase	pred_in	pred_not_in
4747A.3	NULL	NULL
4748case A.4
4749select subcase,
4750(a1, b1, c1)     IN (select * from t_in) pred_in,
4751(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4752from t_out where subcase = 'A.4';
4753subcase	pred_in	pred_not_in
4754A.4	0	1
4755case B.1
4756select subcase,
4757(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4758(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4759from t_out where subcase = 'B.1';
4760subcase	pred_in	pred_not_in
4761B.1	0	1
4762case B.2
4763select subcase,
4764(a1, b1, c1)     IN (select * from t_in) pred_in,
4765(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4766from t_out where subcase = 'B.2';
4767subcase	pred_in	pred_not_in
4768B.2	1	0
4769case B.3
4770select subcase,
4771(a1, b1, c1)     IN (select * from t_in) pred_in,
4772(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4773from t_out where subcase = 'B.3';
4774subcase	pred_in	pred_not_in
4775B.3	NULL	NULL
4776case B.4
4777select subcase,
4778(a1, b1, c1)     IN (select * from t_in) pred_in,
4779(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4780from t_out where subcase = 'B.4';
4781subcase	pred_in	pred_not_in
4782B.4	0	1
4783
4784Test IN as top-level predicate, and
4785as non-top level for cases A.3, B.3 (the only cases with NULL result).
4786
4787case A.1
4788select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4789where subcase = 'A.1' and
4790(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4791pred_in
4792F
4793select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4794where subcase = 'A.1' and
4795(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4796pred_not_in
4797T
4798select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4799where subcase = 'A.1' and
4800NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4801not_pred_in
4802T
4803case A.3
4804select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4805where subcase = 'A.3' and
4806(a1, b1, c1) IN (select * from t_in);
4807pred_in
4808F
4809select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4810where subcase = 'A.3' and
4811(a1, b1, c1) NOT IN (select * from t_in);
4812pred_not_in
4813F
4814select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4815where subcase = 'A.3' and
4816NOT((a1, b1, c1) IN (select * from t_in));
4817not_pred_in
4818F
4819select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4820where subcase = 'A.3' and
4821((a1, b1, c1) IN (select * from t_in)) is NULL and
4822((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4823pred_in
4824N
4825case A.4
4826select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4827where subcase = 'A.4' and
4828(a1, b1, c1) IN (select * from t_in);
4829pred_in
4830F
4831select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4832where subcase = 'A.4' and
4833(a1, b1, c1) NOT IN (select * from t_in);
4834pred_not_in
4835T
4836select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4837where subcase = 'A.4' and
4838NOT((a1, b1, c1) IN (select * from t_in));
4839not_pred_in
4840T
4841case B.1
4842select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4843where subcase = 'B.1' and
4844(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4845pred_in
4846F
4847select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4848where subcase = 'B.1' and
4849(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4850pred_not_in
4851T
4852select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4853where subcase = 'B.1' and
4854NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4855not_pred_in
4856T
4857case B.2
4858select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4859where subcase = 'B.2' and
4860(a1, b1, c1) IN (select * from t_in);
4861pred_in
4862T
4863select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4864where subcase = 'B.2' and
4865(a1, b1, c1) NOT IN (select * from t_in);
4866pred_not_in
4867F
4868select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4869where subcase = 'B.2' and
4870NOT((a1, b1, c1) IN (select * from t_in));
4871not_pred_in
4872F
4873case B.3
4874select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4875where subcase = 'B.3' and
4876(a1, b1, c1) IN (select * from t_in);
4877pred_in
4878F
4879select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4880where subcase = 'B.3' and
4881(a1, b1, c1) NOT IN (select * from t_in);
4882pred_not_in
4883F
4884select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4885where subcase = 'B.3' and
4886NOT((a1, b1, c1) IN (select * from t_in));
4887not_pred_in
4888F
4889select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4890where subcase = 'B.3' and
4891((a1, b1, c1) IN (select * from t_in)) is NULL and
4892((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4893pred_in
4894N
4895case B.4
4896select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4897where subcase = 'B.4' and
4898(a1, b1, c1) IN (select * from t_in);
4899pred_in
4900F
4901select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4902where subcase = 'B.4' and
4903(a1, b1, c1) NOT IN (select * from t_in);
4904pred_not_in
4905T
4906select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4907where subcase = 'B.4' and
4908NOT((a1, b1, c1) IN (select * from t_in));
4909not_pred_in
4910T
4911drop table t_out;
4912drop table t_in;
4913CREATE TABLE t1 (a INT, b INT);
4914INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4915SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4916a
49171
49182
4919SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4920a
4921SELECT a FROM t1 t0
4922WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4923a
49241
49252
4926SET @@sql_mode='ansi';
4927SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4928ERROR HY000: Invalid use of group function
4929SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4930ERROR HY000: Invalid use of group function
4931SELECT a FROM t1 t0
4932WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4933ERROR HY000: Invalid use of group function
4934SET @@sql_mode=default;
4935DROP TABLE t1;
4936CREATE TABLE t1 (s1 CHAR(1));
4937INSERT INTO t1 VALUES ('a');
4938SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
4939s1
4940a
4941DROP TABLE t1;
4942CREATE TABLE t1(c INT, KEY(c));
4943CREATE TABLE t2(a INT, b INT);
4944INSERT INTO t2 VALUES (1, 10), (2, NULL);
4945INSERT INTO t1 VALUES (1), (3);
4946SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4947a	b
4948DROP TABLE t1,t2;
4949CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4950INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4951CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4952INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4953SELECT * FROM t1
4954WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
4955pk	a
49561	10
4957DROP TABLE t1,t2;
4958CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4959INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4960CREATE TABLE t2 (
4961a INT,
4962b INT,
4963c INT,
4964d INT,
4965PRIMARY KEY (a),
4966UNIQUE KEY b (b,c,d),
4967KEY b_2 (b),
4968KEY c (c),
4969KEY d (d)
4970);
4971INSERT INTO t2 VALUES
4972(43, 2, 11 ,30),
4973(44, 2, 12 ,30),
4974(45, 1, 1  ,10000),
4975(46, 1, 2  ,10000),
4976(556,1, 32 ,10000);
4977CREATE TABLE t3 (
4978a INT,
4979b INT,
4980c INT,
4981PRIMARY KEY (a),
4982UNIQUE KEY b (b,c),
4983KEY c (c),
4984KEY b_2 (b)
4985);
4986INSERT INTO t3 VALUES (1,1,1), (2,32,1);
4987explain
4988SELECT 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;
4989id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
49901	PRIMARY	t1	index	PRIMARY	PRIMARY	4	NULL	2	Using index
49911	PRIMARY	t3	ref	b,b_2	b	5	test.t1.a	1	Using index
49922	DEPENDENT SUBQUERY	t2	ref	b,b_2,c	b	10	test.t3.c,test.t1.a	1	Using where; Using index
4993SELECT 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;
4994a	incorrect
49951	1
4996DROP TABLE t1,t2,t3;
4997CREATE TABLE t1 (id int);
4998CREATE TABLE t2 (id int, c int);
4999INSERT INTO t1 (id) VALUES (1);
5000INSERT INTO t2 (id) VALUES (1);
5001INSERT INTO t1 (id) VALUES (1);
5002INSERT INTO t2 (id) VALUES (1);
5003CREATE VIEW v1 AS
5004SELECT t2.c AS c FROM t1, t2
5005WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5006UPDATE v1 SET c=1;
5007CREATE VIEW v2 (a,b) AS
5008SELECT t2.id, t2.c AS c FROM t1, t2
5009WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5010INSERT INTO v2(a,b) VALUES (2,2);
5011ERROR 44000: CHECK OPTION failed `test`.`v2`
5012SELECT * FROM v1;
5013c
50141
50151
50161
50171
5018CREATE VIEW v3 AS
5019SELECT t2.c AS c FROM t2
5020WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5021DELETE FROM v3;
5022DROP VIEW v1,v2,v3;
5023DROP TABLE t1,t2;
5024#
5025# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5026#
5027create table t1(id integer primary key, g integer, v integer, s char(1));
5028create table t2(id integer primary key, g integer, v integer, s char(1));
5029insert into t1 values
5030(10, 10, 10,   'l'),
5031(20, 20, 20,   'l'),
5032(40, 40, 40,   'l'),
5033(41, 40, null, 'l'),
5034(50, 50, 50,   'l'),
5035(51, 50, null, 'l'),
5036(60, 60, 60,   'l'),
5037(61, 60, null, 'l'),
5038(70, 70, 70,   'l'),
5039(90, 90, null, 'l');
5040insert into t2 values
5041(10, 10, 10,   'r'),
5042(30, 30, 30,   'r'),
5043(50, 50, 50,   'r'),
5044(60, 60, 60,   'r'),
5045(61, 60, null, 'r'),
5046(70, 70, 70,   'r'),
5047(71, 70, null, 'r'),
5048(80, 80, 80,   'r'),
5049(81, 80, null, 'r'),
5050(100,100,null, 'r');
5051select *
5052from t1
5053where v in(select v
5054from t2
5055where t1.g=t2.g) is unknown;
5056id	g	v	s
505751	50	NULL	l
505861	60	NULL	l
5059drop table t1, t2;
5060#
5061# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5062#
5063create table t1(id integer primary key, g integer, v integer, s char(1));
5064create table t2(id integer primary key, g integer, v integer, s char(1));
5065insert into t1 values
5066(10, 10, 10,   'l'),
5067(20, 20, 20,   'l'),
5068(40, 40, 40,   'l'),
5069(41, 40, null, 'l'),
5070(50, 50, 50,   'l'),
5071(51, 50, null, 'l'),
5072(60, 60, 60,   'l'),
5073(61, 60, null, 'l'),
5074(70, 70, 70,   'l'),
5075(90, 90, null, 'l');
5076insert into t2 values
5077(10, 10, 10,   'r'),
5078(30, 30, 30,   'r'),
5079(50, 50, 50,   'r'),
5080(60, 60, 60,   'r'),
5081(61, 60, null, 'r'),
5082(70, 70, 70,   'r'),
5083(71, 70, null, 'r'),
5084(80, 80, 80,   'r'),
5085(81, 80, null, 'r'),
5086(100,100,null, 'r');
5087select *
5088from t1
5089where v in(select v
5090from t2
5091where t1.g=t2.g) is unknown;
5092id	g	v	s
509351	50	NULL	l
509461	60	NULL	l
5095drop table t1, t2;
5096#
5097# Bug#33204: INTO is allowed in subselect, causing inconsistent results
5098#
5099CREATE TABLE t1( a INT );
5100INSERT INTO t1 VALUES (1),(2);
5101CREATE TABLE t2( a INT, b INT );
5102SELECT *
5103FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
5104ERROR 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
5105SELECT *
5106FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
5107ERROR 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
5108SELECT *
5109FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
5110ERROR 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
5111SELECT * FROM (
5112SELECT 1 a
5113UNION
5114SELECT a INTO @var FROM t1 WHERE a = 2
5115) t1a;
5116ERROR 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
5117) t1a' at line 4
5118SELECT * FROM (
5119SELECT 1 a
5120UNION
5121SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
5122) t1a;
5123ERROR 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
5124) t1a' at line 4
5125SELECT * FROM (
5126SELECT 1 a
5127UNION
5128SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
5129) t1a;
5130ERROR 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
5131) t1a' at line 4
5132SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
5133a
51342
5135SELECT * FROM (
5136SELECT a FROM t1 WHERE a = 2
5137UNION
5138SELECT a FROM t1 WHERE a = 2
5139) t1a;
5140a
51412
5142SELECT * FROM (
5143SELECT 1 a
5144UNION
5145SELECT a FROM t1 WHERE a = 2
5146UNION
5147SELECT a FROM t1 WHERE a = 2
5148) t1a;
5149a
51501
51512
5152SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
5153a
51541
5155SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
5156a
51571
5158SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
51591
51601
5161SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
5162ERROR 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
5163SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
5164ERROR 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
5165SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5166ERROR 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
5167SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
5168ERROR 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
5169SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5170ERROR 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
5171SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
5172ERROR 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
5173SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
5174ERROR 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
5175SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
5176ERROR 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
5177SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
5178ERROR 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
5179SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
5180a
51811
5182SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
5183a
51841
5185SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
5186a
51871
5188SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
5189a
51901
5191SELECT * FROM t1 JOIN  (SELECT 1 UNION SELECT 1) alias ON 1;
5192a	1
51931	1
51942	1
5195SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
5196ERROR 42000: Every derived table must have its own alias
5197SELECT * FROM t1 JOIN  (t1 t1a UNION SELECT 1)  ON 1;
5198ERROR 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)  ON 1' at line 1
5199SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
5200ERROR 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)) ON 1' at line 1
5201SELECT * FROM t1 JOIN  (t1 t1a)  t1a ON 1;
5202ERROR 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
5203SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
5204ERROR 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
5205SELECT * FROM t1 JOIN  (t1 t1a)  ON 1;
5206a	a
52071	1
52082	1
52091	2
52102	2
5211SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
5212a	a
52131	1
52142	1
52151	2
52162	2
5217SELECT * FROM (t1 t1a);
5218a
52191
52202
5221SELECT * FROM ((t1 t1a));
5222a
52231
52242
5225SELECT * FROM t1 JOIN  (SELECT 1 t1a) alias ON 1;
5226a	t1a
52271	1
52282	1
5229SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
5230a	t1a
52311	1
52322	1
5233SELECT * FROM t1 JOIN  (SELECT 1 a)  a ON 1;
5234a	a
52351	1
52362	1
5237SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
5238a	a
52391	1
52402	1
5241SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
5242ERROR 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
5243SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
5244a
52451
5246SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
5247a
52481
5249SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
5250a
52511
5252SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
5253ERROR 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
5254SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5255ERROR 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
5256SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5257ERROR 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
5258SELECT * FROM t1 WHERE a = ( SELECT 1 );
5259a
52601
5261SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
5262a
52631
5264SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
5265ERROR 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
5266SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
5267ERROR 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
5268SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
5269ERROR 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
5270SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
5271ERROR 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
5272SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5273ERROR 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
5274SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5275ERROR 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
5276SELECT ( SELECT 1 INTO @v );
5277ERROR 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
5278SELECT ( SELECT 1 INTO OUTFILE 'file' );
5279ERROR 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
5280SELECT ( SELECT 1 INTO DUMPFILE 'file' );
5281ERROR 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
5282SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
5283ERROR 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
5284SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5285ERROR 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
5286SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5287ERROR 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
5288SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
5289( SELECT a FROM t1 WHERE a = 1 )	a
52901	1
52911	2
5292SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
5293( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 )	a
52941	1
52951	2
5296SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
5297a	b
5298SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
5299ERROR 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
5300( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5301ERROR 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
5302SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5303ERROR 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
5304SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5305ERROR 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
5306SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5307( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
53081
5309SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
5310((SELECT 1 UNION SELECT 1 UNION SELECT 1))
53111
5312SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5313ERROR 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
5314SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5315ERROR 42000: Every derived table must have its own alias
5316SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
53171
53181
5319SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
53201
53211
5322SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5323ERROR 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
5324SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5325ERROR 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
5326SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5327ERROR 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
5328SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5329ERROR 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
5330SELECT * FROM t1 WHERE a =     ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5331ERROR 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
5332SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5333ERROR 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
5334SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5335ERROR 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
5336SELECT * FROM t1 WHERE a IN    ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5337ERROR 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
5338SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5339a
53401
5341SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5342a
53431
5344SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5345a
53461
5347SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5348a
53491
5350SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
5351ERROR 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
5352SELECT EXISTS(SELECT 1+1);
5353EXISTS(SELECT 1+1)
53541
5355SELECT EXISTS(SELECT 1+1 INTO @test);
5356ERROR 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
5357SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
5358ERROR 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
5359SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
5360ERROR 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
5361SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
5362ERROR 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
5363DROP TABLE t1, t2;
5364CREATE TABLE t1 (a ENUM('rainbow'));
5365INSERT INTO t1 VALUES (),(),(),(),();
5366SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
53671
53681
5369DROP TABLE t1;
5370CREATE TABLE t1 (a LONGBLOB);
5371INSERT INTO t1 SET a = 'aaaa';
5372INSERT INTO t1 SET a = 'aaaa';
5373SELECT 1 FROM t1 GROUP BY
5374(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
53751
53761
5377DROP TABLE t1;
5378#
5379# Bug #49512 : subquery with aggregate function crash
5380#   subselect_single_select_engine::exec()
5381CREATE TABLE t1(a INT);
5382INSERT INTO t1 VALUES();
5383# should not crash
5384SELECT 1 FROM t1 WHERE a <> SOME
5385(
5386SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
5387FROM t1,t1 a
5388);
53891
5390DROP TABLE t1;
5391#
5392# Bug #45989 take 2 : memory leak after explain encounters an
5393# error in the query
5394#
5395CREATE TABLE t1(a LONGTEXT);
5396INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
5397INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
5398EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
5399(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
5400WHERE t1.a = d1.a;
5401ERROR 42S22: Unknown column 'd1.a' in 'where clause'
5402DROP TABLE t1;
5403Set up test tables.
5404CREATE TABLE t1 (
5405t1_id INT UNSIGNED,
5406PRIMARY KEY(t1_id)
5407) Engine=MyISAM;
5408INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
5409CREATE TABLE t2 SELECT * FROM t1;
5410CREATE TABLE t3 (
5411t3_id INT UNSIGNED AUTO_INCREMENT,
5412t1_id INT UNSIGNED,
5413amount DECIMAL(16,2),
5414PRIMARY KEY(t3_id),
5415KEY(t1_id)
5416) Engine=MyISAM;
5417INSERT INTO t3 (t1_id, t3_id, amount)
5418VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
5419This is the 'inner query' running by itself.
5420Produces correct results.
5421SELECT
5422t1.t1_id,
5423IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5424FROM
5425t1
5426LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5427GROUP BY
5428t1.t1_id
5429;
5430t1_id	total_amount
54311	100.00
54322	200.00
54333	0.00
54344	400.00
54355	0.00
5436SELECT * FROM (the same inner query)
5437Produces correct results.
5438SELECT * FROM (
5439SELECT
5440t1.t1_id,
5441IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5442FROM
5443t1
5444LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5445GROUP BY
5446t1.t1_id
5447) AS t;
5448t1_id	total_amount
54491	100.00
54502	200.00
54513	0.00
54524	400.00
54535	0.00
5454Now make t2.t1_id part of a key.
5455ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
5456Same inner query by itself.
5457Still correct results.
5458SELECT
5459t1.t1_id,
5460IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5461FROM
5462t1
5463LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5464GROUP BY
5465t1.t1_id;
5466t1_id	total_amount
54671	100.00
54682	200.00
54693	0.00
54704	400.00
54715	0.00
5472SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
5473SELECT * FROM (
5474SELECT
5475t1.t1_id,
5476IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5477FROM
5478t1
5479LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5480GROUP BY
5481t1.t1_id
5482) AS t;
5483t1_id	total_amount
54841	100.00
54852	200.00
54863	0.00
54874	400.00
54885	0.00
5489DROP TABLE t3;
5490DROP TABLE t2;
5491DROP TABLE t1;
5492#
5493# Bug #52711: Segfault when doing EXPLAIN SELECT with
5494#  union...order by (select... where...)
5495#
5496CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5497INSERT INTO t1 VALUES (1),(2);
5498CREATE TABLE t2 (b INT);
5499INSERT INTO t2 VALUES (1),(2);
5500# Should not crash
5501EXPLAIN
5502SELECT * FROM t2 UNION SELECT * FROM t2
5503ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5504# Should not crash
5505SELECT * FROM t2 UNION SELECT * FROM t2
5506ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5507DROP TABLE t1,t2;
5508#
5509# Bug #58818: Incorrect result for IN/ANY subquery
5510# with HAVING condition
5511#
5512CREATE TABLE t1(i INT);
5513INSERT INTO t1 VALUES (1), (2), (3);
5514CREATE TABLE t1s(i INT);
5515INSERT INTO t1s VALUES (10), (20), (30);
5516CREATE TABLE t2s(i INT);
5517INSERT INTO t2s VALUES (100), (200), (300);
5518SELECT * FROM t1
5519WHERE t1.i NOT IN
5520(
5521SELECT STRAIGHT_JOIN t2s.i
5522FROM
5523t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5524HAVING t2s.i = 999
5525);
5526i
55271
55282
55293
5530SELECT * FROM t1
5531WHERE t1.I IN
5532(
5533SELECT STRAIGHT_JOIN t2s.i
5534FROM
5535t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5536HAVING t2s.i = 999
5537) IS UNKNOWN;
5538i
5539SELECT * FROM t1
5540WHERE NOT t1.I = ANY
5541(
5542SELECT STRAIGHT_JOIN t2s.i
5543FROM
5544t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5545HAVING t2s.i = 999
5546);
5547i
55481
55492
55503
5551SELECT * FROM t1
5552WHERE t1.i = ANY (
5553SELECT STRAIGHT_JOIN t2s.i
5554FROM
5555t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5556HAVING t2s.i = 999
5557) IS UNKNOWN;
5558i
5559DROP TABLE t1,t1s,t2s;
5560# LP BUG#675248 - select->prep_where references on freed memory
5561CREATE TABLE t1 (a int, b int);
5562insert into t1 values (1,1),(0,0);
5563CREATE TABLE t2 (c int);
5564insert into t2 values (1),(2);
5565prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
5566from t1 as table1 group by sub";
5567execute stmt1;
5568sum(a)	sub
55690	NULL
55701	3
5571deallocate prepare stmt1;
5572prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
5573from t1 as table1";
5574execute stmt1;
5575sum(a)	sub
55761	3
5577deallocate prepare stmt1;
5578drop table t1,t2;
5579#
5580# Bug LP#693935/#58727: Assertion failure with
5581# a single row subquery returning more than one row
5582#
5583create table t1 (a char(1) charset utf8);
5584insert into t1 values ('a'), ('b');
5585create table t2 (a binary(1));
5586insert into t2 values ('x'), ('y');
5587select * from t2 where a=(select a from t1) and a='x';
5588ERROR 21000: Subquery returns more than 1 row
5589drop table t1,t2;
5590# End of 5.1 tests
5591#
5592# Bug #11765713 58705:
5593# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5594# CREATED BY OPT_SUM_QUERY
5595#
5596CREATE TABLE t1(a INT NOT NULL, KEY (a));
5597INSERT INTO t1 VALUES (0), (1);
5598SELECT 1 as foo FROM t1 WHERE a < SOME
5599(SELECT a FROM t1 WHERE a <=>
5600(SELECT a FROM t1)
5601);
5602ERROR 21000: Subquery returns more than 1 row
5603SELECT 1 as foo FROM t1 WHERE a < SOME
5604(SELECT a FROM t1 WHERE a <=>
5605(SELECT a FROM t1 where a is null)
5606);
5607foo
5608DROP TABLE t1;
5609#
5610# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
5611#             Assertion `file' failed.
5612#
5613CREATE TABLE t1 (a INT);
5614SELECT 1 FROM
5615(SELECT ROW(
5616(SELECT 1 FROM t1 RIGHT JOIN
5617(SELECT 1 FROM t1, t1 t2) AS d ON 1),
56181) FROM t1) AS e;
5619ERROR 21000: Operand should contain 1 column(s)
5620DROP TABLE t1;
5621#
5622# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5623#
5624CREATE TABLE t1(a TIME NOT NULL);
5625INSERT INTO t1 VALUES ('00:00:32');
5626SELECT 1 FROM t1 WHERE a >
5627(SELECT timestamp(a) AS a FROM t1);
56281
5629DROP TABLE t1;
5630#
5631# No BUG#, a case brought from 5.2's innodb_mysql_lock.test
5632#
5633create table t1 (i int  not null primary key);
5634insert into t1 values (1),(2),(3),(4),(5);
5635create table t2 (j int not null  primary key);
5636insert into t2 values (1),(2),(3),(4),(5);
5637create table t3 (k int not null primary key);
5638insert into t3 values (1),(2),(3);
5639create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
5640select * from t3 where k in (select j from v2);
5641k
56421
56432
56443
5645drop table t1,t2,t3;
5646drop view v2;
5647#
5648# Bug#52068: Optimizer generates invalid semijoin materialization plan
5649#
5650drop table if exists ot1, ot2, it1, it2;
5651CREATE TABLE ot1(a INTEGER);
5652INSERT INTO ot1 VALUES(5), (8);
5653CREATE TABLE it2(a INTEGER);
5654INSERT INTO it2 VALUES(9), (5), (1), (8);
5655CREATE TABLE it3(a INTEGER);
5656INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
5657CREATE TABLE ot4(a INTEGER);
5658INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
5659SELECT * FROM ot1,ot4
5660WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5661FROM it2,it3);
5662a	a
56635	1
56648	1
56655	5
56668	5
56675	7
56688	7
56695	7
56708	7
56715	1
56728	1
5673explain SELECT * FROM ot1,ot4
5674WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5675FROM it2,it3);
5676id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
56771	PRIMARY	ot1	ALL	NULL	NULL	NULL	NULL	2
56781	PRIMARY	ot4	ALL	NULL	NULL	NULL	NULL	8	Using join buffer (flat, BNL join)
56791	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1
56802	MATERIALIZED	it2	ALL	NULL	NULL	NULL	NULL	4
56812	MATERIALIZED	it3	ALL	NULL	NULL	NULL	NULL	6	Using join buffer (flat, BNL join)
5682DROP TABLE IF EXISTS ot1, ot4, it2, it3;
5683#
5684# Bug#729039: NULL keys used to evaluate subquery
5685#
5686CREATE TABLE t1 (a int) ;
5687INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
5688CREATE TABLE t2 (a int, INDEX idx(a)) ;
5689INSERT INTO t2 VALUES (NULL), (1), (NULL);
5690SELECT * FROM t1
5691WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
5692a
56931
5694EXPLAIN
5695SELECT * FROM t1
5696WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
5697id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
56981	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
56991	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
57002	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	3
5701SELECT * FROM t1
5702WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5703a
57041
5705EXPLAIN
5706SELECT * FROM t1
5707WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5708id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57091	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
57101	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
57112	MATERIALIZED	t2	index	idx	idx	5	NULL	3	Using index
5712DROP TABLE t1,t2;
5713#
5714# BUG#752992: Wrong results for a subquery with 'semijoin=on'
5715#
5716CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5717INSERT INTO t1 VALUES (11,0);
5718INSERT INTO t1 VALUES (12,5);
5719INSERT INTO t1 VALUES (15,0);
5720CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5721INSERT INTO t2 VALUES (11,1);
5722INSERT INTO t2 VALUES (12,2);
5723INSERT INTO t2 VALUES (15,4);
5724SET @save_join_cache_level=@@join_cache_level;
5725SET join_cache_level=0;
5726EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5727id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57281	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	3
57291	PRIMARY	it	eq_ref	PRIMARY	PRIMARY	4	test.t1.pk	1	Using index
57301	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	3	Using index; FirstMatch(it)
5731SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5732pk	i
573311	0
573412	5
573515	0
5736SET join_cache_level=@save_join_cache_level;
5737DROP table t1,t2;
5738#
5739# Bug#751350: crash with pushed condition for outer references when
5740#             there should be none of such conditions
5741#
5742CREATE TABLE t1 (a int, b int) ;
5743INSERT INTO t1 VALUES (0,0),(0,0);
5744set @optimizer_switch_save=@@optimizer_switch;
5745set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
5746EXPLAIN
5747SELECT b FROM t1
5748WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5749GROUP BY b;
5750id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57511	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
57522	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
5753SELECT b FROM t1
5754WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5755GROUP BY b;
5756b
57570
5758set @@optimizer_switch=@optimizer_switch_save;
5759DROP TABLE t1;
5760#
5761# Bug #11765713 58705:
5762# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5763# CREATED BY OPT_SUM_QUERY
5764#
5765CREATE TABLE t1(a INT NOT NULL, KEY (a));
5766INSERT INTO t1 VALUES (0), (1);
5767SELECT 1 as foo FROM t1 WHERE a < SOME
5768(SELECT a FROM t1 WHERE a <=>
5769(SELECT a FROM t1)
5770);
5771ERROR 21000: Subquery returns more than 1 row
5772SELECT 1 as foo FROM t1 WHERE a < SOME
5773(SELECT a FROM t1 WHERE a <=>
5774(SELECT a FROM t1 where a is null)
5775);
5776foo
5777DROP TABLE t1;
5778CREATE TABLE t1 (a int(11), b varchar(1));
5779INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5780SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5781a
57825
5783SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5784a
57855
5786SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5787a
57887
5789SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5790a
57917
5792SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5793a
57945
57957
5796SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5797a
57985
57997
5800SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5801a
58025
58037
5804SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5805a
58065
58077
5808SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5809a
58105
58117
5812SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5813a
58145
58157
5816SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5817a
58185
58197
5820SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5821a
58225
58237
5824SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5825a
5826SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5827a
5828SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5829a
5830SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5831a
5832SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5833a
5834SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5835a
5836SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5837a
5838SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5839a
5840SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5841a
5842SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5843a
5844SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5845a
5846SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5847a
5848delete from t1;
5849INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5850SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5851a
58525
5853SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5854a
58555
5856SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5857a
58587
5859SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5860a
58617
5862SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5863a
58645
58657
5866SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5867a
58685
58697
5870SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5871a
58725
58737
5874SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5875a
58765
58777
5878SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5879a
58805
58817
5882SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5883a
58845
58857
5886SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5887a
58885
58897
5890SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5891a
58925
58937
5894SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5895a
5896SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5897a
5898SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5899a
5900SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5901a
5902SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5903a
5904SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5905a
5906SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5907a
5908SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5909a
5910SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5911a
5912SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5913a
5914SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5915a
5916SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5917a
5918drop table t1;
5919#
5920# Fix of lp:780386 (NULL left part with empty ALL subquery).
5921#
5922CREATE TABLE t1 ( f11 int) ;
5923INSERT IGNORE INTO t1 VALUES (0),(0);
5924CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
5925INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
5926DROP TABLE IF EXISTS t3;
5927Warnings:
5928Note	1051	Unknown table 'test.t3'
5929CREATE TABLE t3 ( f3 int) ;
5930INSERT INTO t3 VALUES (0),(0);
5931SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
5932r
5933NULL
59345
5935NULL
59365
5937DROP TABLE t1, t2, t3;
5938#
5939# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
5940#
5941CREATE TABLE t1(a1 int);
5942INSERT INTO t1 VALUES (1),(2);
5943CREATE TABLE t2(a1 int);
5944INSERT INTO t2 VALUES (3);
5945SELECT @@session.sql_mode INTO @old_sql_mode;
5946SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5947SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
59481
59491
59501
5951SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
59521
59531
59541
5955SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
59561
5957Warnings:
5958Warning	1292	Truncated incorrect DOUBLE value: 'a'
5959SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
59601
59611
59621
5963SET SESSION sql_mode=@old_sql_mode;
5964DROP TABLE t1, t2;
5965create table t2(i int);
5966insert into t2 values(0);
5967SELECT @@session.sql_mode INTO @old_sql_mode;
5968SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5969CREATE VIEW v1 AS
5970SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
5971;
5972CREATE TABLE t1 (
5973pk int NOT NULL,
5974col_varchar_key varchar(1) DEFAULT NULL,
5975PRIMARY KEY (pk),
5976KEY col_varchar_key (col_varchar_key)
5977);
5978SELECT t1.pk
5979FROM t1
5980WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
5981;
5982pk
5983SET SESSION sql_mode=@old_sql_mode;
5984drop table t2, t1;
5985drop view v1;
5986#
5987# BUG#50257: Missing info in REF column of the EXPLAIN
5988#            lines for subselects
5989#
5990CREATE TABLE t1 (a INT, b INT, INDEX (a));
5991INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5992EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
5993id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
59941	SIMPLE	t1	ref	a	a	5	const	1
5995EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
5996id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
59971	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
59982	SUBQUERY	t1	ref	a	a	5	const	1	Using index
5999DROP TABLE t1;
6000#
6001# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6002# (duplicate of LP bug #888456)
6003#
6004CREATE TABLE t1 (f1 varchar(1));
6005INSERT INTO t1 VALUES ('v'),('s');
6006CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6007INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6008('d'),('y'),('t'),('d'),('s');
6009EXPLAIN
6010SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
6011WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
6012WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
6013id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60141	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
60151	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
60162	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6017SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
6018WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
6019WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
6020f1	f1_key
6021s	c
6022s	d
6023s	d
6024s	d
6025s	j
6026s	m
6027s	s
6028s	t
6029s	v
6030s	y
6031v	c
6032v	d
6033v	d
6034v	d
6035v	j
6036v	m
6037v	s
6038v	t
6039v	v
6040v	y
6041DROP TABLE t1,t2;
6042#
6043# LP bug 919427: EXPLAIN for a query over a single-row table
6044#                with IN subquery in WHERE condition
6045#
6046CREATE TABLE ot (
6047col_int_nokey int(11),
6048col_varchar_nokey varchar(1)
6049) ;
6050INSERT INTO ot VALUES (1,'x');
6051CREATE TABLE it1(
6052col_int_key int(11),
6053col_varchar_key varchar(1),
6054KEY idx_cvk_cik (col_varchar_key,col_int_key)
6055);
6056INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
6057CREATE TABLE it2 (
6058col_int_key int(11),
6059col_varchar_key varchar(1),
6060col_varchar_key2 varchar(1),
6061KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
6062KEY idx_cvk_cik (col_varchar_key, col_int_key)
6063);
6064INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
6065EXPLAIN
6066SELECT col_int_nokey FROM ot
6067WHERE col_varchar_nokey IN
6068(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6069id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60701	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60711	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
60722	MATERIALIZED	it1	ref	idx_cvk_cik	idx_cvk_cik	9	const,const	1	Using where; Using index
6073SELECT col_int_nokey FROM ot
6074WHERE col_varchar_nokey IN
6075(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6076col_int_nokey
60771
6078EXPLAIN
6079SELECT col_int_nokey FROM ot
6080WHERE (col_varchar_nokey, 'x') IN
6081(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6082id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60831	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60841	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1
60852	MATERIALIZED	it2	ref	idx_cvk_cvk2_cik,idx_cvk_cik	idx_cvk_cvk2_cik	8	const,const	1	Using where; Using index
6086SELECT col_int_nokey FROM ot
6087WHERE (col_varchar_nokey, 'x') IN
6088(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6089col_int_nokey
60901
6091DROP TABLE ot,it1,it2;
6092#
6093# MDEV-746
6094# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
6095# HAS AN EMPTY RESULT
6096#
6097CREATE TABLE t1 (
6098pk int NOT NULL,
6099col_int_nokey int NOT NULL,
6100col_int_key int NOT NULL,
6101col_time_key time NOT NULL,
6102col_varchar_key varchar(1) NOT NULL,
6103col_varchar_nokey varchar(1) NOT NULL,
6104PRIMARY KEY (pk),
6105KEY col_int_key (col_int_key),
6106KEY col_time_key (col_time_key),
6107KEY col_varchar_key (col_varchar_key,col_int_key)
6108) ENGINE=MyISAM;
6109CREATE TABLE t2 (
6110pk int NOT NULL AUTO_INCREMENT,
6111col_int_nokey int NOT NULL,
6112col_int_key int NOT NULL,
6113col_time_key time NOT NULL,
6114col_varchar_key varchar(1) NOT NULL,
6115col_varchar_nokey varchar(1) NOT NULL,
6116PRIMARY KEY (pk),
6117KEY col_int_key (col_int_key),
6118KEY col_time_key (col_time_key),
6119KEY col_varchar_key (col_varchar_key,col_int_key)
6120) ENGINE=MyISAM;
6121INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
6122SET @var2:=4, @var3:=8;
6123
6124Testcase without inner subquery
6125EXPLAIN SELECT @var3:=12, sq4_alias1.*
6126FROM t1 AS sq4_alias1
6127WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6128sq4_alias1.col_varchar_key = @var3;
6129id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61301	SIMPLE	sq4_alias1	system	NULL	NULL	NULL	NULL	0	Const row not found
6131SELECT @var3:=12, sq4_alias1.*
6132FROM t1 AS sq4_alias1
6133WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6134sq4_alias1.col_varchar_key = @var3;
6135@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6136SELECT @var3;
6137@var3
61388
6139EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6140FROM t1 AS sq4_alias1
6141WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6142sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6143id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61441	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	Const row not found
61452	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
6146SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6147FROM t1 AS sq4_alias1
6148WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6149sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6150@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6151SELECT @var3;
6152@var3
61538
6154
6155Testcase with inner subquery; crashed WL#6095
6156SET @var3=8;
6157EXPLAIN SELECT sq4_alias1.*
6158FROM t1 AS sq4_alias1
6159WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6160NOT IN
6161(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6162c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6163FROM t2 AS c_sq1_alias1
6164WHERE (c_sq1_alias1.col_int_nokey != @var2
6165OR c_sq1_alias1.pk != @var3));
6166id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61671	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
61682	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6169SELECT sq4_alias1.*
6170FROM t1 AS sq4_alias1
6171WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6172NOT IN
6173(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6174c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6175FROM t2 AS c_sq1_alias1
6176WHERE (c_sq1_alias1.col_int_nokey != @var2
6177OR c_sq1_alias1.pk != @var3));
6178pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6179EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
6180FROM t1 AS sq4_alias1
6181WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6182NOT IN
6183(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6184c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6185FROM t2 AS c_sq1_alias1
6186WHERE (c_sq1_alias1.col_int_nokey != @var2
6187OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6188id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61891	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
61903	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6191SELECT * FROM ( SELECT sq4_alias1.*
6192FROM t1 AS sq4_alias1
6193WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6194NOT IN
6195(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6196c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6197FROM t2 AS c_sq1_alias1
6198WHERE (c_sq1_alias1.col_int_nokey != @var2
6199OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6200pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6201DROP TABLE t1,t2;
6202# End of 5.2 tests
6203#
6204# BUG#779885: Crash in eliminate_item_equal with materialization=on in
6205#
6206CREATE TABLE t1 ( f1 int );
6207INSERT INTO t1 VALUES (19), (20);
6208CREATE TABLE t2 ( f10 varchar(32) );
6209INSERT INTO t2 VALUES ('c'),('d');
6210CREATE TABLE t3 ( f10 varchar(32) );
6211INSERT INTO t3 VALUES ('a'),('b');
6212SELECT *
6213FROM t1
6214WHERE
6215( 't' ) IN (
6216SELECT t3.f10
6217FROM t3
6218JOIN t2
6219ON t2.f10 = t3.f10
6220);
6221f1
6222DROP TABLE t1,t2,t3;
6223#
6224# BUG lp:813473: Wrong result with outer join + NOT IN subquery
6225# This bug is a duplicate of Bug#11764086 whose test case is added below
6226#
6227CREATE TABLE t1 (c int) ;
6228INSERT INTO t1 VALUES (5),(6);
6229CREATE TABLE t2 (a int, b int) ;
6230INSERT INTO t2 VALUES (20,9),(20,9);
6231create table t3 (d int, e int);
6232insert into t3 values (2, 9), (3,10);
6233SET @save_optimizer_switch=@@optimizer_switch;
6234SET optimizer_switch='outer_join_with_cache=off';
6235EXPLAIN
6236SELECT t2.b , t1.c
6237FROM t2 LEFT JOIN t1 ON t1.c < 3
6238WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
6239id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62401	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2
62411	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
62422	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
6243SELECT t2.b , t1.c
6244FROM t2 LEFT JOIN t1 ON t1.c < 3
6245WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
6246b	c
62479	NULL
62489	NULL
6249SET optimizer_switch=@save_optimizer_switch;
6250drop table t1, t2, t3;
6251#
6252# BUG#50257: Missing info in REF column of the EXPLAIN
6253#            lines for subselects
6254#
6255CREATE TABLE t1 (a INT, b INT, INDEX (a));
6256INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
6257
6258set @tmp_optimizer_switch=@@optimizer_switch;
6259set optimizer_switch='derived_merge=off,derived_with_keys=off';
6260EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6261id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62621	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2
62632	DERIVED	t1	ref	a	a	5	const	1
6264set optimizer_switch=@tmp_optimizer_switch;
6265
6266EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6267id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62681	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
62692	SUBQUERY	t1	ref	a	a	5	const	1	Using index
6270
6271DROP TABLE t1;
6272#
6273# Bug#11764086: Null left operand to NOT IN in WHERE clause
6274# behaves differently than real NULL
6275#
6276CREATE TABLE parent (id int);
6277INSERT INTO parent VALUES (1), (2);
6278CREATE TABLE child (parent_id int, other int);
6279INSERT INTO child VALUES (1,NULL);
6280# Offending query (c.parent_id is NULL for null-complemented rows only)
6281SELECT    p.id, c.parent_id
6282FROM      parent p
6283LEFT JOIN child  c
6284ON        p.id = c.parent_id
6285WHERE     c.parent_id NOT IN (
6286SELECT parent_id
6287FROM   child
6288WHERE  parent_id = 3
6289);
6290id	parent_id
62911	1
62922	NULL
6293# Some syntactic variations with IS FALSE and IS NOT TRUE
6294SELECT    p.id, c.parent_id
6295FROM      parent p
6296LEFT JOIN child  c
6297ON        p.id = c.parent_id
6298WHERE     c.parent_id IN (
6299SELECT parent_id
6300FROM   child
6301WHERE  parent_id = 3
6302) IS NOT TRUE;
6303id	parent_id
63041	1
63052	NULL
6306SELECT    p.id, c.parent_id
6307FROM      parent p
6308LEFT JOIN child  c
6309ON        p.id = c.parent_id
6310WHERE     c.parent_id IN (
6311SELECT parent_id
6312FROM   child
6313WHERE  parent_id = 3
6314) IS FALSE;
6315id	parent_id
63161	1
63172	NULL
6318DROP TABLE parent, child;
6319# End of test for bug#11764086.
6320#
6321# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
6322#                BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
6323#
6324CREATE TABLE t1(a INT);
6325INSERT INTO t1 VALUES (0), (1);
6326CREATE TABLE t2(
6327b TEXT,
6328c INT,
6329PRIMARY KEY (b(1))
6330);
6331INSERT INTO t2 VALUES ('a', 2), ('b', 3);
6332SELECT 1 FROM t1 WHERE a =
6333(SELECT 1 FROM t2 WHERE b =
6334(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6335ORDER BY b
6336);
63371
6338Warnings:
6339Warning	1292	Truncated incorrect DOUBLE value: 'a'
6340Warning	1292	Truncated incorrect DOUBLE value: 'b'
6341Warning	1292	Truncated incorrect DOUBLE value: 'a'
6342Warning	1292	Truncated incorrect DOUBLE value: 'b'
6343SELECT 1 FROM t1 WHERE a =
6344(SELECT 1 FROM t2 WHERE b =
6345(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6346GROUP BY b
6347);
63481
6349Warnings:
6350Warning	1292	Truncated incorrect DOUBLE value: 'a'
6351Warning	1292	Truncated incorrect DOUBLE value: 'b'
6352Warning	1292	Truncated incorrect DOUBLE value: 'a'
6353Warning	1292	Truncated incorrect DOUBLE value: 'b'
6354DROP TABLE t1, t2;
6355#
6356# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6357#
6358CREATE TABLE t1 (f1 varchar(1));
6359INSERT INTO t1 VALUES ('v'),('s');
6360CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6361INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6362('d'),('y'),('t'),('d'),('s');
6363SELECT table1.f1, table2.f1_key
6364FROM t1 AS table1, t2 AS table2
6365WHERE EXISTS
6366(
6367SELECT DISTINCT f1_key
6368FROM t2
6369WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6370f1	f1_key
6371s	c
6372s	d
6373s	d
6374s	d
6375s	j
6376s	m
6377s	s
6378s	t
6379s	v
6380s	y
6381v	c
6382v	d
6383v	d
6384v	d
6385v	j
6386v	m
6387v	s
6388v	t
6389v	v
6390v	y
6391explain SELECT table1.f1, table2.f1_key
6392FROM t1 AS table1, t2 AS table2
6393WHERE EXISTS
6394(
6395SELECT DISTINCT f1_key
6396FROM t2
6397WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6398id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
63991	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
64001	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
64012	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6402DROP TABLE t1,t2;
6403#
6404# lp:826279: assertion failure with GROUP BY a result of subquery
6405#
6406CREATE TABLE t1 (a int);
6407INSERT INTO t1 VALUES (0), (0);
6408CREATE TABLE t2 (a int, b int, c int);
6409INSERT INTO t2 VALUES (10,7,0), (0,7,0);
6410CREATE TABLE t3 (a int, b int);
6411INSERT INTO t3 VALUES (10,7), (0,7);
6412SELECT SUM(DISTINCT b),
6413(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6414WHERE t.a != 0 AND t2.a != 0)
6415FROM (SELECT * FROM t3) AS t
6416GROUP BY 2;
6417SUM(DISTINCT b)	(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6418WHERE t.a != 0 AND t2.a != 0)
64197	NULL
6420SELECT SUM(DISTINCT b),
6421(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
6422FROM (SELECT * FROM t3) AS t
6423GROUP BY 2;
6424SUM(DISTINCT b)	(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
64257	NULL
64267	10
6427DROP TABLE t1,t2,t3;
6428#
6429# Bug#12329653
6430# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
6431#
6432CREATE TABLE t1(a1 int);
6433INSERT INTO t1 VALUES (1),(2);
6434SELECT @@session.sql_mode INTO @old_sql_mode;
6435SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
6436SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
64371
64381
64391
6440PREPARE stmt FROM
6441'SELECT 1 UNION ALL
6442SELECT 1 FROM t1
6443ORDER BY
6444(SELECT 1 FROM t1 AS t1_0
6445  WHERE 1 < SOME (SELECT a1 FROM t1)
6446)' ;
6447EXECUTE stmt ;
6448ERROR 21000: Subquery returns more than 1 row
6449EXECUTE stmt ;
6450ERROR 21000: Subquery returns more than 1 row
6451SET SESSION sql_mode=@old_sql_mode;
6452DEALLOCATE PREPARE stmt;
6453DROP TABLE t1;
6454#
6455# LP BUG#833777 Performance regression with deeply nested subqueries
6456#
6457create table t1 (a int not null, b char(10) not null);
6458insert into t1 values (1, 'a');
6459set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
6460select 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)))))))))))))))))))))))))))));
6461a
64621
6463set @@optimizer_switch=@subselect_tmp;
6464drop table t1;
6465#
6466# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
6467#
6468CREATE TABLE t1 (a varchar(3));
6469INSERT INTO t1 VALUES ('AAA'),('BBB');
6470CREATE TABLE t2 (a varchar(3));
6471INSERT INTO t2 VALUES ('CCC');
6472set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6473SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
6474a
6475set @@optimizer_switch=@subselect_tmp;
6476drop table t1, t2;
6477#
6478# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
6479# with view , UNION and prepared statement (rewriting fake_select
6480# condition).
6481#
6482CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
6483INSERT INTO t1 VALUES (6,'d'),(7,'y');
6484CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
6485INSERT INTO t2 VALUES (10,7);
6486CREATE VIEW v2 AS SELECT * FROM t2;
6487PREPARE st1 FROM "
6488        SELECT *
6489        FROM t1
6490        LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
6491        WHERE v2.f1 NOT IN (
6492                SELECT 1 UNION
6493                SELECT 247
6494        )
6495";
6496EXECUTE st1;
6497f1	f4	f1	f2
64987	y	10	7
6499deallocate prepare st1;
6500DROP VIEW v2;
6501DROP TABLE t1,t2;
6502#
6503# LP bug #887458 Crash in subselect_union_engine::no_rows with
6504# double UNION and join_cache_level=3,8
6505# (IN/ALL/ANY optimizations should not be applied to fake_select)
6506CREATE TABLE t2 ( a int, b varchar(1)) ;
6507INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
6508CREATE TABLE t1 ( b varchar(1)) ;
6509INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
6510set @save_join_cache_level=@@join_cache_level;
6511SET SESSION join_cache_level=3;
6512SELECT *
6513FROM t1, t2
6514WHERE t2.b IN (
6515SELECT 'm' UNION
6516SELECT 'm'
6517) OR t1.b <> SOME (
6518SELECT 'v' UNION
6519SELECT 't'
6520);
6521b	a	b
6522set @@join_cache_level= @save_join_cache_level;
6523drop table t1,t2;
6524#
6525# LP bug #885162 Got error 124 from storage engine with UNION inside
6526# subquery and join_cache_level=3..8
6527# (IN/ALL/ANY optimizations should not be applied to fake_select)
6528#
6529CREATE TABLE t1 (
6530f1 varchar(1) DEFAULT NULL
6531);
6532INSERT INTO t1 VALUES ('c');
6533set @save_join_cache_level=@@join_cache_level;
6534SET SESSION join_cache_level=8;
6535SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
6536f1
6537set @@join_cache_level= @save_join_cache_level;
6538drop table t1;
6539#
6540# LP BUG#747278 incorrect values of the NULL (no rows) single
6541# row subquery requested via element_index() interface
6542#
6543CREATE TABLE t1 (f1a int, f1b int) ;
6544INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
6545CREATE TABLE t2 ( f2 int);
6546INSERT IGNORE INTO t2 VALUES (3),(4);
6547CREATE TABLE t3 (f3a int default 1, f3b int default 2);
6548INSERT INTO t3 VALUES (1,1),(2,2);
6549set @old_optimizer_switch = @@session.optimizer_switch;
6550set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
6551SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6552(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6553NULL
6554NULL
6555SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6556(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6557NULL
6558NULL
6559SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6560(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6561NULL
6562NULL
6563SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6564(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6565NULL
6566SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6567(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6568NULL
6569NULL
6570SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6571(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6572NULL
6573NULL
6574SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6575(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6576NULL
6577NULL
6578SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6579(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6580NULL
6581set @@session.optimizer_switch=@old_optimizer_switch;
6582SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6583(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6584NULL
6585NULL
6586SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6587(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6588NULL
6589NULL
6590SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6591(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6592NULL
6593NULL
6594SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6595(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6596NULL
6597SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6598(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6599NULL
6600NULL
6601SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6602(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6603NULL
6604NULL
6605SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6606(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6607NULL
6608NULL
6609SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6610(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6611NULL
6612select (null, null) = (null, null);
6613(null, null) = (null, null)
6614NULL
6615SELECT (SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0);
6616(SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0)
6617NULL
6618drop tables t1,t2,t3;
6619#
6620# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
6621#
6622CREATE TABLE t1 (a date, KEY (a)) ;
6623INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
6624set @old_optimizer_switch = @@optimizer_switch;
6625SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6626EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6627id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66281	PRIMARY	t1	index	NULL	a	4	NULL	2	Using where; Using index
66292	DEPENDENT SUBQUERY	t1	index_subquery	a	a	4	func	2	Using index
6630SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6631a
66322009-01-01
66332009-02-02
6634SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
6635EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6636id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66371	PRIMARY	t1	index	NULL	a	4	NULL	2	Using where; Using index
66382	MATERIALIZED	t1	index	NULL	a	4	NULL	2	Using index
6639SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6640a
66412009-01-01
66422009-02-02
6643set @@optimizer_switch=@old_optimizer_switch;
6644drop table t1;
6645#
6646# LP BUG#908269 incorrect condition in case of subqueries depending
6647# on constant tables
6648#
6649CREATE TABLE t1 ( a INT );
6650INSERT INTO t1 VALUES (1),(5);
6651CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
6652INSERT INTO t2 VALUES (1);
6653CREATE TABLE t3 ( c INT );
6654INSERT INTO t3 VALUES (4),(5);
6655SET optimizer_switch='subquery_cache=off';
6656SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
6657( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
66581
6659NULL
6660SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
6661( SELECT b FROM t2 WHERE b = a OR b * 0)
66621
6663NULL
6664SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
6665( SELECT b FROM t2 WHERE b = a OR rand() * 0)
66661
6667NULL
6668drop table t1,t2,t3;
6669set optimizer_switch=@subselect_tmp;
6670#
6671# LP BUG#905353 Wrong non-empty result with a constant table,
6672# aggregate function in subquery, MyISAM or Aria
6673#
6674CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
6675INSERT INTO t1 VALUES (1);
6676SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
6677a
6678drop table t1;
6679#
6680# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in
6681# main query and implicit grouping
6682#
6683CREATE TABLE t1 (f1 int) engine=MyISAM;
6684INSERT INTO t1 VALUES (7),(8);
6685CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
6686INSERT INTO t2 VALUES (3,'f');
6687EXPLAIN
6688SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6689id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66901	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
66912	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6692SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6693COUNT(f1)	f4
66940	NULL
6695EXPLAIN
6696SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6697id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66981	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
66992	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6700SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6701COUNT(f1)	f4
67020	0
6703EXPLAIN
6704SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6705id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67061	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67072	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6708SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6709COUNT(f1)	f4
67100	1
6711EXPLAIN
6712SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6713id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67141	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67152	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6716SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6717COUNT(f1)	f4
67180	0
6719drop table t1,t2;
6720#
6721# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
6722# HAVING, UNION in subquery
6723#
6724CREATE TABLE t1 (a INT);
6725INSERT INTO t1 VALUES (7),(0);
6726CREATE TABLE t2 (b INT);
6727EXPLAIN
6728SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6729id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67301	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	Const row not found
67311	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
67322	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67333	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6734NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6735SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6736f1	f2
6737drop table t1,t2;
6738#
6739# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
6740# WHERE and UNION in HAVING
6741#
6742CREATE TABLE t1 (a INT);
6743INSERT INTO t1 VALUES (1),(7);
6744EXPLAIN
6745SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6746id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67471	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
67482	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67493	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6750NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6751SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6752min_a	a
6753EXPLAIN
6754SELECT 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;
6755id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67561	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
67572	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
67583	DEPENDENT UNION	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6759NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6760SELECT 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;
6761min_a	a
6762drop table t1;
6763#
6764# MDEV-367: Different results with and without subquery_cache on
6765# a query with a constant NOT IN condition
6766#
6767CREATE TABLE t1 (a INT) ENGINE=MyISAM;
6768INSERT INTO t1 VALUES (1),(2),(3);
6769set @mdev367_optimizer_switch = @@optimizer_switch;
6770set optimizer_switch = 'subquery_cache=on';
6771SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6772a
6773SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6774a	( 3, 3 ) NOT IN ( SELECT NULL, NULL )
67751	NULL
67762	NULL
67773	NULL
6778set optimizer_switch=@mdev367_optimizer_switch;
6779set optimizer_switch = 'subquery_cache=off';
6780SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6781a
6782SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6783a	( 3, 3 ) NOT IN ( SELECT NULL, NULL )
67841	NULL
67852	NULL
67863	NULL
6787set optimizer_switch=@mdev367_optimizer_switch;
6788DROP TABLE t1;
6789#
6790# MDEV-521 single value subselect transformation problem
6791#
6792CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
6793INSERT INTO t1 VALUES ('u1'),('u2');
6794SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6795f1
6796u1
6797u2
6798FLUSH TABLES;
6799SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6800f1
6801u1
6802u2
6803DROP TABLE t1;
6804# return optimizer switch changed in the beginning of this test
6805set optimizer_switch=@subselect_tmp;
6806#
6807# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
6808# precomputed and thus not part of optimization
6809#
6810CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
6811INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
6812('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
6813EXPLAIN
6814SELECT MAX( alias2.a ) AS field
6815FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6816WHERE alias1.a = alias2.a OR alias1.a = 'y'
6817HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6818id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68191	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible HAVING
68202	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6821SELECT MAX( alias2.a ) AS field
6822FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6823WHERE alias1.a = alias2.a OR alias1.a = 'y'
6824HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6825field
6826EXPLAIN
6827SELECT MAX( alias2.a )
6828FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6829WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6830id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68311	PRIMARY	alias1	index	a	a	19	NULL	11	Using where; Using index
68321	PRIMARY	alias2	ref	a	a	19	test.alias1.a	2	Using index
68331	PRIMARY	alias3	index	NULL	a	19	NULL	11	Using index; Using join buffer (flat, BNL join)
68342	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6835SELECT MAX( alias2.a )
6836FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6837WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6838MAX( alias2.a )
6839Arden-Arcade
6840drop table t1;
6841#
6842# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
6843# with semijoin+materialization, IN and = subqueries
6844#
6845CREATE TABLE t1 (a1 INT);
6846INSERT INTO t1 VALUES (4),(6);
6847CREATE TABLE t2 (b1 INT);
6848INSERT INTO t2 VALUES (1),(7);
6849EXPLAIN
6850SELECT * FROM t1
6851WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6852id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68531	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68542	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
68552	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1
68563	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2
68573	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
6858SELECT * FROM t1
6859WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6860a1
6861drop table t1, t2;
6862#
6863# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
6864# inner joins takes hundreds times longer
6865#
6866CREATE TABLE t1 (a INT);
6867INSERT INTO t1 VALUES (1),(7);
6868CREATE TABLE t2 (b INT);
6869INSERT INTO t2 VALUES (4),(5);
6870CREATE TABLE t3 (c INT);
6871INSERT INTO t3 VALUES (8),(3);
6872set @@expensive_subquery_limit= 0;
6873EXPLAIN
6874SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6875FROM t2 alias1, t1 alias2, t1 alias3;
6876id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68771	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
68781	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68791	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68802	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68812	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
68823	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6883flush status;
6884SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6885FROM t2 alias1, t1 alias2, t1 alias3;
6886(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6887NULL
6888NULL
6889NULL
6890NULL
6891NULL
6892NULL
6893NULL
6894NULL
6895show status like "subquery_cache%";
6896Variable_name	Value
6897Subquery_cache_hit	0
6898Subquery_cache_miss	0
6899show status like '%Handler_read%';
6900Variable_name	Value
6901Handler_read_first	0
6902Handler_read_key	0
6903Handler_read_last	0
6904Handler_read_next	0
6905Handler_read_prev	0
6906Handler_read_retry	0
6907Handler_read_rnd	0
6908Handler_read_rnd_deleted	0
6909Handler_read_rnd_next	58
6910set @@expensive_subquery_limit= default;
6911EXPLAIN
6912SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6913FROM t2 alias1, t1 alias2, t1 alias3;
6914id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69151	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
69161	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69171	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69182	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
69192	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
69203	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6921flush status;
6922SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6923FROM t2 alias1, t1 alias2, t1 alias3;
6924(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6925NULL
6926NULL
6927NULL
6928NULL
6929NULL
6930NULL
6931NULL
6932NULL
6933show status like "subquery_cache%";
6934Variable_name	Value
6935Subquery_cache_hit	0
6936Subquery_cache_miss	0
6937show status like '%Handler_read%';
6938Variable_name	Value
6939Handler_read_first	0
6940Handler_read_key	0
6941Handler_read_last	0
6942Handler_read_next	0
6943Handler_read_prev	0
6944Handler_read_retry	0
6945Handler_read_rnd	0
6946Handler_read_rnd_deleted	0
6947Handler_read_rnd_next	16
6948drop table t1, t2, t3;
6949#
6950# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
6951#
6952CREATE TABLE t1 (a INT);
6953INSERT INTO t1 VALUES (0),(8);
6954CREATE TABLE t2 (b INT PRIMARY KEY);
6955INSERT INTO t2 VALUES (1),(2);
6956EXPLAIN
6957SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6958id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69591	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
69602	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
69613	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6962SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6963a
6964drop table t1,t2;
6965#
6966# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
6967#
6968CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
6969INSERT INTO t1 VALUES ('USA');
6970CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
6971INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
6972CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
6973INSERT INTO t3 VALUES (12),(22),(9),(45);
6974create table t4 like t3;
6975insert into t4 select * from t3;
6976# This should not show range access for table t2
6977explain
6978SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6979WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6980id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69811	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1
69821	PRIMARY	t2	ALL	b	NULL	NULL	NULL	2	Using where
69831	PRIMARY	t3	ref	d	d	5	test.t2.b	2	Using index
69843	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
6985set @tmp_mdev410=@@global.userstat;
6986set global userstat=on;
6987flush table_statistics;
6988flush index_statistics;
6989SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6990WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6991MIN(b)
6992NULL
6993# The following shows that t2 was indeed scanned with a full scan.
6994show table_statistics;
6995Table_schema	Table_name	Rows_read	Rows_changed	Rows_changed_x_#indexes
6996test	t1	2	0	0
6997test	t2	3	0	0
6998show index_statistics;
6999Table_schema	Table_name	Index_name	Rows_read
7000test	t2	b	1
7001set global userstat=@tmp_mdev410;
7002DROP TABLE t1,t2,t3,t4;
7003#
7004# MDEV-430: Server crashes in select_describe on EXPLAIN with
7005#    materialization+semijoin, 2 nested subqueries, aggregate functions
7006#
7007CREATE TABLE t1 (a INT, KEY(a));
7008INSERT INTO t1 VALUES (1),(8);
7009CREATE TABLE t2 (b INT, KEY(b));
7010INSERT INTO t2 VALUES (45),(17),(20);
7011EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7012id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70131	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70142	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70152	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7016DROP TABLE t1,t2;
7017#
7018# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
7019#
7020CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
7021INSERT INTO t1 VALUES (8),(0);
7022CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
7023INSERT INTO t2 VALUES (4,'j'),(6,'v');
7024CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
7025INSERT INTO t3 VALUES ('b'),('c');
7026EXPLAIN
7027SELECT * FROM t1
7028WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7029id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70301	PRIMARY	t1	index	a	a	5	NULL	2	Using where; Using index
70312	SUBQUERY	<subquery3>	ALL	distinct_key	NULL	NULL	NULL	1
70322	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
70333	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	2
7034SELECT * FROM t1
7035WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7036a
7037drop table t1, t2, t3;
7038#
7039# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
7040#
7041CREATE TABLE t1 (a INT, KEY(a));
7042INSERT INTO t1 VALUES (1),(8);
7043CREATE TABLE t2 (b INT, KEY(b));
7044INSERT INTO t2 VALUES (45),(17),(20);
7045EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7046id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70471	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70482	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70492	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7050DROP TABLE t1,t2;
7051#
7052# MDEV-5991: crash in Item_field::used_tables
7053#
7054create table t1 (c int);
7055select exists(select 1 from t1 group by `c` in (select `c` from t1));
7056exists(select 1 from t1 group by `c` in (select `c` from t1))
70570
7058drop table t1;
7059#
7060# MDEV-7565: Server crash with Signal 6 (part 2)
7061#
7062Select
7063(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7064Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7065) As `ControlRev`
7066From
7067(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7068Group By  TestCase.Revenue, TestCase.TemplateID;
7069ControlRev
7070NULL
7071#
7072# MDEV-7445:Server crash with Signal 6
7073#
7074CREATE PROCEDURE procedure2()
7075BEGIN
7076Select
7077(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7078Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7079) As `ControlRev`
7080  From
7081(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7082Group By  TestCase.Revenue, TestCase.TemplateID;
7083END |
7084call procedure2();
7085ControlRev
7086NULL
7087call procedure2();
7088ControlRev
7089NULL
7090drop procedure procedure2;
7091SELECT
7092(SELECT user FROM mysql.user
7093WHERE h.host in (SELECT host FROM mysql.user)
7094) AS sq
7095FROM mysql.host h GROUP BY h.host;
7096sq
7097#
7098# MDEV-7846:Server crashes in Item_subselect::fix
7099#_fields or fails with Thread stack overrun
7100#
7101CREATE TABLE t1 (column1 INT) ENGINE=MyISAM;
7102INSERT INTO t1 VALUES (3),(9);
7103CREATE TABLE t2 (column2 INT) ENGINE=MyISAM;
7104INSERT INTO t2 VALUES (1),(4);
7105CREATE TABLE t3 (column3 INT) ENGINE=MyISAM;
7106INSERT INTO t3 VALUES (6),(8);
7107CREATE TABLE t4 (column4 INT) ENGINE=MyISAM;
7108INSERT INTO t4 VALUES (2),(5);
7109PREPARE stmt FROM "
7110SELECT (
7111  SELECT MAX( table1.column1 ) AS field1
7112  FROM t1 AS table1
7113  WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 )
7114) AS sq
7115FROM t3 AS table3, t4 AS table4 GROUP BY sq
7116";
7117EXECUTE stmt;
7118sq
7119NULL
7120EXECUTE stmt;
7121sq
7122NULL
7123deallocate prepare stmt;
7124drop table t1,t2,t3,t4;
7125#
7126# MDEV-7122
7127# Assertion `0' failed in subselect_hash_sj_engine::init
7128#
7129SET SESSION big_tables=1;
7130CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
7131INSERT INTO t1 VALUES(0),(0),(0);
7132SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1);
7133a
71340
71350
71360
7137DROP TABLE t1;
7138SET SESSION big_tables=0;
7139#
7140# MDEV-10776: Server crash on query
7141#
7142create table t1 (field1 int);
7143insert into t1 values (1);
7144select round((select 1 from t1 limit 1))
7145from t1
7146group by round((select 1 from t1 limit 1));
7147round((select 1 from t1 limit 1))
71481
7149drop table t1;
7150#
7151# MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE ||
7152# m_lock_type != 2' failed in  handler::ha_index_read_map
7153#
7154CREATE TABLE t1 (f1 INT);
7155INSERT INTO t1 VALUES (1),(2);
7156CREATE TABLE t2 (f2 INT, KEY(f2));
7157INSERT INTO t2 VALUES (3);
7158CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2;
7159SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7160ERROR 42000: Can't group on 'sq'
7161SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7162ERROR 42000: Can't group on 'sq'
7163SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0;
7164f2
71653
7166SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 );
7167count(*)
71681
7169delete from t1;
7170SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7171ERROR 42000: Can't group on 'sq'
7172SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7173ERROR 42000: Can't group on 'sq'
7174drop view v2;
7175drop table t1,t2;
7176#
7177# MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*)
7178#
7179CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL);
7180INSERT INTO t1 VALUES ('foo','bar');
7181SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 );
7182f1	f2
7183SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 );
7184f1	f2
7185foo	bar
7186DROP TABLE t1;
7187#
7188# MDEV-10146: Wrong result (or questionable result and behavior)
7189# with aggregate function in uncorrelated SELECT subquery
7190#
7191CREATE TABLE t1 (f1 INT);
7192CREATE VIEW v1 AS SELECT * FROM t1;
7193INSERT INTO t1 VALUES (1),(2);
7194CREATE TABLE t2 (f2 int);
7195INSERT INTO t2 VALUES (3);
7196SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7197( SELECT MAX(f1) FROM t2 )
71982
7199SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7200( SELECT MAX(f1) FROM t2 )
72012
7202INSERT INTO t2 VALUES (4);
7203SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7204ERROR 21000: Subquery returns more than 1 row
7205SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7206ERROR 21000: Subquery returns more than 1 row
7207drop view v1;
7208drop table t1,t2;
7209CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM;
7210INSERT t1 VALUES (4),(8);
7211CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM;
7212INSERT t2 VALUES (6);
7213SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7214(SELECT MAX(sq.f2) FROM t1)
7215NULL
7216#
7217# Disable this query till MDEV-13399 is resolved
7218#
7219# INSERT t2 VALUES (9);
7220# --error ER_SUBQUERY_NO_1_ROW
7221# SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7222#
7223drop table t1, t2;
7224#
7225# MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in
7226# (5.5 test)
7227#
7228SET @optimiser_switch_save= @@optimizer_switch;
7229CREATE TABLE t1 (a INT NOT NULL);
7230INSERT INTO t1 VALUES (1),(1),(1),(5),(5);
7231CREATE TABLE t2 (b INT);
7232INSERT INTO t2 VALUES (5),(1);
7233CREATE TABLE t3 (c INT, KEY(c));
7234INSERT INTO t3 VALUES (5),(5);
7235SET optimizer_switch='semijoin=on';
7236select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7237and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7238a
72395
72405
7241SET optimizer_switch='semijoin=off';
7242select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7243and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7244a
72455
72465
7247SET @@optimizer_switch= @optimiser_switch_save;
7248DROP TABLE t1, t2, t3;
7249#
7250# MDEV-16820: impossible where with inexpensive subquery
7251#
7252create table t1 (a int) engine=myisam;
7253insert into t1 values (3), (1), (7);
7254create table t2 (b int, index idx(b));
7255insert into t2 values (2), (5), (3), (2);
7256explain select * from t1 where (select max(b) from t2) = 10;
7257id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72581	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72592	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7260explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3;
7261id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72621	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72632	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7264drop table t1,t2;
7265#
7266# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7267#
7268create table t10 (a int);
7269insert into t10 values (1),(2),(3);
7270create table t12 (a int);
7271insert into t12 values (1),(2),(3);
7272select * from t10 where exists (select * from t12 order by a limit 0);
7273a
7274explain select * from t10 where exists (select * from t12 order by a limit 0);
7275id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72761	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72772	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Zero limit
7278prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7279set @l=1;
7280execute stmt1 using @l;
7281a
72821
72832
72843
7285set @l=2;
7286execute stmt1 using @l;
7287a
72881
72892
72903
7291set @l=0;
7292execute stmt1 using @l;
7293a
7294deallocate prepare stmt1;
7295drop table t10, t12;
7296End of 5.5 tests
7297# End of 10.0 tests
7298#
7299# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7300# with UNION in ALL subquery
7301#
7302SET NAMES utf8;
7303CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7304INSERT INTO t1 VALUES ('foo');
7305SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7306f
7307foo
7308SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar');
7309f
7310foo
7311drop table t1;
7312SET NAMES default;
7313#
7314# MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops
7315#
7316SET NAMES utf8;
7317CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM;
7318CREATE TABLE t2 (f3 INT) ENGINE=MyISAM;
7319SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' );
7320f1	f2	f3
7321SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo');
7322f1	f2	f3
7323DROP TABLE t1, t2;
7324SET NAMES default;
7325# End of 10.1 tests
7326#
7327# MDEV-12564: IN TO EXISTS transformation for rows after
7328#             conversion an outer join to inner join
7329#
7330CREATE TABLE t (
7331pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
7332) ENGINE=MyISAM;
7333INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
7334SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
7335WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
7336pk	i	c	pk	i	c
73371	10	foo	1	10	foo
7338DROP TABLE t;
7339#
7340# MDEV-25002: Outer reference in ON clause of subselect
7341#
7342create table t1 (
7343pk int primary key,
7344a int
7345) engine=myisam;
7346insert into t1 values (1,1), (2,2);
7347create table t2 (
7348pk int primary key,
7349b int
7350) engine=myisam;
7351insert into t2 values (1,1), (2,3);
7352create table t3 (a int);
7353insert into t3 values (1),(2);
7354select a,
7355(select count(*) from t1, t2
7356where t2.pk=t3.a and t1.pk=1) as sq
7357from t3;
7358a	sq
73591	1
73602	1
7361select a,
7362(select count(*) from t1 join t2 on t2.pk=t3.a
7363where t1.pk=1) as sq
7364from t3;
7365a	sq
73661	1
73672	1
7368select a from t3
7369where a in (select t2.b from t1,t2 where t2.pk=t3.a and t1.pk=1);
7370a
73711
7372select a from t3
7373where a in (select t2.b from t1 join t2 on t2.pk=t3.a where t1.pk=1);
7374a
73751
7376drop table t1,t2,t3;
7377# End of 10.2 tests
7378set optimizer_switch=default;
7379select @@optimizer_switch like '%subquery_cache=on%';
7380@@optimizer_switch like '%subquery_cache=on%'
73811
7382set @join_cache_level_for_subselect_test=NULL;
7383