1set @optimizer_switch_for_subselect_test='semijoin=off,mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
2set join_cache_level=1;
3set @join_cache_level_for_subselect_test=@@join_cache_level;
4drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
5drop view if exists v2;
6call mtr.add_suppression("Sort aborted.*");
7set @subselect_tmp=@@optimizer_switch;
8set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
9"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
10set join_cache_level=@join_cache_level_for_subselect_test;
11SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
12SET optimizer_use_condition_selectivity=4;
13select (select 2);
14(select 2)
152
16explain extended select (select 2);
17id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
181	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
19Warnings:
20Note	1249	Select 2 was reduced during optimization
21Note	1003	select 2 AS `(select 2)`
22SELECT (SELECT 1) UNION SELECT (SELECT 2);
23(SELECT 1)
241
252
26explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
27id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
281	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
293	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
30NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
31Warnings:
32Note	1249	Select 2 was reduced during optimization
33Note	1249	Select 4 was reduced during optimization
34Note	1003	/* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)`
35SELECT (SELECT (SELECT 0 UNION SELECT 0));
36(SELECT (SELECT 0 UNION SELECT 0))
370
38explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
39id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
401	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
413	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
424	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
43NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
44Warnings:
45Note	1249	Select 2 was reduced during optimization
46Note	1003	/* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
47SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
48ERROR 42S22: Reference 'a' not supported (forward reference in item list)
49SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
50ERROR 42S22: Reference 'b' not supported (forward reference in item list)
51SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
52(SELECT 1)	MAX(1)
531	1
54SELECT (SELECT a) as a;
55ERROR 42S22: Reference 'a' not supported (forward reference in item list)
56EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
57id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
581	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	1	100.00
593	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
602	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
61Warnings:
62Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
63Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
64Note	1003	/* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1
65SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
661
671
68SELECT (SELECT 1), a;
69ERROR 42S22: Unknown column 'a' in 'field list'
70SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
71a
721
73SELECT 1 FROM (SELECT (SELECT a) b) c;
74ERROR 42S22: Unknown column 'a' in 'field list'
75SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
76id
771
78SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
79ERROR 21000: Operand should contain 1 column(s)
80SELECT 1 IN (SELECT 1);
811 IN (SELECT 1)
821
83SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
841
851
86select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
87ERROR 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
88SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
89ERROR 42000: PROCEDURE does not support subqueries or stored functions
90SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
91ERROR 42S22: Unknown column 'a' in 'field list'
92SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
93ERROR 42S22: Unknown column 'a' in 'field list'
94SELECT (SELECT 1,2,3) = ROW(1,2,3);
95(SELECT 1,2,3) = ROW(1,2,3)
961
97SELECT (SELECT 1,2,3) = ROW(1,2,1);
98(SELECT 1,2,3) = ROW(1,2,1)
990
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)
1051
106SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
107(SELECT 1,2,3) = ROW(1,2,NULL)
108NULL
109SELECT ROW(1,2,3) = (SELECT 1,2,3);
110ROW(1,2,3) = (SELECT 1,2,3)
1111
112SELECT ROW(1,2,3) = (SELECT 1,2,1);
113ROW(1,2,3) = (SELECT 1,2,1)
1140
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)
1201
121SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
122ROW(1,2,3) = (SELECT 1,2,NULL)
123NULL
124SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
125(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
1261
127SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
128(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
1290
130SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
131(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
1320
133Warnings:
134Warning	1292	Truncated incorrect DOUBLE value: '1.5b'
135SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
136(SELECT 'b',2,'a') = ROW(1.5,2,'a')
1370
138Warnings:
139Warning	1292	Truncated incorrect DOUBLE value: 'b'
140SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
141(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
1421
143SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
144(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
1450
146Warnings:
147Warning	1292	Truncated incorrect DOUBLE value: 'c'
148SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
149ERROR 21000: Operand should contain 1 column(s)
150SELECT 1 as a,(SELECT a+a) b,(SELECT b);
151a	b	(SELECT b)
1521	2	2
153create table t1 (a int);
154create table t2 (a int, b int);
155create table t3 (a int);
156create table t4 (a int not null, b int not null);
157insert into t1 values (2);
158insert into t2 values (1,7),(2,7);
159insert into t4 values (4,8),(3,8),(5,9);
160select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
161ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
162select (select a from t1 where t1.a=t2.a), a from t2;
163(select a from t1 where t1.a=t2.a)	a
164NULL	1
1652	2
166select (select a from t1 where t1.a=t2.b), a from t2;
167(select a from t1 where t1.a=t2.b)	a
168NULL	1
169NULL	2
170select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
171(select a from t1)	a	(select 1 union select 2 limit 1)
1722	1	1
1732	2	1
174select (select a from t3), a from t2;
175(select a from t3)	a
176NULL	1
177NULL	2
178select * from t2 where t2.a=(select a from t1);
179a	b
1802	7
181insert into t3 values (6),(7),(3);
182select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
183a	b
1841	7
1852	7
186(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1))
187union (select * from t4 order by a limit 2) order by a limit 3;
188a	b
1891	7
1902	7
1913	8
192(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);
193a	b
1941	7
1952	7
1964	8
1973	8
198explain 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);
199id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2001	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
2012	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using filesort
2023	UNION	t4	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2034	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
204NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
205Warnings:
206Note	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`))
207select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
208(select a from t3 where a<t2.a*4 order by 1 desc limit 1)	a
2093	1
2107	2
211select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
212(select * from t2 where a>1) as tt;
213(select t3.a from t3 where a<8 order by 1 desc limit 1)	a
2147	2
215set @tmp_optimizer_switch=@@optimizer_switch;
216set optimizer_switch='derived_merge=off,derived_with_keys=off';
217explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
218(select * from t2 where a>1) as tt;
219id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2201	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	100.00
2213	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
2222	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using filesort
223Warnings:
224Note	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`
225set optimizer_switch=@tmp_optimizer_switch;
226select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
227a
2282
229select * 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);
230a
2312
232select * 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);
233a
234select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
235b	(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
2368	7.5000
2378	4.5000
2389	7.5000
239explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
240id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2411	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	3	100.00
2422	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
2433	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
244Warnings:
245Note	1276	Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
246Note	1003	/* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* 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`
247select * from t3 where exists (select * from t2 where t2.b=t3.a);
248a
2497
250select * from t3 where not exists (select * from t2 where t2.b=t3.a);
251a
2526
2533
254select * from t3 where a in (select b from t2);
255a
2567
257select * from t3 where a not in (select b from t2);
258a
2596
2603
261select * from t3 where a = some (select b from t2);
262a
2637
264select * from t3 where a <> any (select b from t2);
265a
2666
2673
268select * from t3 where a = all (select b from t2);
269a
2707
271select * from t3 where a <> all (select b from t2);
272a
2736
2743
275insert into t2 values (100, 5);
276select * from t3 where a < any (select b from t2);
277a
2786
2793
280select * from t3 where a < all (select b from t2);
281a
2823
283select * from t3 where a >= any (select b from t2);
284a
2856
2867
287explain extended select * from t3 where a >= any (select b from t2);
288id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2891	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2902	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	3	100.00
291Warnings:
292Note	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`)))
293select * from t3 where a >= all (select b from t2);
294a
2957
296delete from t2 where a=100;
297select * from t3 where a in (select a,b from t2);
298ERROR 21000: Operand should contain 1 column(s)
299select * from t3 where a in (select * from t2);
300ERROR 21000: Operand should contain 1 column(s)
301insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
302select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
303b	ma
304insert into t2 values (2,10);
305select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
306b	ma
30710	1
308delete from t2 where a=2 and b=10;
309select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
310b	ma
3117	12
312create table t5 (a int);
313select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
314(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
315NULL	1
3162	2
317insert into t5 values (5);
318select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
319(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
320NULL	1
3212	2
322insert into t5 values (2);
323select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
324(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
325NULL	1
3262	2
327explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
328id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3291	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
3302	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
3313	DEPENDENT UNION	t5	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
332NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
333Warnings:
334Note	1276	Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
335Note	1276	Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
336Note	1003	/* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* 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`
337select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
338ERROR 21000: Subquery returns more than 1 row
339create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
340create table t7( uq int primary key, name char(25));
341insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
342insert into t6 values (1,1),(1,2),(2,2),(1,3);
343select * from t6 where exists (select * from t7 where uq = clinic_uq);
344patient_uq	clinic_uq
3451	1
3461	2
3472	2
348explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
349id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3501	PRIMARY	t6	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
3512	MATERIALIZED	t7	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using index
352Warnings:
353Note	1276	Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
354Note	1003	/* select#1 */ select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(<in_optimizer>(`test`.`t6`.`clinic_uq`,`test`.`t6`.`clinic_uq` in ( <materialize> (/* select#2 */ select `test`.`t7`.`uq` from `test`.`t7` where 1 ), <primary_index_lookup>(`test`.`t6`.`clinic_uq` in <temporary table> on distinct_key where `test`.`t6`.`clinic_uq` = `<subquery2>`.`uq`))))
355select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
356ERROR 23000: Column 'a' in field list is ambiguous
357drop table t1,t2,t3;
358CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
359INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
360CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
361INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
362CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
363INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
364SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
365a	b
366W	1732-02-22
367SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
368a	b
369W	1
370SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
371a	b
372W	a
373CREATE TABLE `t8` (
374`pseudo` varchar(35) character set latin1 NOT NULL default '',
375`email` varchar(60) character set latin1 NOT NULL default '',
376PRIMARY KEY  (`pseudo`),
377UNIQUE KEY `email` (`email`)
378) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
379INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
380INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
381INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
382EXPLAIN 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');
383id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3841	PRIMARY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3854	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3862	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00
3873	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
388Warnings:
389Note	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
390SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
391t8 WHERE pseudo='joce');
392ERROR HY000: Illegal parameter data types varchar and row for operation '='
393SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
394pseudo='joce');
395ERROR HY000: Illegal parameter data types varchar and row for operation '='
396SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
397pseudo
398joce
399SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
400ERROR 21000: Subquery returns more than 1 row
401drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
402CREATE TABLE `t1` (
403`topic` mediumint(8) unsigned NOT NULL default '0',
404`date` date NOT NULL default '0000-00-00',
405`pseudo` varchar(35) character set latin1 NOT NULL default '',
406PRIMARY KEY  (`pseudo`,`date`,`topic`),
407KEY `topic` (`topic`)
408) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
409INSERT INTO t1 (topic,date,pseudo) VALUES
410('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
411EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
412id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4131	SIMPLE	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
414Warnings:
415Note	1003	select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03'
416EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
417id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4181	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4192	SUBQUERY	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
420Warnings:
421Note	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')`
422SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
423date
4242002-08-03
425SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
426(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
4272002-08-03
428SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
4291
4301
4311
4321
433SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
434ERROR 21000: Subquery returns more than 1 row
435EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
436id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4371	PRIMARY	t1	index	NULL	topic	3	NULL	2	100.00	Using index
4382	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4393	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
440NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
441Warnings:
442Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
443drop table t1;
444CREATE TABLE `t1` (
445`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
446`maxnumrep` int(10) unsigned NOT NULL default '0',
447PRIMARY KEY  (`numeropost`),
448UNIQUE KEY `maxnumrep` (`maxnumrep`)
449) ENGINE=MyISAM ROW_FORMAT=FIXED;
450INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
451CREATE TABLE `t2` (
452`mot` varchar(30) NOT NULL default '',
453`topic` mediumint(8) unsigned NOT NULL default '0',
454`date` date NOT NULL default '0000-00-00',
455`pseudo` varchar(35) NOT NULL default '',
456PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
457) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
458INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
459select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
460a
46140143
462SELECT 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;
463numeropost	maxnumrep
46443506	2
46540143	1
466SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
467ERROR 42S22: Unknown column 'a' in 'having clause'
468SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
469ERROR 42S22: Unknown column 'a' in 'having clause'
470SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
471mot	topic	date	pseudo
472joce	40143	2002-10-22	joce
473joce	43506	2002-10-22	joce
474SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
475mot	topic	date	pseudo
476SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
477mot	topic	date	pseudo
478SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
479mot	topic	date	pseudo
480joce	40143	2002-10-22	joce
481joce	43506	2002-10-22	joce
482SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
483mot	topic	date	pseudo
484SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
485mot	topic	date	pseudo
486SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
487mot	topic	date	pseudo
488SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
489mot	topic	date	pseudo
490joce	40143	2002-10-22	joce
491joce	43506	2002-10-22	joce
492SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
493mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
494joce	40143	2002-10-22	joce	1
495joce	43506	2002-10-22	joce	1
496SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
497mot	topic	date	pseudo
498SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
499mot	topic	date	pseudo
500joce	40143	2002-10-22	joce
501joce	43506	2002-10-22	joce
502SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
503mot	topic	date	pseudo
504joce	40143	2002-10-22	joce
505SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
506mot	topic	date	pseudo
507joce	40143	2002-10-22	joce
508SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
509mot	topic	date	pseudo
510joce	40143	2002-10-22	joce
511SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
512mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
513joce	40143	2002-10-22	joce	1
514joce	43506	2002-10-22	joce	0
515drop table t1,t2;
516CREATE TABLE `t1` (
517`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
518`maxnumrep` int(10) unsigned NOT NULL default '0',
519PRIMARY KEY  (`numeropost`),
520UNIQUE KEY `maxnumrep` (`maxnumrep`)
521) ENGINE=MyISAM ROW_FORMAT=FIXED;
522INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
523select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
524ERROR 21000: Subquery returns more than 1 row
525select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
526ERROR 21000: Subquery returns more than 1 row
527show warnings;
528Level	Code	Message
529Error	1242	Subquery returns more than 1 row
530Error	1028	Sort aborted: Subquery returns more than 1 row
531drop table t1;
532create table t1 (a int);
533insert into t1 values (1),(2),(3);
534(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
535a
5361
5372
5383
539drop table t1;
540CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
541INSERT INTO t1 VALUES ();
542SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
543ERROR 21000: Subquery returns more than 1 row
544drop table t1;
545CREATE TABLE `t1` (
546`numeropost` mediumint(8) unsigned NOT NULL default '0',
547`numreponse` int(10) unsigned NOT NULL auto_increment,
548`pseudo` varchar(35) NOT NULL default '',
549PRIMARY KEY  (`numeropost`,`numreponse`),
550UNIQUE KEY `numreponse` (`numreponse`),
551KEY `pseudo` (`pseudo`,`numeropost`)
552) ENGINE=MyISAM;
553SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
554ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
555SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
556ERROR 42S22: Unknown column 'a' in 'having clause'
557SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
558numreponse	(SELECT numeropost FROM t1 HAVING numreponse=1)
559INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
560EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
561ERROR 21000: Subquery returns more than 1 row
562SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
563ERROR 21000: Subquery returns more than 1 row
564EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
565id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5661	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
567Warnings:
568Note	1003	select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1'
569EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
570id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5711	PRIMARY	t1	const	PRIMARY,numreponse	PRIMARY	7	const,const	1	100.00	Using index
5722	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
573Warnings:
574Note	1003	/* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1
575drop table t1;
576CREATE TABLE t1 (a int(1));
577INSERT INTO t1 VALUES (1);
578SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
5791
5801
581drop table t1;
582create table t1 (a int NOT NULL, b int, primary key (a));
583create table t2 (a int NOT NULL, b int, primary key (a));
584insert into t1 values (0, 10),(1, 11),(2, 12);
585insert into t2 values (1, 21),(2, 22),(3, 23);
586select * from t1;
587a	b
5880	10
5891	11
5902	12
591update t1 set b= (select b from t2);
592ERROR 21000: Subquery returns more than 1 row
593update t1 set b= (select b from t2 where t1.a = t2.a);
594select * from t1;
595a	b
5960	NULL
5971	21
5982	22
599drop table t1, t2;
600create table t1 (a int NOT NULL, b int, primary key (a));
601create table t2 (a int NOT NULL, b int, primary key (a));
602insert into t1 values (0, 10),(1, 11),(2, 12);
603insert into t2 values (1, 21),(2, 12),(3, 23);
604select * from t1;
605a	b
6060	10
6071	11
6082	12
609select * from t1 where b = (select b from t2 where t1.a = t2.a);
610a	b
6112	12
612delete from t1 where b in (select b from t1);
613affected rows: 3
614insert into t1 values (0, 10),(1, 11),(2, 12);
615delete from t1 where b = (select b from t2);
616ERROR 21000: Subquery returns more than 1 row
617delete from t1 where b = (select b from t2 where t1.a = t2.a);
618select * from t1 order by b;
619a	b
6200	10
6211	11
622drop table t1, t2;
623create table t11 (a int NOT NULL, b int, primary key (a));
624create table t12 (a int NOT NULL, b int, primary key (a));
625create table t2 (a int NOT NULL, b int, primary key (a));
626insert into t11 values (0, 10),(1, 11),(2, 12);
627insert into t12 values (33, 10),(22, 11),(2, 12);
628insert into t2 values (1, 21),(2, 12),(3, 23);
629select * from t11;
630a	b
6310	10
6321	11
6332	12
634select * from t12;
635a	b
63633	10
63722	11
6382	12
639delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
640ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data
641delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
642ERROR 21000: Subquery returns more than 1 row
643delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
644select * from t11;
645a	b
6460	10
6471	11
648select * from t12;
649a	b
65033	10
65122	11
652drop table t11, t12, t2;
653CREATE TABLE t1 (x int) ENGINE=MyISAM;
654create table t2 (a int) ENGINE=MyISAM;
655create table t3 (b int);
656insert into t2 values (1);
657insert into t3 values (1),(2);
658INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
659ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
660INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
661ERROR 21000: Subquery returns more than 1 row
662INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
663select * from t1;
664x
6651
666insert into t2 values (1);
667INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
668select * from t1;
669x
6701
6712
672INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
673select * from t1;
674x
6751
6762
6773
6783
679INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
680select * from t1;
681x
6821
6832
6843
6853
68611
68711
688INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
689ERROR 42S22: Unknown column 'x' in 'field list'
690INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
691select * from t1;
692x
6931
6942
6953
6963
69711
69811
6992
700drop table t1, t2, t3;
701CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
702create table t2 (a int);
703create table t3 (a int);
704insert into t2 values (1);
705insert into t3 values (1),(2);
706select * from t1;
707x	y
708replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
709ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
710replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
711ERROR 21000: Subquery returns more than 1 row
712replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
713select * from t1;
714x	y
7151	2
716replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
717select * from t1;
718x	y
7191	3
720replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
721select * from t1;
722x	y
7231	3
7244	1
725replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
726select * from t1;
727x	y
7281	3
7294	2
730replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
731select * from t1;
732x	y
7331	3
7344	2
7352	1
736drop table t1, t2, t3;
737SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
738ERROR HY000: No tables used
739CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
740INSERT INTO t2 VALUES (1),(2);
741SELECT * FROM t2 WHERE id IN (SELECT 1);
742id
7431
744EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
745id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7461	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using index
747Warnings:
748Note	1249	Select 2 was reduced during optimization
749Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1
750SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
751id
7521
753SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
754id
7552
756EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
757id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7581	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using where; Using index
759Warnings:
760Note	1249	Select 3 was reduced during optimization
761Note	1249	Select 2 was reduced during optimization
762Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1)
763EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
764id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7651	PRIMARY	t2	index	NULL	id	5	NULL	2	100.00	Using where; Using index
7662	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
7673	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
768NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
769Warnings:
770Note	1003	/* select#1 */ select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<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))))
771SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
772id
773SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
774id
7752
776INSERT INTO t2 VALUES ((SELECT * FROM t2));
777ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
778INSERT INTO t2 VALUES ((SELECT id FROM t2));
779ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
780SELECT * FROM t2;
781id
7821
7832
784CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
785INSERT INTO t1 values (1),(1);
786UPDATE t2 SET id=(SELECT * FROM t1);
787ERROR 21000: Subquery returns more than 1 row
788drop table t2, t1;
789create table t1 (a int);
790insert into t1 values (1),(2),(3);
791select 1 IN (SELECT * from t1);
7921 IN (SELECT * from t1)
7931
794select 10 IN (SELECT * from t1);
79510 IN (SELECT * from t1)
7960
797select NULL IN (SELECT * from t1);
798NULL IN (SELECT * from t1)
799NULL
800update t1 set a=NULL where a=2;
801select 1 IN (SELECT * from t1);
8021 IN (SELECT * from t1)
8031
804select 3 IN (SELECT * from t1);
8053 IN (SELECT * from t1)
8061
807select 10 IN (SELECT * from t1);
80810 IN (SELECT * from t1)
809NULL
810select 1 > ALL (SELECT * from t1);
8111 > ALL (SELECT * from t1)
8120
813select 10 > ALL (SELECT * from t1);
81410 > ALL (SELECT * from t1)
815NULL
816select 1 > ANY (SELECT * from t1);
8171 > ANY (SELECT * from t1)
818NULL
819select 10 > ANY (SELECT * from t1);
82010 > ANY (SELECT * from t1)
8211
822drop table t1;
823create table t1 (a varchar(20));
824insert into t1 values ('A'),('BC'),('DEF');
825select 'A' IN (SELECT * from t1);
826'A' IN (SELECT * from t1)
8271
828select 'XYZS' IN (SELECT * from t1);
829'XYZS' IN (SELECT * from t1)
8300
831select NULL IN (SELECT * from t1);
832NULL IN (SELECT * from t1)
833NULL
834update t1 set a=NULL where a='BC';
835select 'A' IN (SELECT * from t1);
836'A' IN (SELECT * from t1)
8371
838select 'DEF' IN (SELECT * from t1);
839'DEF' IN (SELECT * from t1)
8401
841select 'XYZS' IN (SELECT * from t1);
842'XYZS' IN (SELECT * from t1)
843NULL
844select 'A' > ALL (SELECT * from t1);
845'A' > ALL (SELECT * from t1)
8460
847select 'XYZS' > ALL (SELECT * from t1);
848'XYZS' > ALL (SELECT * from t1)
849NULL
850select 'A' > ANY (SELECT * from t1);
851'A' > ANY (SELECT * from t1)
852NULL
853select 'XYZS' > ANY (SELECT * from t1);
854'XYZS' > ANY (SELECT * from t1)
8551
856drop table t1;
857create table t1 (a float);
858insert into t1 values (1.5),(2.5),(3.5);
859select 1.5 IN (SELECT * from t1);
8601.5 IN (SELECT * from t1)
8611
862select 10.5 IN (SELECT * from t1);
86310.5 IN (SELECT * from t1)
8640
865select NULL IN (SELECT * from t1);
866NULL IN (SELECT * from t1)
867NULL
868update t1 set a=NULL where a=2.5;
869select 1.5 IN (SELECT * from t1);
8701.5 IN (SELECT * from t1)
8711
872select 3.5 IN (SELECT * from t1);
8733.5 IN (SELECT * from t1)
8741
875select 10.5 IN (SELECT * from t1);
87610.5 IN (SELECT * from t1)
877NULL
878select 1.5 > ALL (SELECT * from t1);
8791.5 > ALL (SELECT * from t1)
8800
881select 10.5 > ALL (SELECT * from t1);
88210.5 > ALL (SELECT * from t1)
883NULL
884select 1.5 > ANY (SELECT * from t1);
8851.5 > ANY (SELECT * from t1)
886NULL
887select 10.5 > ANY (SELECT * from t1);
88810.5 > ANY (SELECT * from t1)
8891
890explain extended select (select a+1) from t1;
891id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
8921	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
893Warnings:
894Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
895Note	1249	Select 2 was reduced during optimization
896Note	1003	select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
897select (select a+1) from t1;
898(select a+1)
8992.5
900NULL
9014.5
902drop table t1;
903CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY  (a));
904CREATE TABLE t2 (a int(11) default '0', INDEX (a));
905INSERT INTO t1 VALUES (1),(2),(3),(4);
906INSERT INTO t2 VALUES (1),(2),(3);
907SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
908a	t1.a in (select t2.a from t2)
9091	1
9102	1
9113	1
9124	0
913explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
914id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9151	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9162	MATERIALIZED	t2	index	a	a	5	NULL	3	100.00	Using index
917Warnings:
918Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`)))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
919CREATE TABLE t3 (a int(11) default '0');
920INSERT INTO t3 VALUES (1),(2),(3);
921SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
922a	t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
9231	1
9242	1
9253	1
9264	0
927explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
928id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9291	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9302	MATERIALIZED	t2	index	a	a	5	NULL	3	100.00	Using index
9312	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (flat, BNL join)
932Warnings:
933Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where `test`.`t3`.`a` = `test`.`t2`.`a` ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
934drop table t1,t2,t3;
935# check correct NULL Processing for normal IN/ALL/ANY
936# and 2 ways of max/min optimization
937create table t1 (a int);
938insert into t1 values (1), (100), (NULL), (1000);
939create table t2 (a int not null);
940# subselect returns empty set (for NULL and non-NULL left part)
941select a, a in (select * from t2) from t1;
942a	a in (select * from t2)
9431	0
944100	0
945NULL	0
9461000	0
947select a, a > any (select * from t2) from t1;
948a	a > any (select * from t2)
9491	0
950100	0
951NULL	0
9521000	0
953select a, a > all (select * from t2) from t1;
954a	a > all (select * from t2)
9551	1
956100	1
957NULL	1
9581000	1
959select a from t1 where a in (select * from t2);
960a
961select a from t1 where a > any (select * from t2);
962a
963select a from t1 where a > all (select * from t2);
964a
9651
966100
967NULL
9681000
969select a from t1 where a in (select * from t2 group by a);
970a
971select a from t1 where a > any (select * from t2 group by a);
972a
973select a from t1 where a > all (select * from t2 group by a);
974a
9751
976100
977NULL
9781000
979insert into t2 values (1),(200);
980# sebselect returns non-empty set without NULLs
981select a, a in (select * from t2) from t1;
982a	a in (select * from t2)
9831	1
984100	0
985NULL	NULL
9861000	0
987select a, a > any (select * from t2) from t1;
988a	a > any (select * from t2)
9891	0
990100	1
991NULL	NULL
9921000	1
993select a, a > all (select * from t2) from t1;
994a	a > all (select * from t2)
9951	0
996100	0
997NULL	NULL
9981000	1
999select a from t1 where a in (select * from t2);
1000a
10011
1002select a from t1 where a > any (select * from t2);
1003a
1004100
10051000
1006select a from t1 where a > all (select * from t2);
1007a
10081000
1009select a from t1 where a in (select * from t2 group by a);
1010a
10111
1012select a from t1 where a > any (select * from t2 group by a);
1013a
1014100
10151000
1016select a from t1 where a > all (select * from t2 group by a);
1017a
10181000
1019drop table t2;
1020create table t2 (a int);
1021insert into t2 values (1),(NULL),(200);
1022# sebselect returns non-empty set with NULLs
1023select a, a in (select * from t2) from t1;
1024a	a in (select * from t2)
10251	1
1026100	NULL
1027NULL	NULL
10281000	NULL
1029select a, a > any (select * from t2) from t1;
1030a	a > any (select * from t2)
10311	NULL
1032100	1
1033NULL	NULL
10341000	1
1035select a, a > all (select * from t2) from t1;
1036a	a > all (select * from t2)
10371	0
1038100	0
1039NULL	NULL
10401000	NULL
1041select a from t1 where a in (select * from t2);
1042a
10431
1044select a from t1 where a > any (select * from t2);
1045a
1046100
10471000
1048select a from t1 where a > all (select * from t2);
1049a
1050select a from t1 where a in (select * from t2 group by a);
1051a
10521
1053select a from t1 where a > any (select * from t2 group by a);
1054a
1055100
10561000
1057select a from t1 where a > all (select * from t2 group by a);
1058a
1059drop table t1, t2;
1060create table t1 (a float);
1061select 10.5 IN (SELECT * from t1 LIMIT 1);
1062ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1063select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
1064ERROR 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
1065select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1);
1066ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1067drop table t1;
1068create table t1 (a int, b int, c varchar(10));
1069create table t2 (a int);
1070insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
1071insert into t2 values (1),(2),(NULL);
1072select 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;
1073a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a')	(select c from t1 where a=t2.a)
10741	1	a
10752	0	b
1076NULL	NULL	NULL
1077select 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;
1078a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b')	(select c from t1 where a=t2.a)
10791	0	a
10802	1	b
1081NULL	NULL	NULL
1082select 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;
1083a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c')	(select c from t1 where a=t2.a)
10841	0	a
10852	0	b
1086NULL	NULL	NULL
1087drop table t1,t2;
1088create table t1 (a int, b real, c varchar(10));
1089insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
1090select ROW(1, 1, 'a') IN (select a,b,c from t1);
1091ROW(1, 1, 'a') IN (select a,b,c from t1)
10921
1093select ROW(1, 2, 'a') IN (select a,b,c from t1);
1094ROW(1, 2, 'a') IN (select a,b,c from t1)
10950
1096select ROW(1, 1, 'a') IN (select b,a,c from t1);
1097ROW(1, 1, 'a') IN (select b,a,c from t1)
10981
1099select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
1100ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
11011
1102select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
1103ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
11040
1105select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
1106ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
11071
1108select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1109ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11101
1111select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1112ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11130
1114select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
1115ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
11161
1117select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
1118ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1119drop table t1;
1120create table t1 (a int);
1121insert into t1 values (1);
1122do @a:=(SELECT a from t1);
1123select @a;
1124@a
11251
1126set @a:=2;
1127set @a:=(SELECT a from t1);
1128select @a;
1129@a
11301
1131drop table t1;
1132do (SELECT a from t1);
1133ERROR 42S02: Table 'test.t1' doesn't exist
1134set @a:=(SELECT a from t1);
1135ERROR 42S02: Table 'test.t1' doesn't exist
1136CREATE TABLE t1 (a int, KEY(a));
1137HANDLER t1 OPEN;
1138HANDLER t1 READ a=((SELECT 1));
1139ERROR 42000: HANDLER..READ does not support subqueries or stored functions
1140HANDLER t1 CLOSE;
1141drop table t1;
1142create table t1 (a int);
1143create table t2 (b int);
1144insert into t1 values (1),(2);
1145insert into t2 values (1);
1146select a from t1 where a in (select a from t1 where a in (select b from t2));
1147a
11481
1149drop table t1, t2;
1150create table t1 (a int, b int);
1151create table t2 like t1;
1152insert into t1 values (1,2),(1,3),(1,4),(1,5);
1153insert into t2 values (1,2),(1,3);
1154select * from t1 where row(a,b) in (select a,b from t2);
1155a	b
11561	2
11571	3
1158drop table t1, t2;
1159CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;
1160INSERT INTO t1 VALUES (1);
1161UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1162select * from t1;
1163i
11642
1165drop table t1;
1166CREATE TABLE t1 (a int(1));
1167EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
1168id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11691	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11702	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1171Warnings:
1172Note	1003	/* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
1173EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
1174id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11751	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11762	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1177Warnings:
1178Note	1003	/* select#1 */ select (/* select#2 */ select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
1179EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
1180id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11811	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11822	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1183Warnings:
1184Note	1003	/* select#1 */ select (/* select#2 */ select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
1185drop table t1;
1186CREATE TABLE `t1` (
1187`mot` varchar(30) character set latin1 NOT NULL default '',
1188`topic` mediumint(8) unsigned NOT NULL default '0',
1189`date` date NOT NULL default '0000-00-00',
1190`pseudo` varchar(35) character set latin1 NOT NULL default '',
1191PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1192KEY `pseudo` (`pseudo`,`date`,`topic`),
1193KEY `topic` (`topic`)
1194) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1195CREATE TABLE `t2` (
1196`mot` varchar(30) character set latin1 NOT NULL default '',
1197`topic` mediumint(8) unsigned NOT NULL default '0',
1198`date` date NOT NULL default '0000-00-00',
1199`pseudo` varchar(35) character set latin1 NOT NULL default '',
1200PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1201KEY `pseudo` (`pseudo`,`date`,`topic`),
1202KEY `topic` (`topic`)
1203) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1204CREATE TABLE `t3` (
1205`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
1206`maxnumrep` int(10) unsigned NOT NULL default '0',
1207PRIMARY KEY  (`numeropost`),
1208UNIQUE KEY `maxnumrep` (`maxnumrep`)
1209) ENGINE=MyISAM CHARSET=latin1;
1210INSERT IGNORE INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
1211Warnings:
1212Warning	1265	Data truncated for column 'date' at row 1
1213Warning	1265	Data truncated for column 'date' at row 2
1214INSERT IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
1215Warnings:
1216Warning	1265	Data truncated for column 'date' at row 1
1217Warning	1265	Data truncated for column 'date' at row 2
1218INSERT INTO t3 VALUES (1,1);
1219SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
1220numeropost=topic);
1221topic
12222
1223select * from t1;
1224mot	topic	date	pseudo
1225joce	1	0000-00-00	joce
1226test	2	0000-00-00	test
1227DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
1228EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
1229select * from t1;
1230mot	topic	date	pseudo
1231joce	1	0000-00-00	joce
1232drop table t1, t2, t3;
1233SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1234a	(SELECT a)
12351	1
1236CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
1237SHOW CREATE TABLE t1;
1238Table	Create Table
1239t1	CREATE TABLE `t1` (
1240  `a` int(1) NOT NULL DEFAULT 0,
1241  `(SELECT 1)` int(1) NOT NULL DEFAULT 0
1242) ENGINE=MyISAM DEFAULT CHARSET=latin1
1243drop table t1;
1244CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1245SHOW CREATE TABLE t1;
1246Table	Create Table
1247t1	CREATE TABLE `t1` (
1248  `a` int(1) NOT NULL DEFAULT 0,
1249  `(SELECT a)` int(1) NOT NULL DEFAULT 0
1250) ENGINE=MyISAM DEFAULT CHARSET=latin1
1251drop table t1;
1252CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1253SHOW CREATE TABLE t1;
1254Table	Create Table
1255t1	CREATE TABLE `t1` (
1256  `a` int(1) NOT NULL DEFAULT 0,
1257  `(SELECT a+0)` int(3) NOT NULL DEFAULT 0
1258) ENGINE=MyISAM DEFAULT CHARSET=latin1
1259drop table t1;
1260CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
1261select * from t1;
1262a
12632
1264SHOW CREATE TABLE t1;
1265Table	Create Table
1266t1	CREATE TABLE `t1` (
1267  `a` int(3) NOT NULL
1268) ENGINE=MyISAM DEFAULT CHARSET=latin1
1269drop table t1;
1270create table t1 (a int);
1271insert into t1 values (1), (2), (3);
1272explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
1273from t1;
1274id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
12751	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12762	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12773	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
1278Warnings:
1279Note	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`
1280drop table t1;
1281select 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);
1282ERROR 42S02: Table 'test.t1' doesn't exist
1283CREATE TABLE t1 (
1284ID int(11) NOT NULL auto_increment,
1285name char(35) NOT NULL default '',
1286t2 char(3) NOT NULL default '',
1287District char(20) NOT NULL default '',
1288Population int(11) NOT NULL default '0',
1289PRIMARY KEY  (ID)
1290) ENGINE=MyISAM;
1291INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
1292INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
1293INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
1294CREATE TABLE t2 (
1295Code char(3) NOT NULL default '',
1296Name char(52) NOT NULL default '',
1297Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1298Region char(26) NOT NULL default '',
1299SurfaceArea float(10,2) NOT NULL default '0.00',
1300IndepYear smallint(6) default NULL,
1301Population int(11) NOT NULL default '0',
1302LifeExpectancy float(3,1) default NULL,
1303GNP float(10,2) default NULL,
1304GNPOld float(10,2) default NULL,
1305LocalName char(45) NOT NULL default '',
1306GovernmentForm char(45) NOT NULL default '',
1307HeadOfState char(60) default NULL,
1308Capital int(11) default NULL,
1309Code2 char(2) NOT NULL default '',
1310PRIMARY KEY  (Code)
1311) ENGINE=MyISAM;
1312INSERT 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');
1313INSERT 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');
1314select 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);
1315Continent	Name	Population
1316Oceania	Sydney	3276207
1317drop table t1, t2;
1318CREATE TABLE `t1` (
1319`id` mediumint(8) unsigned NOT NULL auto_increment,
1320`pseudo` varchar(35) character set latin1 NOT NULL default '',
1321PRIMARY KEY  (`id`),
1322UNIQUE KEY `pseudo` (`pseudo`)
1323) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1324INSERT INTO t1 (pseudo) VALUES ('test');
1325SELECT 0 IN (SELECT 1 FROM t1 a);
13260 IN (SELECT 1 FROM t1 a)
13270
1328EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1329id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13301	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13312	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1332Warnings:
1333Note	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)`
1334INSERT INTO t1 (pseudo) VALUES ('test1');
1335SELECT 0 IN (SELECT 1 FROM t1 a);
13360 IN (SELECT 1 FROM t1 a)
13370
1338EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1339id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13401	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13412	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1342Warnings:
1343Note	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)`
1344drop table t1;
1345CREATE TABLE `t1` (
1346`i` int(11) NOT NULL default '0',
1347PRIMARY KEY  (`i`)
1348) ENGINE=MyISAM CHARSET=latin1;
1349INSERT INTO t1 VALUES (1);
1350UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
1351UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1352UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
1353ERROR 42S22: Unknown column 't.i' in 'field list'
1354select * from t1;
1355i
13563
1357drop table t1;
1358CREATE TABLE t1 (
1359id int(11) default NULL
1360) ENGINE=MyISAM CHARSET=latin1;
1361INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
1362CREATE TABLE t2 (
1363id int(11) default NULL,
1364name varchar(15) default NULL
1365) ENGINE=MyISAM CHARSET=latin1;
1366INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
1367update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
1368select * from t2;
1369id	name
13704	vita
13711	lenka
13722	lenka
13731	lenka
1374drop table t1,t2;
1375create table t1 (a int, unique index indexa (a));
1376insert into t1 values (-1), (-4), (-2), (NULL);
1377select -10 IN (select a from t1 FORCE INDEX (indexa));
1378-10 IN (select a from t1 FORCE INDEX (indexa))
1379NULL
1380drop table t1;
1381create table t1 (id int not null auto_increment primary key, salary int, key(salary));
1382insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
1383explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
1384id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13851	PRIMARY	t1	ref	salary	salary	5	const	1	100.00	Using where
13862	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1387Warnings:
1388Note	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`)
1389drop table t1;
1390CREATE TABLE t1 (
1391ID int(10) unsigned NOT NULL auto_increment,
1392SUB_ID int(3) unsigned NOT NULL default '0',
1393REF_ID int(10) unsigned default NULL,
1394REF_SUB int(3) unsigned default '0',
1395PRIMARY KEY (ID,SUB_ID),
1396UNIQUE KEY t1_PK (ID,SUB_ID),
1397KEY t1_FK (REF_ID,REF_SUB),
1398KEY t1_REFID (REF_ID)
1399) ENGINE=MyISAM CHARSET=cp1251;
1400INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
1401SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
1402REF_ID
1403DROP TABLE t1;
1404create table t1 (a int, b int);
1405create table t2 (a int, b int);
1406insert into t1 values (1,0), (2,0), (3,0);
1407insert into t2 values (1,1), (2,1), (3,1), (2,2);
1408update ignore t1 set b=(select b from t2 where t1.a=t2.a);
1409Warnings:
1410Warning	1242	Subquery returns more than 1 row
1411select * from t1;
1412a	b
14131	1
14142	NULL
14153	1
1416drop table t1, t2;
1417CREATE TABLE `t1` (
1418`id` mediumint(8) unsigned NOT NULL auto_increment,
1419`pseudo` varchar(35) NOT NULL default '',
1420`email` varchar(60) NOT NULL default '',
1421PRIMARY KEY  (`id`),
1422UNIQUE KEY `email` (`email`),
1423UNIQUE KEY `pseudo` (`pseudo`)
1424) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1425INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
1426SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
1427a	b
1428test	test
1429test1	test1
1430drop table if exists t1;
1431(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
1432a
14331
1434create table t1 (a int not null, b int, primary key (a));
1435create table t2 (a int not null, primary key (a));
1436create table t3 (a int not null, b int, primary key (a));
1437insert into t1 values (1,10), (2,20), (3,30),  (4,40);
1438insert into t2 values (2), (3), (4), (5);
1439insert into t3 values (10,3), (20,4), (30,5);
1440select * from t2 where t2.a in (select a from t1);
1441a
14422
14433
14444
1445explain extended select * from t2 where t2.a in (select a from t1);
1446id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14471	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	4	100.00	Using where; Using index
14482	MATERIALIZED	t1	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
1449Warnings:
1450Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
1451select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1452a
14532
14544
1455explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1456id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14571	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	4	100.00	Using where; Using index
14582	MATERIALIZED	t1	ALL	PRIMARY	NULL	NULL	NULL	4	100.00	Using where
1459Warnings:
1460Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` where `test`.`t1`.`b` <> 30 ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
1461select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1462a
14632
14643
1465explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1466id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14671	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	4	100.00	Using where; Using index
14682	MATERIALIZED	t3	index	PRIMARY	PRIMARY	4	NULL	3	100.00	Using index
14692	MATERIALIZED	t1	ALL	PRIMARY	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (flat, BNL join)
1470Warnings:
1471Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` join `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`a` ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
1472drop table t1, t2, t3;
1473create table t1 (a int, b int, index a (a,b));
1474create table t2 (a int, index a (a));
1475create table t3 (a int, b int, index a (a));
1476insert into t1 values (1,10), (2,20), (3,30), (4,40);
1477create table t0(a int);
1478insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1479insert into t1
1480select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
1481insert into t2 values (2), (3), (4), (5);
1482insert into t3 values (10,3), (20,4), (30,5);
1483select * from t2 where t2.a in (select a from t1);
1484a
14852
14863
14874
1488explain extended select * from t2 where t2.a in (select a from t1);
1489id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14901	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
14912	DEPENDENT SUBQUERY	t1	index_subquery	a	a	5	func	1001	100.00	Using index
1492Warnings:
1493Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a))))
1494select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1495a
14962
14974
1498explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1499id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15001	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
15012	DEPENDENT SUBQUERY	t1	index_subquery	a	a	5	func	1001	100.00	Using index; Using where
1502Warnings:
1503Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where `test`.`t1`.`b` <> 30 and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
1504select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1505a
15062
15073
1508explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1509id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15101	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
15112	DEPENDENT SUBQUERY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index
15122	DEPENDENT SUBQUERY	t1	ref	a	a	10	func,test.t3.a	1167	100.00	Using index
1513Warnings:
1514Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` join `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`a` and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`)))
1515insert into t1 values (3,31);
1516select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1517a
15182
15193
15204
1521select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
1522a
15232
15244
1525explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1526id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15271	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
15282	DEPENDENT SUBQUERY	t1	index_subquery	a	a	5	func	1001	100.00	Using index; Using where
1529Warnings:
1530Note	1003	/* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where `test`.`t1`.`b` <> 30 and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
1531drop table t0, t1, t2, t3;
1532create table t1 (a int, b int);
1533create table t2 (a int, b int);
1534create table t3 (a int, b int);
1535insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
1536insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
1537insert into t3 values (3,3), (2,2), (1,1);
1538select 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;
1539a	(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)
15403	1
15412	2
15421	2
1543drop table t1,t2,t3;
1544create table t1 (s1 int);
1545create table t2 (s1 int);
1546insert into t1 values (1);
1547insert into t2 values (1);
1548select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
1549s1
15501
1551drop table t1,t2;
1552create table t1 (s1 int);
1553create table t2 (s1 int);
1554insert into t1 values (1);
1555insert into t2 values (1);
1556update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
1557ERROR 42S22: Unknown column 'x.s1' in 'field list'
1558DROP TABLE t1, t2;
1559CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
1560s2 CHAR(5) COLLATE latin1_swedish_ci);
1561INSERT INTO t1 VALUES ('z','?');
1562select * from t1 where s1 > (select max(s2) from t1);
1563ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
1564select * from t1 where s1 > any (select max(s2) from t1);
1565ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
1566drop table t1;
1567create table t1(toid int,rd int);
1568create table t2(userid int,pmnew int,pmtotal int);
1569insert into t2 values(1,0,0),(2,0,0);
1570insert 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);
1571select 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);
1572userid	pmtotal	pmnew	calc_total	calc_new
15731	0	0	9	3
15742	0	0	4	2
1575drop table t1, t2;
1576create table t1 (s1 char(5));
1577select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
1578ERROR 21000: Operand should contain 1 column(s)
1579insert into t1 values ('tttt');
1580select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
1581s1
1582tttt
1583explain extended (select * from t1);
1584id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15851	SIMPLE	t1	system	NULL	NULL	NULL	NULL	1	100.00
1586Warnings:
1587Note	1003	(select 'tttt' AS `s1` from dual)
1588(select * from t1);
1589s1
1590tttt
1591drop table t1;
1592create table t1 (s1 char(5), index s1(s1));
1593create table t2 (s1 char(5), index s1(s1));
1594insert into t1 values ('a1'),('a2'),('a3');
1595insert into t2 values ('a1'),('a2');
1596select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1597s1	s1 NOT IN (SELECT s1 FROM t2)
1598a1	0
1599a2	0
1600a3	1
1601select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1602s1	s1 = ANY (SELECT s1 FROM t2)
1603a1	1
1604a2	1
1605a3	0
1606select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1607s1	s1 <> ALL (SELECT s1 FROM t2)
1608a1	0
1609a2	0
1610a3	1
1611select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1612s1	s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
1613a1	0
1614a2	1
1615a3	1
1616explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1617id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16181	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16192	MATERIALIZED	t2	index	s1	s1	6	NULL	2	100.00	Using index
1620Warnings:
1621Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
1622explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1623id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16241	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16252	MATERIALIZED	t2	index	s1	s1	6	NULL	2	100.00	Using index
1626Warnings:
1627Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
1628explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1629id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16301	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16312	MATERIALIZED	t2	index	s1	s1	6	NULL	2	100.00	Using index
1632Warnings:
1633Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
1634explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1635id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16361	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16372	MATERIALIZED	t2	index	s1	s1	6	NULL	2	50.00	Using where; Using index
1638Warnings:
1639Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` where `test`.`t2`.`s1` < 'a2' ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
1640drop table t1,t2;
1641create table t2 (a int, b int not null);
1642create table t3 (a int);
1643insert into t3 values (6),(7),(3);
1644select * from t3 where a >= all (select b from t2);
1645a
16466
16477
16483
1649explain extended select * from t3 where a >= all (select b from t2);
1650id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16511	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16522	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1653Warnings:
1654Note	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`)))
1655select * from t3 where a >= some (select b from t2);
1656a
1657explain extended select * from t3 where a >= some (select b from t2);
1658id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16591	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16602	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1661Warnings:
1662Note	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`)))
1663select * from t3 where a >= all (select b from t2 group by 1);
1664a
16656
16667
16673
1668explain extended select * from t3 where a >= all (select b from t2 group by 1);
1669id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16701	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16712	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1672Warnings:
1673Note	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`)))
1674select * from t3 where a >= some (select b from t2 group by 1);
1675a
1676explain extended select * from t3 where a >= some (select b from t2 group by 1);
1677id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16781	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16792	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1680Warnings:
1681Note	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`)))
1682select * from t3 where NULL >= any (select b from t2);
1683a
1684explain extended select * from t3 where NULL >= any (select b from t2);
1685id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16861	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16872	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1688Warnings:
1689Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1690select * from t3 where NULL >= any (select b from t2 group by 1);
1691a
1692explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
1693id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16941	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16952	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1696Warnings:
1697Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1698select * from t3 where NULL >= some (select b from t2);
1699a
1700explain extended select * from t3 where NULL >= some (select b from t2);
1701id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17021	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17032	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1704Warnings:
1705Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1706select * from t3 where NULL >= some (select b from t2 group by 1);
1707a
1708explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
1709id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17101	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17112	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1712Warnings:
1713Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1714insert into t2 values (2,2), (2,1), (3,3), (3,1);
1715select * from t3 where a > all (select max(b) from t2 group by a);
1716a
17176
17187
1719explain extended select * from t3 where a > all (select max(b) from t2 group by a);
1720id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17211	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
17222	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
1723Warnings:
1724Note	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`)))
1725drop table t2, t3;
1726CREATE 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 ;
1727INSERT 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());
1728CREATE 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;
1729INSERT 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);
1730CREATE 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 ;
1731INSERT 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);
1732CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1733INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
1734select  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;
1735dbid	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')
1736-1	Valid	1
1737-1	Valid 2	1
1738-1	Should Not Return	0
1739SELECT 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;
1740dbid	name
1741-1	Valid
1742-1	Valid 2
1743drop table t1,t2,t3,t4;
1744CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1745INSERT INTO t1 VALUES (1),(5);
1746CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1747INSERT INTO t2 VALUES (2),(6);
1748select * from t1 where (1,2,6) in (select * from t2);
1749ERROR 21000: Operand should contain 3 column(s)
1750DROP TABLE t1,t2;
1751create table t1 (s1 int);
1752insert into t1 values (1);
1753insert into t1 values (2);
1754set sort_buffer_size = (select s1 from t1);
1755ERROR 21000: Subquery returns more than 1 row
1756do (select * from t1);
1757Warnings:
1758Error	1242	Subquery returns more than 1 row
1759drop table t1;
1760create table t1 (s1 char);
1761insert into t1 values ('e');
1762select * from t1 where 'f' > any (select s1 from t1);
1763s1
1764e
1765select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1766s1
1767e
1768explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1769id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17701	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17712	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17723	UNION	t1	system	NULL	NULL	NULL	NULL	1	100.00
1773NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
1774Warnings:
1775Note	1003	/* select#1 */ select 'e' AS `s1` from dual where 1
1776drop table t1;
1777CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1778INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
1779CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
1780INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
1781select 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;
1782phone	code
178369294728265	6
178418621828126	1862
178589356874041	NULL
178695895001874	NULL
1787drop table t1, t2;
1788create table t1 (s1 int);
1789create table t2 (s1 int);
1790select * from t1 where (select count(*) from t2 where t1.s2) = 1;
1791ERROR 42S22: Unknown column 't1.s2' in 'where clause'
1792select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
1793ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1794select count(*) from t2 group by t1.s2;
1795ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1796drop table t1, t2;
1797CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
1798CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1799INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1800INSERT INTO t2 VALUES (100, 200, 'C');
1801SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1802COLC
1803DROP TABLE t1, t2;
1804CREATE TABLE t1 (a int(1));
1805INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1806SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1807(SELECT a)
18081
18092
18103
18114
18125
1813DROP TABLE t1;
1814create table t1 (a int, b decimal(13, 3));
1815insert into t1 values (1, 0.123);
1816select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
1817delete from t1;
1818load data infile "../../tmp/subselect.out.file.1" into table t1;
1819select * from t1;
1820a	b
18211	0.123
1822drop table t1;
1823CREATE TABLE `t1` (
1824`id` int(11) NOT NULL auto_increment,
1825`id_cns` tinyint(3) unsigned NOT NULL default '0',
1826`tipo` enum('','UNO','DUE') NOT NULL default '',
1827`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1828`particolare` mediumint(8) unsigned NOT NULL default '0',
1829`generale` mediumint(8) unsigned NOT NULL default '0',
1830`bis` tinyint(3) unsigned NOT NULL default '0',
1831PRIMARY KEY  (`id`),
1832UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1833UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1834);
1835INSERT 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);
1836CREATE TABLE `t2` (
1837`id` tinyint(3) unsigned NOT NULL auto_increment,
1838`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1839PRIMARY KEY  (`id`)
1840);
1841INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1842SELECT 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;
1843id	max_anno_dep	PIPPO
184416	1987	1
184550	1990	0
184651	1990	NULL
1847DROP TABLE t1, t2;
1848create table t1 (a int);
1849insert into t1 values (1), (2), (3);
1850SET SQL_SELECT_LIMIT=1;
1851select sum(a) from (select * from t1) as a;
1852sum(a)
18536
1854select 2 in (select * from t1);
18552 in (select * from t1)
18561
1857SET SQL_SELECT_LIMIT=default;
1858drop table t1;
1859CREATE TABLE t1 (a int, b int, INDEX (a));
1860INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1861SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1862a	b
18631	1
18641	2
18651	3
1866DROP TABLE t1;
1867create table t1(val varchar(10));
1868insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1869select 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%');
1870count(*)
18710
1872drop table t1;
1873create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1874insert 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');
1875select * from t1 where id not in (select id from t1 where id < 8);
1876id	text
18778	text8
18789	text9
187910	text10
188011	text11
188112	text12
1882select * 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);
1883id	text
18848	text8
18859	text9
188610	text10
188711	text11
188812	text12
1889explain extended select * from t1 where id not in (select id from t1 where id < 8);
1890id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18911	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
18922	MATERIALIZED	t1	range	PRIMARY	PRIMARY	4	NULL	7	100.00	Using where; Using index
1893Warnings:
1894Note	1003	/* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where !<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,`test`.`t1`.`id` in ( <materialize> (/* select#2 */ select `test`.`t1`.`id` from `test`.`t1` where `test`.`t1`.`id` < 8 ), <primary_index_lookup>(`test`.`t1`.`id` in <temporary table> on distinct_key where `test`.`t1`.`id` = `<subquery2>`.`id`))))
1895explain 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);
1896id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18971	PRIMARY	tt	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
18982	DEPENDENT SUBQUERY	t1	eq_ref	PRIMARY	PRIMARY	4	test.tt.id	1	100.00	Using where; Using index
1899Warnings:
1900Note	1276	Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1901Note	1003	/* select#1 */ select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where !<in_optimizer>(1,<expr_cache><`test`.`tt`.`id`>(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)))
1902insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1903create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1904insert 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');
1905select * 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);
1906id	text	id	text	id	text
19071	text1	1	text1	1	text1
19082	text2	2	text2	2	text2
19093	text3	3	text3	3	text3
19104	text4	4	text4	4	text4
19115	text5	5	text5	5	text5
19126	text6	6	text6	6	text6
19137	text7	7	text7	7	text7
19148	text8	8	text8	8	text8
19159	text9	9	text9	9	text9
191610	text10	10	text10	10	text10
191711	text11	11	text1	11	text11
191812	text12	12	text2	12	text12
19191000	text1000	NULL	NULL	1000	text1000
19201001	text1001	NULL	NULL	1000	text1000
1921explain 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);
1922id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
19231	SIMPLE	a	ALL	NULL	NULL	NULL	NULL	14	100.00
19241	SIMPLE	b	eq_ref	PRIMARY	PRIMARY	4	test.a.id	2	100.00
19251	SIMPLE	c	eq_ref	PRIMARY	PRIMARY	4	func	1	100.00	Using index condition
1926Warnings:
1927Note	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`
1928drop table t1,t2;
1929create table t1 (a int);
1930insert into t1 values (1);
1931explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1932id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
19331	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
19342	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1
1935drop table t1;
1936create table t1(id int);
1937create table t2(id int);
1938create table t3(flag int);
1939select (select * from t3 where id not null) from t1, t2;
1940ERROR 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
1941drop table t1,t2,t3;
1942CREATE TABLE t1 (id INT);
1943CREATE TABLE t2 (id INT);
1944INSERT INTO t1 VALUES (1), (2);
1945INSERT INTO t2 VALUES (1);
1946SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1947id	c
19481	1
19492	0
1950SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1951id	c
19521	1
19532	0
1954SELECT 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;
1955id	c
19561	1
19572	0
1958SELECT 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;
1959id	c
19601	1
19612	0
1962DROP TABLE t1,t2;
1963CREATE TABLE t1 ( a int, b int );
1964INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1965SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1966a
19673
1968SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1969a
19701
1971SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1972a
19732
1974SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1975a
19762
19773
1978SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1979a
19801
19812
1982SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1983a
19841
19853
1986SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1987a
19883
1989SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1990a
19911
1992SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1993a
19942
1995SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1996a
19972
19983
1999SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2000a
20011
20022
2003SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2004a
20051
20063
2007ALTER TABLE t1 ADD INDEX (a);
2008SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
2009a
20103
2011SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
2012a
20131
2014SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
2015a
20162
2017SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
2018a
20192
20203
2021SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
2022a
20231
20242
2025SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
2026a
20271
20283
2029SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
2030a
20313
2032SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
2033a
20341
2035SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
2036a
20372
2038SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
2039a
20402
20413
2042SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2043a
20441
20452
2046SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2047a
20481
20493
2050SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
2051a
20523
2053SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
2054a
20551
2056SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
2057a
20582
2059SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
2060a
20612
20623
2063SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
2064a
20651
20662
2067SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
2068a
20691
20703
2071SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
2072a
20733
2074SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
2075a
20761
2077SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
2078a
20792
2080SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
2081a
20822
20833
2084SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
2085a
20861
20872
2088SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
2089a
20901
20913
2092SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2093a
20943
2095SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2096a
20971
2098SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2099a
21002
2101SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2102a
21032
21043
2105SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2106a
21071
21082
2109SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2110a
21111
21123
2113SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2114a
21153
2116SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2117a
21181
2119SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2120a
21212
2122SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2123a
21242
21253
2126SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2127a
21281
21292
2130SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2131a
21321
21333
2134SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2135a
21363
2137SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2138a
21391
2140SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2141a
21422
2143SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2144a
21452
21463
2147SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2148a
21491
21502
2151SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2152a
21531
21543
2155SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2156a
21573
2158SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2159a
21601
2161SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2162a
21632
2164SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2165a
21662
21673
2168SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2169a
21701
21712
2172SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2173a
21741
21753
2176SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
2177ERROR 21000: Operand should contain 1 column(s)
2178SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2179ERROR 21000: Operand should contain 1 column(s)
2180SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2181ERROR 21000: Operand should contain 1 column(s)
2182SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
2183ERROR 21000: Operand should contain 1 column(s)
2184SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2185ERROR 21000: Operand should contain 1 column(s)
2186SELECT a FROM t1 WHERE (1,2) > 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) <> ANY (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 FROM t1 WHERE b = 2);
2193ERROR 21000: Operand should contain 2 column(s)
2194SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2195ERROR 21000: Operand should contain 1 column(s)
2196SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2197a
2198SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
2199ERROR 21000: Operand should contain 2 column(s)
2200SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2201ERROR 21000: Operand should contain 1 column(s)
2202SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2203a
22041
22052
22063
2207SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
2208a
22092
2210SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
2211a
22121
22133
2214SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
2215a
22162
2217SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
2218a
22191
22203
2221SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2222a
22232
2224SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2225a
22261
22273
2228SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2229a
22302
2231SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2232a
22331
22343
2235SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2236a
22373
2238SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2239a
22401
2241SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2242a
22432
2244SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2245a
22462
22473
2248SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2249a
22501
22512
2252SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2253a
22541
22553
2256SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2257a
22583
2259SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2260a
22611
2262SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2263a
22642
2265SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2266a
22672
22683
2269SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2270a
22711
22722
2273SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2274a
22751
22763
2277SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
2278a
22793
2280SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
2281a
22821
2283SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
2284a
22852
2286SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2287a
22882
22893
2290SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2291a
22921
22932
2294SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
2295a
22961
22973
2298SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
2299a
23003
2301SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
2302a
23031
2304SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
2305a
23062
2307SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2308a
23092
23103
2311SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2312a
23131
23142
2315SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
2316a
23171
23183
2319SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
2320concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
23210-
23220-
23231-
2324SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
2325concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
23261-
23270-
23280-
2329SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
2330concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
23310-
23321-
23330-
2334DROP TABLE t1;
2335CREATE TABLE t1 ( a double, b double );
2336INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
2337SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
2338a
23393
2340SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
2341a
23421
2343SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
2344a
23452
2346SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
2347a
23482
23493
2350SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
2351a
23521
23532
2354SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
2355a
23561
23573
2358SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
2359a
23603
2361SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
2362a
23631
2364SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
2365a
23662
2367SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
2368a
23692
23703
2371SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
2372a
23731
23742
2375SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
2376a
23771
23783
2379DROP TABLE t1;
2380CREATE TABLE t1 ( a char(1), b char(1));
2381INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
2382SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
2383a
23843
2385SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
2386a
23871
2388SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
2389a
23902
2391SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
2392a
23932
23943
2395SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
2396a
23971
23982
2399SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
2400a
24011
24023
2403SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
2404a
24053
2406SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
2407a
24081
2409SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
2410a
24112
2412SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
2413a
24142
24153
2416SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
2417a
24181
24192
2420SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
2421a
24221
24233
2424DROP TABLE t1;
2425create table t1 (a int, b int);
2426insert into t1 values (1,2),(3,4);
2427select * from t1 up where exists (select * from t1 where t1.a=up.a);
2428a	b
24291	2
24303	4
2431explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
2432id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
24331	PRIMARY	up	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
24342	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
2435Warnings:
2436Note	1276	Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2437Note	1003	/* select#1 */ select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(<in_optimizer>(`test`.`up`.`a`,`test`.`up`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` where 1 ), <primary_index_lookup>(`test`.`up`.`a` in <temporary table> on distinct_key where `test`.`up`.`a` = `<subquery2>`.`a`))))
2438drop table t1;
2439CREATE TABLE t1 (t1_a int);
2440INSERT INTO t1 VALUES (1);
2441CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
2442INSERT INTO t2 VALUES (1, 1), (1, 2);
2443SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
2444HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
2445t1_a	t2_a	t2_b
24461	1	2
2447DROP TABLE t1, t2;
2448CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
2449INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
2450CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
2451INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
2452SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
2453id	name	id	pet
24541	Tim	1	Fido
24552	Rebecca	2	Spot
24563	NULL	3	Felix
2457drop table t1,t2;
2458CREATE TABLE t1 ( a int, b int );
2459CREATE TABLE t2 ( c int, d int );
2460INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
2461SELECT a AS abc, b FROM t1 outr WHERE b =
2462(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2463abc	b
24641	2
24652	3
24663	4
2467INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
2468(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2469select * from t2;
2470c	d
24711	2
24722	3
24733	4
2474CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
2475(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2476select * from t3;
2477abc	b
24781	2
24792	3
24803	4
2481prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2482execute stmt1;
2483deallocate prepare stmt1;
2484select * from t2;
2485c	d
24861	2
24872	3
24883	4
24891	2
24902	3
24913	4
2492drop table t3;
2493prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2494execute stmt1;
2495select * from t3;
2496abc	b
24971	2
24982	3
24993	4
2500deallocate prepare stmt1;
2501DROP TABLE t1, t2, t3;
2502CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2503insert into t1 values (1);
2504CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2505insert into t2 values (1,2);
2506select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
2507a	C
25081	1
2509drop table t1,t2;
2510create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
2511insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
2512create table t2 (a int);
2513insert into t2 values (1),(3),(2),(7);
2514select a,b from t1 where match(b) against ('Ball') > 0;
2515a	b
25161	ball
25172	ball games
2518select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
2519a
25201
25212
2522drop table t1,t2;
2523CREATE 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);
2524CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
2525INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
2526INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
2527INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
2528INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
2529SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
2530IZAVORGANG_ID
2531D0000000001
2532drop table t1;
2533CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2534CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2535insert into t1 values (1,1),(1,2),(2,1),(2,2);
2536insert into t2 values (1,2),(2,2);
2537select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2538aid	bid
25391	1
25402	1
2541alter table t2 drop primary key;
2542alter table t2 add key KEY1 (aid, bid);
2543select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2544aid	bid
25451	1
25462	1
2547alter table t2 drop key KEY1;
2548alter table t2 add primary key (bid, aid);
2549select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2550aid	bid
25511	1
25522	1
2553drop table t1,t2;
2554CREATE TABLE t1 (howmanyvalues bigint, avalue int);
2555INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
2556SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
2557howmanyvalues	count(*)
25581	1
25592	2
25603	3
25614	4
2562SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2563howmanyvalues	mycount
25641	1
25652	2
25663	3
25674	4
2568CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
2569SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
2570howmanyvalues	mycount
25711	1
25722	2
25733	3
25744	4
2575SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2576howmanyvalues	mycount
25771	1
25782	2
25793	3
25804	4
2581SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
2582howmanyvalues	mycount
25831	1
25842	1
25853	1
25864	1
2587drop table t1;
2588create table t1 (x int);
2589select  (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
2590(select b.x from t1 as b where b.x=a.x)
2591drop table t1;
2592CREATE 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`));
2593INSERT 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);
2594CREATE 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`)) ;
2595INSERT 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');
2596SELECT 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;
2597ERROR 42S22: Unknown column 'b.sc' in 'field list'
2598SELECT 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;
2599ac
2600700
2601NULL
2602drop tables t1,t2;
2603create table t1 (a int not null, b int not null, c int, primary key (a,b));
2604insert into t1 values (1,1,1), (2,2,2), (3,3,3);
2605set @b:= 0;
2606explain select sum(a) from t1 where b > @b;
2607id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26081	SIMPLE	t1	index	NULL	PRIMARY	8	NULL	3	Using where; Using index
2609set @a:= (select sum(a) from t1 where b > @b);
2610explain select a from t1 where c=2;
2611id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26121	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2613do @a:= (select sum(a) from t1 where b > @b);
2614explain select a from t1 where c=2;
2615id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26161	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2617drop table t1;
2618connect  root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK;
2619connection root;
2620set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
2621disconnect root;
2622connection default;
2623create table t1 (a int, b int);
2624create table t2 (a int, b int);
2625insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
2626insert into t2 values (1,3),(2,1);
2627select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
2628a	b	(select max(b) from t2 where t1.b=t2.a)
26291	1	3
26301	2	1
26311	3	NULL
26322	4	NULL
26332	5	NULL
2634drop table t1, t2;
2635create table t1 (id int);
2636create table t2 (id int, body text, fulltext (body));
2637insert into t1 values(1),(2),(3);
2638insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
2639select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
2640count(distinct id)
26411
2642drop table t2,t1;
2643create table t1 (s1 int,s2 int);
2644insert into t1 values (20,15);
2645select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
2646s1	s2
2647drop table t1;
2648create table t1 (s1 int);
2649insert into t1 values (1),(null);
2650select * from t1 where s1 < all (select s1 from t1);
2651s1
2652select s1, s1 < all (select s1 from t1) from t1;
2653s1	s1 < all (select s1 from t1)
26541	0
2655NULL	NULL
2656drop table t1;
2657CREATE TABLE t1 (
2658Code char(3) NOT NULL default '',
2659Name char(52) NOT NULL default '',
2660Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
2661Region char(26) NOT NULL default '',
2662SurfaceArea float(10,2) NOT NULL default '0.00',
2663IndepYear smallint(6) default NULL,
2664Population int(11) NOT NULL default '0',
2665LifeExpectancy float(3,1) default NULL,
2666GNP float(10,2) default NULL,
2667GNPOld float(10,2) default NULL,
2668LocalName char(45) NOT NULL default '',
2669GovernmentForm char(45) NOT NULL default '',
2670HeadOfState char(60) default NULL,
2671Capital int(11) default NULL,
2672Code2 char(2) NOT NULL default ''
2673) ENGINE=MyISAM;
2674INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
2675INSERT 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');
2676INSERT 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');
2677INSERT 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');
2678/*!40000 ALTER TABLE t1 ENABLE KEYS */;
2679SELECT DISTINCT Continent AS c FROM t1 outr WHERE
2680Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
2681Population < 200);
2682c
2683Oceania
2684drop table t1;
2685create table t1 (a1 int);
2686create table t2 (b1 int);
2687select * from t1 where a2 > any(select b1 from t2);
2688ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
2689select * from t1 where a1 > any(select b1 from t2);
2690a1
2691drop table t1,t2;
2692create table t1 (a integer, b integer);
2693select (select * from t1) = (select 1,2);
2694(select * from t1) = (select 1,2)
2695NULL
2696select (select 1,2) = (select * from t1);
2697(select 1,2) = (select * from t1)
2698NULL
2699select  row(1,2) = ANY (select * from t1);
2700row(1,2) = ANY (select * from t1)
27010
2702select  row(1,2) != ALL (select * from t1);
2703row(1,2) != ALL (select * from t1)
27041
2705drop table t1;
2706create table t1 (a integer, b integer);
2707select row(1,(2,2)) in (select * from t1 );
2708ERROR 21000: Operand should contain 2 column(s)
2709select row(1,(2,2)) = (select * from t1 );
2710ERROR 21000: Operand should contain 2 column(s)
2711select (select * from t1) = row(1,(2,2));
2712ERROR 21000: Operand should contain 1 column(s)
2713drop table t1;
2714create  table t1 (a integer);
2715insert into t1 values (1);
2716select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
2717ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2718select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2719ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2720select 1 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
2721xx	1 = ALL (  select 1 from t1 where 1 = xx )
27221	1
2723select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2724ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2725select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
2726ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2727drop table t1;
2728CREATE TABLE t1 (
2729categoryId int(11) NOT NULL,
2730courseId int(11) NOT NULL,
2731startDate datetime NOT NULL,
2732endDate datetime NOT NULL,
2733createDate datetime NOT NULL,
2734modifyDate timestamp NOT NULL,
2735attributes text NOT NULL
2736);
2737INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2738(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2739(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2740(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
2741(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
2742(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2743(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2744(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2745(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
2746CREATE TABLE t2 (
2747userId int(11) NOT NULL,
2748courseId int(11) NOT NULL,
2749date datetime NOT NULL
2750);
2751INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
2752(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
2753(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
2754(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
2755(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
2756(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
2757CREATE TABLE t3 (
2758groupId int(11) NOT NULL,
2759parentId int(11) NOT NULL,
2760startDate datetime NOT NULL,
2761endDate datetime NOT NULL,
2762createDate datetime NOT NULL,
2763modifyDate timestamp NOT NULL,
2764ordering int(11)
2765);
2766INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
2767CREATE TABLE t4 (
2768id int(11) NOT NULL,
2769groupTypeId int(11) NOT NULL,
2770groupKey varchar(50) NOT NULL,
2771name text,
2772ordering int(11),
2773description text,
2774createDate datetime NOT NULL,
2775modifyDate timestamp NOT NULL
2776);
2777INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
2778(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
2779CREATE TABLE t5 (
2780userId int(11) NOT NULL,
2781groupId int(11) NOT NULL,
2782createDate datetime NOT NULL,
2783modifyDate timestamp NOT NULL
2784);
2785INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
2786select
2787count(distinct t2.userid) pass,
2788groupstuff.*,
2789count(t2.courseid) crse,
2790t1.categoryid,
2791t2.courseid,
2792date_format(date, '%b%y') as colhead
2793from t2
2794join t1 on t2.courseid=t1.courseid
2795join
2796(
2797select
2798t5.userid,
2799parentid,
2800parentgroup,
2801childid,
2802groupname,
2803grouptypeid
2804from t5
2805join
2806(
2807select t4.id as parentid,
2808t4.name as parentgroup,
2809t4.id as childid,
2810t4.name as groupname,
2811t4.grouptypeid
2812from t4
2813) as gin on t5.groupid=gin.childid
2814) as groupstuff on t2.userid = groupstuff.userid
2815group by
2816groupstuff.groupname, colhead , t2.courseid;
2817pass	userid	parentid	parentgroup	childid	groupname	grouptypeid	crse	categoryid	courseid	colhead
28181	5141	12	group2	12	group2	5	1	5	12	Aug04
28191	5141	12	group2	12	group2	5	1	1	41	Aug04
28201	5141	12	group2	12	group2	5	1	2	52	Aug04
28211	5141	12	group2	12	group2	5	1	2	53	Aug04
28221	5141	12	group2	12	group2	5	1	3	51	Oct04
28231	5141	12	group2	12	group2	5	1	1	86	Oct04
28241	5141	12	group2	12	group2	5	1	1	87	Oct04
28251	5141	12	group2	12	group2	5	1	2	88	Oct04
28261	5141	12	group2	12	group2	5	1	2	89	Oct04
2827drop table t1, t2, t3, t4, t5;
2828create table t1 (a int);
2829insert into t1 values (1), (2), (3);
2830SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
28311
28321
28331
28341
2835drop table t1;
2836create table t1 (a int);
2837create table t2 (a int);
2838insert into t1 values (1),(2);
2839insert into t2 values (0),(1),(2),(3);
2840select a from t2 where a in (select a from t1);
2841a
28421
28432
2844select a from t2 having a in (select a from t1);
2845a
28461
28472
2848prepare stmt1 from "select a from t2 where a in (select a from t1)";
2849execute stmt1;
2850a
28511
28522
2853execute stmt1;
2854a
28551
28562
2857deallocate prepare stmt1;
2858prepare stmt1 from "select a from t2 having a in (select a from t1)";
2859execute stmt1;
2860a
28611
28622
2863execute stmt1;
2864a
28651
28662
2867deallocate prepare stmt1;
2868drop table t1, t2;
2869create table t1 (a int, b int);
2870insert into t1 values (1,2);
2871select 1 = (select * from t1);
2872ERROR HY000: Illegal parameter data types int and row for operation '='
2873select (select * from t1) = 1;
2874ERROR HY000: Illegal parameter data types row and int for operation '='
2875select (1,2) = (select a from t1);
2876ERROR HY000: Illegal parameter data types row and int for operation '='
2877select (select a from t1) = (1,2);
2878ERROR HY000: Illegal parameter data types int and row for operation '='
2879select (1,2,3) = (select * from t1);
2880ERROR 21000: Operand should contain 3 column(s)
2881select (select * from t1) = (1,2,3);
2882ERROR 21000: Operand should contain 2 column(s)
2883drop table t1;
2884CREATE TABLE `t1` (
2885`itemid` bigint(20) unsigned NOT NULL auto_increment,
2886`sessionid` bigint(20) unsigned default NULL,
2887`time` int(10) unsigned NOT NULL default '0',
2888`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2889NULL default '',
2890`data` text collate latin1_general_ci NOT NULL,
2891PRIMARY KEY  (`itemid`)
2892) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2893INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2894CREATE TABLE `t2` (
2895`sessionid` bigint(20) unsigned NOT NULL auto_increment,
2896`pid` int(10) unsigned NOT NULL default '0',
2897`date` int(10) unsigned NOT NULL default '0',
2898`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2899PRIMARY KEY  (`sessionid`)
2900) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2901INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2902SELECT 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;
2903ip	count( e.itemid )
290410.10.10.1	1
2905drop tables t1,t2;
2906create table t1 (fld enum('0','1'));
2907insert into t1 values ('1');
2908select * from (select max(fld) from t1) as foo;
2909max(fld)
29101
2911drop table t1;
2912set @tmp11867_optimizer_switch=@@optimizer_switch;
2913set optimizer_switch='semijoin_with_cache=off';
2914CREATE TABLE t1 (one int, two int, flag char(1));
2915CREATE TABLE t2 (one int, two int, flag char(1));
2916INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2917INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2918SELECT * FROM t1
2919WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
2920one	two	flag
29215	6	N
29227	8	N
2923SELECT * FROM t1
2924WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
2925one	two	flag
29265	6	N
29277	8	N
2928insert into t2 values (null,null,'N');
2929insert into t2 values (null,3,'0');
2930insert into t2 values (null,5,'0');
2931insert into t2 values (10,null,'0');
2932insert into t1 values (10,3,'0');
2933insert into t1 values (10,5,'0');
2934insert into t1 values (10,10,'0');
2935SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
2936one	two	test
29371	2	NULL
29382	3	NULL
29393	4	NULL
29405	6	1
29417	8	1
294210	3	NULL
294310	5	NULL
294410	10	NULL
2945SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2946one	two
29475	6
29487	8
2949SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
2950one	two	test
29511	2	NULL
29522	3	NULL
29533	4	NULL
29545	6	1
29557	8	1
295610	3	NULL
295710	5	NULL
295810	10	NULL
2959SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2960one	two	test
29611	2	0
29622	3	NULL
29633	4	0
29645	6	0
29657	8	0
296610	3	NULL
296710	5	NULL
296810	10	NULL
2969SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2970one	two	test
29711	2	0
29722	3	NULL
29733	4	0
29745	6	0
29757	8	0
297610	3	NULL
297710	5	NULL
297810	10	NULL
2979explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2980id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29811	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29822	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2983Warnings:
2984Note	1003	/* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`)))) AS `test` from `test`.`t1`
2985explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2986id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29871	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00	Using where
29882	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2989Warnings:
2990Note	1003	/* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` where <expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = 'N' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`))))
2991explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2992id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29931	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29942	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2995Warnings:
2996Note	1003	/* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`)))) AS `test` from `test`.`t1`
2997DROP TABLE t1,t2;
2998set optimizer_switch=@tmp11867_optimizer_switch;
2999CREATE TABLE t1 (a char(5), b char(5));
3000INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
3001SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
3002a	b
3003aaa	aaa
3004DROP TABLE t1;
3005CREATE TABLE t1 (a int);
3006CREATE TABLE t2 (a int, b int);
3007CREATE TABLE t3 (b int NOT NULL);
3008INSERT INTO t1 VALUES (1), (2), (3), (4);
3009INSERT INTO t2 VALUES (1,10), (3,30);
3010SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3011WHERE t3.b IS NOT NULL OR t2.a > 10;
3012a	b	b
3013SELECT * FROM t1
3014WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3015WHERE t3.b IS NOT NULL OR t2.a > 10);
3016a
30171
30182
30193
30204
3021DROP TABLE t1,t2,t3;
3022CREATE TABLE t1 (f1 INT);
3023CREATE TABLE t2 (f2 INT);
3024INSERT INTO t1 VALUES (1);
3025SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
3026f1
30271
3028SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
3029f1
30301
3031INSERT INTO t2 VALUES (1);
3032INSERT INTO t2 VALUES (2);
3033SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
3034f1
30351
3036DROP TABLE t1, t2;
3037select 1 from dual where 1 < any (select 2);
30381
30391
3040select 1 from dual where 1 < all (select 2);
30411
30421
3043select 1 from dual where 2 > any (select 1);
30441
30451
3046select 1 from dual where 2 > all (select 1);
30471
30481
3049select 1 from dual where 1 < any (select 2 from dual);
30501
30511
3052select 1 from dual where 1 < all (select 2 from dual where 1!=1);
30531
30541
3055create table t1 (s1 char);
3056insert into t1 values (1),(2);
3057select * from t1 where (s1 < any (select s1 from t1));
3058s1
30591
3060select * from t1 where not (s1 < any (select s1 from t1));
3061s1
30622
3063select * from t1 where (s1 < ALL (select s1+1 from t1));
3064s1
30651
3066select * from t1 where not(s1 < ALL (select s1+1 from t1));
3067s1
30682
3069select * from t1 where (s1+1 = ANY (select s1 from t1));
3070s1
30711
3072select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
3073s1
30742
3075select * from t1 where (s1 = ALL (select s1/s1 from t1));
3076s1
30771
3078select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
3079s1
30802
3081drop table t1;
3082create table t1 (
3083retailerID varchar(8) NOT NULL,
3084statusID   int(10) unsigned NOT NULL,
3085changed    datetime NOT NULL,
3086UNIQUE KEY retailerID (retailerID, statusID, changed)
3087);
3088INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
3089INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
3090INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
3091INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
3092INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
3093INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
3094select * from t1 r1
3095where (r1.retailerID,(r1.changed)) in
3096(SELECT r2.retailerId,(max(changed)) from t1 r2
3097group by r2.retailerId);
3098retailerID	statusID	changed
30990026	2	2006-01-06 12:25:53
31000037	2	2006-01-06 12:25:53
31010048	1	2006-01-06 12:37:50
31020059	1	2006-01-06 12:37:50
3103drop table t1;
3104create table t1(a int, primary key (a));
3105insert into t1 values (10);
3106create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
3107insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
3108insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999');
3109analyze table t1;
3110Table	Op	Msg_type	Msg_text
3111test.t1	analyze	status	Engine-independent statistics collected
3112test.t1	analyze	status	OK
3113explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3114ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3115             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3116id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31171	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31181	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31192	SUBQUERY	t2	range	b	b	40	NULL	3	Using where
3120SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3121ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3122            ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3123a	a	b
312410	3	35989
3125explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3126ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3127            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3128id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31291	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31301	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31312	SUBQUERY	t2	range	b	b	40	NULL	3	Using index condition
3132SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3133ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3134            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3135a	a	b
313610	1	359
3137drop table t1,t2;
3138CREATE TABLE t1 (
3139field1 int NOT NULL,
3140field2 int NOT NULL,
3141field3 int NOT NULL,
3142PRIMARY KEY  (field1,field2,field3)
3143);
3144CREATE TABLE t2 (
3145fieldA int NOT NULL,
3146fieldB int NOT NULL,
3147PRIMARY KEY  (fieldA,fieldB)
3148);
3149INSERT INTO t1 VALUES
3150(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
3151INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
3152SELECT field1, field2, COUNT(*)
3153FROM t1 GROUP BY field1, field2;
3154field1	field2	COUNT(*)
31551	1	2
31561	2	3
31571	3	1
3158SELECT field1, field2
3159FROM  t1
3160GROUP BY field1, field2
3161HAVING COUNT(*) >= ALL (SELECT fieldB
3162FROM t2 WHERE fieldA = field1);
3163field1	field2
31641	2
3165SELECT field1, field2
3166FROM  t1
3167GROUP BY field1, field2
3168HAVING COUNT(*) < ANY (SELECT fieldB
3169FROM t2 WHERE fieldA = field1);
3170field1	field2
31711	1
31721	3
3173DROP TABLE t1, t2;
3174CREATE TABLE t1(a int, INDEX (a));
3175INSERT INTO t1 VALUES (1), (3), (5), (7);
3176INSERT INTO t1 VALUES (NULL);
3177CREATE TABLE t2(a int);
3178INSERT INTO t2 VALUES (1),(2),(3);
3179EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
3180id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31811	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3
31822	MATERIALIZED	t1	index	a	a	5	NULL	5	Using index
3183SELECT a, a IN (SELECT a FROM t1) FROM t2;
3184a	a IN (SELECT a FROM t1)
31851	1
31862	NULL
31873	1
3188DROP TABLE t1,t2;
3189CREATE TABLE t1 (a DATETIME);
3190INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
3191CREATE TABLE t2 AS SELECT
3192(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
3193FROM t1 WHERE a > '2000-01-01';
3194SHOW CREATE TABLE t2;
3195Table	Create Table
3196t2	CREATE TABLE `t2` (
3197  `sub_a` datetime DEFAULT NULL
3198) ENGINE=MyISAM DEFAULT CHARSET=latin1
3199CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
3200SHOW CREATE TABLE t3;
3201Table	Create Table
3202t3	CREATE TABLE `t3` (
3203  `a` datetime DEFAULT NULL
3204) ENGINE=MyISAM DEFAULT CHARSET=latin1
3205DROP TABLE t1,t2,t3;
3206CREATE TABLE t1 (a int);
3207INSERT INTO t1 VALUES (1), (2);
3208SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
3209a
3210SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3211a
32121
32132
3214EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3215id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
32161	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
32172	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
3218DROP TABLE t1;
3219CREATE TABLE t1 (a int);
3220INSERT INTO t1 VALUES (2), (4), (1), (3);
3221CREATE TABLE t2 (b int, c int);
3222INSERT INTO t2 VALUES
3223(2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
3224SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
3225a
32262
32274
32281
32293
3230SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
3231ERROR 21000: Subquery returns more than 1 row
3232SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
3233a
32341
32352
32363
32374
3238SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
3239ERROR 21000: Subquery returns more than 1 row
3240SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
3241b	MAX(c)
32421	4
32432	2
32444	4
3245SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
3246ERROR 21000: Subquery returns more than 1 row
3247SELECT a FROM t1 GROUP BY a
3248HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
3249(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3250a
32511
32522
32533
32544
3255SELECT a FROM t1 GROUP BY a
3256HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
3257(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3258ERROR 21000: Subquery returns more than 1 row
3259SELECT a FROM t1 GROUP BY a
3260HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3261(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3262a
32634
3264SELECT a FROM t1 GROUP BY a
3265HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3266(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
3267ERROR 21000: Subquery returns more than 1 row
3268SELECT a FROM t1
3269ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
3270(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3271a
32721
32732
32743
32754
3276SELECT a FROM t1
3277ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
3278(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3279ERROR 21000: Subquery returns more than 1 row
3280SELECT a FROM t1
3281ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3282(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3283a
32841
32852
32863
32874
3288SELECT a FROM t1
3289ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3290(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3291ERROR 21000: Subquery returns more than 1 row
3292DROP TABLE t1,t2;
3293create table t1 (df decimal(5,1));
3294insert into t1 values(1.1);
3295insert into t1 values(2.2);
3296select * from t1 where df <= all (select avg(df) from t1 group by df);
3297df
32981.1
3299select * from t1 where df >= all (select avg(df) from t1 group by df);
3300df
33012.2
3302drop table t1;
3303create table t1 (df decimal(5,1));
3304insert into t1 values(1.1);
3305select 1.1 * exists(select * from t1);
33061.1 * exists(select * from t1)
33071.1
3308drop table t1;
3309CREATE TABLE t1 (
3310grp int(11) default NULL,
3311a decimal(10,2) default NULL);
3312insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
3313select * from t1;
3314grp	a
33151	1.00
33162	2.00
33172	3.00
33183	4.00
33193	5.00
33203	6.00
3321NULL	NULL
3322select min(a) from t1 group by grp;
3323min(a)
3324NULL
33251.00
33262.00
33274.00
3328drop table t1;
3329CREATE table t1 ( c1 integer );
3330INSERT INTO t1 VALUES ( 1 );
3331INSERT INTO t1 VALUES ( 2 );
3332INSERT INTO t1 VALUES ( 3 );
3333CREATE TABLE t2 ( c2 integer );
3334INSERT INTO t2 VALUES ( 1 );
3335INSERT INTO t2 VALUES ( 4 );
3336INSERT INTO t2 VALUES ( 5 );
3337SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
3338c1	c2
33391	1
3340SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
3341WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
3342c1	c2
33431	1
3344DROP TABLE t1,t2;
3345CREATE TABLE t1 ( c1 integer );
3346INSERT INTO t1 VALUES ( 1 );
3347INSERT INTO t1 VALUES ( 2 );
3348INSERT INTO t1 VALUES ( 3 );
3349INSERT INTO t1 VALUES ( 6 );
3350CREATE TABLE t2 ( c2 integer );
3351INSERT INTO t2 VALUES ( 1 );
3352INSERT INTO t2 VALUES ( 4 );
3353INSERT INTO t2 VALUES ( 5 );
3354INSERT INTO t2 VALUES ( 6 );
3355CREATE TABLE t3 ( c3 integer );
3356INSERT INTO t3 VALUES ( 7 );
3357INSERT INTO t3 VALUES ( 8 );
3358SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
3359WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
3360c1	c2
33612	NULL
33623	NULL
3363DROP TABLE t1,t2,t3;
3364CREATE TABLE `t1` (
3365`itemid` bigint(20) unsigned NOT NULL auto_increment,
3366`sessionid` bigint(20) unsigned default NULL,
3367`time` int(10) unsigned NOT NULL default '0',
3368`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
3369NULL default '',
3370`data` text collate latin1_general_ci NOT NULL,
3371PRIMARY KEY  (`itemid`)
3372) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3373INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
3374CREATE TABLE `t2` (
3375`sessionid` bigint(20) unsigned NOT NULL auto_increment,
3376`pid` int(10) unsigned NOT NULL default '0',
3377`date` int(10) unsigned NOT NULL default '0',
3378`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
3379PRIMARY KEY  (`sessionid`)
3380) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3381INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
3382SELECT 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;
3383ip	count( e.itemid )
338410.10.10.1	1
3385drop tables t1,t2;
3386CREATE TABLE t1 (EMPNUM   CHAR(3));
3387CREATE TABLE t2 (EMPNUM   CHAR(3) );
3388INSERT INTO t1 VALUES ('E1'),('E2');
3389INSERT INTO t2 VALUES ('E1');
3390DELETE FROM t1
3391WHERE t1.EMPNUM NOT IN
3392(SELECT t2.EMPNUM
3393FROM t2
3394WHERE t1.EMPNUM = t2.EMPNUM);
3395select * from t1;
3396EMPNUM
3397E1
3398DROP TABLE t1,t2;
3399CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
3400INSERT INTO t1 VALUES (1, 1);
3401CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
3402PRIMARY KEY(select_id,values_id));
3403INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
3404SELECT values_id FROM t1
3405WHERE values_id IN (SELECT values_id FROM t2
3406WHERE select_id IN (1, 0));
3407values_id
34081
3409SELECT values_id FROM t1
3410WHERE values_id IN (SELECT values_id FROM t2
3411WHERE select_id BETWEEN 0 AND 1);
3412values_id
34131
3414SELECT values_id FROM t1
3415WHERE values_id IN (SELECT values_id FROM t2
3416WHERE select_id = 0 OR select_id = 1);
3417values_id
34181
3419DROP TABLE t1, t2;
3420create table t1 (fld enum('0','1'));
3421insert into t1 values ('1');
3422select * from (select max(fld) from t1) as foo;
3423max(fld)
34241
3425drop table t1;
3426CREATE TABLE t1 (a int, b int);
3427CREATE TABLE t2 (c int, d int);
3428CREATE TABLE t3 (e int);
3429INSERT INTO t1 VALUES
3430(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
3431INSERT INTO t2 VALUES
3432(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
3433INSERT INTO t3 VALUES (10), (30), (10), (20) ;
3434SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
3435a	MAX(b)	MIN(b)
34361	20	10
34372	30	10
34383	20	20
34394	40	40
3440SELECT * FROM t2;
3441c	d
34422	10
34432	20
34444	10
34455	10
34463	20
34472	40
3448SELECT * FROM t3;
3449e
345010
345130
345210
345320
3454SELECT a FROM t1 GROUP BY a
3455HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
3456a
34572
34584
3459SELECT a FROM t1 GROUP BY a
3460HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
3461a
34622
3463SELECT a FROM t1 GROUP BY a
3464HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
3465a
34662
34674
3468SELECT a FROM t1 GROUP BY a
3469HAVING a IN (SELECT c FROM t2
3470WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3471a
34722
34733
3474SELECT a FROM t1 GROUP BY a
3475HAVING a IN (SELECT c FROM t2
3476WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3477a
34782
34793
3480SELECT a FROM t1 GROUP BY a
3481HAVING a IN (SELECT c FROM t2
3482WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3483a
34842
3485SELECT a FROM t1 GROUP BY a
3486HAVING a IN (SELECT c FROM t2
3487WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
3488a
34892
3490SELECT a FROM t1 GROUP BY a
3491HAVING a IN (SELECT c FROM t2
3492WHERE MIN(b) < d AND
3493EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3494a
34952
3496SELECT a, SUM(a) FROM t1 GROUP BY a;
3497a	SUM(a)
34981	2
34992	6
35003	3
35014	4
3502SELECT a FROM t1
3503WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
3504a
35053
35064
3507SELECT a FROM t1 GROUP BY a
3508HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
3509a
35101
35113
35124
3513SELECT a FROM t1
3514WHERE a < 3 AND
3515EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
3516a
35171
35182
3519SELECT a FROM t1
3520WHERE a < 3 AND
3521EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
3522a
35231
35242
35251
35262
35272
3528SELECT t1.a FROM t1 GROUP BY t1.a
3529HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
3530HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3531HAVING SUM(t1.a+t2.c) < t3.e/4));
3532a
35331
35342
3535SELECT t1.a FROM t1 GROUP BY t1.a
3536HAVING t1.a > ALL(SELECT t2.c FROM t2
3537WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3538HAVING SUM(t1.a+t2.c) < t3.e/4));
3539a
35404
3541SELECT t1.a FROM t1 GROUP BY t1.a
3542HAVING t1.a > ALL(SELECT t2.c FROM t2
3543WHERE EXISTS(SELECT t3.e FROM t3
3544WHERE SUM(t1.a+t2.c) < t3.e/4));
3545ERROR HY000: Invalid use of group function
3546SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
3547ERROR HY000: Invalid use of group function
3548SELECT t1.a FROM t1 GROUP BY t1.a
3549HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3550HAVING AVG(t2.c+SUM(t1.b)) > 20);
3551a
35522
35533
35544
3555SELECT t1.a FROM t1 GROUP BY t1.a
3556HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3557HAVING AVG(SUM(t1.b)) > 20);
3558a
35592
35604
3561SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
3562HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3563HAVING t2.c+sum > 20);
3564a	sum
35652	60
35663	20
35674	40
3568DROP TABLE t1,t2,t3;
3569CREATE TABLE t1 (a varchar(5), b varchar(10));
3570INSERT INTO t1 VALUES
3571('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
3572('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
3573SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3574a	b
3575BBB	4
3576CCC	7
3577AAA	8
3578EXPLAIN
3579SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3580id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
35811	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	Using where
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	NULL	NULL	NULL	NULL	9	Using where
35932	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	9	Using temporary
3594DROP TABLE t1;
3595create table t1( f1 int,f2 int);
3596insert into t1 values (1,1),(2,2);
3597select 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';
3598t
3599crash1
3600crash1
3601drop table t1;
3602create table t1 (c int, key(c));
3603insert into t1 values (1142477582), (1142455969);
3604create table t2 (a int, b int);
3605insert into t2 values (2, 1), (1, 0);
3606delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
3607drop table t1, t2;
3608CREATE TABLE t1 (a INT);
3609CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3610ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3611CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
3612ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
3613SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3614ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3615DROP TABLE t1;
3616create table t1 (i int, j bigint);
3617insert into t1 values (1, 2), (2, 2), (3, 2);
3618select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
3619min(i)
36201
3621drop table t1;
3622CREATE TABLE t1 (i BIGINT UNSIGNED);
3623INSERT INTO t1 VALUES (10000000000000000000);
3624INSERT INTO t1 VALUES (1);
3625CREATE TABLE t2 (i BIGINT UNSIGNED);
3626INSERT INTO t2 VALUES (10000000000000000000);
3627INSERT INTO t2 VALUES (1);
3628/* simple test */
3629SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
3630i
363110000000000000000000
36321
3633/* subquery test */
3634SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3635i
363610000000000000000000
3637/* subquery test with cast*/
3638SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
3639i
364010000000000000000000
3641DROP TABLE t1;
3642DROP TABLE t2;
3643CREATE TABLE t1 (
3644id bigint(20) unsigned NOT NULL auto_increment,
3645name varchar(255) NOT NULL,
3646PRIMARY KEY  (id)
3647);
3648INSERT INTO t1 VALUES
3649(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
3650CREATE TABLE t2 (
3651id bigint(20) unsigned NOT NULL auto_increment,
3652mid bigint(20) unsigned NOT NULL,
3653date date NOT NULL,
3654PRIMARY KEY  (id)
3655);
3656INSERT INTO t2 VALUES
3657(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
3658(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
3659SELECT *,
3660(SELECT date FROM t2 WHERE mid = t1.id
3661ORDER BY date DESC LIMIT 0, 1) AS date_last,
3662(SELECT date FROM t2 WHERE mid = t1.id
3663ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
3664FROM t1;
3665id	name	date_last	date_next_to_last
36661	Balazs	2006-05-01	NULL
36672	Joe	2006-04-20	NULL
36683	Frank	2006-04-13	NULL
3669SELECT *,
3670(SELECT COUNT(*) FROM t2 WHERE mid = t1.id
3671ORDER BY date DESC LIMIT 1, 1) AS date_count
3672FROM t1;
3673id	name	date_count
36741	Balazs	NULL
36752	Joe	NULL
36763	Frank	NULL
3677SELECT *,
3678(SELECT date FROM t2 WHERE mid = t1.id
3679ORDER BY date DESC LIMIT 0, 1) AS date_last,
3680(SELECT date FROM t2 WHERE mid = t1.id
3681ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
3682FROM t1;
3683id	name	date_last	date_next_to_last
36841	Balazs	2006-05-01	2006-03-30
36852	Joe	2006-04-20	2006-04-06
36863	Frank	2006-04-13	NULL
3687DROP TABLE t1,t2;
3688CREATE TABLE t1 (
3689i1 int(11) NOT NULL default '0',
3690i2 int(11) NOT NULL default '0',
3691t datetime NOT NULL default '0000-00-00 00:00:00',
3692PRIMARY KEY  (i1,i2,t)
3693);
3694INSERT INTO t1 VALUES
3695(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
3696(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
3697(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
3698(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
3699(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
3700(24,2,'2005-05-27 12:40:06');
3701CREATE TABLE t2 (
3702i1 int(11) NOT NULL default '0',
3703i2 int(11) NOT NULL default '0',
3704t datetime default NULL,
3705PRIMARY KEY  (i1)
3706);
3707INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
3708EXPLAIN
3709SELECT * FROM t1,t2
3710WHERE t1.t = (SELECT t1.t FROM t1
3711WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3712ORDER BY t1.t DESC LIMIT 1);
3713id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37141	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1
37151	PRIMARY	t1	index	NULL	PRIMARY	13	NULL	11	Using where; Using index
37162	SUBQUERY	t1	range	PRIMARY	PRIMARY	13	NULL	6	Using where; Using index
3717SELECT * FROM t1,t2
3718WHERE t1.t = (SELECT t1.t FROM t1
3719WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3720ORDER BY t1.t DESC LIMIT 1);
3721i1	i2	t	i1	i2	t
372224	1	2005-05-27 12:40:30	24	1	2006-06-20 12:29:40
3723DROP TABLE t1, t2;
3724CREATE TABLE t1 (i INT);
3725(SELECT i FROM t1) UNION (SELECT i FROM t1);
3726i
3727SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
3728(
3729(SELECT i FROM t1) UNION
3730(SELECT i FROM t1)
3731);
3732i
3733SELECT * FROM t1
3734WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
3735i
3736explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
3737from t1;
3738id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37391	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
37402	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
37413	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
3742NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
3743explain select * from t1 where not exists
3744((select t11.i from t1 t11) union (select t12.i from t1 t12));
3745id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37461	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
37472	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
37483	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
3749NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
3750DROP TABLE t1;
3751CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
3752insert into t1 (a) values (FLOOR(rand() * 100));
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;
3764insert into t1 (a) select FLOOR(rand() * 100) from t1;
3765insert into t1 (a) select FLOOR(rand() * 100) from t1;
3766SELECT a,
3767(SELECT REPEAT(' ',250) FROM t1 i1
3768WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
3769FROM t1 ORDER BY a LIMIT 5;
3770a	a
37710	NULL
37720	NULL
37730	NULL
37740	NULL
37750	NULL
3776DROP TABLE t1;
3777CREATE TABLE t1 (a INT, b INT);
3778CREATE TABLE t2 (a INT);
3779INSERT INTO t2 values (1);
3780INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
3781SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
3782(SELECT COUNT(DISTINCT t1.b) from t2)
37832
37841
37851
3786SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
3787FROM t1 GROUP BY t1.a;
3788(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
37892
37901
37911
3792SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
3793COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b))
37942	2
37951	1
37961	1
3797SELECT COUNT(DISTINCT t1.b),
3798(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
3799FROM t1 GROUP BY t1.a;
3800COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
38012	2
38021	1
38031	1
3804SELECT (
3805SELECT (
3806SELECT COUNT(DISTINCT t1.b)
3807)
3808)
3809FROM t1 GROUP BY t1.a;
3810(
3811SELECT (
3812SELECT COUNT(DISTINCT t1.b)
3813)
3814)
38152
38161
38171
3818SELECT (
3819SELECT (
3820SELECT (
3821SELECT COUNT(DISTINCT t1.b)
3822)
3823)
3824FROM t1 GROUP BY t1.a LIMIT 1)
3825FROM t1 t2
3826GROUP BY t2.a;
3827(
3828SELECT (
3829SELECT (
3830SELECT COUNT(DISTINCT t1.b)
3831)
3832)
3833FROM t1 GROUP BY t1.a LIMIT 1)
38342
38352
38362
3837DROP TABLE t1,t2;
3838CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
3839CREATE TABLE t2 (x int auto_increment, y int, z int,
3840PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
3841create table t3 (a int);
3842insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3843insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
3844from t3 A, t3 B, t3 C, t3 D where D.a<3;
3845insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
3846SET SESSION sort_buffer_size = 32 * 1024;
3847SELECT SQL_NO_CACHE COUNT(*)
3848FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3849FROM t1) t;
3850COUNT(*)
38513000
3852SET SESSION sort_buffer_size = 8 * 1024 * 1024;
3853SELECT SQL_NO_CACHE COUNT(*)
3854FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3855FROM t1) t;
3856COUNT(*)
38573000
3858DROP TABLE t1,t2,t3;
3859CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
3860CREATE TABLE t2 (c int);
3861INSERT INTO t1 VALUES ('aa', 1);
3862INSERT INTO t2 VALUES (1);
3863SELECT * FROM t1
3864WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3865UNION
3866SELECT c from t2 WHERE c=t1.c);
3867id	c
3868aa	1
3869INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
3870SELECT * FROM t1
3871WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3872UNION
3873SELECT c from t2 WHERE c=t1.c);
3874id	c
3875aa	1
3876bb	2
3877cc	3
3878dd	1
3879INSERT INTO t2 VALUES (2);
3880CREATE TABLE t3 (c int);
3881INSERT INTO t3 VALUES (1);
3882SELECT * FROM t1
3883WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
3884UNION
3885SELECT c from t2 WHERE c=t1.c);
3886id	c
3887aa	1
3888bb	2
3889cc	3
3890dd	1
3891DROP TABLE t1,t2,t3;
3892CREATE TABLE t1(f1 int);
3893CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
3894INSERT INTO t1 VALUES (1),(1),(2),(2);
3895INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
3896SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
3897sq
38982
38994
3900SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
3901tt
39022
39032
3904PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
3905EXECUTE stmt1;
3906sq
39072
39084
3909EXECUTE stmt1;
3910sq
39112
39124
3913DEALLOCATE PREPARE stmt1;
3914SELECT f2, AVG(f21),
3915(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
3916FROM t2 GROUP BY f2;
3917f2	AVG(f21)	test
39181	1.0000	2004-02-29 11:11:11
39192	2.0000	2004-02-29 11:11:11
3920DROP TABLE t1,t2;
3921CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
3922INSERT INTO t1 VALUES
3923(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
3924(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
3925(3,2,'k'), (3,1,'l'), (1,9,'m');
3926SELECT a, MAX(b),
3927(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
3928FROM t1 GROUP BY a;
3929a	MAX(b)	test
39301	9	m
39312	3	h
39323	4	i
3933DROP TABLE t1;
3934DROP TABLE IF EXISTS t1;
3935DROP TABLE IF EXISTS t2;
3936DROP TABLE IF EXISTS t1xt2;
3937CREATE TABLE t1 (
3938id_1 int(5) NOT NULL,
3939t varchar(4) DEFAULT NULL
3940);
3941CREATE TABLE t2 (
3942id_2 int(5) NOT NULL,
3943t varchar(4) DEFAULT NULL
3944);
3945CREATE TABLE t1xt2 (
3946id_1 int(5) NOT NULL,
3947id_2 int(5) NOT NULL
3948);
3949INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
3950INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
3951INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
3952SELECT DISTINCT t1.id_1 FROM t1 WHERE
3953(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3954id_1
3955SELECT DISTINCT t1.id_1 FROM t1 WHERE
3956(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3957id_1
3958SELECT DISTINCT t1.id_1 FROM t1 WHERE
3959(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3960id_1
3961SELECT DISTINCT t1.id_1 FROM t1 WHERE
3962(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3963id_1
39641
39652
39663
39674
3968SELECT DISTINCT t1.id_1 FROM t1 WHERE
3969(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
3970id_1
39711
39722
39733
39744
3975SELECT DISTINCT t1.id_1 FROM t1 WHERE
3976(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
3977id_1
39781
39792
39803
39814
3982insert INTO t1xt2 VALUES (1, 12);
3983SELECT DISTINCT t1.id_1 FROM t1 WHERE
3984(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3985id_1
39861
3987SELECT DISTINCT t1.id_1 FROM t1 WHERE
3988(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3989id_1
39901
3991SELECT DISTINCT t1.id_1 FROM t1 WHERE
3992(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3993id_1
39941
3995SELECT DISTINCT t1.id_1 FROM t1 WHERE
3996(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3997id_1
39982
39993
40004
4001SELECT DISTINCT t1.id_1 FROM t1 WHERE
4002(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4003id_1
40042
40053
40064
4007SELECT DISTINCT t1.id_1 FROM t1 WHERE
4008(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4009id_1
40102
40113
40124
4013insert INTO t1xt2 VALUES (2, 12);
4014SELECT DISTINCT t1.id_1 FROM t1 WHERE
4015(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4016id_1
40171
40182
4019SELECT DISTINCT t1.id_1 FROM t1 WHERE
4020(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4021id_1
40221
40232
4024SELECT DISTINCT t1.id_1 FROM t1 WHERE
4025(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4026id_1
40271
40282
4029SELECT DISTINCT t1.id_1 FROM t1 WHERE
4030(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4031id_1
40323
40334
4034SELECT DISTINCT t1.id_1 FROM t1 WHERE
4035(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4036id_1
40373
40384
4039SELECT DISTINCT t1.id_1 FROM t1 WHERE
4040(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4041id_1
40423
40434
4044DROP TABLE t1;
4045DROP TABLE t2;
4046DROP TABLE t1xt2;
4047CREATE TABLE t1 (a int);
4048INSERT INTO t1 VALUES (3), (1), (2);
4049SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
4050col1	col2
4051this is a test.	3
4052this is a test.	1
4053this is a test.	2
4054SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
4055col1	t2
4056this is a test.	3
4057this is a test.	1
4058this is a test.	2
4059DROP table t1;
4060CREATE TABLE t1 (a int, b int);
4061CREATE TABLE t2 (m int, n int);
4062INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4063INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4064SELECT COUNT(*), a,
4065(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
4066FROM t1 GROUP BY a;
4067COUNT(*)	a	(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
40682	2	2
40693	3	3
40701	4	1
4071SELECT COUNT(*), a,
4072(SELECT MIN(m) FROM t2 WHERE m = count(*))
4073FROM t1 GROUP BY a;
4074COUNT(*)	a	(SELECT MIN(m) FROM t2 WHERE m = count(*))
40752	2	2
40763	3	3
40771	4	1
4078SELECT COUNT(*), a
4079FROM t1 GROUP BY a
4080HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
4081COUNT(*)	a
40822	2
40833	3
4084DROP TABLE t1,t2;
4085CREATE TABLE t1 (a int, b int);
4086CREATE TABLE t2 (m int, n int);
4087INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4088INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4089SELECT COUNT(*) c, a,
4090(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
4091FROM t1 GROUP BY a;
4092c	a	(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
40932	2	2
40943	3	3
40951	4	1,1
4096SELECT COUNT(*) c, a,
4097(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
4098FROM t1 GROUP BY a;
4099c	a	(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
41002	2	3
41013	3	4
41021	4	2,2
4103DROP table t1,t2;
4104CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
4105INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
4106(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
4107(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
4108SELECT a, MAX(b),
4109(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
4110FROM t1 GROUP BY a;
4111a	MAX(b)	test
41121	9	m
41132	3	h
41143	4	i
4115SELECT a x, MAX(b),
4116(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
4117FROM t1 GROUP BY a;
4118x	MAX(b)	test
41191	9	m
41202	3	h
41213	4	i
4122SELECT a, AVG(b),
4123(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
4124FROM t1 WHERE t1.d=0 GROUP BY a;
4125a	AVG(b)	test
41261	4.0000	d
41272	2.0000	g
41283	2.5000	NULL
4129SELECT tt.a,
4130(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4131LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4132FROM t1 as tt;
4133a	test
41341	n
41351	n
41361	n
41371	n
41381	n
41391	n
41401	n
41412	o
41422	o
41432	o
41442	o
41453	p
41463	p
41473	p
41483	p
41493	p
4150SELECT tt.a,
4151(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4152LIMIT 1)
4153FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4154FROM t1 as tt GROUP BY tt.a;
4155a	test
41561	n
41572	o
41583	p
4159SELECT tt.a, MAX(
4160(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4161LIMIT 1)
4162FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
4163FROM t1 as tt GROUP BY tt.a;
4164a	test
41651	n
41662	o
41673	p
4168DROP TABLE t1;
4169CREATE TABLE t1 (a int, b int);
4170INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4171SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4172a
41731
41742
4175SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4176a
4177SELECT a FROM t1 t0
4178WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4179a
41801
41812
4182SET @@sql_mode='ansi';
4183SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4184ERROR HY000: Invalid use of group function
4185SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4186ERROR HY000: Invalid use of group function
4187SELECT a FROM t1 t0
4188WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4189ERROR HY000: Invalid use of group function
4190SET @@sql_mode=default;
4191DROP TABLE t1;
4192CREATE TABLE t1 (a INT);
4193INSERT INTO t1 values (1),(1),(1),(1);
4194CREATE TABLE t2 (x INT);
4195INSERT INTO t1 values (1000),(1001),(1002);
4196SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
4197ERROR HY000: Invalid use of group function
4198SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
4199ERROR HY000: Invalid use of group function
4200SELECT COUNT(1) FROM DUAL;
4201COUNT(1)
42021
4203SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
4204ERROR HY000: Invalid use of group function
4205SELECT
4206SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
4207FROM t1;
4208ERROR HY000: Invalid use of group function
4209SELECT t1.a as XXA,
4210SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
4211FROM t1;
4212ERROR HY000: Invalid use of group function
4213DROP TABLE t1,t2;
4214CREATE TABLE t1 (a int, b int, KEY (a));
4215INSERT INTO t1 VALUES (1,1),(2,1);
4216EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
4217id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
42181	PRIMARY	t1	ref	a	a	5	const	1	Using where; Using index
42192	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
4220DROP TABLE t1;
4221CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
4222INSERT INTO t1 VALUES
4223(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
4224CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
4225INSERT INTO t2 VALUES (7), (5), (1), (3);
4226SELECT id, st FROM t1
4227WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4228id	st
42293	FL
42301	GA
42317	FL
4232SELECT id, st FROM t1
4233WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4234GROUP BY id;
4235id	st
42361	GA
42373	FL
42387	FL
4239SELECT id, st FROM t1
4240WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4241id	st
42422	GA
42434	FL
4244SELECT id, st FROM t1
4245WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4246GROUP BY id;
4247id	st
42482	GA
42494	FL
4250DROP TABLE t1,t2;
4251CREATE TABLE t1 (a int);
4252INSERT INTO t1 VALUES (1), (2);
4253EXPLAIN EXTENDED
4254SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
4255id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
42561	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	100.00
42572	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
4258Warnings:
4259Note	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`
4260DROP TABLE t1;
4261CREATE TABLE t1 (
4262a varchar(255) default NULL,
4263b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
4264INDEX idx(a,b)
4265);
4266CREATE TABLE t2 (
4267a varchar(255) default NULL
4268);
4269INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
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 SELECT * FROM t1;
4277INSERT INTO t1 SELECT * FROM t1;
4278INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
4279INSERT INTO `t2` VALUES ('abcdefghijk');
4280INSERT INTO `t2` VALUES ('asdf');
4281SET session sort_buffer_size=8192;
4282SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
4283d1
42841
42851
4286DROP TABLE t1,t2;
4287CREATE TABLE t1 (a INTEGER, b INTEGER);
4288CREATE TABLE t2 (x INTEGER);
4289INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
4290INSERT INTO t2 VALUES (1), (2);
4291SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
4292ERROR 21000: Subquery returns more than 1 row
4293SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
4294ERROR 21000: Subquery returns more than 1 row
4295SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
4296(SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
42973.3333
4298DROP TABLE t1,t2;
4299CREATE TABLE t1 (a INT, b INT);
4300INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
4301SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
4302AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
4303GROUP BY a1.a;
4304a	COUNT(*)
43051	3
4306DROP TABLE t1;
4307CREATE TABLE t1 (a INT);
4308CREATE TABLE t2 (a INT);
4309INSERT INTO t1 VALUES (1),(2);
4310INSERT INTO t2 VALUES (1),(2);
4311SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
4312(SELECT SUM(t1.a) FROM t2 WHERE a=0)
4313NULL
4314SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
4315ERROR 21000: Subquery returns more than 1 row
4316SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
4317(SELECT SUM(t1.a) FROM t2 WHERE a=1)
43183
4319DROP TABLE t1,t2;
4320CREATE TABLE t1 (a1 INT, a2 INT);
4321CREATE TABLE t2 (b1 INT, b2 INT);
4322INSERT INTO t1 VALUES (100, 200);
4323INSERT INTO t1 VALUES (101, 201);
4324INSERT INTO t2 VALUES (101, 201);
4325INSERT INTO t2 VALUES (103, 203);
4326SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
4327((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
43280
43290
4330DROP TABLE t1, t2;
4331CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
4332INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
4333SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4334s1	s2
4335SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4336s1	s2
4337CREATE INDEX I1 ON t1 (s1);
4338CREATE INDEX I2 ON t1 (s2);
4339SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4340s1	s2
4341SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4342s1	s2
4343TRUNCATE t1;
4344INSERT INTO t1 VALUES (0x41,0x41);
4345SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
4346s1	s2
4347DROP TABLE t1;
4348CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
4349CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
4350CREATE TABLE t3 (a3 BINARY(2) default '0');
4351INSERT INTO t1 VALUES (1),(2),(3),(4);
4352INSERT INTO t2 VALUES (1),(2),(3);
4353INSERT INTO t3 VALUES (1),(2),(3);
4354SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
4355LEFT(t2.a2, 1)
43561
43572
43583
4359SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
4360a1	t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
43611	0
43622	0
43633	0
43644	0
4365DROP TABLE t1,t2,t3;
4366CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
4367CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
4368CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
4369INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
4370INSERT INTO t2 VALUES (2), (3), (4), (5);
4371INSERT INTO t3 VALUES (10), (20), (30);
4372SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
4373LEFT(t1.a1,1)
43741
43752
43763
4377SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
4378a2
4379DROP TABLE t1, t2, t3;
4380SET @save_optimizer_switch=@@optimizer_switch;
4381SET optimizer_switch='semijoin_with_cache=off';
4382SET optimizer_switch='materialization=off';
4383CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
4384INSERT INTO t1 VALUES ('a', 'aa');
4385INSERT INTO t1 VALUES ('a', 'aaa');
4386SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4387a	b
4388CREATE INDEX I1 ON t1 (a);
4389CREATE INDEX I2 ON t1 (b);
4390EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4391id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
43921	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
43932	DEPENDENT SUBQUERY	t1	index_subquery	I1	I1	2	func	2	Using index; Using where
4394SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4395a	b
4396CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
4397INSERT INTO t2 SELECT * FROM t1;
4398CREATE INDEX I1 ON t2 (a);
4399CREATE INDEX I2 ON t2 (b);
4400EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4401id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44021	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
44032	DEPENDENT SUBQUERY	t2	index_subquery	I1	I1	4	func	2	Using index; Using where
4404SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4405a	b
4406EXPLAIN
4407SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4408id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44091	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
44102	DEPENDENT SUBQUERY	t1	index_subquery	I1	I1	2	func	2	Using index; Using where
4411SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4412a	b
4413DROP TABLE t1,t2;
4414SET optimizer_switch= @save_optimizer_switch;
4415CREATE TABLE t1(a INT, b INT);
4416INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
4417EXPLAIN
4418SELECT a AS out_a, MIN(b) FROM t1
4419WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4420GROUP BY a;
4421ERROR 42S22: Unknown column 'out_a' in 'where clause'
4422SELECT a AS out_a, MIN(b) FROM t1
4423WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4424GROUP BY a;
4425ERROR 42S22: Unknown column 'out_a' in 'where clause'
4426EXPLAIN
4427SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4428WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4429GROUP BY a;
4430id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44311	PRIMARY	t1_outer	ALL	NULL	NULL	NULL	NULL	4	Using where; Using temporary; Using filesort
44322	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
4433SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4434WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4435GROUP BY a;
4436out_a	MIN(b)
44371	2
44382	4
4439DROP TABLE t1;
4440CREATE TABLE t1 (a INT);
4441CREATE TABLE t2 (a INT);
4442INSERT INTO t1 VALUES (1),(2);
4443INSERT INTO t2 VALUES (1),(2);
4444SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
44452
44462
44472
4448EXPLAIN EXTENDED
4449SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
4450id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44511	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44522	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
4453Warnings:
4454Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4455Note	1003	/* select#1 */ select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`))))
4456EXPLAIN EXTENDED
4457SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
4458(SELECT 1 FROM t2 WHERE t1.a = t2.a));
4459id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44601	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44612	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44623	DEPENDENT UNION	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
4463NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
4464Warnings:
4465Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4466Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4467Note	1003	/* select#1 */ select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(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))
4468DROP TABLE t1,t2;
4469create table t0(a int);
4470insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4471create table t1(f11 int, f12 int);
4472create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
4473insert into t1 values(1,1),(2,2), (3, 3);
4474insert ignore into t2
4475select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
4476from t0 A, t0 B, t0 C, t0 D;
4477set session sort_buffer_size= 33*1024;
4478select count(*) from t1 where f12 =
4479(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
4480count(*)
44813
4482drop table t0,t1,t2;
4483CREATE TABLE t4 (
4484f7 varchar(32) collate utf8_bin NOT NULL default '',
4485f10 varchar(32) collate utf8_bin default NULL,
4486PRIMARY KEY  (f7)
4487);
4488INSERT INTO t4 VALUES(1,1), (2,null);
4489CREATE TABLE t2 (
4490f4 varchar(32) collate utf8_bin NOT NULL default '',
4491f2 varchar(50) collate utf8_bin default NULL,
4492f3 varchar(10) collate utf8_bin default NULL,
4493PRIMARY KEY  (f4),
4494UNIQUE KEY uk1 (f2)
4495);
4496INSERT INTO t2 VALUES(1,1,null), (2,2,null);
4497CREATE TABLE t1 (
4498f8 varchar(32) collate utf8_bin NOT NULL default '',
4499f1 varchar(10) collate utf8_bin default NULL,
4500f9 varchar(32) collate utf8_bin default NULL,
4501PRIMARY KEY  (f8)
4502);
4503INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
4504CREATE TABLE t3 (
4505f6 varchar(32) collate utf8_bin NOT NULL default '',
4506f5 varchar(50) collate utf8_bin default NULL,
4507PRIMARY KEY (f6)
4508);
4509INSERT INTO t3 VALUES (1,null), (2,null);
4510SELECT
4511IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
4512IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
4513SUM(
4514IF(
4515(SELECT VPC.f2
4516FROM t2 VPC, t4 a2, t2 a3
4517WHERE
4518VPC.f4 = a2.f10 AND a3.f2 = a4
4519LIMIT 1) IS NULL,
45200,
4521t3.f5
4522)
4523) AS a6
4524FROM
4525t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
4526GROUP BY a4;
4527a4	f3	a6
45281	NULL	NULL
45292	NULL	NULL
4530DROP TABLE t1, t2, t3, t4;
4531create table t1 (a float(5,4) zerofill);
4532create table t2 (a float(5,4),b float(2,0));
4533select t1.a from t1 where
4534t1.a= (select b from t2 limit 1) and not
4535t1.a= (select a from t2 limit 1) ;
4536a
4537drop table t1, t2;
4538CREATE TABLE t1 (a INT);
4539INSERT INTO t1 VALUES (1),(2);
4540SET @save_join_cache_level=@@join_cache_level;
4541SET join_cache_level=0;
4542EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
4543id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45441	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
45452	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary
4546Warnings:
4547Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 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	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
45512	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using temporary
4552Warnings:
4553Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 0
4554SET join_cache_level=@save_join_cache_level;
4555DROP TABLE t1;
4556#
4557# Bug#45061: Incorrectly market field caused wrong result.
4558#
4559CREATE TABLE `C` (
4560`int_nokey` int(11) NOT NULL,
4561`int_key` int(11) NOT NULL,
4562KEY `int_key` (`int_key`)
4563);
4564INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
4565(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
4566(5,2), (1,8), (7,0), (0,9), (9,5);
4567SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4568int_nokey	int_key
45699	9
45700	0
45715	5
45720	0
4573EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4574id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45751	SIMPLE	C	ALL	NULL	NULL	NULL	NULL	20	100.00	Using where
4576DROP TABLE C;
4577# End of test for bug#45061.
4578#
4579# Bug #46749: Segfault in add_key_fields() with outer subquery level
4580#   field references
4581#
4582CREATE TABLE t1 (
4583a int,
4584b int,
4585UNIQUE (a), KEY (b)
4586);
4587INSERT INTO t1 VALUES (1,1), (2,1);
4588CREATE TABLE st1 like t1;
4589INSERT INTO st1 VALUES (1,1), (2,1);
4590CREATE TABLE st2 like t1;
4591INSERT INTO st2 VALUES (1,1), (2,1);
4592EXPLAIN
4593SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4594FROM t1
4595WHERE a = 230;
4596id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
45971	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
45982	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
4599SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4600FROM t1
4601WHERE a = 230;
4602MAX(b)	(SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4603NULL	NULL
4604DROP TABLE t1, st1, st2;
4605#
4606# Bug #48709: Assertion failed in sql_select.cc:11782:
4607#   int join_read_key(JOIN_TAB*)
4608#
4609CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
4610INSERT INTO t1 VALUES (10,1), (14,1);
4611CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
4612INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
4613# should have eq_ref for t1
4614EXPLAIN
4615SELECT * FROM t2 outr
4616WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4617ORDER BY outr.pk;
4618id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
4619x	x	outr	ALL	x	x	x	x	x	x
4620x	x	t1	index	x	x	x	x	x	x
4621x	x	t2	index	x	x	x	x	x	x
4622# should not crash on debug binaries
4623SELECT * FROM t2 outr
4624WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4625ORDER BY outr.pk;
4626pk	int_key
46273	3
46287	3
4629DROP TABLE t1,t2;
4630#
4631# Bug#12329653
4632# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4633#
4634CREATE TABLE t1(a1 int);
4635INSERT INTO t1 VALUES (1),(2);
4636SELECT @@session.sql_mode INTO @old_sql_mode;
4637SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4638EXPLAIN EXTENDED
4639SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4640id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
46411	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
46422	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
4643Warnings:
4644Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
4645SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
46461
46471
46481
4649PREPARE stmt FROM
4650'SELECT 1 UNION ALL
4651SELECT 1 FROM t1
4652ORDER BY
4653(SELECT 1 FROM t1 AS t1_0
4654  WHERE 1 < SOME (SELECT a1 FROM t1)
4655)' ;
4656EXECUTE stmt ;
4657ERROR 21000: Subquery returns more than 1 row
4658EXECUTE stmt ;
4659ERROR 21000: Subquery returns more than 1 row
4660SET SESSION sql_mode=@old_sql_mode;
4661DEALLOCATE PREPARE stmt;
4662DROP TABLE t1;
4663#
4664# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4665#
4666CREATE TABLE t1(a1 int);
4667INSERT INTO t1 VALUES (1),(2);
4668CREATE TABLE t2(a1 int);
4669INSERT INTO t2 VALUES (3);
4670SELECT @@session.sql_mode INTO @old_sql_mode;
4671SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4672SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
46731
46741
46751
4676SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
46771
46781
46791
4680SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
46811
4682Warnings:
4683Warning	1292	Truncated incorrect DOUBLE value: 'a'
4684SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
46851
46861
46871
4688SET SESSION sql_mode=@old_sql_mode;
4689DROP TABLE t1, t2;
4690#
4691# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4692#
4693create table t2(i int);
4694insert into t2 values(0);
4695SELECT @@session.sql_mode INTO @old_sql_mode;
4696SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4697CREATE VIEW v1 AS
4698SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
4699;
4700CREATE TABLE t1 (
4701pk int NOT NULL,
4702col_varchar_key varchar(1) DEFAULT NULL,
4703PRIMARY KEY (pk),
4704KEY col_varchar_key (col_varchar_key)
4705);
4706SELECT t1.pk
4707FROM t1
4708WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4709;
4710pk
4711SET SESSION sql_mode=@old_sql_mode;
4712drop table t2, t1;
4713drop view v1;
4714# End of 5.0 tests.
4715create table t_out (subcase char(3),
4716a1 char(2), b1 char(2), c1 char(2));
4717create table t_in  (a2 char(2), b2 char(2), c2 char(2));
4718insert into t_out values ('A.1','2a', NULL, '2a');
4719insert into t_out values ('A.3', '2a', NULL, '2a');
4720insert into t_out values ('A.4', '2a', NULL, 'xx');
4721insert into t_out values ('B.1', '2a', '2a', '2a');
4722insert into t_out values ('B.2', '2a', '2a', '2a');
4723insert into t_out values ('B.3', '3a', 'xx', '3a');
4724insert into t_out values ('B.4', 'xx', '3a', '3a');
4725insert into t_in values ('1a', '1a', '1a');
4726insert into t_in values ('2a', '2a', '2a');
4727insert into t_in values (NULL, '2a', '2a');
4728insert into t_in values ('3a', NULL, '3a');
4729
4730Test general IN semantics (not top-level)
4731
4732case A.1
4733select subcase,
4734(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4735(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4736from t_out where subcase = 'A.1';
4737subcase	pred_in	pred_not_in
4738A.1	0	1
4739case A.2 - impossible
4740case A.3
4741select subcase,
4742(a1, b1, c1)     IN (select * from t_in) pred_in,
4743(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4744from t_out where subcase = 'A.3';
4745subcase	pred_in	pred_not_in
4746A.3	NULL	NULL
4747case A.4
4748select subcase,
4749(a1, b1, c1)     IN (select * from t_in) pred_in,
4750(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4751from t_out where subcase = 'A.4';
4752subcase	pred_in	pred_not_in
4753A.4	0	1
4754case B.1
4755select subcase,
4756(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4757(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4758from t_out where subcase = 'B.1';
4759subcase	pred_in	pred_not_in
4760B.1	0	1
4761case B.2
4762select subcase,
4763(a1, b1, c1)     IN (select * from t_in) pred_in,
4764(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4765from t_out where subcase = 'B.2';
4766subcase	pred_in	pred_not_in
4767B.2	1	0
4768case B.3
4769select subcase,
4770(a1, b1, c1)     IN (select * from t_in) pred_in,
4771(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4772from t_out where subcase = 'B.3';
4773subcase	pred_in	pred_not_in
4774B.3	NULL	NULL
4775case B.4
4776select subcase,
4777(a1, b1, c1)     IN (select * from t_in) pred_in,
4778(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4779from t_out where subcase = 'B.4';
4780subcase	pred_in	pred_not_in
4781B.4	0	1
4782
4783Test IN as top-level predicate, and
4784as non-top level for cases A.3, B.3 (the only cases with NULL result).
4785
4786case A.1
4787select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4788where subcase = 'A.1' and
4789(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4790pred_in
4791F
4792select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4793where subcase = 'A.1' and
4794(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4795pred_not_in
4796T
4797select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4798where subcase = 'A.1' and
4799NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4800not_pred_in
4801T
4802case A.3
4803select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4804where subcase = 'A.3' and
4805(a1, b1, c1) IN (select * from t_in);
4806pred_in
4807F
4808select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4809where subcase = 'A.3' and
4810(a1, b1, c1) NOT IN (select * from t_in);
4811pred_not_in
4812F
4813select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4814where subcase = 'A.3' and
4815NOT((a1, b1, c1) IN (select * from t_in));
4816not_pred_in
4817F
4818select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4819where subcase = 'A.3' and
4820((a1, b1, c1) IN (select * from t_in)) is NULL and
4821((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4822pred_in
4823N
4824case A.4
4825select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4826where subcase = 'A.4' and
4827(a1, b1, c1) IN (select * from t_in);
4828pred_in
4829F
4830select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4831where subcase = 'A.4' and
4832(a1, b1, c1) NOT IN (select * from t_in);
4833pred_not_in
4834T
4835select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4836where subcase = 'A.4' and
4837NOT((a1, b1, c1) IN (select * from t_in));
4838not_pred_in
4839T
4840case B.1
4841select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4842where subcase = 'B.1' and
4843(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4844pred_in
4845F
4846select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4847where subcase = 'B.1' and
4848(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4849pred_not_in
4850T
4851select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4852where subcase = 'B.1' and
4853NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4854not_pred_in
4855T
4856case B.2
4857select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4858where subcase = 'B.2' and
4859(a1, b1, c1) IN (select * from t_in);
4860pred_in
4861T
4862select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4863where subcase = 'B.2' and
4864(a1, b1, c1) NOT IN (select * from t_in);
4865pred_not_in
4866F
4867select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4868where subcase = 'B.2' and
4869NOT((a1, b1, c1) IN (select * from t_in));
4870not_pred_in
4871F
4872case B.3
4873select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4874where subcase = 'B.3' and
4875(a1, b1, c1) IN (select * from t_in);
4876pred_in
4877F
4878select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4879where subcase = 'B.3' and
4880(a1, b1, c1) NOT IN (select * from t_in);
4881pred_not_in
4882F
4883select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4884where subcase = 'B.3' and
4885NOT((a1, b1, c1) IN (select * from t_in));
4886not_pred_in
4887F
4888select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4889where subcase = 'B.3' and
4890((a1, b1, c1) IN (select * from t_in)) is NULL and
4891((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4892pred_in
4893N
4894case B.4
4895select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4896where subcase = 'B.4' and
4897(a1, b1, c1) IN (select * from t_in);
4898pred_in
4899F
4900select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4901where subcase = 'B.4' and
4902(a1, b1, c1) NOT IN (select * from t_in);
4903pred_not_in
4904T
4905select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4906where subcase = 'B.4' and
4907NOT((a1, b1, c1) IN (select * from t_in));
4908not_pred_in
4909T
4910drop table t_out;
4911drop table t_in;
4912CREATE TABLE t1 (a INT, b INT);
4913INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4914SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4915a
49161
49172
4918SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4919a
4920SELECT a FROM t1 t0
4921WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4922a
49231
49242
4925SET @@sql_mode='ansi';
4926SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4927ERROR HY000: Invalid use of group function
4928SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4929ERROR HY000: Invalid use of group function
4930SELECT a FROM t1 t0
4931WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4932ERROR HY000: Invalid use of group function
4933SET @@sql_mode=default;
4934DROP TABLE t1;
4935CREATE TABLE t1 (s1 CHAR(1));
4936INSERT INTO t1 VALUES ('a');
4937SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
4938s1
4939a
4940DROP TABLE t1;
4941CREATE TABLE t1(c INT, KEY(c));
4942CREATE TABLE t2(a INT, b INT);
4943INSERT INTO t2 VALUES (1, 10), (2, NULL);
4944INSERT INTO t1 VALUES (1), (3);
4945SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4946a	b
4947DROP TABLE t1,t2;
4948CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4949INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4950CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4951INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4952SELECT * FROM t1
4953WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
4954pk	a
49551	10
4956DROP TABLE t1,t2;
4957CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4958INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4959CREATE TABLE t2 (
4960a INT,
4961b INT,
4962c INT,
4963d INT,
4964PRIMARY KEY (a),
4965UNIQUE KEY b (b,c,d),
4966KEY b_2 (b),
4967KEY c (c),
4968KEY d (d)
4969);
4970INSERT INTO t2 VALUES
4971(43, 2, 11 ,30),
4972(44, 2, 12 ,30),
4973(45, 1, 1  ,10000),
4974(46, 1, 2  ,10000),
4975(556,1, 32 ,10000);
4976CREATE TABLE t3 (
4977a INT,
4978b INT,
4979c INT,
4980PRIMARY KEY (a),
4981UNIQUE KEY b (b,c),
4982KEY c (c),
4983KEY b_2 (b)
4984);
4985INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2);
4986explain
4987SELECT 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;
4988id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
49891	PRIMARY	t1	index	PRIMARY	PRIMARY	4	NULL	2	Using index
49901	PRIMARY	t3	ref	b,b_2	b	5	test.t1.a	1	Using index
49912	DEPENDENT SUBQUERY	t2	ref	b,b_2,c	b	10	test.t3.c,test.t1.a	1	Using where; Using index
4992SELECT 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;
4993a	incorrect
49941	1
4995DROP TABLE t1,t2,t3;
4996CREATE TABLE t1 (id int);
4997CREATE TABLE t2 (id int, c int);
4998INSERT INTO t1 (id) VALUES (1);
4999INSERT INTO t2 (id) VALUES (1);
5000INSERT INTO t1 (id) VALUES (1);
5001INSERT INTO t2 (id) VALUES (1);
5002CREATE VIEW v1 AS
5003SELECT t2.c AS c FROM t1, t2
5004WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5005UPDATE v1 SET c=1;
5006CREATE VIEW v2 (a,b) AS
5007SELECT t2.id, t2.c AS c FROM t1, t2
5008WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5009INSERT INTO v2(a,b) VALUES (2,2);
5010ERROR 44000: CHECK OPTION failed `test`.`v2`
5011SELECT * FROM v1;
5012c
50131
50141
50151
50161
5017CREATE VIEW v3 AS
5018SELECT t2.c AS c FROM t2
5019WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5020DELETE FROM v3;
5021DROP VIEW v1,v2,v3;
5022DROP TABLE t1,t2;
5023#
5024# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5025#
5026create table t1(id integer primary key, g integer, v integer, s char(1));
5027create table t2(id integer primary key, g integer, v integer, s char(1));
5028insert into t1 values
5029(10, 10, 10,   'l'),
5030(20, 20, 20,   'l'),
5031(40, 40, 40,   'l'),
5032(41, 40, null, 'l'),
5033(50, 50, 50,   'l'),
5034(51, 50, null, 'l'),
5035(60, 60, 60,   'l'),
5036(61, 60, null, 'l'),
5037(70, 70, 70,   'l'),
5038(90, 90, null, 'l');
5039insert into t2 values
5040(10, 10, 10,   'r'),
5041(30, 30, 30,   'r'),
5042(50, 50, 50,   'r'),
5043(60, 60, 60,   'r'),
5044(61, 60, null, 'r'),
5045(70, 70, 70,   'r'),
5046(71, 70, null, 'r'),
5047(80, 80, 80,   'r'),
5048(81, 80, null, 'r'),
5049(100,100,null, 'r');
5050select *
5051from t1
5052where v in(select v
5053from t2
5054where t1.g=t2.g) is unknown;
5055id	g	v	s
505651	50	NULL	l
505761	60	NULL	l
5058drop table t1, t2;
5059#
5060# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5061#
5062create table t1(id integer primary key, g integer, v integer, s char(1));
5063create table t2(id integer primary key, g integer, v integer, s char(1));
5064insert into t1 values
5065(10, 10, 10,   'l'),
5066(20, 20, 20,   'l'),
5067(40, 40, 40,   'l'),
5068(41, 40, null, 'l'),
5069(50, 50, 50,   'l'),
5070(51, 50, null, 'l'),
5071(60, 60, 60,   'l'),
5072(61, 60, null, 'l'),
5073(70, 70, 70,   'l'),
5074(90, 90, null, 'l');
5075insert into t2 values
5076(10, 10, 10,   'r'),
5077(30, 30, 30,   'r'),
5078(50, 50, 50,   'r'),
5079(60, 60, 60,   'r'),
5080(61, 60, null, 'r'),
5081(70, 70, 70,   'r'),
5082(71, 70, null, 'r'),
5083(80, 80, 80,   'r'),
5084(81, 80, null, 'r'),
5085(100,100,null, 'r');
5086select *
5087from t1
5088where v in(select v
5089from t2
5090where t1.g=t2.g) is unknown;
5091id	g	v	s
509251	50	NULL	l
509361	60	NULL	l
5094drop table t1, t2;
5095#
5096# Bug#33204: INTO is allowed in subselect, causing inconsistent results
5097#
5098CREATE TABLE t1( a INT );
5099INSERT INTO t1 VALUES (1),(2);
5100CREATE TABLE t2( a INT, b INT );
5101SELECT *
5102FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
5103ERROR 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
5104SELECT *
5105FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
5106ERROR 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
5107SELECT *
5108FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
5109ERROR 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
5110SELECT * FROM (
5111SELECT 1 a
5112UNION
5113SELECT a INTO @var FROM t1 WHERE a = 2
5114) t1a;
5115ERROR 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
5116) t1a' at line 4
5117SELECT * FROM (
5118SELECT 1 a
5119UNION
5120SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
5121) t1a;
5122ERROR 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
5123) t1a' at line 4
5124SELECT * FROM (
5125SELECT 1 a
5126UNION
5127SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
5128) t1a;
5129ERROR 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
5130) t1a' at line 4
5131SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
5132a
51332
5134SELECT * FROM (
5135SELECT a FROM t1 WHERE a = 2
5136UNION
5137SELECT a FROM t1 WHERE a = 2
5138) t1a;
5139a
51402
5141SELECT * FROM (
5142SELECT 1 a
5143UNION
5144SELECT a FROM t1 WHERE a = 2
5145UNION
5146SELECT a FROM t1 WHERE a = 2
5147) t1a;
5148a
51491
51502
5151SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
5152a
51531
5154SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
5155a
51561
5157SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
51581
51591
5160SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
5161ERROR 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
5162SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
5163ERROR 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
5164SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5165ERROR 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
5166SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
5167ERROR 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
5168SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
5169ERROR 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
5170SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
5171ERROR 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
5172SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
5173ERROR 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
5174SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
5175ERROR 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
5176SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
5177ERROR 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
5178SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
5179a
51801
5181SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
5182a
51831
5184SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
5185a
51861
5187SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
5188a
51891
5190SELECT * FROM t1 JOIN  (SELECT 1 UNION SELECT 1) alias ON 1;
5191a	1
51921	1
51932	1
5194SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
5195ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON 1' at line 1
5196SELECT * FROM t1 JOIN  (t1 t1a UNION SELECT 1)  ON 1;
5197ERROR 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)  ON 1' at line 1
5198SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
5199ERROR 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)) ON 1' at line 1
5200SELECT * FROM t1 JOIN  (t1 t1a)  t1a ON 1;
5201ERROR 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
5202SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
5203ERROR 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
5204SELECT * FROM t1 JOIN  (t1 t1a)  ON 1;
5205a	a
52061	1
52072	1
52081	2
52092	2
5210SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
5211a	a
52121	1
52132	1
52141	2
52152	2
5216SELECT * FROM (t1 t1a);
5217a
52181
52192
5220SELECT * FROM ((t1 t1a));
5221a
52221
52232
5224SELECT * FROM t1 JOIN  (SELECT 1 t1a) alias ON 1;
5225a	t1a
52261	1
52272	1
5228SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
5229a	t1a
52301	1
52312	1
5232SELECT * FROM t1 JOIN  (SELECT 1 a)  a ON 1;
5233a	a
52341	1
52352	1
5236SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
5237a	a
52381	1
52392	1
5240SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
5241ERROR 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
5242SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
5243a
52441
5245SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
5246a
52471
5248SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
5249a
52501
5251SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
5252ERROR 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
5253SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5254ERROR 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
5255SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5256ERROR 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
5257SELECT * FROM t1 WHERE a = ( SELECT 1 );
5258a
52591
5260SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
5261a
52621
5263SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
5264ERROR 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
5265SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
5266ERROR 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
5267SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
5268ERROR 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
5269SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
5270ERROR 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
5271SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5272ERROR 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
5273SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5274ERROR 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
5275SELECT ( SELECT 1 INTO @v );
5276ERROR 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
5277SELECT ( SELECT 1 INTO OUTFILE 'file' );
5278ERROR 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
5279SELECT ( SELECT 1 INTO DUMPFILE 'file' );
5280ERROR 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
5281SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
5282ERROR 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
5283SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5284ERROR 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
5285SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5286ERROR 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
5287SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
5288( SELECT a FROM t1 WHERE a = 1 )	a
52891	1
52901	2
5291SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
5292( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 )	a
52931	1
52941	2
5295SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
5296a	b
5297SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
52981
52991
5300( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
53011
53021
5303SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5304( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) )
53051
5306SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5307ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
5308SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5309( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
53101
5311SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
5312((SELECT 1 UNION SELECT 1 UNION SELECT 1))
53131
5314SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5315ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
5316SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5317ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
5318SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
53191
53201
5321SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
53221
53231
5324SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5325a
53261
5327SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5328a
53291
5330SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5331a
53321
5333SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5334a
53351
5336SELECT * FROM t1 WHERE a =     ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5337a
53381
5339SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5340a
53411
5342SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5343a
53441
5345SELECT * FROM t1 WHERE a IN    ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5346a
53471
5348SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5349a
53501
5351SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5352a
53531
5354SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5355a
53561
5357SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5358a
53591
5360SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
5361ERROR 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
5362SELECT EXISTS(SELECT 1+1);
5363EXISTS(SELECT 1+1)
53641
5365SELECT EXISTS(SELECT 1+1 INTO @test);
5366ERROR 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
5367SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
5368ERROR 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
5369SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
5370ERROR 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
5371SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
5372ERROR 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
5373DROP TABLE t1, t2;
5374CREATE TABLE t1 (a ENUM('rainbow'));
5375INSERT INTO t1 VALUES (),(),(),(),();
5376SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
53771
53781
5379DROP TABLE t1;
5380CREATE TABLE t1 (a LONGBLOB);
5381INSERT INTO t1 SET a = 'aaaa';
5382INSERT INTO t1 SET a = 'aaaa';
5383SELECT 1 FROM t1 GROUP BY
5384(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
53851
53861
5387DROP TABLE t1;
5388#
5389# Bug #49512 : subquery with aggregate function crash
5390#   subselect_single_select_engine::exec()
5391CREATE TABLE t1(a INT);
5392INSERT INTO t1 VALUES();
5393# should not crash
5394SELECT 1 FROM t1 WHERE a <> SOME
5395(
5396SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
5397FROM t1,t1 a
5398);
53991
5400DROP TABLE t1;
5401#
5402# Bug #45989 take 2 : memory leak after explain encounters an
5403# error in the query
5404#
5405CREATE TABLE t1(a LONGTEXT);
5406INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
5407INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
5408EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
5409(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
5410WHERE t1.a = d1.a;
5411ERROR 42S22: Unknown column 'd1.a' in 'where clause'
5412DROP TABLE t1;
5413Set up test tables.
5414CREATE TABLE t1 (
5415t1_id INT UNSIGNED,
5416PRIMARY KEY(t1_id)
5417) Engine=MyISAM;
5418INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
5419CREATE TABLE t2 SELECT * FROM t1;
5420CREATE TABLE t3 (
5421t3_id INT UNSIGNED AUTO_INCREMENT,
5422t1_id INT UNSIGNED,
5423amount DECIMAL(16,2),
5424PRIMARY KEY(t3_id),
5425KEY(t1_id)
5426) Engine=MyISAM;
5427INSERT INTO t3 (t1_id, t3_id, amount)
5428VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
5429This is the 'inner query' running by itself.
5430Produces correct results.
5431SELECT
5432t1.t1_id,
5433IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5434FROM
5435t1
5436LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5437GROUP BY
5438t1.t1_id
5439;
5440t1_id	total_amount
54411	100.00
54422	200.00
54433	0.00
54444	400.00
54455	0.00
5446SELECT * FROM (the same inner query)
5447Produces correct results.
5448SELECT * FROM (
5449SELECT
5450t1.t1_id,
5451IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5452FROM
5453t1
5454LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5455GROUP BY
5456t1.t1_id
5457) AS t;
5458t1_id	total_amount
54591	100.00
54602	200.00
54613	0.00
54624	400.00
54635	0.00
5464Now make t2.t1_id part of a key.
5465ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
5466Same inner query by itself.
5467Still correct results.
5468SELECT
5469t1.t1_id,
5470IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5471FROM
5472t1
5473LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5474GROUP BY
5475t1.t1_id;
5476t1_id	total_amount
54771	100.00
54782	200.00
54793	0.00
54804	400.00
54815	0.00
5482SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
5483SELECT * FROM (
5484SELECT
5485t1.t1_id,
5486IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5487FROM
5488t1
5489LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5490GROUP BY
5491t1.t1_id
5492) AS t;
5493t1_id	total_amount
54941	100.00
54952	200.00
54963	0.00
54974	400.00
54985	0.00
5499DROP TABLE t3;
5500DROP TABLE t2;
5501DROP TABLE t1;
5502#
5503# Bug #52711: Segfault when doing EXPLAIN SELECT with
5504#  union...order by (select... where...)
5505#
5506CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5507INSERT INTO t1 VALUES (1),(2);
5508CREATE TABLE t2 (b INT);
5509INSERT INTO t2 VALUES (1),(2);
5510# Should not crash
5511EXPLAIN
5512SELECT * FROM t2 UNION SELECT * FROM t2
5513ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5514# Should not crash
5515SELECT * FROM t2 UNION SELECT * FROM t2
5516ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5517DROP TABLE t1,t2;
5518#
5519# Bug #58818: Incorrect result for IN/ANY subquery
5520# with HAVING condition
5521#
5522CREATE TABLE t1(i INT);
5523INSERT INTO t1 VALUES (1), (2), (3);
5524CREATE TABLE t1s(i INT);
5525INSERT INTO t1s VALUES (10), (20), (30);
5526CREATE TABLE t2s(i INT);
5527INSERT INTO t2s VALUES (100), (200), (300);
5528SELECT * FROM t1
5529WHERE t1.i NOT IN
5530(
5531SELECT STRAIGHT_JOIN t2s.i
5532FROM
5533t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5534HAVING t2s.i = 999
5535);
5536i
55371
55382
55393
5540SELECT * FROM t1
5541WHERE t1.I IN
5542(
5543SELECT STRAIGHT_JOIN t2s.i
5544FROM
5545t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5546HAVING t2s.i = 999
5547) IS UNKNOWN;
5548i
5549SELECT * FROM t1
5550WHERE NOT t1.I = ANY
5551(
5552SELECT STRAIGHT_JOIN t2s.i
5553FROM
5554t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5555HAVING t2s.i = 999
5556);
5557i
55581
55592
55603
5561SELECT * FROM t1
5562WHERE t1.i = ANY (
5563SELECT STRAIGHT_JOIN t2s.i
5564FROM
5565t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5566HAVING t2s.i = 999
5567) IS UNKNOWN;
5568i
5569DROP TABLE t1,t1s,t2s;
5570# LP BUG#675248 - select->prep_where references on freed memory
5571CREATE TABLE t1 (a int, b int);
5572insert into t1 values (1,1),(0,0);
5573CREATE TABLE t2 (c int);
5574insert into t2 values (1),(2);
5575prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
5576from t1 as table1 group by sub";
5577execute stmt1;
5578sum(a)	sub
55790	NULL
55801	3
5581deallocate prepare stmt1;
5582prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
5583from t1 as table1";
5584execute stmt1;
5585sum(a)	sub
55861	3
5587deallocate prepare stmt1;
5588drop table t1,t2;
5589#
5590# Bug LP#693935/#58727: Assertion failure with
5591# a single row subquery returning more than one row
5592#
5593create table t1 (a char(1) charset utf8);
5594insert into t1 values ('a'), ('b');
5595create table t2 (a binary(1));
5596insert into t2 values ('x'), ('y');
5597select * from t2 where a=(select a from t1) and a='x';
5598ERROR 21000: Subquery returns more than 1 row
5599drop table t1,t2;
5600# End of 5.1 tests
5601#
5602# Bug #11765713 58705:
5603# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5604# CREATED BY OPT_SUM_QUERY
5605#
5606CREATE TABLE t1(a INT NOT NULL, KEY (a));
5607INSERT INTO t1 VALUES (0), (1);
5608SELECT 1 as foo FROM t1 WHERE a < SOME
5609(SELECT a FROM t1 WHERE a <=>
5610(SELECT a FROM t1)
5611);
5612ERROR 21000: Subquery returns more than 1 row
5613SELECT 1 as foo FROM t1 WHERE a < SOME
5614(SELECT a FROM t1 WHERE a <=>
5615(SELECT a FROM t1 where a is null)
5616);
5617foo
5618DROP TABLE t1;
5619#
5620# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
5621#             Assertion `file' failed.
5622#
5623CREATE TABLE t1 (a INT);
5624SELECT 1 FROM
5625(SELECT ROW(
5626(SELECT 1 FROM t1 RIGHT JOIN
5627(SELECT 1 FROM t1, t1 t2) AS d ON 1),
56281) FROM t1) AS e;
5629ERROR 21000: Operand should contain 1 column(s)
5630DROP TABLE t1;
5631#
5632# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5633#
5634CREATE TABLE t1(a TIME NOT NULL);
5635INSERT INTO t1 VALUES ('00:00:32');
5636SELECT 1 FROM t1 WHERE a >
5637(SELECT timestamp(a) AS a FROM t1);
56381
5639DROP TABLE t1;
5640#
5641# No BUG#, a case brought from 5.2's innodb_mysql_lock.test
5642#
5643create table t1 (i int  not null primary key);
5644insert into t1 values (1),(2),(3),(4),(5);
5645create table t2 (j int not null  primary key);
5646insert into t2 values (1),(2),(3),(4),(5);
5647create table t3 (k int not null primary key);
5648insert into t3 values (1),(2),(3);
5649create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
5650select * from t3 where k in (select j from v2);
5651k
56521
56532
56543
5655drop table t1,t2,t3;
5656drop view v2;
5657#
5658# Bug#52068: Optimizer generates invalid semijoin materialization plan
5659#
5660drop table if exists ot1, ot2, it1, it2;
5661CREATE TABLE ot1(a INTEGER);
5662INSERT INTO ot1 VALUES(5), (8);
5663CREATE TABLE it2(a INTEGER);
5664INSERT INTO it2 VALUES(9), (5), (1), (8);
5665CREATE TABLE it3(a INTEGER);
5666INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
5667CREATE TABLE ot4(a INTEGER);
5668INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
5669SELECT * FROM ot1,ot4
5670WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5671FROM it2,it3);
5672a	a
56735	1
56748	1
56755	5
56768	5
56775	7
56788	7
56795	7
56808	7
56815	1
56828	1
5683explain SELECT * FROM ot1,ot4
5684WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5685FROM it2,it3);
5686id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
56871	PRIMARY	ot1	ALL	NULL	NULL	NULL	NULL	2
56881	PRIMARY	ot4	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
56892	MATERIALIZED	it2	ALL	NULL	NULL	NULL	NULL	4
56902	MATERIALIZED	it3	ALL	NULL	NULL	NULL	NULL	6	Using join buffer (flat, BNL join)
5691DROP TABLE IF EXISTS ot1, ot4, it2, it3;
5692#
5693# Bug#729039: NULL keys used to evaluate subquery
5694#
5695CREATE TABLE t1 (a int) ;
5696INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
5697CREATE TABLE t2 (a int, INDEX idx(a)) ;
5698INSERT INTO t2 VALUES (NULL), (1), (NULL);
5699SELECT * FROM t1
5700WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
5701a
57021
5703EXPLAIN
5704SELECT * FROM t1
5705WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
5706id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57071	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
57082	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	3
5709SELECT * FROM t1
5710WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5711a
57121
5713EXPLAIN
5714SELECT * FROM t1
5715WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5716id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57171	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
57182	MATERIALIZED	t2	index	idx	idx	5	NULL	3	Using index
5719DROP TABLE t1,t2;
5720#
5721# BUG#752992: Wrong results for a subquery with 'semijoin=on'
5722#
5723CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5724INSERT INTO t1 VALUES (11,0);
5725INSERT INTO t1 VALUES (12,5);
5726INSERT INTO t1 VALUES (15,0);
5727CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5728INSERT INTO t2 VALUES (11,1);
5729INSERT INTO t2 VALUES (12,2);
5730INSERT INTO t2 VALUES (15,4);
5731SET @save_join_cache_level=@@join_cache_level;
5732SET join_cache_level=0;
5733EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5734id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57351	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
57362	MATERIALIZED	t2	index	NULL	PRIMARY	4	NULL	3	Using index
57372	MATERIALIZED	it	index	PRIMARY	PRIMARY	4	NULL	3	Using index
5738SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5739pk	i
574011	0
574112	5
574215	0
5743SET join_cache_level=@save_join_cache_level;
5744DROP table t1,t2;
5745#
5746# Bug#751350: crash with pushed condition for outer references when
5747#             there should be none of such conditions
5748#
5749CREATE TABLE t1 (a int, b int) ;
5750INSERT INTO t1 VALUES (0,0),(0,0);
5751set @optimizer_switch_save=@@optimizer_switch;
5752set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
5753EXPLAIN
5754SELECT b FROM t1
5755WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5756GROUP BY b;
5757id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57581	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
57592	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
5760SELECT b FROM t1
5761WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5762GROUP BY b;
5763b
57640
5765set @@optimizer_switch=@optimizer_switch_save;
5766DROP TABLE t1;
5767#
5768# Bug #11765713 58705:
5769# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5770# CREATED BY OPT_SUM_QUERY
5771#
5772CREATE TABLE t1(a INT NOT NULL, KEY (a));
5773INSERT INTO t1 VALUES (0), (1);
5774SELECT 1 as foo FROM t1 WHERE a < SOME
5775(SELECT a FROM t1 WHERE a <=>
5776(SELECT a FROM t1)
5777);
5778ERROR 21000: Subquery returns more than 1 row
5779SELECT 1 as foo FROM t1 WHERE a < SOME
5780(SELECT a FROM t1 WHERE a <=>
5781(SELECT a FROM t1 where a is null)
5782);
5783foo
5784DROP TABLE t1;
5785CREATE TABLE t1 (a int(11), b varchar(1));
5786INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5787SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5788a
57895
5790SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5791a
57925
5793SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5794a
57957
5796SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5797a
57987
5799SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5800a
58015
58027
5803SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5804a
58055
58067
5807SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5808a
58095
58107
5811SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5812a
58135
58147
5815SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5816a
58175
58187
5819SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5820a
58215
58227
5823SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5824a
58255
58267
5827SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5828a
58295
58307
5831SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5832a
5833SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5834a
5835SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5836a
5837SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5838a
5839SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5840a
5841SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5842a
5843SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5844a
5845SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5846a
5847SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5848a
5849SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5850a
5851SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5852a
5853SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5854a
5855delete from t1;
5856INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5857SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5858a
58595
5860SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5861a
58625
5863SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5864a
58657
5866SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5867a
58687
5869SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5870a
58715
58727
5873SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5874a
58755
58767
5877SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5878a
58795
58807
5881SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5882a
58835
58847
5885SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5886a
58875
58887
5889SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5890a
58915
58927
5893SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5894a
58955
58967
5897SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5898a
58995
59007
5901SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5902a
5903SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5904a
5905SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5906a
5907SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5908a
5909SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5910a
5911SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5912a
5913SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5914a
5915SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5916a
5917SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5918a
5919SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5920a
5921SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5922a
5923SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5924a
5925drop table t1;
5926#
5927# Fix of lp:780386 (NULL left part with empty ALL subquery).
5928#
5929CREATE TABLE t1 ( f11 int) ;
5930INSERT IGNORE INTO t1 VALUES (0),(0);
5931CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
5932INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
5933DROP TABLE IF EXISTS t3;
5934Warnings:
5935Note	1051	Unknown table 'test.t3'
5936CREATE TABLE t3 ( f3 int) ;
5937INSERT INTO t3 VALUES (0),(0);
5938SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
5939r
5940NULL
59415
5942NULL
59435
5944DROP TABLE t1, t2, t3;
5945#
5946# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
5947#
5948CREATE TABLE t1(a1 int);
5949INSERT INTO t1 VALUES (1),(2);
5950CREATE TABLE t2(a1 int);
5951INSERT INTO t2 VALUES (3);
5952SELECT @@session.sql_mode INTO @old_sql_mode;
5953SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5954SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
59551
59561
59571
5958SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
59591
59601
59611
5962SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
59631
5964Warnings:
5965Warning	1292	Truncated incorrect DOUBLE value: 'a'
5966SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
59671
59681
59691
5970SET SESSION sql_mode=@old_sql_mode;
5971DROP TABLE t1, t2;
5972create table t2(i int);
5973insert into t2 values(0);
5974SELECT @@session.sql_mode INTO @old_sql_mode;
5975SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5976CREATE VIEW v1 AS
5977SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
5978;
5979CREATE TABLE t1 (
5980pk int NOT NULL,
5981col_varchar_key varchar(1) DEFAULT NULL,
5982PRIMARY KEY (pk),
5983KEY col_varchar_key (col_varchar_key)
5984);
5985SELECT t1.pk
5986FROM t1
5987WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
5988;
5989pk
5990SET SESSION sql_mode=@old_sql_mode;
5991drop table t2, t1;
5992drop view v1;
5993#
5994# BUG#50257: Missing info in REF column of the EXPLAIN
5995#            lines for subselects
5996#
5997CREATE TABLE t1 (a INT, b INT, INDEX (a));
5998INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
5999EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6000id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60011	SIMPLE	t1	ref	a	a	5	const	1
6002EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6003id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60041	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
60052	SUBQUERY	t1	ref	a	a	5	const	1	Using index
6006DROP TABLE t1;
6007#
6008# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6009# (duplicate of LP bug #888456)
6010#
6011CREATE TABLE t1 (f1 varchar(1));
6012INSERT INTO t1 VALUES ('v'),('s');
6013CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6014INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6015('d'),('y'),('t'),('d'),('s');
6016EXPLAIN
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);
6020id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60211	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
60221	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
60232	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6024SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
6025WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
6026WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
6027f1	f1_key
6028s	c
6029s	d
6030s	d
6031s	d
6032s	j
6033s	m
6034s	s
6035s	t
6036s	v
6037s	y
6038v	c
6039v	d
6040v	d
6041v	d
6042v	j
6043v	m
6044v	s
6045v	t
6046v	v
6047v	y
6048DROP TABLE t1,t2;
6049#
6050# LP bug 919427: EXPLAIN for a query over a single-row table
6051#                with IN subquery in WHERE condition
6052#
6053CREATE TABLE ot (
6054col_int_nokey int(11),
6055col_varchar_nokey varchar(1)
6056) ;
6057INSERT INTO ot VALUES (1,'x');
6058CREATE TABLE it1(
6059col_int_key int(11),
6060col_varchar_key varchar(1),
6061KEY idx_cvk_cik (col_varchar_key,col_int_key)
6062);
6063INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
6064CREATE TABLE it2 (
6065col_int_key int(11),
6066col_varchar_key varchar(1),
6067col_varchar_key2 varchar(1),
6068KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
6069KEY idx_cvk_cik (col_varchar_key, col_int_key)
6070);
6071INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
6072EXPLAIN
6073SELECT col_int_nokey FROM ot
6074WHERE col_varchar_nokey IN
6075(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6076id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60771	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60782	DEPENDENT SUBQUERY	it1	index_subquery	idx_cvk_cik	idx_cvk_cik	9	func,const	2	Using index; Using where
6079SELECT col_int_nokey FROM ot
6080WHERE col_varchar_nokey IN
6081(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6082col_int_nokey
60831
6084EXPLAIN
6085SELECT col_int_nokey FROM ot
6086WHERE (col_varchar_nokey, 'x') IN
6087(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6088id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60891	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60902	DEPENDENT SUBQUERY	it2	index_subquery	idx_cvk_cvk2_cik,idx_cvk_cik	idx_cvk_cvk2_cik	8	func,const	1	Using index; Using where
6091SELECT col_int_nokey FROM ot
6092WHERE (col_varchar_nokey, 'x') IN
6093(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6094col_int_nokey
60951
6096DROP TABLE ot,it1,it2;
6097#
6098# MDEV-746
6099# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
6100# HAS AN EMPTY RESULT
6101#
6102CREATE TABLE t1 (
6103pk int NOT NULL,
6104col_int_nokey int NOT NULL,
6105col_int_key int NOT NULL,
6106col_time_key time NOT NULL,
6107col_varchar_key varchar(1) NOT NULL,
6108col_varchar_nokey varchar(1) NOT NULL,
6109PRIMARY KEY (pk),
6110KEY col_int_key (col_int_key),
6111KEY col_time_key (col_time_key),
6112KEY col_varchar_key (col_varchar_key,col_int_key)
6113) ENGINE=MyISAM;
6114CREATE TABLE t2 (
6115pk int NOT NULL AUTO_INCREMENT,
6116col_int_nokey int NOT NULL,
6117col_int_key int NOT NULL,
6118col_time_key time NOT NULL,
6119col_varchar_key varchar(1) NOT NULL,
6120col_varchar_nokey varchar(1) NOT NULL,
6121PRIMARY KEY (pk),
6122KEY col_int_key (col_int_key),
6123KEY col_time_key (col_time_key),
6124KEY col_varchar_key (col_varchar_key,col_int_key)
6125) ENGINE=MyISAM;
6126INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
6127SET @var2:=4, @var3:=8;
6128
6129Testcase without inner subquery
6130EXPLAIN SELECT @var3:=12, sq4_alias1.*
6131FROM t1 AS sq4_alias1
6132WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6133sq4_alias1.col_varchar_key = @var3;
6134id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61351	SIMPLE	sq4_alias1	system	NULL	NULL	NULL	NULL	0	Const row not found
6136SELECT @var3:=12, sq4_alias1.*
6137FROM t1 AS sq4_alias1
6138WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6139sq4_alias1.col_varchar_key = @var3;
6140@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6141SELECT @var3;
6142@var3
61438
6144EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6145FROM t1 AS sq4_alias1
6146WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6147sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6148id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61491	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	Const row not found
61502	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
6151SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6152FROM t1 AS sq4_alias1
6153WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6154sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6155@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6156SELECT @var3;
6157@var3
61588
6159
6160Testcase with inner subquery; crashed WL#6095
6161SET @var3=8;
6162EXPLAIN SELECT sq4_alias1.*
6163FROM t1 AS sq4_alias1
6164WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6165NOT IN
6166(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6167c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6168FROM t2 AS c_sq1_alias1
6169WHERE (c_sq1_alias1.col_int_nokey != @var2
6170OR c_sq1_alias1.pk != @var3));
6171id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61721	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
61732	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6174SELECT sq4_alias1.*
6175FROM t1 AS sq4_alias1
6176WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6177NOT IN
6178(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6179c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6180FROM t2 AS c_sq1_alias1
6181WHERE (c_sq1_alias1.col_int_nokey != @var2
6182OR c_sq1_alias1.pk != @var3));
6183pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6184EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
6185FROM t1 AS sq4_alias1
6186WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6187NOT IN
6188(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6189c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6190FROM t2 AS c_sq1_alias1
6191WHERE (c_sq1_alias1.col_int_nokey != @var2
6192OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6193id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61941	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
61953	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6196SELECT * FROM ( SELECT sq4_alias1.*
6197FROM t1 AS sq4_alias1
6198WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6199NOT IN
6200(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6201c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6202FROM t2 AS c_sq1_alias1
6203WHERE (c_sq1_alias1.col_int_nokey != @var2
6204OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6205pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6206DROP TABLE t1,t2;
6207# End of 5.2 tests
6208#
6209# BUG#779885: Crash in eliminate_item_equal with materialization=on in
6210#
6211CREATE TABLE t1 ( f1 int );
6212INSERT INTO t1 VALUES (19), (20);
6213CREATE TABLE t2 ( f10 varchar(32) );
6214INSERT INTO t2 VALUES ('c'),('d');
6215CREATE TABLE t3 ( f10 varchar(32) );
6216INSERT INTO t3 VALUES ('a'),('b');
6217SELECT *
6218FROM t1
6219WHERE
6220( 't' ) IN (
6221SELECT t3.f10
6222FROM t3
6223JOIN t2
6224ON t2.f10 = t3.f10
6225);
6226f1
6227DROP TABLE t1,t2,t3;
6228#
6229# BUG lp:813473: Wrong result with outer join + NOT IN subquery
6230# This bug is a duplicate of Bug#11764086 whose test case is added below
6231#
6232CREATE TABLE t1 (c int) ;
6233INSERT INTO t1 VALUES (5),(6);
6234CREATE TABLE t2 (a int, b int) ;
6235INSERT INTO t2 VALUES (20,9),(20,9);
6236create table t3 (d int, e int);
6237insert into t3 values (2, 9), (3,10);
6238SET @save_optimizer_switch=@@optimizer_switch;
6239SET optimizer_switch='outer_join_with_cache=off';
6240EXPLAIN
6241SELECT t2.b , t1.c
6242FROM t2 LEFT JOIN t1 ON t1.c < 3
6243WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
6244id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62451	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2
62461	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
62472	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	2
6248SELECT t2.b , t1.c
6249FROM t2 LEFT JOIN t1 ON t1.c < 3
6250WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
6251b	c
62529	NULL
62539	NULL
6254SET optimizer_switch=@save_optimizer_switch;
6255drop table t1, t2, t3;
6256#
6257# BUG#50257: Missing info in REF column of the EXPLAIN
6258#            lines for subselects
6259#
6260CREATE TABLE t1 (a INT, b INT, INDEX (a));
6261INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
6262
6263set @tmp_optimizer_switch=@@optimizer_switch;
6264set optimizer_switch='derived_merge=off,derived_with_keys=off';
6265EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6266id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62671	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2
62682	DERIVED	t1	ref	a	a	5	const	1
6269set optimizer_switch=@tmp_optimizer_switch;
6270
6271EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6272id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62731	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
62742	SUBQUERY	t1	ref	a	a	5	const	1	Using index
6275
6276DROP TABLE t1;
6277#
6278# Bug#11764086: Null left operand to NOT IN in WHERE clause
6279# behaves differently than real NULL
6280#
6281CREATE TABLE parent (id int);
6282INSERT INTO parent VALUES (1), (2);
6283CREATE TABLE child (parent_id int, other int);
6284INSERT INTO child VALUES (1,NULL);
6285# Offending query (c.parent_id is NULL for null-complemented rows only)
6286SELECT    p.id, c.parent_id
6287FROM      parent p
6288LEFT JOIN child  c
6289ON        p.id = c.parent_id
6290WHERE     c.parent_id NOT IN (
6291SELECT parent_id
6292FROM   child
6293WHERE  parent_id = 3
6294);
6295id	parent_id
62961	1
62972	NULL
6298# Some syntactic variations with IS FALSE and IS NOT TRUE
6299SELECT    p.id, c.parent_id
6300FROM      parent p
6301LEFT JOIN child  c
6302ON        p.id = c.parent_id
6303WHERE     c.parent_id IN (
6304SELECT parent_id
6305FROM   child
6306WHERE  parent_id = 3
6307) IS NOT TRUE;
6308id	parent_id
63091	1
63102	NULL
6311SELECT    p.id, c.parent_id
6312FROM      parent p
6313LEFT JOIN child  c
6314ON        p.id = c.parent_id
6315WHERE     c.parent_id IN (
6316SELECT parent_id
6317FROM   child
6318WHERE  parent_id = 3
6319) IS FALSE;
6320id	parent_id
63211	1
63222	NULL
6323DROP TABLE parent, child;
6324# End of test for bug#11764086.
6325#
6326# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
6327#                BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
6328#
6329CREATE TABLE t1(a INT);
6330INSERT INTO t1 VALUES (0), (1);
6331CREATE TABLE t2(
6332b TEXT,
6333c INT,
6334PRIMARY KEY (b(1))
6335);
6336INSERT INTO t2 VALUES ('a', 2), ('b', 3);
6337SELECT 1 FROM t1 WHERE a =
6338(SELECT 1 FROM t2 WHERE b =
6339(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6340ORDER BY b
6341);
63421
6343Warnings:
6344Warning	1292	Truncated incorrect DOUBLE value: 'a'
6345Warning	1292	Truncated incorrect DOUBLE value: 'b'
6346Warning	1292	Truncated incorrect DOUBLE value: 'a'
6347Warning	1292	Truncated incorrect DOUBLE value: 'b'
6348SELECT 1 FROM t1 WHERE a =
6349(SELECT 1 FROM t2 WHERE b =
6350(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6351GROUP BY b
6352);
63531
6354Warnings:
6355Warning	1292	Truncated incorrect DOUBLE value: 'a'
6356Warning	1292	Truncated incorrect DOUBLE value: 'b'
6357Warning	1292	Truncated incorrect DOUBLE value: 'a'
6358Warning	1292	Truncated incorrect DOUBLE value: 'b'
6359DROP TABLE t1, t2;
6360#
6361# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6362#
6363CREATE TABLE t1 (f1 varchar(1));
6364INSERT INTO t1 VALUES ('v'),('s');
6365CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6366INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6367('d'),('y'),('t'),('d'),('s');
6368SELECT table1.f1, table2.f1_key
6369FROM t1 AS table1, t2 AS table2
6370WHERE EXISTS
6371(
6372SELECT DISTINCT f1_key
6373FROM t2
6374WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6375f1	f1_key
6376s	c
6377s	d
6378s	d
6379s	d
6380s	j
6381s	m
6382s	s
6383s	t
6384s	v
6385s	y
6386v	c
6387v	d
6388v	d
6389v	d
6390v	j
6391v	m
6392v	s
6393v	t
6394v	v
6395v	y
6396explain SELECT table1.f1, table2.f1_key
6397FROM t1 AS table1, t2 AS table2
6398WHERE EXISTS
6399(
6400SELECT DISTINCT f1_key
6401FROM t2
6402WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6403id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
64041	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
64051	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
64062	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6407DROP TABLE t1,t2;
6408#
6409# lp:826279: assertion failure with GROUP BY a result of subquery
6410#
6411CREATE TABLE t1 (a int);
6412INSERT INTO t1 VALUES (0), (0);
6413CREATE TABLE t2 (a int, b int, c int);
6414INSERT INTO t2 VALUES (10,7,0), (0,7,0);
6415CREATE TABLE t3 (a int, b int);
6416INSERT INTO t3 VALUES (10,7), (0,7);
6417SELECT SUM(DISTINCT b),
6418(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6419WHERE t.a != 0 AND t2.a != 0)
6420FROM (SELECT * FROM t3) AS t
6421GROUP BY 2;
6422SUM(DISTINCT b)	(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6423WHERE t.a != 0 AND t2.a != 0)
64247	NULL
6425SELECT SUM(DISTINCT b),
6426(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
6427FROM (SELECT * FROM t3) AS t
6428GROUP BY 2;
6429SUM(DISTINCT b)	(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
64307	NULL
64317	10
6432DROP TABLE t1,t2,t3;
6433#
6434# Bug#12329653
6435# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
6436#
6437CREATE TABLE t1(a1 int);
6438INSERT INTO t1 VALUES (1),(2);
6439SELECT @@session.sql_mode INTO @old_sql_mode;
6440SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
6441SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
64421
64431
64441
6445PREPARE stmt FROM
6446'SELECT 1 UNION ALL
6447SELECT 1 FROM t1
6448ORDER BY
6449(SELECT 1 FROM t1 AS t1_0
6450  WHERE 1 < SOME (SELECT a1 FROM t1)
6451)' ;
6452EXECUTE stmt ;
6453ERROR 21000: Subquery returns more than 1 row
6454EXECUTE stmt ;
6455ERROR 21000: Subquery returns more than 1 row
6456SET SESSION sql_mode=@old_sql_mode;
6457DEALLOCATE PREPARE stmt;
6458DROP TABLE t1;
6459#
6460# LP BUG#833777 Performance regression with deeply nested subqueries
6461#
6462create table t1 (a int not null, b char(10) not null);
6463insert into t1 values (1, 'a');
6464set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
6465select 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)))))))))))))))))))))))))))));
6466a
64671
6468set @@optimizer_switch=@subselect_tmp;
6469drop table t1;
6470#
6471# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
6472#
6473CREATE TABLE t1 (a varchar(3));
6474INSERT INTO t1 VALUES ('AAA'),('BBB');
6475CREATE TABLE t2 (a varchar(3));
6476INSERT INTO t2 VALUES ('CCC');
6477set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6478SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
6479a
6480set @@optimizer_switch=@subselect_tmp;
6481drop table t1, t2;
6482#
6483# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
6484# with view , UNION and prepared statement (rewriting fake_select
6485# condition).
6486#
6487CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
6488INSERT INTO t1 VALUES (6,'d'),(7,'y');
6489CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
6490INSERT INTO t2 VALUES (10,7);
6491CREATE VIEW v2 AS SELECT * FROM t2;
6492PREPARE st1 FROM "
6493        SELECT *
6494        FROM t1
6495        LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
6496        WHERE v2.f1 NOT IN (
6497                SELECT 1 UNION
6498                SELECT 247
6499        )
6500";
6501EXECUTE st1;
6502f1	f4	f1	f2
65037	y	10	7
6504deallocate prepare st1;
6505DROP VIEW v2;
6506DROP TABLE t1,t2;
6507#
6508# LP bug #887458 Crash in subselect_union_engine::no_rows with
6509# double UNION and join_cache_level=3,8
6510# (IN/ALL/ANY optimizations should not be applied to fake_select)
6511CREATE TABLE t2 ( a int, b varchar(1)) ;
6512INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
6513CREATE TABLE t1 ( b varchar(1)) ;
6514INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
6515set @save_join_cache_level=@@join_cache_level;
6516SET SESSION join_cache_level=3;
6517SELECT *
6518FROM t1, t2
6519WHERE t2.b IN (
6520SELECT 'm' UNION
6521SELECT 'm'
6522) OR t1.b <> SOME (
6523SELECT 'v' UNION
6524SELECT 't'
6525);
6526b	a	b
6527set @@join_cache_level= @save_join_cache_level;
6528drop table t1,t2;
6529#
6530# LP bug #885162 Got error 124 from storage engine with UNION inside
6531# subquery and join_cache_level=3..8
6532# (IN/ALL/ANY optimizations should not be applied to fake_select)
6533#
6534CREATE TABLE t1 (
6535f1 varchar(1) DEFAULT NULL
6536);
6537INSERT INTO t1 VALUES ('c');
6538set @save_join_cache_level=@@join_cache_level;
6539SET SESSION join_cache_level=8;
6540SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
6541f1
6542set @@join_cache_level= @save_join_cache_level;
6543drop table t1;
6544#
6545# LP BUG#747278 incorrect values of the NULL (no rows) single
6546# row subquery requested via element_index() interface
6547#
6548CREATE TABLE t1 (f1a int, f1b int) ;
6549INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
6550CREATE TABLE t2 ( f2 int);
6551INSERT IGNORE INTO t2 VALUES (3),(4);
6552CREATE TABLE t3 (f3a int default 1, f3b int default 2);
6553INSERT INTO t3 VALUES (1,1),(2,2);
6554set @old_optimizer_switch = @@session.optimizer_switch;
6555set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
6556SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6557(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6558NULL
6559NULL
6560SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6561(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6562NULL
6563NULL
6564SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6565(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6566NULL
6567NULL
6568SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6569(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6570NULL
6571SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6572(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6573NULL
6574NULL
6575SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6576(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6577NULL
6578NULL
6579SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6580(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6581NULL
6582NULL
6583SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6584(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6585NULL
6586set @@session.optimizer_switch=@old_optimizer_switch;
6587SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6588(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6589NULL
6590NULL
6591SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6592(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6593NULL
6594NULL
6595SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6596(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6597NULL
6598NULL
6599SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6600(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6601NULL
6602SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6603(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6604NULL
6605NULL
6606SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6607(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6608NULL
6609NULL
6610SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6611(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6612NULL
6613NULL
6614SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6615(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6616NULL
6617select (null, null) = (null, null);
6618(null, null) = (null, null)
6619NULL
6620SELECT (SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0);
6621(SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0)
6622NULL
6623drop tables t1,t2,t3;
6624#
6625# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
6626#
6627CREATE TABLE t1 (a date, KEY (a)) ;
6628INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
6629set @old_optimizer_switch = @@optimizer_switch;
6630SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6631EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6632id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66331	PRIMARY	t1	index	NULL	a	4	NULL	2	Using where; Using index
66342	DEPENDENT SUBQUERY	t1	index_subquery	a	a	4	func	2	Using index
6635SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6636a
66372009-01-01
66382009-02-02
6639SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
6640EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6641id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66421	PRIMARY	t1	index	NULL	a	4	NULL	2	Using where; Using index
66432	MATERIALIZED	t1	index	NULL	a	4	NULL	2	Using index
6644SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6645a
66462009-01-01
66472009-02-02
6648set @@optimizer_switch=@old_optimizer_switch;
6649drop table t1;
6650#
6651# LP BUG#908269 incorrect condition in case of subqueries depending
6652# on constant tables
6653#
6654CREATE TABLE t1 ( a INT );
6655INSERT INTO t1 VALUES (1),(5);
6656CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
6657INSERT INTO t2 VALUES (1);
6658CREATE TABLE t3 ( c INT );
6659INSERT INTO t3 VALUES (4),(5);
6660SET optimizer_switch='subquery_cache=off';
6661SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
6662( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
66631
6664NULL
6665SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
6666( SELECT b FROM t2 WHERE b = a OR b * 0)
66671
6668NULL
6669SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
6670( SELECT b FROM t2 WHERE b = a OR rand() * 0)
66711
6672NULL
6673drop table t1,t2,t3;
6674set optimizer_switch=@subselect_tmp;
6675#
6676# LP BUG#905353 Wrong non-empty result with a constant table,
6677# aggregate function in subquery, MyISAM or Aria
6678#
6679CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
6680INSERT INTO t1 VALUES (1);
6681SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
6682a
6683drop table t1;
6684#
6685# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in
6686# main query and implicit grouping
6687#
6688CREATE TABLE t1 (f1 int) engine=MyISAM;
6689INSERT INTO t1 VALUES (7),(8);
6690CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
6691INSERT INTO t2 VALUES (3,'f');
6692EXPLAIN
6693SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6694id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66951	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
66962	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6697SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6698COUNT(f1)	f4
66990	NULL
6700EXPLAIN
6701SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6702id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67031	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67042	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6705SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6706COUNT(f1)	f4
67070	0
6708EXPLAIN
6709SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6710id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67111	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67122	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6713SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6714COUNT(f1)	f4
67150	1
6716EXPLAIN
6717SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6718id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67191	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67202	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6721SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6722COUNT(f1)	f4
67230	0
6724drop table t1,t2;
6725#
6726# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
6727# HAVING, UNION in subquery
6728#
6729CREATE TABLE t1 (a INT);
6730INSERT INTO t1 VALUES (7),(0);
6731CREATE TABLE t2 (b INT);
6732EXPLAIN
6733SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6734id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67351	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	Const row not found
67361	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
67372	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67383	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6739NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6740SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6741f1	f2
6742drop table t1,t2;
6743#
6744# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
6745# WHERE and UNION in HAVING
6746#
6747CREATE TABLE t1 (a INT);
6748INSERT INTO t1 VALUES (1),(7);
6749EXPLAIN
6750SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6751id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67521	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
67532	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67543	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6755NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6756SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6757min_a	a
6758EXPLAIN
6759SELECT 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;
6760id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67611	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
67622	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
67633	DEPENDENT UNION	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6764NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6765SELECT 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;
6766min_a	a
6767drop table t1;
6768#
6769# MDEV-367: Different results with and without subquery_cache on
6770# a query with a constant NOT IN condition
6771#
6772CREATE TABLE t1 (a INT) ENGINE=MyISAM;
6773INSERT INTO t1 VALUES (1),(2),(3);
6774set @mdev367_optimizer_switch = @@optimizer_switch;
6775set optimizer_switch = 'subquery_cache=on';
6776SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6777a
6778SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6779a	( 3, 3 ) NOT IN ( SELECT NULL, NULL )
67801	NULL
67812	NULL
67823	NULL
6783set optimizer_switch=@mdev367_optimizer_switch;
6784set optimizer_switch = 'subquery_cache=off';
6785SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6786a
6787SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6788a	( 3, 3 ) NOT IN ( SELECT NULL, NULL )
67891	NULL
67902	NULL
67913	NULL
6792set optimizer_switch=@mdev367_optimizer_switch;
6793DROP TABLE t1;
6794#
6795# MDEV-521 single value subselect transformation problem
6796#
6797CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
6798INSERT INTO t1 VALUES ('u1'),('u2');
6799SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6800f1
6801u1
6802u2
6803FLUSH TABLES;
6804SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6805f1
6806u1
6807u2
6808DROP TABLE t1;
6809# return optimizer switch changed in the beginning of this test
6810set optimizer_switch=@subselect_tmp;
6811#
6812# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
6813# precomputed and thus not part of optimization
6814#
6815CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
6816INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
6817('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
6818EXPLAIN
6819SELECT MAX( alias2.a ) AS field
6820FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6821WHERE alias1.a = alias2.a OR alias1.a = 'y'
6822HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6823id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68241	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible HAVING
68252	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6826SELECT MAX( alias2.a ) AS field
6827FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6828WHERE alias1.a = alias2.a OR alias1.a = 'y'
6829HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6830field
6831EXPLAIN
6832SELECT MAX( alias2.a )
6833FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6834WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6835id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68361	PRIMARY	alias1	index	a	a	19	NULL	11	Using where; Using index
68371	PRIMARY	alias2	ref	a	a	19	test.alias1.a	2	Using index
68381	PRIMARY	alias3	index	NULL	a	19	NULL	11	Using index; Using join buffer (flat, BNL join)
68392	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6840SELECT MAX( alias2.a )
6841FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6842WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6843MAX( alias2.a )
6844Arden-Arcade
6845drop table t1;
6846#
6847# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
6848# with semijoin+materialization, IN and = subqueries
6849#
6850CREATE TABLE t1 (a1 INT);
6851INSERT INTO t1 VALUES (4),(6);
6852CREATE TABLE t2 (b1 INT);
6853INSERT INTO t2 VALUES (1),(7);
6854EXPLAIN
6855SELECT * FROM t1
6856WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6857id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68581	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68592	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
68602	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1
68613	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2
68623	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
6863SELECT * FROM t1
6864WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6865a1
6866drop table t1, t2;
6867#
6868# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
6869# inner joins takes hundreds times longer
6870#
6871CREATE TABLE t1 (a INT);
6872INSERT INTO t1 VALUES (1),(7);
6873CREATE TABLE t2 (b INT);
6874INSERT INTO t2 VALUES (4),(5);
6875CREATE TABLE t3 (c INT);
6876INSERT INTO t3 VALUES (8),(3);
6877set @@expensive_subquery_limit= 0;
6878EXPLAIN
6879SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6880FROM t2 alias1, t1 alias2, t1 alias3;
6881id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68821	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
68831	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68841	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68852	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68862	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
68873	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6888flush status;
6889SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6890FROM t2 alias1, t1 alias2, t1 alias3;
6891(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6892NULL
6893NULL
6894NULL
6895NULL
6896NULL
6897NULL
6898NULL
6899NULL
6900show status like "subquery_cache%";
6901Variable_name	Value
6902Subquery_cache_hit	6
6903Subquery_cache_miss	2
6904show status like '%Handler_read%';
6905Variable_name	Value
6906Handler_read_first	0
6907Handler_read_key	8
6908Handler_read_last	0
6909Handler_read_next	0
6910Handler_read_prev	0
6911Handler_read_retry	0
6912Handler_read_rnd	0
6913Handler_read_rnd_deleted	0
6914Handler_read_rnd_next	22
6915set @@expensive_subquery_limit= default;
6916EXPLAIN
6917SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6918FROM t2 alias1, t1 alias2, t1 alias3;
6919id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69201	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
69211	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69221	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69232	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
69242	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
69253	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6926flush status;
6927SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6928FROM t2 alias1, t1 alias2, t1 alias3;
6929(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6930NULL
6931NULL
6932NULL
6933NULL
6934NULL
6935NULL
6936NULL
6937NULL
6938show status like "subquery_cache%";
6939Variable_name	Value
6940Subquery_cache_hit	0
6941Subquery_cache_miss	0
6942show status like '%Handler_read%';
6943Variable_name	Value
6944Handler_read_first	0
6945Handler_read_key	0
6946Handler_read_last	0
6947Handler_read_next	0
6948Handler_read_prev	0
6949Handler_read_retry	0
6950Handler_read_rnd	0
6951Handler_read_rnd_deleted	0
6952Handler_read_rnd_next	16
6953drop table t1, t2, t3;
6954#
6955# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
6956#
6957CREATE TABLE t1 (a INT);
6958INSERT INTO t1 VALUES (0),(8);
6959CREATE TABLE t2 (b INT PRIMARY KEY);
6960INSERT INTO t2 VALUES (1),(2);
6961EXPLAIN
6962SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6963id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69641	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
69652	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
69663	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6967SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6968a
6969drop table t1,t2;
6970#
6971# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
6972#
6973CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
6974INSERT INTO t1 VALUES ('USA');
6975CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
6976INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
6977CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
6978INSERT INTO t3 VALUES (12),(22),(9),(45);
6979create table t4 like t3;
6980insert into t4 select * from t3;
6981# This should not show range access for table t2
6982explain
6983SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6984WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6985id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69861	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1
69871	PRIMARY	t2	ALL	b	NULL	NULL	NULL	2	Using where
69881	PRIMARY	t3	ref	d	d	5	test.t2.b	2	Using index
69893	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
6990set @tmp_mdev410=@@global.userstat;
6991set global userstat=on;
6992flush table_statistics;
6993flush index_statistics;
6994SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6995WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6996MIN(b)
6997NULL
6998# The following shows that t2 was indeed scanned with a full scan.
6999show table_statistics;
7000Table_schema	Table_name	Rows_read	Rows_changed	Rows_changed_x_#indexes
7001test	t1	2	0	0
7002test	t2	3	0	0
7003show index_statistics;
7004Table_schema	Table_name	Index_name	Rows_read
7005test	t2	b	1
7006set global userstat=@tmp_mdev410;
7007DROP TABLE t1,t2,t3,t4;
7008#
7009# MDEV-430: Server crashes in select_describe on EXPLAIN with
7010#    materialization+semijoin, 2 nested subqueries, aggregate functions
7011#
7012CREATE TABLE t1 (a INT, KEY(a));
7013INSERT INTO t1 VALUES (1),(8);
7014CREATE TABLE t2 (b INT, KEY(b));
7015INSERT INTO t2 VALUES (45),(17),(20);
7016EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7017id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70181	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70192	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70202	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7021DROP TABLE t1,t2;
7022#
7023# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
7024#
7025CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
7026INSERT INTO t1 VALUES (8),(0);
7027CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
7028INSERT INTO t2 VALUES (4,'j'),(6,'v');
7029CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
7030INSERT INTO t3 VALUES ('b'),('c');
7031EXPLAIN
7032SELECT * FROM t1
7033WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7034id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70351	PRIMARY	t1	index	a	a	5	NULL	2	Using where; Using index
70362	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
70372	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	test.t2.c	1
70383	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	2
7039SELECT * FROM t1
7040WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7041a
7042drop table t1, t2, t3;
7043#
7044# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
7045#
7046CREATE TABLE t1 (a INT, KEY(a));
7047INSERT INTO t1 VALUES (1),(8);
7048CREATE TABLE t2 (b INT, KEY(b));
7049INSERT INTO t2 VALUES (45),(17),(20);
7050EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7051id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70521	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70532	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70542	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7055DROP TABLE t1,t2;
7056#
7057# MDEV-5991: crash in Item_field::used_tables
7058#
7059create table t1 (c int);
7060select exists(select 1 from t1 group by `c` in (select `c` from t1));
7061exists(select 1 from t1 group by `c` in (select `c` from t1))
70620
7063drop table t1;
7064#
7065# MDEV-7565: Server crash with Signal 6 (part 2)
7066#
7067Select
7068(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7069Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7070) As `ControlRev`
7071From
7072(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7073Group By  TestCase.Revenue, TestCase.TemplateID;
7074ControlRev
7075NULL
7076#
7077# MDEV-7445:Server crash with Signal 6
7078#
7079CREATE PROCEDURE procedure2()
7080BEGIN
7081Select
7082(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7083Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7084) As `ControlRev`
7085  From
7086(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7087Group By  TestCase.Revenue, TestCase.TemplateID;
7088END |
7089call procedure2();
7090ControlRev
7091NULL
7092call procedure2();
7093ControlRev
7094NULL
7095drop procedure procedure2;
7096#
7097# MDEV-7846:Server crashes in Item_subselect::fix
7098#_fields or fails with Thread stack overrun
7099#
7100CREATE TABLE t1 (column1 INT) ENGINE=MyISAM;
7101INSERT INTO t1 VALUES (3),(9);
7102CREATE TABLE t2 (column2 INT) ENGINE=MyISAM;
7103INSERT INTO t2 VALUES (1),(4);
7104CREATE TABLE t3 (column3 INT) ENGINE=MyISAM;
7105INSERT INTO t3 VALUES (6),(8);
7106CREATE TABLE t4 (column4 INT) ENGINE=MyISAM;
7107INSERT INTO t4 VALUES (2),(5);
7108PREPARE stmt FROM "
7109SELECT (
7110  SELECT MAX( table1.column1 ) AS field1
7111  FROM t1 AS table1
7112  WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 )
7113) AS sq
7114FROM t3 AS table3, t4 AS table4 GROUP BY sq
7115";
7116EXECUTE stmt;
7117sq
7118NULL
7119EXECUTE stmt;
7120sq
7121NULL
7122deallocate prepare stmt;
7123drop table t1,t2,t3,t4;
7124#
7125# MDEV-7122
7126# Assertion `0' failed in subselect_hash_sj_engine::init
7127#
7128SET SESSION big_tables=1;
7129CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
7130INSERT INTO t1 VALUES(0),(0),(0);
7131SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1);
7132a
71330
71340
71350
7136DROP TABLE t1;
7137SET SESSION big_tables=0;
7138#
7139# MDEV-10776: Server crash on query
7140#
7141create table t1 (field1 int);
7142insert into t1 values (1);
7143select round((select 1 from t1 limit 1))
7144from t1
7145group by round((select 1 from t1 limit 1));
7146round((select 1 from t1 limit 1))
71471
7148drop table t1;
7149#
7150# MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE ||
7151# m_lock_type != 2' failed in  handler::ha_index_read_map
7152#
7153CREATE TABLE t1 (f1 INT);
7154INSERT INTO t1 VALUES (1),(2);
7155CREATE TABLE t2 (f2 INT, KEY(f2));
7156INSERT INTO t2 VALUES (3);
7157CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2;
7158SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7159ERROR 42000: Can't group on 'sq'
7160SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7161ERROR 42000: Can't group on 'sq'
7162SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0;
7163f2
71643
7165SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 );
7166count(*)
71671
7168delete from t1;
7169SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7170ERROR 42000: Can't group on 'sq'
7171SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7172ERROR 42000: Can't group on 'sq'
7173drop view v2;
7174drop table t1,t2;
7175#
7176# MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*)
7177#
7178CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL);
7179INSERT INTO t1 VALUES ('foo','bar');
7180SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 );
7181f1	f2
7182SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 );
7183f1	f2
7184foo	bar
7185DROP TABLE t1;
7186#
7187# MDEV-10146: Wrong result (or questionable result and behavior)
7188# with aggregate function in uncorrelated SELECT subquery
7189#
7190CREATE TABLE t1 (f1 INT);
7191CREATE VIEW v1 AS SELECT * FROM t1;
7192INSERT INTO t1 VALUES (1),(2);
7193CREATE TABLE t2 (f2 int);
7194INSERT INTO t2 VALUES (3);
7195SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7196( SELECT MAX(f1) FROM t2 )
71972
7198SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7199( SELECT MAX(f1) FROM t2 )
72002
7201INSERT INTO t2 VALUES (4);
7202SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7203ERROR 21000: Subquery returns more than 1 row
7204SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7205ERROR 21000: Subquery returns more than 1 row
7206drop view v1;
7207drop table t1,t2;
7208CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM;
7209INSERT t1 VALUES (4),(8);
7210CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM;
7211INSERT t2 VALUES (6);
7212SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7213(SELECT MAX(sq.f2) FROM t1)
7214NULL
7215#
7216# Disable this query till MDEV-13399 is resolved
7217#
7218# INSERT t2 VALUES (9);
7219# --error ER_SUBQUERY_NO_1_ROW
7220# SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7221#
7222drop table t1, t2;
7223#
7224# MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in
7225# (5.5 test)
7226#
7227SET @optimiser_switch_save= @@optimizer_switch;
7228CREATE TABLE t1 (a INT NOT NULL);
7229INSERT INTO t1 VALUES (1),(1),(1),(5),(5);
7230CREATE TABLE t2 (b INT);
7231INSERT INTO t2 VALUES (5),(1);
7232CREATE TABLE t3 (c INT, KEY(c));
7233INSERT INTO t3 VALUES (5),(5);
7234SET optimizer_switch='semijoin=on';
7235select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7236and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7237a
72385
72395
7240SET optimizer_switch='semijoin=off';
7241select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7242and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7243a
72445
72455
7246SET @@optimizer_switch= @optimiser_switch_save;
7247DROP TABLE t1, t2, t3;
7248#
7249# MDEV-16820: impossible where with inexpensive subquery
7250#
7251create table t1 (a int) engine=myisam;
7252insert into t1 values (3), (1), (7);
7253create table t2 (b int, index idx(b));
7254insert into t2 values (2), (5), (3), (2);
7255explain select * from t1 where (select max(b) from t2) = 10;
7256id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72571	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72582	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7259explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3;
7260id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72611	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72622	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7263drop table t1,t2;
7264#
7265# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7266#
7267create table t10 (a int);
7268insert into t10 values (1),(2),(3);
7269create table t12 (a int);
7270insert into t12 values (1),(2),(3);
7271select * from t10 where exists (select * from t12 order by a limit 0);
7272a
7273explain select * from t10 where exists (select * from t12 order by a limit 0);
7274id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72751	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72762	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Zero limit
7277prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7278set @l=1;
7279execute stmt1 using @l;
7280a
72811
72822
72833
7284set @l=2;
7285execute stmt1 using @l;
7286a
72871
72882
72893
7290set @l=0;
7291execute stmt1 using @l;
7292a
7293deallocate prepare stmt1;
7294drop table t10, t12;
7295End of 5.5 tests
7296# End of 10.0 tests
7297#
7298# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7299# with UNION in ALL subquery
7300#
7301SET NAMES utf8;
7302CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7303INSERT INTO t1 VALUES ('foo');
7304SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7305f
7306foo
7307SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar');
7308f
7309foo
7310drop table t1;
7311SET NAMES default;
7312#
7313# MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops
7314#
7315SET NAMES utf8;
7316CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM;
7317CREATE TABLE t2 (f3 INT) ENGINE=MyISAM;
7318SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' );
7319f1	f2	f3
7320SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo');
7321f1	f2	f3
7322DROP TABLE t1, t2;
7323SET NAMES default;
7324# End of 10.1 tests
7325#
7326# MDEV-12564: IN TO EXISTS transformation for rows after
7327#             conversion an outer join to inner join
7328#
7329CREATE TABLE t (
7330pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
7331) ENGINE=MyISAM;
7332INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
7333SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
7334WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
7335pk	i	c	pk	i	c
73361	10	foo	1	10	foo
7337DROP TABLE t;
7338#
7339# MDEV-25002: Outer reference in ON clause of subselect
7340#
7341create table t1 (
7342pk int primary key,
7343a int
7344) engine=myisam;
7345insert into t1 values (1,1), (2,2);
7346create table t2 (
7347pk int primary key,
7348b int
7349) engine=myisam;
7350insert into t2 values (1,1), (2,3);
7351create table t3 (a int);
7352insert into t3 values (1),(2);
7353select a,
7354(select count(*) from t1, t2
7355where t2.pk=t3.a and t1.pk=1) as sq
7356from t3;
7357a	sq
73581	1
73592	1
7360select a,
7361(select count(*) from t1 join t2 on t2.pk=t3.a
7362where t1.pk=1) as sq
7363from t3;
7364a	sq
73651	1
73662	1
7367select a from t3
7368where a in (select t2.b from t1,t2 where t2.pk=t3.a and t1.pk=1);
7369a
73701
7371select a from t3
7372where a in (select t2.b from t1 join t2 on t2.pk=t3.a where t1.pk=1);
7373a
73741
7375drop table t1,t2,t3;
7376# End of 10.2 tests
7377#
7378# Start of 10.4 tests
7379#
7380#
7381# MDEV-16861 Split Item::update_null_value() into a new virtual method in Type_handler
7382#
7383SELECT ROW(1,2) = EXISTS (SELECT 1);
7384ERROR HY000: Illegal parameter data types row and boolean for operation '='
7385SELECT ROW(1,2) = 1 IN (SELECT 1 UNION SELECT 2);
7386ERROR HY000: Illegal parameter data types row and boolean for operation '='
7387SELECT ROW(1,2) = (1 = ANY (SELECT 1 UNION SELECT 2));
7388ERROR HY000: Illegal parameter data types row and boolean for operation '='
7389#
7390# End of 10.4 tests
7391#
7392#
7393# MDEV-19714: JOIN::pseudo_bits_cond is not visible in EXPLAIN FORMAT=JSON
7394#
7395CREATE TABLE t1 ( a INT );
7396INSERT INTO t1 VALUES (1),(5);
7397CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
7398INSERT INTO t2 VALUES (1);
7399CREATE TABLE t3 ( c INT );
7400INSERT INTO t3 VALUES (4),(5);
7401SET @tmp19714=@@optimizer_switch;
7402SET optimizer_switch='subquery_cache=off';
7403explain format=json
7404SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
7405EXPLAIN
7406{
7407  "query_block": {
7408    "select_id": 1,
7409    "table": {
7410      "table_name": "t1",
7411      "access_type": "ALL",
7412      "rows": 2,
7413      "filtered": 100
7414    },
7415    "subqueries": [
7416      {
7417        "query_block": {
7418          "select_id": 2,
7419          "pseudo_bits_condition": "1 = t1.a or <in_optimizer>(1,<exists>(subquery#3))",
7420          "table": {
7421            "table_name": "t2",
7422            "access_type": "system",
7423            "rows": 1,
7424            "filtered": 100
7425          },
7426          "subqueries": [
7427            {
7428              "query_block": {
7429                "select_id": 3,
7430                "table": {
7431                  "table_name": "t3",
7432                  "access_type": "ALL",
7433                  "rows": 2,
7434                  "filtered": 100,
7435                  "attached_condition": "1 = t3.c"
7436                }
7437              }
7438            }
7439          ]
7440        }
7441      }
7442    ]
7443  }
7444}
7445SET optimizer_switch=@tmp19714;
7446drop table t1,t2,t3;
7447set @optimizer_switch_for_subselect_test=null;
7448set @join_cache_level_for_subselect_test=NULL;
7449