1drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
2drop view if exists v2;
3call mtr.add_suppression("Sort aborted.*");
4set @subselect_tmp=@@optimizer_switch;
5set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
6"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
7set join_cache_level=1;
8SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
9SET optimizer_use_condition_selectivity=4;
10select (select 2);
11(select 2)
122
13explain extended select (select 2);
14id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
151	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
16Warnings:
17Note	1249	Select 2 was reduced during optimization
18Note	1003	select 2 AS `(select 2)`
19SELECT (SELECT 1) UNION SELECT (SELECT 2);
20(SELECT 1)
211
222
23explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
24id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
251	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
263	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
27NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
28Warnings:
29Note	1249	Select 2 was reduced during optimization
30Note	1249	Select 4 was reduced during optimization
31Note	1003	/* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)`
32SELECT (SELECT (SELECT 0 UNION SELECT 0));
33(SELECT (SELECT 0 UNION SELECT 0))
340
35explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
36id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
371	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
383	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
394	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
40NULL	UNION RESULT	<union3,4>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
41Warnings:
42Note	1249	Select 2 was reduced during optimization
43Note	1003	/* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
44SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
45ERROR 42S22: Reference 'a' not supported (forward reference in item list)
46SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
47ERROR 42S22: Reference 'b' not supported (forward reference in item list)
48SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
49(SELECT 1)	MAX(1)
501	1
51SELECT (SELECT a) as a;
52ERROR 42S22: Reference 'a' not supported (forward reference in item list)
53EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
54id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
551	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	1	100.00
563	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
572	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
58Warnings:
59Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
60Note	1276	Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
61Note	1003	/* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1
62SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
631
641
65SELECT (SELECT 1), a;
66ERROR 42S22: Unknown column 'a' in 'field list'
67SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
68a
691
70SELECT 1 FROM (SELECT (SELECT a) b) c;
71ERROR 42S22: Unknown column 'a' in 'field list'
72SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
73id
741
75SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
76ERROR 21000: Operand should contain 1 column(s)
77SELECT 1 IN (SELECT 1);
781 IN (SELECT 1)
791
80SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
811
821
83select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
84ERROR 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
85SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
86ERROR 42000: PROCEDURE does not support subqueries or stored functions
87SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
88ERROR 42S22: Unknown column 'a' in 'field list'
89SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
90ERROR 42S22: Unknown column 'a' in 'field list'
91SELECT (SELECT 1,2,3) = ROW(1,2,3);
92(SELECT 1,2,3) = ROW(1,2,3)
931
94SELECT (SELECT 1,2,3) = ROW(1,2,1);
95(SELECT 1,2,3) = ROW(1,2,1)
960
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)
1021
103SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
104(SELECT 1,2,3) = ROW(1,2,NULL)
105NULL
106SELECT ROW(1,2,3) = (SELECT 1,2,3);
107ROW(1,2,3) = (SELECT 1,2,3)
1081
109SELECT ROW(1,2,3) = (SELECT 1,2,1);
110ROW(1,2,3) = (SELECT 1,2,1)
1110
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)
1171
118SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
119ROW(1,2,3) = (SELECT 1,2,NULL)
120NULL
121SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
122(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
1231
124SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
125(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
1260
127SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
128(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
1290
130Warnings:
131Warning	1292	Truncated incorrect DOUBLE value: '1.5b'
132SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
133(SELECT 'b',2,'a') = ROW(1.5,2,'a')
1340
135Warnings:
136Warning	1292	Truncated incorrect DOUBLE value: 'b'
137SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
138(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
1391
140SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
141(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
1420
143Warnings:
144Warning	1292	Truncated incorrect DOUBLE value: 'c'
145SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
146ERROR 21000: Operand should contain 1 column(s)
147SELECT 1 as a,(SELECT a+a) b,(SELECT b);
148a	b	(SELECT b)
1491	2	2
150create table t1 (a int);
151create table t2 (a int, b int);
152create table t3 (a int);
153create table t4 (a int not null, b int not null);
154insert into t1 values (2);
155insert into t2 values (1,7),(2,7);
156insert into t4 values (4,8),(3,8),(5,9);
157select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
158ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
159select (select a from t1 where t1.a=t2.a), a from t2;
160(select a from t1 where t1.a=t2.a)	a
161NULL	1
1622	2
163select (select a from t1 where t1.a=t2.b), a from t2;
164(select a from t1 where t1.a=t2.b)	a
165NULL	1
166NULL	2
167select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
168(select a from t1)	a	(select 1 union select 2 limit 1)
1692	1	1
1702	2	1
171select (select a from t3), a from t2;
172(select a from t3)	a
173NULL	1
174NULL	2
175select * from t2 where t2.a=(select a from t1);
176a	b
1772	7
178insert into t3 values (6),(7),(3);
179select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
180a	b
1811	7
1822	7
183(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1))
184union (select * from t4 order by a limit 2) order by a limit 3;
185a	b
1861	7
1872	7
1883	8
189(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);
190a	b
1911	7
1922	7
1934	8
1943	8
195explain 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);
196id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1971	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
1982	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using filesort
1993	UNION	t4	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2004	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
201NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
202Warnings:
203Note	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`))
204select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
205(select a from t3 where a<t2.a*4 order by 1 desc limit 1)	a
2063	1
2077	2
208select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
209(select * from t2 where a>1) as tt;
210(select t3.a from t3 where a<8 order by 1 desc limit 1)	a
2117	2
212set @tmp_optimizer_switch=@@optimizer_switch;
213set optimizer_switch='derived_merge=off,derived_with_keys=off';
214explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
215(select * from t2 where a>1) as tt;
216id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2171	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	2	100.00
2183	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
2192	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using filesort
220Warnings:
221Note	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`
222set optimizer_switch=@tmp_optimizer_switch;
223select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
224a
2252
226select * 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);
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
231select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
232b	(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
2338	7.5000
2348	4.5000
2359	7.5000
236explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
237id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2381	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	3	100.00
2392	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
2403	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
241Warnings:
242Note	1276	Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
243Note	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`
244select * from t3 where exists (select * from t2 where t2.b=t3.a);
245a
2467
247select * from t3 where not exists (select * from t2 where t2.b=t3.a);
248a
2496
2503
251select * from t3 where a in (select b from t2);
252a
2537
254select * from t3 where a not in (select b from t2);
255a
2566
2573
258select * from t3 where a = some (select b from t2);
259a
2607
261select * from t3 where a <> any (select b from t2);
262a
2636
2643
265select * from t3 where a = all (select b from t2);
266a
2677
268select * from t3 where a <> all (select b from t2);
269a
2706
2713
272insert into t2 values (100, 5);
273select * from t3 where a < any (select b from t2);
274a
2756
2763
277select * from t3 where a < all (select b from t2);
278a
2793
280select * from t3 where a >= any (select b from t2);
281a
2826
2837
284explain extended select * from t3 where a >= any (select b from t2);
285id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
2861	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
2872	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	3	100.00
288Warnings:
289Note	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`)))
290select * from t3 where a >= all (select b from t2);
291a
2927
293delete from t2 where a=100;
294select * from t3 where a in (select a,b from t2);
295ERROR 21000: Operand should contain 1 column(s)
296select * from t3 where a in (select * from t2);
297ERROR 21000: Operand should contain 1 column(s)
298insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
299select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
300b	ma
301insert into t2 values (2,10);
302select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
303b	ma
30410	1
305delete from t2 where a=2 and b=10;
306select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
307b	ma
3087	12
309create table t5 (a int);
310select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
311(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
312NULL	1
3132	2
314insert into t5 values (5);
315select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
316(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
317NULL	1
3182	2
319insert into t5 values (2);
320select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
321(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)	a
322NULL	1
3232	2
324explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
325id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3261	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
3272	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
3283	DEPENDENT UNION	t5	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
329NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
330Warnings:
331Note	1276	Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
332Note	1276	Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
333Note	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`
334select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
335ERROR 21000: Subquery returns more than 1 row
336create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
337create table t7( uq int primary key, name char(25));
338insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
339insert into t6 values (1,1),(1,2),(2,2),(1,3);
340select * from t6 where exists (select * from t7 where uq = clinic_uq);
341patient_uq	clinic_uq
3421	1
3431	2
3442	2
345explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
346id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3471	PRIMARY	t7	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using index
3481	PRIMARY	t6	ALL	i1	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (flat, BNL join)
349Warnings:
350Note	1276	Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
351Note	1003	select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t7` join `test`.`t6` where `test`.`t6`.`clinic_uq` = `test`.`t7`.`uq`
352select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
353ERROR 23000: Column 'a' in field list is ambiguous
354drop table t1,t2,t3;
355CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
356INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
357CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
358INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
359CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
360INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
361SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
362a	b
363W	1732-02-22
364SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
365a	b
366W	1
367SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
368a	b
369W	a
370CREATE TABLE `t8` (
371`pseudo` varchar(35) character set latin1 NOT NULL default '',
372`email` varchar(60) character set latin1 NOT NULL default '',
373PRIMARY KEY  (`pseudo`),
374UNIQUE KEY `email` (`email`)
375) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
376INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
377INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
378INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
379EXPLAIN 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');
380id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
3811	PRIMARY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3824	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
3832	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00
3843	SUBQUERY	t8	const	PRIMARY	PRIMARY	37	const	1	100.00	Using index
385Warnings:
386Note	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
387SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
388t8 WHERE pseudo='joce');
389ERROR HY000: Illegal parameter data types varchar and row for operation '='
390SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
391pseudo='joce');
392ERROR HY000: Illegal parameter data types varchar and row for operation '='
393SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
394pseudo
395joce
396SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
397ERROR 21000: Subquery returns more than 1 row
398drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
399CREATE TABLE `t1` (
400`topic` mediumint(8) unsigned NOT NULL default '0',
401`date` date NOT NULL default '0000-00-00',
402`pseudo` varchar(35) character set latin1 NOT NULL default '',
403PRIMARY KEY  (`pseudo`,`date`,`topic`),
404KEY `topic` (`topic`)
405) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
406INSERT INTO t1 (topic,date,pseudo) VALUES
407('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
408EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
409id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4101	SIMPLE	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
411Warnings:
412Note	1003	select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03'
413EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
414id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4151	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4162	SUBQUERY	t1	index	NULL	PRIMARY	43	NULL	2	100.00	Using where; Using index
417Warnings:
418Note	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')`
419SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
420date
4212002-08-03
422SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
423(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
4242002-08-03
425SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
4261
4271
4281
4291
430SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
431ERROR 21000: Subquery returns more than 1 row
432EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
433id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
4341	PRIMARY	t1	index	NULL	topic	3	NULL	2	100.00	Using index
4352	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
4363	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
437NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
438Warnings:
439Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
440drop table t1;
441CREATE TABLE `t1` (
442`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
443`maxnumrep` int(10) unsigned NOT NULL default '0',
444PRIMARY KEY  (`numeropost`),
445UNIQUE KEY `maxnumrep` (`maxnumrep`)
446) ENGINE=MyISAM ROW_FORMAT=FIXED;
447INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
448CREATE TABLE `t2` (
449`mot` varchar(30) NOT NULL default '',
450`topic` mediumint(8) unsigned NOT NULL default '0',
451`date` date NOT NULL default '0000-00-00',
452`pseudo` varchar(35) NOT NULL default '',
453PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
454) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
455INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
456select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
457a
45840143
459SELECT 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;
460numeropost	maxnumrep
46143506	2
46240143	1
463SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
464ERROR 42S22: Unknown column 'a' in 'having clause'
465SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
466ERROR 42S22: Unknown column 'a' in 'having clause'
467SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
468mot	topic	date	pseudo
469joce	40143	2002-10-22	joce
470joce	43506	2002-10-22	joce
471SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
472mot	topic	date	pseudo
473SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
474mot	topic	date	pseudo
475SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
476mot	topic	date	pseudo
477joce	40143	2002-10-22	joce
478joce	43506	2002-10-22	joce
479SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
480mot	topic	date	pseudo
481SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
482mot	topic	date	pseudo
483SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
484mot	topic	date	pseudo
485SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
486mot	topic	date	pseudo
487joce	40143	2002-10-22	joce
488joce	43506	2002-10-22	joce
489SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
490mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
491joce	40143	2002-10-22	joce	1
492joce	43506	2002-10-22	joce	1
493SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
494mot	topic	date	pseudo
495SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
496mot	topic	date	pseudo
497joce	40143	2002-10-22	joce
498joce	43506	2002-10-22	joce
499SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
500mot	topic	date	pseudo
501joce	40143	2002-10-22	joce
502SELECT * from t2 where topic = any (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 = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
506mot	topic	date	pseudo
507joce	40143	2002-10-22	joce
508SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
509mot	topic	date	pseudo	topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
510joce	40143	2002-10-22	joce	1
511joce	43506	2002-10-22	joce	0
512drop table t1,t2;
513CREATE TABLE `t1` (
514`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
515`maxnumrep` int(10) unsigned NOT NULL default '0',
516PRIMARY KEY  (`numeropost`),
517UNIQUE KEY `maxnumrep` (`maxnumrep`)
518) ENGINE=MyISAM ROW_FORMAT=FIXED;
519INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
520select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
521ERROR 21000: Subquery returns more than 1 row
522select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
523ERROR 21000: Subquery returns more than 1 row
524show warnings;
525Level	Code	Message
526Error	1242	Subquery returns more than 1 row
527Error	1028	Sort aborted: Subquery returns more than 1 row
528drop table t1;
529create table t1 (a int);
530insert into t1 values (1),(2),(3);
531(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
532a
5331
5342
5353
536drop table t1;
537CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
538INSERT INTO t1 VALUES ();
539SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
540ERROR 21000: Subquery returns more than 1 row
541drop table t1;
542CREATE TABLE `t1` (
543`numeropost` mediumint(8) unsigned NOT NULL default '0',
544`numreponse` int(10) unsigned NOT NULL auto_increment,
545`pseudo` varchar(35) NOT NULL default '',
546PRIMARY KEY  (`numeropost`,`numreponse`),
547UNIQUE KEY `numreponse` (`numreponse`),
548KEY `pseudo` (`pseudo`,`numeropost`)
549) ENGINE=MyISAM;
550SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
551ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
552SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
553ERROR 42S22: Unknown column 'a' in 'having clause'
554SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
555numreponse	(SELECT numeropost FROM t1 HAVING numreponse=1)
556INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
557EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
558ERROR 21000: Subquery returns more than 1 row
559SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
560ERROR 21000: Subquery returns more than 1 row
561EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
562id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5631	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
564Warnings:
565Note	1003	select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1'
566EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
567id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
5681	PRIMARY	t1	const	PRIMARY,numreponse	PRIMARY	7	const,const	1	100.00	Using index
5692	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
570Warnings:
571Note	1003	/* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1
572drop table t1;
573CREATE TABLE t1 (a int(1));
574INSERT INTO t1 VALUES (1);
575SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
5761
5771
578drop table t1;
579create table t1 (a int NOT NULL, b int, primary key (a));
580create table t2 (a int NOT NULL, b int, primary key (a));
581insert into t1 values (0, 10),(1, 11),(2, 12);
582insert into t2 values (1, 21),(2, 22),(3, 23);
583select * from t1;
584a	b
5850	10
5861	11
5872	12
588update t1 set b= (select b from t2);
589ERROR 21000: Subquery returns more than 1 row
590update t1 set b= (select b from t2 where t1.a = t2.a);
591select * from t1;
592a	b
5930	NULL
5941	21
5952	22
596drop table t1, t2;
597create table t1 (a int NOT NULL, b int, primary key (a));
598create table t2 (a int NOT NULL, b int, primary key (a));
599insert into t1 values (0, 10),(1, 11),(2, 12);
600insert into t2 values (1, 21),(2, 12),(3, 23);
601select * from t1;
602a	b
6030	10
6041	11
6052	12
606select * from t1 where b = (select b from t2 where t1.a = t2.a);
607a	b
6082	12
609delete from t1 where b in (select b from t1);
610affected rows: 3
611insert into t1 values (0, 10),(1, 11),(2, 12);
612delete from t1 where b = (select b from t2);
613ERROR 21000: Subquery returns more than 1 row
614delete from t1 where b = (select b from t2 where t1.a = t2.a);
615select * from t1 order by b;
616a	b
6170	10
6181	11
619drop table t1, t2;
620create table t11 (a int NOT NULL, b int, primary key (a));
621create table t12 (a int NOT NULL, b int, primary key (a));
622create table t2 (a int NOT NULL, b int, primary key (a));
623insert into t11 values (0, 10),(1, 11),(2, 12);
624insert into t12 values (33, 10),(22, 11),(2, 12);
625insert into t2 values (1, 21),(2, 12),(3, 23);
626select * from t11;
627a	b
6280	10
6291	11
6302	12
631select * from t12;
632a	b
63333	10
63422	11
6352	12
636delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
637ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data
638delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
639ERROR 21000: Subquery returns more than 1 row
640delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
641select * from t11;
642a	b
6430	10
6441	11
645select * from t12;
646a	b
64733	10
64822	11
649drop table t11, t12, t2;
650CREATE TABLE t1 (x int) ENGINE=MyISAM;
651create table t2 (a int) ENGINE=MyISAM;
652create table t3 (b int);
653insert into t2 values (1);
654insert into t3 values (1),(2);
655INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
656ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
657INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
658ERROR 21000: Subquery returns more than 1 row
659INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
660select * from t1;
661x
6621
663insert into t2 values (1);
664INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
665select * from t1;
666x
6671
6682
669INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
670select * from t1;
671x
6721
6732
6743
6753
676INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
677select * from t1;
678x
6791
6802
6813
6823
68311
68411
685INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
686ERROR 42S22: Unknown column 'x' in 'field list'
687INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
688select * from t1;
689x
6901
6912
6923
6933
69411
69511
6962
697drop table t1, t2, t3;
698CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
699create table t2 (a int);
700create table t3 (a int);
701insert into t2 values (1);
702insert into t3 values (1),(2);
703select * from t1;
704x	y
705replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
706ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
707replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
708ERROR 21000: Subquery returns more than 1 row
709replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
710select * from t1;
711x	y
7121	2
713replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
714select * from t1;
715x	y
7161	3
717replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
718select * from t1;
719x	y
7201	3
7214	1
722replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
723select * from t1;
724x	y
7251	3
7264	2
727replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
728select * from t1;
729x	y
7301	3
7314	2
7322	1
733drop table t1, t2, t3;
734SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
735ERROR HY000: No tables used
736CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
737INSERT INTO t2 VALUES (1),(2);
738SELECT * FROM t2 WHERE id IN (SELECT 1);
739id
7401
741EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
742id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7431	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using index
744Warnings:
745Note	1249	Select 2 was reduced during optimization
746Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1
747SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
748id
7491
750SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
751id
7522
753EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
754id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7551	SIMPLE	t2	ref	id	id	5	const	1	100.00	Using where; Using index
756Warnings:
757Note	1249	Select 3 was reduced during optimization
758Note	1249	Select 2 was reduced during optimization
759Note	1003	select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1)
760EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
761id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
7621	PRIMARY	t2	index	NULL	id	5	NULL	2	100.00	Using where; Using index
7632	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
7643	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
765NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
766Warnings:
767Note	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))))
768SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
769id
770SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
771id
7722
773INSERT INTO t2 VALUES ((SELECT * FROM t2));
774ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
775INSERT INTO t2 VALUES ((SELECT id FROM t2));
776ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
777SELECT * FROM t2;
778id
7791
7802
781CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
782INSERT INTO t1 values (1),(1);
783UPDATE t2 SET id=(SELECT * FROM t1);
784ERROR 21000: Subquery returns more than 1 row
785drop table t2, t1;
786create table t1 (a int);
787insert into t1 values (1),(2),(3);
788select 1 IN (SELECT * from t1);
7891 IN (SELECT * from t1)
7901
791select 10 IN (SELECT * from t1);
79210 IN (SELECT * from t1)
7930
794select NULL IN (SELECT * from t1);
795NULL IN (SELECT * from t1)
796NULL
797update t1 set a=NULL where a=2;
798select 1 IN (SELECT * from t1);
7991 IN (SELECT * from t1)
8001
801select 3 IN (SELECT * from t1);
8023 IN (SELECT * from t1)
8031
804select 10 IN (SELECT * from t1);
80510 IN (SELECT * from t1)
806NULL
807select 1 > ALL (SELECT * from t1);
8081 > ALL (SELECT * from t1)
8090
810select 10 > ALL (SELECT * from t1);
81110 > ALL (SELECT * from t1)
812NULL
813select 1 > ANY (SELECT * from t1);
8141 > ANY (SELECT * from t1)
815NULL
816select 10 > ANY (SELECT * from t1);
81710 > ANY (SELECT * from t1)
8181
819drop table t1;
820create table t1 (a varchar(20));
821insert into t1 values ('A'),('BC'),('DEF');
822select 'A' IN (SELECT * from t1);
823'A' IN (SELECT * from t1)
8241
825select 'XYZS' IN (SELECT * from t1);
826'XYZS' IN (SELECT * from t1)
8270
828select NULL IN (SELECT * from t1);
829NULL IN (SELECT * from t1)
830NULL
831update t1 set a=NULL where a='BC';
832select 'A' IN (SELECT * from t1);
833'A' IN (SELECT * from t1)
8341
835select 'DEF' IN (SELECT * from t1);
836'DEF' IN (SELECT * from t1)
8371
838select 'XYZS' IN (SELECT * from t1);
839'XYZS' IN (SELECT * from t1)
840NULL
841select 'A' > ALL (SELECT * from t1);
842'A' > ALL (SELECT * from t1)
8430
844select 'XYZS' > ALL (SELECT * from t1);
845'XYZS' > ALL (SELECT * from t1)
846NULL
847select 'A' > ANY (SELECT * from t1);
848'A' > ANY (SELECT * from t1)
849NULL
850select 'XYZS' > ANY (SELECT * from t1);
851'XYZS' > ANY (SELECT * from t1)
8521
853drop table t1;
854create table t1 (a float);
855insert into t1 values (1.5),(2.5),(3.5);
856select 1.5 IN (SELECT * from t1);
8571.5 IN (SELECT * from t1)
8581
859select 10.5 IN (SELECT * from t1);
86010.5 IN (SELECT * from t1)
8610
862select NULL IN (SELECT * from t1);
863NULL IN (SELECT * from t1)
864NULL
865update t1 set a=NULL where a=2.5;
866select 1.5 IN (SELECT * from t1);
8671.5 IN (SELECT * from t1)
8681
869select 3.5 IN (SELECT * from t1);
8703.5 IN (SELECT * from t1)
8711
872select 10.5 IN (SELECT * from t1);
87310.5 IN (SELECT * from t1)
874NULL
875select 1.5 > ALL (SELECT * from t1);
8761.5 > ALL (SELECT * from t1)
8770
878select 10.5 > ALL (SELECT * from t1);
87910.5 > ALL (SELECT * from t1)
880NULL
881select 1.5 > ANY (SELECT * from t1);
8821.5 > ANY (SELECT * from t1)
883NULL
884select 10.5 > ANY (SELECT * from t1);
88510.5 > ANY (SELECT * from t1)
8861
887explain extended select (select a+1) from t1;
888id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
8891	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
890Warnings:
891Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
892Note	1249	Select 2 was reduced during optimization
893Note	1003	select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
894select (select a+1) from t1;
895(select a+1)
8962.5
897NULL
8984.5
899drop table t1;
900CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY  (a));
901CREATE TABLE t2 (a int(11) default '0', INDEX (a));
902INSERT INTO t1 VALUES (1),(2),(3),(4);
903INSERT INTO t2 VALUES (1),(2),(3);
904SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
905a	t1.a in (select t2.a from t2)
9061	1
9072	1
9083	1
9094	0
910explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
911id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9121	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9132	SUBQUERY	t2	index_subquery	a	a	5	func	2	100.00	Using index
914Warnings:
915Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having `test`.`t2`.`a` is null)))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
916CREATE TABLE t3 (a int(11) default '0');
917INSERT INTO t3 VALUES (1),(2),(3);
918SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
919a	t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
9201	1
9212	1
9223	1
9234	0
924explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
925id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
9261	PRIMARY	t1	index	NULL	PRIMARY	4	NULL	4	100.00	Using index
9272	DEPENDENT SUBQUERY	t2	ref_or_null	a	a	5	func	2	100.00	Using where; Using index
9282	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (flat, BNL join)
929Warnings:
930Note	1003	/* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where `test`.`t3`.`a` = `test`.`t2`.`a` and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a` or `test`.`t2`.`a` is null) having `test`.`t2`.`a` is null))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
931drop table t1,t2,t3;
932# check correct NULL Processing for normal IN/ALL/ANY
933# and 2 ways of max/min optimization
934create table t1 (a int);
935insert into t1 values (1), (100), (NULL), (1000);
936create table t2 (a int not null);
937# subselect returns empty set (for NULL and non-NULL left part)
938select a, a in (select * from t2) from t1;
939a	a in (select * from t2)
9401	0
941100	0
942NULL	0
9431000	0
944select a, a > any (select * from t2) from t1;
945a	a > any (select * from t2)
9461	0
947100	0
948NULL	0
9491000	0
950select a, a > all (select * from t2) from t1;
951a	a > all (select * from t2)
9521	1
953100	1
954NULL	1
9551000	1
956select a from t1 where a in (select * from t2);
957a
958select a from t1 where a > any (select * from t2);
959a
960select a from t1 where a > all (select * from t2);
961a
9621
963100
964NULL
9651000
966select a from t1 where a in (select * from t2 group by a);
967a
968select a from t1 where a > any (select * from t2 group by a);
969a
970select a from t1 where a > all (select * from t2 group by a);
971a
9721
973100
974NULL
9751000
976insert into t2 values (1),(200);
977# sebselect returns non-empty set without NULLs
978select a, a in (select * from t2) from t1;
979a	a in (select * from t2)
9801	1
981100	0
982NULL	NULL
9831000	0
984select a, a > any (select * from t2) from t1;
985a	a > any (select * from t2)
9861	0
987100	1
988NULL	NULL
9891000	1
990select a, a > all (select * from t2) from t1;
991a	a > all (select * from t2)
9921	0
993100	0
994NULL	NULL
9951000	1
996select a from t1 where a in (select * from t2);
997a
9981
999select a from t1 where a > any (select * from t2);
1000a
1001100
10021000
1003select a from t1 where a > all (select * from t2);
1004a
10051000
1006select a from t1 where a in (select * from t2 group by a);
1007a
10081
1009select a from t1 where a > any (select * from t2 group by a);
1010a
1011100
10121000
1013select a from t1 where a > all (select * from t2 group by a);
1014a
10151000
1016drop table t2;
1017create table t2 (a int);
1018insert into t2 values (1),(NULL),(200);
1019# sebselect returns non-empty set with NULLs
1020select a, a in (select * from t2) from t1;
1021a	a in (select * from t2)
10221	1
1023100	NULL
1024NULL	NULL
10251000	NULL
1026select a, a > any (select * from t2) from t1;
1027a	a > any (select * from t2)
10281	NULL
1029100	1
1030NULL	NULL
10311000	1
1032select a, a > all (select * from t2) from t1;
1033a	a > all (select * from t2)
10341	0
1035100	0
1036NULL	NULL
10371000	NULL
1038select a from t1 where a in (select * from t2);
1039a
10401
1041select a from t1 where a > any (select * from t2);
1042a
1043100
10441000
1045select a from t1 where a > all (select * from t2);
1046a
1047select a from t1 where a in (select * from t2 group by a);
1048a
10491
1050select a from t1 where a > any (select * from t2 group by a);
1051a
1052100
10531000
1054select a from t1 where a > all (select * from t2 group by a);
1055a
1056drop table t1, t2;
1057create table t1 (a float);
1058select 10.5 IN (SELECT * from t1 LIMIT 1);
1059ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1060select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
1061ERROR 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
1062select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1);
1063ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1064drop table t1;
1065create table t1 (a int, b int, c varchar(10));
1066create table t2 (a int);
1067insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
1068insert into t2 values (1),(2),(NULL);
1069select 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;
1070a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a')	(select c from t1 where a=t2.a)
10711	1	a
10722	0	b
1073NULL	NULL	NULL
1074select 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;
1075a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b')	(select c from t1 where a=t2.a)
10761	0	a
10772	1	b
1078NULL	NULL	NULL
1079select 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;
1080a	(select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c')	(select c from t1 where a=t2.a)
10811	0	a
10822	0	b
1083NULL	NULL	NULL
1084drop table t1,t2;
1085create table t1 (a int, b real, c varchar(10));
1086insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
1087select ROW(1, 1, 'a') IN (select a,b,c from t1);
1088ROW(1, 1, 'a') IN (select a,b,c from t1)
10891
1090select ROW(1, 2, 'a') IN (select a,b,c from t1);
1091ROW(1, 2, 'a') IN (select a,b,c from t1)
10920
1093select ROW(1, 1, 'a') IN (select b,a,c from t1);
1094ROW(1, 1, 'a') IN (select b,a,c from t1)
10951
1096select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
1097ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
10981
1099select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
1100ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
11010
1102select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
1103ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
11041
1105select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1106ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11071
1108select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
1109ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
11100
1111select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
1112ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
11131
1114select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
1115ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
1116drop table t1;
1117create table t1 (a int);
1118insert into t1 values (1);
1119do @a:=(SELECT a from t1);
1120select @a;
1121@a
11221
1123set @a:=2;
1124set @a:=(SELECT a from t1);
1125select @a;
1126@a
11271
1128drop table t1;
1129do (SELECT a from t1);
1130ERROR 42S02: Table 'test.t1' doesn't exist
1131set @a:=(SELECT a from t1);
1132ERROR 42S02: Table 'test.t1' doesn't exist
1133CREATE TABLE t1 (a int, KEY(a));
1134HANDLER t1 OPEN;
1135HANDLER t1 READ a=((SELECT 1));
1136ERROR 42000: HANDLER..READ does not support subqueries or stored functions
1137HANDLER t1 CLOSE;
1138drop table t1;
1139create table t1 (a int);
1140create table t2 (b int);
1141insert into t1 values (1),(2);
1142insert into t2 values (1);
1143select a from t1 where a in (select a from t1 where a in (select b from t2));
1144a
11451
1146drop table t1, t2;
1147create table t1 (a int, b int);
1148create table t2 like t1;
1149insert into t1 values (1,2),(1,3),(1,4),(1,5);
1150insert into t2 values (1,2),(1,3);
1151select * from t1 where row(a,b) in (select a,b from t2);
1152a	b
11531	2
11541	3
1155drop table t1, t2;
1156CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;
1157INSERT INTO t1 VALUES (1);
1158UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1159select * from t1;
1160i
11612
1162drop table t1;
1163CREATE TABLE t1 (a int(1));
1164EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
1165id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11661	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11672	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1168Warnings:
1169Note	1003	/* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
1170EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
1171id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11721	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11732	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1174Warnings:
1175Note	1003	/* select#1 */ select (/* select#2 */ select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
1176EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
1177id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
11781	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
11792	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1180Warnings:
1181Note	1003	/* select#1 */ select (/* select#2 */ select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
1182drop table t1;
1183CREATE TABLE `t1` (
1184`mot` varchar(30) character set latin1 NOT NULL default '',
1185`topic` mediumint(8) unsigned NOT NULL default '0',
1186`date` date NOT NULL default '0000-00-00',
1187`pseudo` varchar(35) character set latin1 NOT NULL default '',
1188PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1189KEY `pseudo` (`pseudo`,`date`,`topic`),
1190KEY `topic` (`topic`)
1191) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1192CREATE TABLE `t2` (
1193`mot` varchar(30) character set latin1 NOT NULL default '',
1194`topic` mediumint(8) unsigned NOT NULL default '0',
1195`date` date NOT NULL default '0000-00-00',
1196`pseudo` varchar(35) character set latin1 NOT NULL default '',
1197PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
1198KEY `pseudo` (`pseudo`,`date`,`topic`),
1199KEY `topic` (`topic`)
1200) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1201CREATE TABLE `t3` (
1202`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
1203`maxnumrep` int(10) unsigned NOT NULL default '0',
1204PRIMARY KEY  (`numeropost`),
1205UNIQUE KEY `maxnumrep` (`maxnumrep`)
1206) ENGINE=MyISAM CHARSET=latin1;
1207INSERT IGNORE INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
1208Warnings:
1209Warning	1265	Data truncated for column 'date' at row 1
1210Warning	1265	Data truncated for column 'date' at row 2
1211INSERT IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
1212Warnings:
1213Warning	1265	Data truncated for column 'date' at row 1
1214Warning	1265	Data truncated for column 'date' at row 2
1215INSERT INTO t3 VALUES (1,1);
1216SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
1217numeropost=topic);
1218topic
12192
1220select * from t1;
1221mot	topic	date	pseudo
1222joce	1	0000-00-00	joce
1223test	2	0000-00-00	test
1224DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
1225EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
1226select * from t1;
1227mot	topic	date	pseudo
1228joce	1	0000-00-00	joce
1229drop table t1, t2, t3;
1230SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1231a	(SELECT a)
12321	1
1233CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
1234SHOW CREATE TABLE t1;
1235Table	Create Table
1236t1	CREATE TABLE `t1` (
1237  `a` int(1) NOT NULL DEFAULT 0,
1238  `(SELECT 1)` int(1) NOT NULL DEFAULT 0
1239) ENGINE=MyISAM DEFAULT CHARSET=latin1
1240drop table t1;
1241CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1242SHOW CREATE TABLE t1;
1243Table	Create Table
1244t1	CREATE TABLE `t1` (
1245  `a` int(1) NOT NULL DEFAULT 0,
1246  `(SELECT a)` int(1) NOT NULL DEFAULT 0
1247) ENGINE=MyISAM DEFAULT CHARSET=latin1
1248drop table t1;
1249CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1250SHOW CREATE TABLE t1;
1251Table	Create Table
1252t1	CREATE TABLE `t1` (
1253  `a` int(1) NOT NULL DEFAULT 0,
1254  `(SELECT a+0)` int(3) NOT NULL DEFAULT 0
1255) ENGINE=MyISAM DEFAULT CHARSET=latin1
1256drop table t1;
1257CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
1258select * from t1;
1259a
12602
1261SHOW CREATE TABLE t1;
1262Table	Create Table
1263t1	CREATE TABLE `t1` (
1264  `a` int(3) NOT NULL
1265) ENGINE=MyISAM DEFAULT CHARSET=latin1
1266drop table t1;
1267create table t1 (a int);
1268insert into t1 values (1), (2), (3);
1269explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
1270from t1;
1271id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
12721	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12732	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
12743	UNCACHEABLE SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00
1275Warnings:
1276Note	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`
1277drop table t1;
1278select 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);
1279ERROR 42S02: Table 'test.t1' doesn't exist
1280CREATE TABLE t1 (
1281ID int(11) NOT NULL auto_increment,
1282name char(35) NOT NULL default '',
1283t2 char(3) NOT NULL default '',
1284District char(20) NOT NULL default '',
1285Population int(11) NOT NULL default '0',
1286PRIMARY KEY  (ID)
1287) ENGINE=MyISAM;
1288INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
1289INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
1290INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
1291CREATE TABLE t2 (
1292Code char(3) NOT NULL default '',
1293Name char(52) NOT NULL default '',
1294Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1295Region char(26) NOT NULL default '',
1296SurfaceArea float(10,2) NOT NULL default '0.00',
1297IndepYear smallint(6) default NULL,
1298Population int(11) NOT NULL default '0',
1299LifeExpectancy float(3,1) default NULL,
1300GNP float(10,2) default NULL,
1301GNPOld float(10,2) default NULL,
1302LocalName char(45) NOT NULL default '',
1303GovernmentForm char(45) NOT NULL default '',
1304HeadOfState char(60) default NULL,
1305Capital int(11) default NULL,
1306Code2 char(2) NOT NULL default '',
1307PRIMARY KEY  (Code)
1308) ENGINE=MyISAM;
1309INSERT 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');
1310INSERT 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');
1311select 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);
1312Continent	Name	Population
1313Oceania	Sydney	3276207
1314drop table t1, t2;
1315CREATE TABLE `t1` (
1316`id` mediumint(8) unsigned NOT NULL auto_increment,
1317`pseudo` varchar(35) character set latin1 NOT NULL default '',
1318PRIMARY KEY  (`id`),
1319UNIQUE KEY `pseudo` (`pseudo`)
1320) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1321INSERT INTO t1 (pseudo) VALUES ('test');
1322SELECT 0 IN (SELECT 1 FROM t1 a);
13230 IN (SELECT 1 FROM t1 a)
13240
1325EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1326id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13271	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13282	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1329Warnings:
1330Note	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)`
1331INSERT INTO t1 (pseudo) VALUES ('test1');
1332SELECT 0 IN (SELECT 1 FROM t1 a);
13330 IN (SELECT 1 FROM t1 a)
13340
1335EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
1336id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13371	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
13382	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
1339Warnings:
1340Note	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)`
1341drop table t1;
1342CREATE TABLE `t1` (
1343`i` int(11) NOT NULL default '0',
1344PRIMARY KEY  (`i`)
1345) ENGINE=MyISAM CHARSET=latin1;
1346INSERT INTO t1 VALUES (1);
1347UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
1348UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
1349UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
1350ERROR 42S22: Unknown column 't.i' in 'field list'
1351select * from t1;
1352i
13533
1354drop table t1;
1355CREATE TABLE t1 (
1356id int(11) default NULL
1357) ENGINE=MyISAM CHARSET=latin1;
1358INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
1359CREATE TABLE t2 (
1360id int(11) default NULL,
1361name varchar(15) default NULL
1362) ENGINE=MyISAM CHARSET=latin1;
1363INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
1364update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
1365select * from t2;
1366id	name
13674	vita
13681	lenka
13692	lenka
13701	lenka
1371drop table t1,t2;
1372create table t1 (a int, unique index indexa (a));
1373insert into t1 values (-1), (-4), (-2), (NULL);
1374select -10 IN (select a from t1 FORCE INDEX (indexa));
1375-10 IN (select a from t1 FORCE INDEX (indexa))
1376NULL
1377drop table t1;
1378create table t1 (id int not null auto_increment primary key, salary int, key(salary));
1379insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
1380explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
1381id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
13821	PRIMARY	t1	ref	salary	salary	5	const	1	100.00	Using where
13832	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
1384Warnings:
1385Note	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`)
1386drop table t1;
1387CREATE TABLE t1 (
1388ID int(10) unsigned NOT NULL auto_increment,
1389SUB_ID int(3) unsigned NOT NULL default '0',
1390REF_ID int(10) unsigned default NULL,
1391REF_SUB int(3) unsigned default '0',
1392PRIMARY KEY (ID,SUB_ID),
1393UNIQUE KEY t1_PK (ID,SUB_ID),
1394KEY t1_FK (REF_ID,REF_SUB),
1395KEY t1_REFID (REF_ID)
1396) ENGINE=MyISAM CHARSET=cp1251;
1397INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
1398SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
1399REF_ID
1400DROP TABLE t1;
1401create table t1 (a int, b int);
1402create table t2 (a int, b int);
1403insert into t1 values (1,0), (2,0), (3,0);
1404insert into t2 values (1,1), (2,1), (3,1), (2,2);
1405update ignore t1 set b=(select b from t2 where t1.a=t2.a);
1406Warnings:
1407Warning	1242	Subquery returns more than 1 row
1408select * from t1;
1409a	b
14101	1
14112	NULL
14123	1
1413drop table t1, t2;
1414CREATE TABLE `t1` (
1415`id` mediumint(8) unsigned NOT NULL auto_increment,
1416`pseudo` varchar(35) NOT NULL default '',
1417`email` varchar(60) NOT NULL default '',
1418PRIMARY KEY  (`id`),
1419UNIQUE KEY `email` (`email`),
1420UNIQUE KEY `pseudo` (`pseudo`)
1421) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1422INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
1423SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
1424a	b
1425test	test
1426test1	test1
1427drop table if exists t1;
1428(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
1429a
14301
1431create table t1 (a int not null, b int, primary key (a));
1432create table t2 (a int not null, primary key (a));
1433create table t3 (a int not null, b int, primary key (a));
1434insert into t1 values (1,10), (2,20), (3,30),  (4,40);
1435insert into t2 values (2), (3), (4), (5);
1436insert into t3 values (10,3), (20,4), (30,5);
1437select * from t2 where t2.a in (select a from t1);
1438a
14392
14403
14414
1442explain extended select * from t2 where t2.a in (select a from t1);
1443id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14441	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14451	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t2.a	1	100.00	Using index
1446Warnings:
1447Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`
1448select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1449a
14502
14514
1452explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1453id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14541	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14551	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t2.a	1	100.00	Using where
1456Warnings:
1457Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a` and `test`.`t1`.`b` <> 30
1458select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1459a
14602
14613
1462explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1463id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14641	PRIMARY	t2	index	PRIMARY	PRIMARY	4	NULL	4	100.00	Using index
14651	PRIMARY	t1	eq_ref	PRIMARY	PRIMARY	4	test.t2.a	1	100.00
14661	PRIMARY	t3	index	PRIMARY	PRIMARY	4	NULL	3	100.00	Using where; Using index; Using join buffer (flat, BNL join)
1467Warnings:
1468Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t3` join `test`.`t2` where `test`.`t3`.`a` = `test`.`t1`.`b` and `test`.`t1`.`a` = `test`.`t2`.`a`
1469drop table t1, t2, t3;
1470create table t1 (a int, b int, index a (a,b));
1471create table t2 (a int, index a (a));
1472create table t3 (a int, b int, index a (a));
1473insert into t1 values (1,10), (2,20), (3,30), (4,40);
1474create table t0(a int);
1475insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1476insert into t1
1477select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
1478insert into t2 values (2), (3), (4), (5);
1479insert into t3 values (10,3), (20,4), (30,5);
1480select * from t2 where t2.a in (select a from t1);
1481a
14822
14833
14844
1485explain extended select * from t2 where t2.a in (select a from t1);
1486id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14871	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
14881	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using index; FirstMatch(t2)
1489Warnings:
1490Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a`
1491select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1492a
14932
14944
1495explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1496id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
14971	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
14981	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; FirstMatch(t2)
1499Warnings:
1500Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a` and `test`.`t1`.`b` <> 30
1501select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1502a
15032
15043
1505explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
1506id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15071	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
15081	PRIMARY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index
15091	PRIMARY	t1	ref	a	a	10	test.t2.a,test.t3.a	116	100.00	Using index; FirstMatch(t2)
1510Warnings:
1511Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1` join `test`.`t3`) where `test`.`t1`.`b` = `test`.`t3`.`a` and `test`.`t1`.`a` = `test`.`t2`.`a`
1512insert into t1 values (3,31);
1513select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1514a
15152
15163
15174
1518select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
1519a
15202
15214
1522explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
1523id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15241	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
15251	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; FirstMatch(t2)
1526Warnings:
1527Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a` and `test`.`t1`.`b` <> 30
1528drop table t0, t1, t2, t3;
1529create table t1 (a int, b int);
1530create table t2 (a int, b int);
1531create table t3 (a int, b int);
1532insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
1533insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
1534insert into t3 values (3,3), (2,2), (1,1);
1535select 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;
1536a	(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)
15373	1
15382	2
15391	2
1540drop table t1,t2,t3;
1541create table t1 (s1 int);
1542create table t2 (s1 int);
1543insert into t1 values (1);
1544insert into t2 values (1);
1545select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
1546s1
15471
1548drop table t1,t2;
1549create table t1 (s1 int);
1550create table t2 (s1 int);
1551insert into t1 values (1);
1552insert into t2 values (1);
1553update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
1554ERROR 42S22: Unknown column 'x.s1' in 'field list'
1555DROP TABLE t1, t2;
1556CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
1557s2 CHAR(5) COLLATE latin1_swedish_ci);
1558INSERT INTO t1 VALUES ('z','?');
1559select * from t1 where s1 > (select max(s2) from t1);
1560ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
1561select * from t1 where s1 > any (select max(s2) from t1);
1562ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
1563drop table t1;
1564create table t1(toid int,rd int);
1565create table t2(userid int,pmnew int,pmtotal int);
1566insert into t2 values(1,0,0),(2,0,0);
1567insert 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);
1568select 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);
1569userid	pmtotal	pmnew	calc_total	calc_new
15701	0	0	9	3
15712	0	0	4	2
1572drop table t1, t2;
1573create table t1 (s1 char(5));
1574select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
1575ERROR 21000: Operand should contain 1 column(s)
1576insert into t1 values ('tttt');
1577select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
1578s1
1579tttt
1580explain extended (select * from t1);
1581id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
15821	SIMPLE	t1	system	NULL	NULL	NULL	NULL	1	100.00
1583Warnings:
1584Note	1003	(select 'tttt' AS `s1` from dual)
1585(select * from t1);
1586s1
1587tttt
1588drop table t1;
1589create table t1 (s1 char(5), index s1(s1));
1590create table t2 (s1 char(5), index s1(s1));
1591insert into t1 values ('a1'),('a2'),('a3');
1592insert into t2 values ('a1'),('a2');
1593select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1594s1	s1 NOT IN (SELECT s1 FROM t2)
1595a1	0
1596a2	0
1597a3	1
1598select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1599s1	s1 = ANY (SELECT s1 FROM t2)
1600a1	1
1601a2	1
1602a3	0
1603select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1604s1	s1 <> ALL (SELECT s1 FROM t2)
1605a1	0
1606a2	0
1607a3	1
1608select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1609s1	s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
1610a1	0
1611a2	1
1612a3	1
1613explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1614id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16151	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16162	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1617Warnings:
1618Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
1619explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1620id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16211	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16222	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1623Warnings:
1624Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
1625explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1626id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16271	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16282	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	100.00	Using index; Full scan on NULL key
1629Warnings:
1630Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
1631explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1632id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16331	PRIMARY	t1	index	NULL	s1	6	NULL	3	100.00	Using index
16342	SUBQUERY	t2	index_subquery	s1	s1	6	func	2	50.00	Using index; Using where; Full scan on NULL key
1635Warnings:
1636Note	1003	/* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where `test`.`t2`.`s1` < 'a2' having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
1637drop table t1,t2;
1638create table t2 (a int, b int not null);
1639create table t3 (a int);
1640insert into t3 values (6),(7),(3);
1641select * from t3 where a >= all (select b from t2);
1642a
16436
16447
16453
1646explain extended select * from t3 where a >= all (select b from t2);
1647id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16481	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16492	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1650Warnings:
1651Note	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`)))
1652select * from t3 where a >= some (select b from t2);
1653a
1654explain extended select * from t3 where a >= some (select b from t2);
1655id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16561	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16572	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1658Warnings:
1659Note	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`)))
1660select * from t3 where a >= all (select b from t2 group by 1);
1661a
16626
16637
16643
1665explain extended select * from t3 where a >= all (select b from t2 group by 1);
1666id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16671	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16682	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1669Warnings:
1670Note	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`)))
1671select * from t3 where a >= some (select b from t2 group by 1);
1672a
1673explain extended select * from t3 where a >= some (select b from t2 group by 1);
1674id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16751	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
16762	SUBQUERY	t2	system	NULL	NULL	NULL	NULL	0	0.00	Const row not found
1677Warnings:
1678Note	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`)))
1679select * from t3 where NULL >= any (select b from t2);
1680a
1681explain extended select * from t3 where NULL >= any (select b from t2);
1682id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16831	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16842	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1685Warnings:
1686Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1687select * from t3 where NULL >= any (select b from t2 group by 1);
1688a
1689explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
1690id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16911	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
16922	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1693Warnings:
1694Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1695select * from t3 where NULL >= some (select b from t2);
1696a
1697explain extended select * from t3 where NULL >= some (select b from t2);
1698id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
16991	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17002	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1701Warnings:
1702Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1703select * from t3 where NULL >= some (select b from t2 group by 1);
1704a
1705explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
1706id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17071	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
17082	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
1709Warnings:
1710Note	1003	/* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
1711insert into t2 values (2,2), (2,1), (3,3), (3,1);
1712select * from t3 where a > all (select max(b) from t2 group by a);
1713a
17146
17157
1716explain extended select * from t3 where a > all (select max(b) from t2 group by a);
1717id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17181	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
17192	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	4	100.00	Using temporary
1720Warnings:
1721Note	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`)))
1722drop table t2, t3;
1723CREATE 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 ;
1724INSERT 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());
1725CREATE 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;
1726INSERT 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);
1727CREATE 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 ;
1728INSERT 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);
1729CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1730INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
1731select  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;
1732dbid	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')
1733-1	Valid	1
1734-1	Valid 2	1
1735-1	Should Not Return	0
1736SELECT 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;
1737dbid	name
1738-1	Valid
1739-1	Valid 2
1740drop table t1,t2,t3,t4;
1741CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1742INSERT INTO t1 VALUES (1),(5);
1743CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
1744INSERT INTO t2 VALUES (2),(6);
1745select * from t1 where (1,2,6) in (select * from t2);
1746ERROR 21000: Operand should contain 3 column(s)
1747DROP TABLE t1,t2;
1748create table t1 (s1 int);
1749insert into t1 values (1);
1750insert into t1 values (2);
1751set sort_buffer_size = (select s1 from t1);
1752ERROR 21000: Subquery returns more than 1 row
1753do (select * from t1);
1754Warnings:
1755Error	1242	Subquery returns more than 1 row
1756drop table t1;
1757create table t1 (s1 char);
1758insert into t1 values ('e');
1759select * from t1 where 'f' > any (select s1 from t1);
1760s1
1761e
1762select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1763s1
1764e
1765explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
1766id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
17671	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17682	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	100.00
17693	UNION	t1	system	NULL	NULL	NULL	NULL	1	100.00
1770NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
1771Warnings:
1772Note	1003	/* select#1 */ select 'e' AS `s1` from dual where 1
1773drop table t1;
1774CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1775INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
1776CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
1777INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
1778select 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;
1779phone	code
178069294728265	6
178118621828126	1862
178289356874041	NULL
178395895001874	NULL
1784drop table t1, t2;
1785create table t1 (s1 int);
1786create table t2 (s1 int);
1787select * from t1 where (select count(*) from t2 where t1.s2) = 1;
1788ERROR 42S22: Unknown column 't1.s2' in 'where clause'
1789select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
1790ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1791select count(*) from t2 group by t1.s2;
1792ERROR 42S22: Unknown column 't1.s2' in 'group statement'
1793drop table t1, t2;
1794CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
1795CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
1796INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
1797INSERT INTO t2 VALUES (100, 200, 'C');
1798SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
1799COLC
1800DROP TABLE t1, t2;
1801CREATE TABLE t1 (a int(1));
1802INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
1803SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
1804(SELECT a)
18051
18062
18073
18084
18095
1810DROP TABLE t1;
1811create table t1 (a int, b decimal(13, 3));
1812insert into t1 values (1, 0.123);
1813select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
1814delete from t1;
1815load data infile "../../tmp/subselect.out.file.1" into table t1;
1816select * from t1;
1817a	b
18181	0.123
1819drop table t1;
1820CREATE TABLE `t1` (
1821`id` int(11) NOT NULL auto_increment,
1822`id_cns` tinyint(3) unsigned NOT NULL default '0',
1823`tipo` enum('','UNO','DUE') NOT NULL default '',
1824`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1825`particolare` mediumint(8) unsigned NOT NULL default '0',
1826`generale` mediumint(8) unsigned NOT NULL default '0',
1827`bis` tinyint(3) unsigned NOT NULL default '0',
1828PRIMARY KEY  (`id`),
1829UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1830UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1831);
1832INSERT 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);
1833CREATE TABLE `t2` (
1834`id` tinyint(3) unsigned NOT NULL auto_increment,
1835`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1836PRIMARY KEY  (`id`)
1837);
1838INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1839SELECT 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;
1840id	max_anno_dep	PIPPO
184116	1987	1
184250	1990	0
184351	1990	NULL
1844DROP TABLE t1, t2;
1845create table t1 (a int);
1846insert into t1 values (1), (2), (3);
1847SET SQL_SELECT_LIMIT=1;
1848select sum(a) from (select * from t1) as a;
1849sum(a)
18506
1851select 2 in (select * from t1);
18522 in (select * from t1)
18531
1854SET SQL_SELECT_LIMIT=default;
1855drop table t1;
1856CREATE TABLE t1 (a int, b int, INDEX (a));
1857INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
1858SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
1859a	b
18601	1
18611	2
18621	3
1863DROP TABLE t1;
1864create table t1(val varchar(10));
1865insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
1866select 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%');
1867count(*)
18680
1869drop table t1;
1870create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1871insert 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');
1872select * from t1 where id not in (select id from t1 where id < 8);
1873id	text
18748	text8
18759	text9
187610	text10
187711	text11
187812	text12
1879select * 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);
1880id	text
18818	text8
18829	text9
188310	text10
188411	text11
188512	text12
1886explain extended select * from t1 where id not in (select id from t1 where id < 8);
1887id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18881	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
18892	DEPENDENT SUBQUERY	t1	unique_subquery	PRIMARY	PRIMARY	4	func	1	100.00	Using index; Using where
1890Warnings:
1891Note	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`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where `test`.`t1`.`id` < 8 and <cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`))))
1892explain 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);
1893id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
18941	PRIMARY	tt	ALL	NULL	NULL	NULL	NULL	12	100.00	Using where
18952	DEPENDENT SUBQUERY	t1	eq_ref	PRIMARY	PRIMARY	4	test.tt.id	1	100.00	Using where; Using index
1896Warnings:
1897Note	1276	Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1898Note	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)))
1899insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1900create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1901insert 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');
1902select * 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);
1903id	text	id	text	id	text
19041	text1	1	text1	1	text1
19052	text2	2	text2	2	text2
19063	text3	3	text3	3	text3
19074	text4	4	text4	4	text4
19085	text5	5	text5	5	text5
19096	text6	6	text6	6	text6
19107	text7	7	text7	7	text7
19118	text8	8	text8	8	text8
19129	text9	9	text9	9	text9
191310	text10	10	text10	10	text10
191411	text11	11	text1	11	text11
191512	text12	12	text2	12	text12
19161000	text1000	NULL	NULL	1000	text1000
19171001	text1001	NULL	NULL	1000	text1000
1918explain 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);
1919id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
19201	SIMPLE	a	ALL	NULL	NULL	NULL	NULL	14	100.00
19211	SIMPLE	b	eq_ref	PRIMARY	PRIMARY	4	test.a.id	2	100.00
19221	SIMPLE	c	eq_ref	PRIMARY	PRIMARY	4	func	1	100.00	Using index condition
1923Warnings:
1924Note	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`
1925drop table t1,t2;
1926create table t1 (a int);
1927insert into t1 values (1);
1928explain select benchmark(1000, (select a from t1 where a=sha(rand())));
1929id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
19301	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
19312	UNCACHEABLE SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1
1932drop table t1;
1933create table t1(id int);
1934create table t2(id int);
1935create table t3(flag int);
1936select (select * from t3 where id not null) from t1, t2;
1937ERROR 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
1938drop table t1,t2,t3;
1939CREATE TABLE t1 (id INT);
1940CREATE TABLE t2 (id INT);
1941INSERT INTO t1 VALUES (1), (2);
1942INSERT INTO t2 VALUES (1);
1943SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1944id	c
19451	1
19462	0
1947SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
1948id	c
19491	1
19502	0
1951SELECT 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;
1952id	c
19531	1
19542	0
1955SELECT 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;
1956id	c
19571	1
19582	0
1959DROP TABLE t1,t2;
1960CREATE TABLE t1 ( a int, b int );
1961INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
1962SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
1963a
19643
1965SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
1966a
19671
1968SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
1969a
19702
1971SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
1972a
19732
19743
1975SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
1976a
19771
19782
1979SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
1980a
19811
19823
1983SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
1984a
19853
1986SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
1987a
19881
1989SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
1990a
19912
1992SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
1993a
19942
19953
1996SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
1997a
19981
19992
2000SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2001a
20021
20033
2004ALTER TABLE t1 ADD INDEX (a);
2005SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
2006a
20073
2008SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
2009a
20101
2011SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
2012a
20132
2014SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
2015a
20162
20173
2018SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
2019a
20201
20212
2022SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
2023a
20241
20253
2026SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
2027a
20283
2029SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
2030a
20311
2032SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
2033a
20342
2035SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
2036a
20372
20383
2039SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
2040a
20411
20422
2043SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
2044a
20451
20463
2047SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
2048a
20493
2050SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
2051a
20521
2053SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
2054a
20552
2056SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
2057a
20582
20593
2060SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
2061a
20621
20632
2064SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
2065a
20661
20673
2068SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
2069a
20703
2071SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
2072a
20731
2074SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
2075a
20762
2077SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
2078a
20792
20803
2081SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
2082a
20831
20842
2085SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
2086a
20871
20883
2089SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2090a
20913
2092SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2093a
20941
2095SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2096a
20972
2098SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2099a
21002
21013
2102SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2103a
21041
21052
2106SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2107a
21081
21093
2110SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2111a
21123
2113SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2114a
21151
2116SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2117a
21182
2119SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2120a
21212
21223
2123SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2124a
21251
21262
2127SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
2128a
21291
21303
2131SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2132a
21333
2134SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2135a
21361
2137SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2138a
21392
2140SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2141a
21422
21433
2144SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2145a
21461
21472
2148SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2149a
21501
21513
2152SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2153a
21543
2155SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2156a
21571
2158SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2159a
21602
2161SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2162a
21632
21643
2165SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2166a
21671
21682
2169SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
2170a
21711
21723
2173SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
2174ERROR 21000: Operand should contain 1 column(s)
2175SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2176ERROR 21000: Operand should contain 1 column(s)
2177SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
2178ERROR 21000: Operand should contain 1 column(s)
2179SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
2180ERROR 21000: Operand should contain 1 column(s)
2181SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2182ERROR 21000: Operand should contain 1 column(s)
2183SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
2184ERROR 21000: Operand should contain 1 column(s)
2185SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
2186ERROR 21000: Operand should contain 1 column(s)
2187SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
2188ERROR 21000: Operand should contain 1 column(s)
2189SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
2190ERROR 21000: Operand should contain 2 column(s)
2191SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2192ERROR 21000: Operand should contain 1 column(s)
2193SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
2194a
2195SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
2196ERROR 21000: Operand should contain 2 column(s)
2197SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2198ERROR 21000: Operand should contain 1 column(s)
2199SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
2200a
22011
22022
22033
2204SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
2205a
22062
2207SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
2208a
22091
22103
2211SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
2212a
22132
2214SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
2215a
22161
22173
2218SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2219a
22202
2221SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
2222a
22231
22243
2225SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2226a
22272
2228SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
2229a
22301
22313
2232SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2233a
22343
2235SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2236a
22371
2238SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2239a
22402
2241SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2242a
22432
22443
2245SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2246a
22471
22482
2249SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
2250a
22511
22523
2253SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2254a
22553
2256SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2257a
22581
2259SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2260a
22612
2262SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2263a
22642
22653
2266SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2267a
22681
22692
2270SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
2271a
22721
22733
2274SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
2275a
22763
2277SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
2278a
22791
2280SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
2281a
22822
2283SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2284a
22852
22863
2287SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
2288a
22891
22902
2291SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
2292a
22931
22943
2295SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
2296a
22973
2298SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
2299a
23001
2301SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
2302a
23032
2304SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2305a
23062
23073
2308SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
2309a
23101
23112
2312SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
2313a
23141
23153
2316SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
2317concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
23180-
23190-
23201-
2321SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
2322concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
23231-
23240-
23250-
2326SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
2327concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
23280-
23291-
23300-
2331DROP TABLE t1;
2332CREATE TABLE t1 ( a double, b double );
2333INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
2334SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
2335a
23363
2337SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
2338a
23391
2340SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
2341a
23422
2343SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
2344a
23452
23463
2347SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
2348a
23491
23502
2351SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
2352a
23531
23543
2355SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
2356a
23573
2358SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
2359a
23601
2361SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
2362a
23632
2364SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
2365a
23662
23673
2368SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
2369a
23701
23712
2372SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
2373a
23741
23753
2376DROP TABLE t1;
2377CREATE TABLE t1 ( a char(1), b char(1));
2378INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
2379SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
2380a
23813
2382SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
2383a
23841
2385SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
2386a
23872
2388SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
2389a
23902
23913
2392SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
2393a
23941
23952
2396SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
2397a
23981
23993
2400SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
2401a
24023
2403SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
2404a
24051
2406SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
2407a
24082
2409SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
2410a
24112
24123
2413SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
2414a
24151
24162
2417SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
2418a
24191
24203
2421DROP TABLE t1;
2422create table t1 (a int, b int);
2423insert into t1 values (1,2),(3,4);
2424select * from t1 up where exists (select * from t1 where t1.a=up.a);
2425a	b
24261	2
24273	4
2428explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
2429id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
24301	PRIMARY	up	ALL	NULL	NULL	NULL	NULL	2	100.00
24311	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	100.00
24322	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
2433Warnings:
2434Note	1276	Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
2435Note	1003	select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` semi join (`test`.`t1`) where 1
2436drop table t1;
2437CREATE TABLE t1 (t1_a int);
2438INSERT INTO t1 VALUES (1);
2439CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
2440INSERT INTO t2 VALUES (1, 1), (1, 2);
2441SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
2442HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
2443t1_a	t2_a	t2_b
24441	1	2
2445DROP TABLE t1, t2;
2446CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
2447INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
2448CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
2449INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
2450SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
2451id	name	id	pet
24521	Tim	1	Fido
24532	Rebecca	2	Spot
24543	NULL	3	Felix
2455drop table t1,t2;
2456CREATE TABLE t1 ( a int, b int );
2457CREATE TABLE t2 ( c int, d int );
2458INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
2459SELECT a AS abc, b FROM t1 outr WHERE b =
2460(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2461abc	b
24621	2
24632	3
24643	4
2465INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
2466(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2467select * from t2;
2468c	d
24691	2
24702	3
24713	4
2472CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
2473(SELECT MIN(b) FROM t1 WHERE a=outr.a);
2474select * from t3;
2475abc	b
24761	2
24772	3
24783	4
2479prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2480execute stmt1;
2481deallocate prepare stmt1;
2482select * from t2;
2483c	d
24841	2
24852	3
24863	4
24871	2
24882	3
24893	4
2490drop table t3;
2491prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
2492execute stmt1;
2493select * from t3;
2494abc	b
24951	2
24962	3
24973	4
2498deallocate prepare stmt1;
2499DROP TABLE t1, t2, t3;
2500CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2501insert into t1 values (1);
2502CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2503insert into t2 values (1,2);
2504select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
2505a	C
25061	1
2507drop table t1,t2;
2508create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
2509insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
2510create table t2 (a int);
2511insert into t2 values (1),(3),(2),(7);
2512select a,b from t1 where match(b) against ('Ball') > 0;
2513a	b
25141	ball
25152	ball games
2516select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
2517a
25181
25192
2520drop table t1,t2;
2521CREATE 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);
2522CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
2523INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
2524INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
2525INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
2526INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
2527SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
2528IZAVORGANG_ID
2529D0000000001
2530drop table t1;
2531CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2532CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
2533insert into t1 values (1,1),(1,2),(2,1),(2,2);
2534insert into t2 values (1,2),(2,2);
2535select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2536aid	bid
25371	1
25382	1
2539alter table t2 drop primary key;
2540alter table t2 add key KEY1 (aid, bid);
2541select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2542aid	bid
25431	1
25442	1
2545alter table t2 drop key KEY1;
2546alter table t2 add primary key (bid, aid);
2547select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
2548aid	bid
25491	1
25502	1
2551drop table t1,t2;
2552CREATE TABLE t1 (howmanyvalues bigint, avalue int);
2553INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
2554SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
2555howmanyvalues	count(*)
25561	1
25572	2
25583	3
25594	4
2560SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2561howmanyvalues	mycount
25621	1
25632	2
25643	3
25654	4
2566CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
2567SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
2568howmanyvalues	mycount
25691	1
25702	2
25713	3
25724	4
2573SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
2574howmanyvalues	mycount
25751	1
25762	2
25773	3
25784	4
2579SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
2580howmanyvalues	mycount
25811	1
25822	1
25833	1
25844	1
2585drop table t1;
2586create table t1 (x int);
2587select  (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
2588(select b.x from t1 as b where b.x=a.x)
2589drop table t1;
2590CREATE 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`));
2591INSERT 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);
2592CREATE 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`)) ;
2593INSERT 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');
2594SELECT 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;
2595ERROR 42S22: Unknown column 'b.sc' in 'field list'
2596SELECT 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;
2597ac
2598700
2599NULL
2600drop tables t1,t2;
2601create table t1 (a int not null, b int not null, c int, primary key (a,b));
2602insert into t1 values (1,1,1), (2,2,2), (3,3,3);
2603set @b:= 0;
2604explain select sum(a) from t1 where b > @b;
2605id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26061	SIMPLE	t1	index	NULL	PRIMARY	8	NULL	3	Using where; Using index
2607set @a:= (select sum(a) from t1 where b > @b);
2608explain select a from t1 where c=2;
2609id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26101	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2611do @a:= (select sum(a) from t1 where b > @b);
2612explain select a from t1 where c=2;
2613id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
26141	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2615drop table t1;
2616connect  root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK;
2617connection root;
2618set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
2619disconnect root;
2620connection default;
2621create table t1 (a int, b int);
2622create table t2 (a int, b int);
2623insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
2624insert into t2 values (1,3),(2,1);
2625select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
2626a	b	(select max(b) from t2 where t1.b=t2.a)
26271	1	3
26281	2	1
26291	3	NULL
26302	4	NULL
26312	5	NULL
2632drop table t1, t2;
2633create table t1 (id int);
2634create table t2 (id int, body text, fulltext (body));
2635insert into t1 values(1),(2),(3);
2636insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
2637select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
2638count(distinct id)
26391
2640drop table t2,t1;
2641create table t1 (s1 int,s2 int);
2642insert into t1 values (20,15);
2643select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
2644s1	s2
2645drop table t1;
2646create table t1 (s1 int);
2647insert into t1 values (1),(null);
2648select * from t1 where s1 < all (select s1 from t1);
2649s1
2650select s1, s1 < all (select s1 from t1) from t1;
2651s1	s1 < all (select s1 from t1)
26521	0
2653NULL	NULL
2654drop table t1;
2655CREATE TABLE t1 (
2656Code char(3) NOT NULL default '',
2657Name char(52) NOT NULL default '',
2658Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
2659Region char(26) NOT NULL default '',
2660SurfaceArea float(10,2) NOT NULL default '0.00',
2661IndepYear smallint(6) default NULL,
2662Population int(11) NOT NULL default '0',
2663LifeExpectancy float(3,1) default NULL,
2664GNP float(10,2) default NULL,
2665GNPOld float(10,2) default NULL,
2666LocalName char(45) NOT NULL default '',
2667GovernmentForm char(45) NOT NULL default '',
2668HeadOfState char(60) default NULL,
2669Capital int(11) default NULL,
2670Code2 char(2) NOT NULL default ''
2671) ENGINE=MyISAM;
2672INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
2673INSERT 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');
2674INSERT 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');
2675INSERT 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');
2676/*!40000 ALTER TABLE t1 ENABLE KEYS */;
2677SELECT DISTINCT Continent AS c FROM t1 outr WHERE
2678Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
2679Population < 200);
2680c
2681Oceania
2682drop table t1;
2683create table t1 (a1 int);
2684create table t2 (b1 int);
2685select * from t1 where a2 > any(select b1 from t2);
2686ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
2687select * from t1 where a1 > any(select b1 from t2);
2688a1
2689drop table t1,t2;
2690create table t1 (a integer, b integer);
2691select (select * from t1) = (select 1,2);
2692(select * from t1) = (select 1,2)
2693NULL
2694select (select 1,2) = (select * from t1);
2695(select 1,2) = (select * from t1)
2696NULL
2697select  row(1,2) = ANY (select * from t1);
2698row(1,2) = ANY (select * from t1)
26990
2700select  row(1,2) != ALL (select * from t1);
2701row(1,2) != ALL (select * from t1)
27021
2703drop table t1;
2704create table t1 (a integer, b integer);
2705select row(1,(2,2)) in (select * from t1 );
2706ERROR 21000: Operand should contain 2 column(s)
2707select row(1,(2,2)) = (select * from t1 );
2708ERROR 21000: Operand should contain 2 column(s)
2709select (select * from t1) = row(1,(2,2));
2710ERROR 21000: Operand should contain 1 column(s)
2711drop table t1;
2712create  table t1 (a integer);
2713insert into t1 values (1);
2714select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
2715ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
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 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
2719xx	1 = ALL (  select 1 from t1 where 1 = xx )
27201	1
2721select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
2722ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2723select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
2724ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
2725drop table t1;
2726CREATE TABLE t1 (
2727categoryId int(11) NOT NULL,
2728courseId int(11) NOT NULL,
2729startDate datetime NOT NULL,
2730endDate datetime NOT NULL,
2731createDate datetime NOT NULL,
2732modifyDate timestamp NOT NULL,
2733attributes text NOT NULL
2734);
2735INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2736(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2737(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2738(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
2739(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
2740(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2741(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2742(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2743(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
2744CREATE TABLE t2 (
2745userId int(11) NOT NULL,
2746courseId int(11) NOT NULL,
2747date datetime NOT NULL
2748);
2749INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
2750(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
2751(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
2752(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
2753(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
2754(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
2755CREATE TABLE t3 (
2756groupId int(11) NOT NULL,
2757parentId int(11) NOT NULL,
2758startDate datetime NOT NULL,
2759endDate datetime NOT NULL,
2760createDate datetime NOT NULL,
2761modifyDate timestamp NOT NULL,
2762ordering int(11)
2763);
2764INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
2765CREATE TABLE t4 (
2766id int(11) NOT NULL,
2767groupTypeId int(11) NOT NULL,
2768groupKey varchar(50) NOT NULL,
2769name text,
2770ordering int(11),
2771description text,
2772createDate datetime NOT NULL,
2773modifyDate timestamp NOT NULL
2774);
2775INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
2776(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
2777CREATE TABLE t5 (
2778userId int(11) NOT NULL,
2779groupId int(11) NOT NULL,
2780createDate datetime NOT NULL,
2781modifyDate timestamp NOT NULL
2782);
2783INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
2784select
2785count(distinct t2.userid) pass,
2786groupstuff.*,
2787count(t2.courseid) crse,
2788t1.categoryid,
2789t2.courseid,
2790date_format(date, '%b%y') as colhead
2791from t2
2792join t1 on t2.courseid=t1.courseid
2793join
2794(
2795select
2796t5.userid,
2797parentid,
2798parentgroup,
2799childid,
2800groupname,
2801grouptypeid
2802from t5
2803join
2804(
2805select t4.id as parentid,
2806t4.name as parentgroup,
2807t4.id as childid,
2808t4.name as groupname,
2809t4.grouptypeid
2810from t4
2811) as gin on t5.groupid=gin.childid
2812) as groupstuff on t2.userid = groupstuff.userid
2813group by
2814groupstuff.groupname, colhead , t2.courseid;
2815pass	userid	parentid	parentgroup	childid	groupname	grouptypeid	crse	categoryid	courseid	colhead
28161	5141	12	group2	12	group2	5	1	5	12	Aug04
28171	5141	12	group2	12	group2	5	1	1	41	Aug04
28181	5141	12	group2	12	group2	5	1	2	52	Aug04
28191	5141	12	group2	12	group2	5	1	2	53	Aug04
28201	5141	12	group2	12	group2	5	1	3	51	Oct04
28211	5141	12	group2	12	group2	5	1	1	86	Oct04
28221	5141	12	group2	12	group2	5	1	1	87	Oct04
28231	5141	12	group2	12	group2	5	1	2	88	Oct04
28241	5141	12	group2	12	group2	5	1	2	89	Oct04
2825drop table t1, t2, t3, t4, t5;
2826create table t1 (a int);
2827insert into t1 values (1), (2), (3);
2828SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
28291
28301
28311
28321
2833drop table t1;
2834create table t1 (a int);
2835create table t2 (a int);
2836insert into t1 values (1),(2);
2837insert into t2 values (0),(1),(2),(3);
2838select a from t2 where a in (select a from t1);
2839a
28401
28412
2842select a from t2 having a in (select a from t1);
2843a
28441
28452
2846prepare stmt1 from "select a from t2 where a in (select a from t1)";
2847execute stmt1;
2848a
28491
28502
2851execute stmt1;
2852a
28531
28542
2855deallocate prepare stmt1;
2856prepare stmt1 from "select a from t2 having a in (select a from t1)";
2857execute stmt1;
2858a
28591
28602
2861execute stmt1;
2862a
28631
28642
2865deallocate prepare stmt1;
2866drop table t1, t2;
2867create table t1 (a int, b int);
2868insert into t1 values (1,2);
2869select 1 = (select * from t1);
2870ERROR HY000: Illegal parameter data types int and row for operation '='
2871select (select * from t1) = 1;
2872ERROR HY000: Illegal parameter data types row and int for operation '='
2873select (1,2) = (select a from t1);
2874ERROR HY000: Illegal parameter data types row and int for operation '='
2875select (select a from t1) = (1,2);
2876ERROR HY000: Illegal parameter data types int and row for operation '='
2877select (1,2,3) = (select * from t1);
2878ERROR 21000: Operand should contain 3 column(s)
2879select (select * from t1) = (1,2,3);
2880ERROR 21000: Operand should contain 2 column(s)
2881drop table t1;
2882CREATE TABLE `t1` (
2883`itemid` bigint(20) unsigned NOT NULL auto_increment,
2884`sessionid` bigint(20) unsigned default NULL,
2885`time` int(10) unsigned NOT NULL default '0',
2886`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2887NULL default '',
2888`data` text collate latin1_general_ci NOT NULL,
2889PRIMARY KEY  (`itemid`)
2890) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2891INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2892CREATE TABLE `t2` (
2893`sessionid` bigint(20) unsigned NOT NULL auto_increment,
2894`pid` int(10) unsigned NOT NULL default '0',
2895`date` int(10) unsigned NOT NULL default '0',
2896`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2897PRIMARY KEY  (`sessionid`)
2898) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2899INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2900SELECT 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;
2901ip	count( e.itemid )
290210.10.10.1	1
2903drop tables t1,t2;
2904create table t1 (fld enum('0','1'));
2905insert into t1 values ('1');
2906select * from (select max(fld) from t1) as foo;
2907max(fld)
29081
2909drop table t1;
2910set @tmp11867_optimizer_switch=@@optimizer_switch;
2911set optimizer_switch='semijoin_with_cache=off';
2912CREATE TABLE t1 (one int, two int, flag char(1));
2913CREATE TABLE t2 (one int, two int, flag char(1));
2914INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2915INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2916SELECT * FROM t1
2917WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
2918one	two	flag
29195	6	N
29207	8	N
2921SELECT * FROM t1
2922WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
2923one	two	flag
29245	6	N
29257	8	N
2926insert into t2 values (null,null,'N');
2927insert into t2 values (null,3,'0');
2928insert into t2 values (null,5,'0');
2929insert into t2 values (10,null,'0');
2930insert into t1 values (10,3,'0');
2931insert into t1 values (10,5,'0');
2932insert into t1 values (10,10,'0');
2933SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
2934one	two	test
29351	2	NULL
29362	3	NULL
29373	4	NULL
29385	6	1
29397	8	1
294010	3	NULL
294110	5	NULL
294210	10	NULL
2943SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2944one	two
29455	6
29467	8
2947SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
2948one	two	test
29491	2	NULL
29502	3	NULL
29513	4	NULL
29525	6	1
29537	8	1
295410	3	NULL
295510	5	NULL
295610	10	NULL
2957SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2958one	two	test
29591	2	0
29602	3	NULL
29613	4	0
29625	6	0
29637	8	0
296410	3	NULL
296510	5	NULL
296610	10	NULL
2967SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2968one	two	test
29691	2	0
29702	3	NULL
29713	4	0
29725	6	0
29737	8	0
297410	3	NULL
297510	5	NULL
297610	10	NULL
2977explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
2978id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29791	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29802	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2981Warnings:
2982Note	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`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null)))) AS `test` from `test`.`t1`
2983explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2984id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29851	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29861	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1	100.00
29872	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2988Warnings:
2989Note	1003	select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`flag` = 'N'
2990explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
2991id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
29921	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	100.00
29932	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	9	100.00	Using where
2994Warnings:
2995Note	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`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null)))) AS `test` from `test`.`t1`
2996DROP TABLE t1,t2;
2997set optimizer_switch=@tmp11867_optimizer_switch;
2998CREATE TABLE t1 (a char(5), b char(5));
2999INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
3000SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
3001a	b
3002aaa	aaa
3003DROP TABLE t1;
3004CREATE TABLE t1 (a int);
3005CREATE TABLE t2 (a int, b int);
3006CREATE TABLE t3 (b int NOT NULL);
3007INSERT INTO t1 VALUES (1), (2), (3), (4);
3008INSERT INTO t2 VALUES (1,10), (3,30);
3009SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3010WHERE t3.b IS NOT NULL OR t2.a > 10;
3011a	b	b
3012SELECT * FROM t1
3013WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
3014WHERE t3.b IS NOT NULL OR t2.a > 10);
3015a
30161
30172
30183
30194
3020DROP TABLE t1,t2,t3;
3021CREATE TABLE t1 (f1 INT);
3022CREATE TABLE t2 (f2 INT);
3023INSERT INTO t1 VALUES (1);
3024SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
3025f1
30261
3027SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
3028f1
30291
3030INSERT INTO t2 VALUES (1);
3031INSERT INTO t2 VALUES (2);
3032SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
3033f1
30341
3035DROP TABLE t1, t2;
3036select 1 from dual where 1 < any (select 2);
30371
30381
3039select 1 from dual where 1 < all (select 2);
30401
30411
3042select 1 from dual where 2 > any (select 1);
30431
30441
3045select 1 from dual where 2 > all (select 1);
30461
30471
3048select 1 from dual where 1 < any (select 2 from dual);
30491
30501
3051select 1 from dual where 1 < all (select 2 from dual where 1!=1);
30521
30531
3054create table t1 (s1 char);
3055insert into t1 values (1),(2);
3056select * from t1 where (s1 < any (select s1 from t1));
3057s1
30581
3059select * from t1 where not (s1 < any (select s1 from t1));
3060s1
30612
3062select * from t1 where (s1 < ALL (select s1+1 from t1));
3063s1
30641
3065select * from t1 where not(s1 < ALL (select s1+1 from t1));
3066s1
30672
3068select * from t1 where (s1+1 = ANY (select s1 from t1));
3069s1
30701
3071select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
3072s1
30732
3074select * from t1 where (s1 = ALL (select s1/s1 from t1));
3075s1
30761
3077select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
3078s1
30792
3080drop table t1;
3081create table t1 (
3082retailerID varchar(8) NOT NULL,
3083statusID   int(10) unsigned NOT NULL,
3084changed    datetime NOT NULL,
3085UNIQUE KEY retailerID (retailerID, statusID, changed)
3086);
3087INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
3088INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
3089INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
3090INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
3091INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
3092INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
3093select * from t1 r1
3094where (r1.retailerID,(r1.changed)) in
3095(SELECT r2.retailerId,(max(changed)) from t1 r2
3096group by r2.retailerId);
3097retailerID	statusID	changed
30980026	2	2006-01-06 12:25:53
30990037	2	2006-01-06 12:25:53
31000048	1	2006-01-06 12:37:50
31010059	1	2006-01-06 12:37:50
3102drop table t1;
3103create table t1(a int, primary key (a));
3104insert into t1 values (10);
3105create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
3106insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
3107insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999');
3108analyze table t1;
3109Table	Op	Msg_type	Msg_text
3110test.t1	analyze	status	Engine-independent statistics collected
3111test.t1	analyze	status	OK
3112explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3113ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3114             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3115id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31161	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31171	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31182	SUBQUERY	t2	range	b	b	40	NULL	3	Using where
3119SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3120ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3121            ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
3122a	a	b
312310	3	35989
3124explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3125ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3126            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3127id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31281	PRIMARY	t1	system	PRIMARY	NULL	NULL	NULL	1
31291	PRIMARY	r	const	PRIMARY	PRIMARY	4	const	1
31302	SUBQUERY	t2	range	b	b	40	NULL	3	Using index condition
3131SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
3132ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
3133            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
3134a	a	b
313510	1	359
3136drop table t1,t2;
3137CREATE TABLE t1 (
3138field1 int NOT NULL,
3139field2 int NOT NULL,
3140field3 int NOT NULL,
3141PRIMARY KEY  (field1,field2,field3)
3142);
3143CREATE TABLE t2 (
3144fieldA int NOT NULL,
3145fieldB int NOT NULL,
3146PRIMARY KEY  (fieldA,fieldB)
3147);
3148INSERT INTO t1 VALUES
3149(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
3150INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
3151SELECT field1, field2, COUNT(*)
3152FROM t1 GROUP BY field1, field2;
3153field1	field2	COUNT(*)
31541	1	2
31551	2	3
31561	3	1
3157SELECT field1, field2
3158FROM  t1
3159GROUP BY field1, field2
3160HAVING COUNT(*) >= ALL (SELECT fieldB
3161FROM t2 WHERE fieldA = field1);
3162field1	field2
31631	2
3164SELECT field1, field2
3165FROM  t1
3166GROUP BY field1, field2
3167HAVING COUNT(*) < ANY (SELECT fieldB
3168FROM t2 WHERE fieldA = field1);
3169field1	field2
31701	1
31711	3
3172DROP TABLE t1, t2;
3173CREATE TABLE t1(a int, INDEX (a));
3174INSERT INTO t1 VALUES (1), (3), (5), (7);
3175INSERT INTO t1 VALUES (NULL);
3176CREATE TABLE t2(a int);
3177INSERT INTO t2 VALUES (1),(2),(3);
3178EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
3179id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
31801	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3
31812	SUBQUERY	t1	index_subquery	a	a	5	func	2	Using index; Full scan on NULL key
3182SELECT a, a IN (SELECT a FROM t1) FROM t2;
3183a	a IN (SELECT a FROM t1)
31841	1
31852	NULL
31863	1
3187DROP TABLE t1,t2;
3188CREATE TABLE t1 (a DATETIME);
3189INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
3190CREATE TABLE t2 AS SELECT
3191(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
3192FROM t1 WHERE a > '2000-01-01';
3193SHOW CREATE TABLE t2;
3194Table	Create Table
3195t2	CREATE TABLE `t2` (
3196  `sub_a` datetime DEFAULT NULL
3197) ENGINE=MyISAM DEFAULT CHARSET=latin1
3198CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
3199SHOW CREATE TABLE t3;
3200Table	Create Table
3201t3	CREATE TABLE `t3` (
3202  `a` datetime DEFAULT NULL
3203) ENGINE=MyISAM DEFAULT CHARSET=latin1
3204DROP TABLE t1,t2,t3;
3205CREATE TABLE t1 (a int);
3206INSERT INTO t1 VALUES (1), (2);
3207SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
3208a
3209SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3210a
32111
32122
3213EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
3214id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
32151	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
32162	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
3217DROP TABLE t1;
3218CREATE TABLE t1 (a int);
3219INSERT INTO t1 VALUES (2), (4), (1), (3);
3220CREATE TABLE t2 (b int, c int);
3221INSERT INTO t2 VALUES
3222(2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
3223SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
3224a
32252
32264
32271
32283
3229SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
3230ERROR 21000: Subquery returns more than 1 row
3231SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
3232a
32331
32342
32353
32364
3237SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
3238ERROR 21000: Subquery returns more than 1 row
3239SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
3240b	MAX(c)
32411	4
32422	2
32434	4
3244SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
3245ERROR 21000: Subquery returns more than 1 row
3246SELECT a FROM t1 GROUP BY a
3247HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
3248(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3249a
32501
32512
32523
32534
3254SELECT a FROM t1 GROUP BY a
3255HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
3256(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3257ERROR 21000: Subquery returns more than 1 row
3258SELECT a FROM t1 GROUP BY a
3259HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3260(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
3261a
32624
3263SELECT a FROM t1 GROUP BY a
3264HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
3265(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
3266ERROR 21000: Subquery returns more than 1 row
3267SELECT a FROM t1
3268ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
3269(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3270a
32711
32722
32733
32744
3275SELECT a FROM t1
3276ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
3277(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3278ERROR 21000: Subquery returns more than 1 row
3279SELECT a FROM t1
3280ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3281(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
3282a
32831
32842
32853
32864
3287SELECT a FROM t1
3288ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
3289(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
3290ERROR 21000: Subquery returns more than 1 row
3291DROP TABLE t1,t2;
3292create table t1 (df decimal(5,1));
3293insert into t1 values(1.1);
3294insert into t1 values(2.2);
3295select * from t1 where df <= all (select avg(df) from t1 group by df);
3296df
32971.1
3298select * from t1 where df >= all (select avg(df) from t1 group by df);
3299df
33002.2
3301drop table t1;
3302create table t1 (df decimal(5,1));
3303insert into t1 values(1.1);
3304select 1.1 * exists(select * from t1);
33051.1 * exists(select * from t1)
33061.1
3307drop table t1;
3308CREATE TABLE t1 (
3309grp int(11) default NULL,
3310a decimal(10,2) default NULL);
3311insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
3312select * from t1;
3313grp	a
33141	1.00
33152	2.00
33162	3.00
33173	4.00
33183	5.00
33193	6.00
3320NULL	NULL
3321select min(a) from t1 group by grp;
3322min(a)
3323NULL
33241.00
33252.00
33264.00
3327drop table t1;
3328CREATE table t1 ( c1 integer );
3329INSERT INTO t1 VALUES ( 1 );
3330INSERT INTO t1 VALUES ( 2 );
3331INSERT INTO t1 VALUES ( 3 );
3332CREATE TABLE t2 ( c2 integer );
3333INSERT INTO t2 VALUES ( 1 );
3334INSERT INTO t2 VALUES ( 4 );
3335INSERT INTO t2 VALUES ( 5 );
3336SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
3337c1	c2
33381	1
3339SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
3340WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
3341c1	c2
33421	1
3343DROP TABLE t1,t2;
3344CREATE TABLE t1 ( c1 integer );
3345INSERT INTO t1 VALUES ( 1 );
3346INSERT INTO t1 VALUES ( 2 );
3347INSERT INTO t1 VALUES ( 3 );
3348INSERT INTO t1 VALUES ( 6 );
3349CREATE TABLE t2 ( c2 integer );
3350INSERT INTO t2 VALUES ( 1 );
3351INSERT INTO t2 VALUES ( 4 );
3352INSERT INTO t2 VALUES ( 5 );
3353INSERT INTO t2 VALUES ( 6 );
3354CREATE TABLE t3 ( c3 integer );
3355INSERT INTO t3 VALUES ( 7 );
3356INSERT INTO t3 VALUES ( 8 );
3357SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
3358WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
3359c1	c2
33602	NULL
33613	NULL
3362DROP TABLE t1,t2,t3;
3363CREATE TABLE `t1` (
3364`itemid` bigint(20) unsigned NOT NULL auto_increment,
3365`sessionid` bigint(20) unsigned default NULL,
3366`time` int(10) unsigned NOT NULL default '0',
3367`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
3368NULL default '',
3369`data` text collate latin1_general_ci NOT NULL,
3370PRIMARY KEY  (`itemid`)
3371) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3372INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
3373CREATE TABLE `t2` (
3374`sessionid` bigint(20) unsigned NOT NULL auto_increment,
3375`pid` int(10) unsigned NOT NULL default '0',
3376`date` int(10) unsigned NOT NULL default '0',
3377`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
3378PRIMARY KEY  (`sessionid`)
3379) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
3380INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
3381SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
3382ip	count( e.itemid )
338310.10.10.1	1
3384drop tables t1,t2;
3385CREATE TABLE t1 (EMPNUM   CHAR(3));
3386CREATE TABLE t2 (EMPNUM   CHAR(3) );
3387INSERT INTO t1 VALUES ('E1'),('E2');
3388INSERT INTO t2 VALUES ('E1');
3389DELETE FROM t1
3390WHERE t1.EMPNUM NOT IN
3391(SELECT t2.EMPNUM
3392FROM t2
3393WHERE t1.EMPNUM = t2.EMPNUM);
3394select * from t1;
3395EMPNUM
3396E1
3397DROP TABLE t1,t2;
3398CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
3399INSERT INTO t1 VALUES (1, 1);
3400CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
3401PRIMARY KEY(select_id,values_id));
3402INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
3403SELECT values_id FROM t1
3404WHERE values_id IN (SELECT values_id FROM t2
3405WHERE select_id IN (1, 0));
3406values_id
34071
3408SELECT values_id FROM t1
3409WHERE values_id IN (SELECT values_id FROM t2
3410WHERE select_id BETWEEN 0 AND 1);
3411values_id
34121
3413SELECT values_id FROM t1
3414WHERE values_id IN (SELECT values_id FROM t2
3415WHERE select_id = 0 OR select_id = 1);
3416values_id
34171
3418DROP TABLE t1, t2;
3419create table t1 (fld enum('0','1'));
3420insert into t1 values ('1');
3421select * from (select max(fld) from t1) as foo;
3422max(fld)
34231
3424drop table t1;
3425CREATE TABLE t1 (a int, b int);
3426CREATE TABLE t2 (c int, d int);
3427CREATE TABLE t3 (e int);
3428INSERT INTO t1 VALUES
3429(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
3430INSERT INTO t2 VALUES
3431(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
3432INSERT INTO t3 VALUES (10), (30), (10), (20) ;
3433SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
3434a	MAX(b)	MIN(b)
34351	20	10
34362	30	10
34373	20	20
34384	40	40
3439SELECT * FROM t2;
3440c	d
34412	10
34422	20
34434	10
34445	10
34453	20
34462	40
3447SELECT * FROM t3;
3448e
344910
345030
345110
345220
3453SELECT a FROM t1 GROUP BY a
3454HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
3455a
34562
34574
3458SELECT a FROM t1 GROUP BY a
3459HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
3460a
34612
3462SELECT a FROM t1 GROUP BY a
3463HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
3464a
34652
34664
3467SELECT a FROM t1 GROUP BY a
3468HAVING a IN (SELECT c FROM t2
3469WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3470a
34712
34723
3473SELECT a FROM t1 GROUP BY a
3474HAVING a IN (SELECT c FROM t2
3475WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3476a
34772
34783
3479SELECT a FROM t1 GROUP BY a
3480HAVING a IN (SELECT c FROM t2
3481WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
3482a
34832
3484SELECT a FROM t1 GROUP BY a
3485HAVING a IN (SELECT c FROM t2
3486WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
3487a
34882
3489SELECT a FROM t1 GROUP BY a
3490HAVING a IN (SELECT c FROM t2
3491WHERE MIN(b) < d AND
3492EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
3493a
34942
3495SELECT a, SUM(a) FROM t1 GROUP BY a;
3496a	SUM(a)
34971	2
34982	6
34993	3
35004	4
3501SELECT a FROM t1
3502WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
3503a
35043
35054
3506SELECT a FROM t1 GROUP BY a
3507HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
3508a
35091
35103
35114
3512SELECT a FROM t1
3513WHERE a < 3 AND
3514EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
3515a
35161
35172
3518SELECT a FROM t1
3519WHERE a < 3 AND
3520EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
3521a
35221
35232
35241
35252
35262
3527SELECT t1.a FROM t1 GROUP BY t1.a
3528HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
3529HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3530HAVING SUM(t1.a+t2.c) < t3.e/4));
3531a
35321
35332
3534SELECT t1.a FROM t1 GROUP BY t1.a
3535HAVING t1.a > ALL(SELECT t2.c FROM t2
3536WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
3537HAVING SUM(t1.a+t2.c) < t3.e/4));
3538a
35394
3540SELECT t1.a FROM t1 GROUP BY t1.a
3541HAVING t1.a > ALL(SELECT t2.c FROM t2
3542WHERE EXISTS(SELECT t3.e FROM t3
3543WHERE SUM(t1.a+t2.c) < t3.e/4));
3544ERROR HY000: Invalid use of group function
3545SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
3546ERROR HY000: Invalid use of group function
3547SELECT t1.a FROM t1 GROUP BY t1.a
3548HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3549HAVING AVG(t2.c+SUM(t1.b)) > 20);
3550a
35512
35523
35534
3554SELECT t1.a FROM t1 GROUP BY t1.a
3555HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3556HAVING AVG(SUM(t1.b)) > 20);
3557a
35582
35594
3560SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
3561HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
3562HAVING t2.c+sum > 20);
3563a	sum
35642	60
35653	20
35664	40
3567DROP TABLE t1,t2,t3;
3568CREATE TABLE t1 (a varchar(5), b varchar(10));
3569INSERT INTO t1 VALUES
3570('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
3571('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
3572SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3573a	b
3574BBB	4
3575CCC	7
3576AAA	8
3577EXPLAIN
3578SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3579id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
35801	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	Using where
35811	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	21	test.t1.a,test.t1.b	1
35822	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	9	Using temporary
3583ALTER TABLE t1 ADD INDEX(a);
3584SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3585a	b
3586BBB	4
3587CCC	7
3588AAA	8
3589EXPLAIN
3590SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
3591id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
35921	PRIMARY	t1	ALL	a	NULL	NULL	NULL	9	Using where
35931	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	21	test.t1.a,test.t1.b	1
35942	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	9	Using temporary
3595DROP TABLE t1;
3596create table t1( f1 int,f2 int);
3597insert into t1 values (1,1),(2,2);
3598select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1';
3599t
3600crash1
3601crash1
3602drop table t1;
3603create table t1 (c int, key(c));
3604insert into t1 values (1142477582), (1142455969);
3605create table t2 (a int, b int);
3606insert into t2 values (2, 1), (1, 0);
3607delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
3608drop table t1, t2;
3609CREATE TABLE t1 (a INT);
3610CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3611ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3612CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
3613ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
3614SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
3615ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
3616DROP TABLE t1;
3617create table t1 (i int, j bigint);
3618insert into t1 values (1, 2), (2, 2), (3, 2);
3619select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
3620min(i)
36211
3622drop table t1;
3623CREATE TABLE t1 (i BIGINT UNSIGNED);
3624INSERT INTO t1 VALUES (10000000000000000000);
3625INSERT INTO t1 VALUES (1);
3626CREATE TABLE t2 (i BIGINT UNSIGNED);
3627INSERT INTO t2 VALUES (10000000000000000000);
3628INSERT INTO t2 VALUES (1);
3629/* simple test */
3630SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
3631i
363210000000000000000000
36331
3634/* subquery test */
3635SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3636i
363710000000000000000000
3638/* subquery test with cast*/
3639SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
3640i
364110000000000000000000
3642DROP TABLE t1;
3643DROP TABLE t2;
3644CREATE TABLE t1 (
3645id bigint(20) unsigned NOT NULL auto_increment,
3646name varchar(255) NOT NULL,
3647PRIMARY KEY  (id)
3648);
3649INSERT INTO t1 VALUES
3650(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
3651CREATE TABLE t2 (
3652id bigint(20) unsigned NOT NULL auto_increment,
3653mid bigint(20) unsigned NOT NULL,
3654date date NOT NULL,
3655PRIMARY KEY  (id)
3656);
3657INSERT INTO t2 VALUES
3658(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
3659(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
3660SELECT *,
3661(SELECT date FROM t2 WHERE mid = t1.id
3662ORDER BY date DESC LIMIT 0, 1) AS date_last,
3663(SELECT date FROM t2 WHERE mid = t1.id
3664ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
3665FROM t1;
3666id	name	date_last	date_next_to_last
36671	Balazs	2006-05-01	NULL
36682	Joe	2006-04-20	NULL
36693	Frank	2006-04-13	NULL
3670SELECT *,
3671(SELECT COUNT(*) FROM t2 WHERE mid = t1.id
3672ORDER BY date DESC LIMIT 1, 1) AS date_count
3673FROM t1;
3674id	name	date_count
36751	Balazs	NULL
36762	Joe	NULL
36773	Frank	NULL
3678SELECT *,
3679(SELECT date FROM t2 WHERE mid = t1.id
3680ORDER BY date DESC LIMIT 0, 1) AS date_last,
3681(SELECT date FROM t2 WHERE mid = t1.id
3682ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
3683FROM t1;
3684id	name	date_last	date_next_to_last
36851	Balazs	2006-05-01	2006-03-30
36862	Joe	2006-04-20	2006-04-06
36873	Frank	2006-04-13	NULL
3688DROP TABLE t1,t2;
3689CREATE TABLE t1 (
3690i1 int(11) NOT NULL default '0',
3691i2 int(11) NOT NULL default '0',
3692t datetime NOT NULL default '0000-00-00 00:00:00',
3693PRIMARY KEY  (i1,i2,t)
3694);
3695INSERT INTO t1 VALUES
3696(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
3697(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
3698(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
3699(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
3700(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
3701(24,2,'2005-05-27 12:40:06');
3702CREATE TABLE t2 (
3703i1 int(11) NOT NULL default '0',
3704i2 int(11) NOT NULL default '0',
3705t datetime default NULL,
3706PRIMARY KEY  (i1)
3707);
3708INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
3709EXPLAIN
3710SELECT * FROM t1,t2
3711WHERE t1.t = (SELECT t1.t FROM t1
3712WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3713ORDER BY t1.t DESC LIMIT 1);
3714id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37151	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1
37161	PRIMARY	t1	index	NULL	PRIMARY	13	NULL	11	Using where; Using index
37172	SUBQUERY	t1	range	PRIMARY	PRIMARY	13	NULL	6	Using where; Using index
3718SELECT * FROM t1,t2
3719WHERE t1.t = (SELECT t1.t FROM t1
3720WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
3721ORDER BY t1.t DESC LIMIT 1);
3722i1	i2	t	i1	i2	t
372324	1	2005-05-27 12:40:30	24	1	2006-06-20 12:29:40
3724DROP TABLE t1, t2;
3725CREATE TABLE t1 (i INT);
3726(SELECT i FROM t1) UNION (SELECT i FROM t1);
3727i
3728SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
3729(
3730(SELECT i FROM t1) UNION
3731(SELECT i FROM t1)
3732);
3733i
3734SELECT * FROM t1
3735WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
3736i
3737explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
3738from t1;
3739id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37401	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
37412	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
37423	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
3743NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
3744explain select * from t1 where not exists
3745((select t11.i from t1 t11) union (select t12.i from t1 t12));
3746id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
37471	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	Const row not found
37482	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
37493	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
3750NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
3751DROP TABLE t1;
3752CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
3753insert into t1 (a) values (FLOOR(rand() * 100));
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;
3766insert into t1 (a) select FLOOR(rand() * 100) from t1;
3767SELECT a,
3768(SELECT REPEAT(' ',250) FROM t1 i1
3769WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
3770FROM t1 ORDER BY a LIMIT 5;
3771a	a
37720	NULL
37730	NULL
37740	NULL
37750	NULL
37760	NULL
3777DROP TABLE t1;
3778CREATE TABLE t1 (a INT, b INT);
3779CREATE TABLE t2 (a INT);
3780INSERT INTO t2 values (1);
3781INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
3782SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
3783(SELECT COUNT(DISTINCT t1.b) from t2)
37842
37851
37861
3787SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
3788FROM t1 GROUP BY t1.a;
3789(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
37902
37911
37921
3793SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
3794COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b))
37952	2
37961	1
37971	1
3798SELECT COUNT(DISTINCT t1.b),
3799(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
3800FROM t1 GROUP BY t1.a;
3801COUNT(DISTINCT t1.b)	(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
38022	2
38031	1
38041	1
3805SELECT (
3806SELECT (
3807SELECT COUNT(DISTINCT t1.b)
3808)
3809)
3810FROM t1 GROUP BY t1.a;
3811(
3812SELECT (
3813SELECT COUNT(DISTINCT t1.b)
3814)
3815)
38162
38171
38181
3819SELECT (
3820SELECT (
3821SELECT (
3822SELECT COUNT(DISTINCT t1.b)
3823)
3824)
3825FROM t1 GROUP BY t1.a LIMIT 1)
3826FROM t1 t2
3827GROUP BY t2.a;
3828(
3829SELECT (
3830SELECT (
3831SELECT COUNT(DISTINCT t1.b)
3832)
3833)
3834FROM t1 GROUP BY t1.a LIMIT 1)
38352
38362
38372
3838DROP TABLE t1,t2;
3839CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
3840CREATE TABLE t2 (x int auto_increment, y int, z int,
3841PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
3842create table t3 (a int);
3843insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3844insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
3845from t3 A, t3 B, t3 C, t3 D where D.a<3;
3846insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
3847SET SESSION sort_buffer_size = 32 * 1024;
3848SELECT SQL_NO_CACHE COUNT(*)
3849FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3850FROM t1) t;
3851COUNT(*)
38523000
3853SET SESSION sort_buffer_size = 8 * 1024 * 1024;
3854SELECT SQL_NO_CACHE COUNT(*)
3855FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
3856FROM t1) t;
3857COUNT(*)
38583000
3859DROP TABLE t1,t2,t3;
3860CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
3861CREATE TABLE t2 (c int);
3862INSERT INTO t1 VALUES ('aa', 1);
3863INSERT INTO t2 VALUES (1);
3864SELECT * FROM t1
3865WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3866UNION
3867SELECT c from t2 WHERE c=t1.c);
3868id	c
3869aa	1
3870INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
3871SELECT * FROM t1
3872WHERE EXISTS (SELECT c FROM t2 WHERE c=1
3873UNION
3874SELECT c from t2 WHERE c=t1.c);
3875id	c
3876aa	1
3877bb	2
3878cc	3
3879dd	1
3880INSERT INTO t2 VALUES (2);
3881CREATE TABLE t3 (c int);
3882INSERT INTO t3 VALUES (1);
3883SELECT * FROM t1
3884WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
3885UNION
3886SELECT c from t2 WHERE c=t1.c);
3887id	c
3888aa	1
3889bb	2
3890cc	3
3891dd	1
3892DROP TABLE t1,t2,t3;
3893CREATE TABLE t1(f1 int);
3894CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
3895INSERT INTO t1 VALUES (1),(1),(2),(2);
3896INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
3897SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
3898sq
38992
39004
3901SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
3902tt
39032
39042
3905PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
3906EXECUTE stmt1;
3907sq
39082
39094
3910EXECUTE stmt1;
3911sq
39122
39134
3914DEALLOCATE PREPARE stmt1;
3915SELECT f2, AVG(f21),
3916(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
3917FROM t2 GROUP BY f2;
3918f2	AVG(f21)	test
39191	1.0000	2004-02-29 11:11:11
39202	2.0000	2004-02-29 11:11:11
3921DROP TABLE t1,t2;
3922CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
3923INSERT INTO t1 VALUES
3924(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
3925(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
3926(3,2,'k'), (3,1,'l'), (1,9,'m');
3927SELECT a, MAX(b),
3928(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
3929FROM t1 GROUP BY a;
3930a	MAX(b)	test
39311	9	m
39322	3	h
39333	4	i
3934DROP TABLE t1;
3935DROP TABLE IF EXISTS t1;
3936DROP TABLE IF EXISTS t2;
3937DROP TABLE IF EXISTS t1xt2;
3938CREATE TABLE t1 (
3939id_1 int(5) NOT NULL,
3940t varchar(4) DEFAULT NULL
3941);
3942CREATE TABLE t2 (
3943id_2 int(5) NOT NULL,
3944t varchar(4) DEFAULT NULL
3945);
3946CREATE TABLE t1xt2 (
3947id_1 int(5) NOT NULL,
3948id_2 int(5) NOT NULL
3949);
3950INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
3951INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
3952INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
3953SELECT DISTINCT t1.id_1 FROM t1 WHERE
3954(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3955id_1
3956SELECT DISTINCT t1.id_1 FROM t1 WHERE
3957(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3958id_1
3959SELECT DISTINCT t1.id_1 FROM t1 WHERE
3960(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3961id_1
3962SELECT DISTINCT t1.id_1 FROM t1 WHERE
3963(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3964id_1
39651
39662
39673
39684
3969SELECT DISTINCT t1.id_1 FROM t1 WHERE
3970(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
3971id_1
39721
39732
39743
39754
3976SELECT DISTINCT t1.id_1 FROM t1 WHERE
3977(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
3978id_1
39791
39802
39813
39824
3983insert INTO t1xt2 VALUES (1, 12);
3984SELECT DISTINCT t1.id_1 FROM t1 WHERE
3985(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3986id_1
39871
3988SELECT DISTINCT t1.id_1 FROM t1 WHERE
3989(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
3990id_1
39911
3992SELECT DISTINCT t1.id_1 FROM t1 WHERE
3993(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
3994id_1
39951
3996SELECT DISTINCT t1.id_1 FROM t1 WHERE
3997(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
3998id_1
39992
40003
40014
4002SELECT DISTINCT t1.id_1 FROM t1 WHERE
4003(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4004id_1
40052
40063
40074
4008SELECT DISTINCT t1.id_1 FROM t1 WHERE
4009(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4010id_1
40112
40123
40134
4014insert INTO t1xt2 VALUES (2, 12);
4015SELECT DISTINCT t1.id_1 FROM t1 WHERE
4016(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4017id_1
40181
40192
4020SELECT DISTINCT t1.id_1 FROM t1 WHERE
4021(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4022id_1
40231
40242
4025SELECT DISTINCT t1.id_1 FROM t1 WHERE
4026(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4027id_1
40281
40292
4030SELECT DISTINCT t1.id_1 FROM t1 WHERE
4031(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
4032id_1
40333
40344
4035SELECT DISTINCT t1.id_1 FROM t1 WHERE
4036(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
4037id_1
40383
40394
4040SELECT DISTINCT t1.id_1 FROM t1 WHERE
4041(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
4042id_1
40433
40444
4045DROP TABLE t1;
4046DROP TABLE t2;
4047DROP TABLE t1xt2;
4048CREATE TABLE t1 (a int);
4049INSERT INTO t1 VALUES (3), (1), (2);
4050SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
4051col1	col2
4052this is a test.	3
4053this is a test.	1
4054this is a test.	2
4055SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
4056col1	t2
4057this is a test.	3
4058this is a test.	1
4059this is a test.	2
4060DROP table t1;
4061CREATE TABLE t1 (a int, b int);
4062CREATE TABLE t2 (m int, n int);
4063INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4064INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4065SELECT COUNT(*), a,
4066(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
4067FROM t1 GROUP BY a;
4068COUNT(*)	a	(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
40692	2	2
40703	3	3
40711	4	1
4072SELECT COUNT(*), a,
4073(SELECT MIN(m) FROM t2 WHERE m = count(*))
4074FROM t1 GROUP BY a;
4075COUNT(*)	a	(SELECT MIN(m) FROM t2 WHERE m = count(*))
40762	2	2
40773	3	3
40781	4	1
4079SELECT COUNT(*), a
4080FROM t1 GROUP BY a
4081HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
4082COUNT(*)	a
40832	2
40843	3
4085DROP TABLE t1,t2;
4086CREATE TABLE t1 (a int, b int);
4087CREATE TABLE t2 (m int, n int);
4088INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
4089INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
4090SELECT COUNT(*) c, a,
4091(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
4092FROM t1 GROUP BY a;
4093c	a	(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
40942	2	2
40953	3	3
40961	4	1,1
4097SELECT COUNT(*) c, a,
4098(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
4099FROM t1 GROUP BY a;
4100c	a	(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
41012	2	3
41023	3	4
41031	4	2,2
4104DROP table t1,t2;
4105CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
4106INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
4107(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
4108(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
4109SELECT a, MAX(b),
4110(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
4111FROM t1 GROUP BY a;
4112a	MAX(b)	test
41131	9	m
41142	3	h
41153	4	i
4116SELECT a x, MAX(b),
4117(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
4118FROM t1 GROUP BY a;
4119x	MAX(b)	test
41201	9	m
41212	3	h
41223	4	i
4123SELECT a, AVG(b),
4124(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
4125FROM t1 WHERE t1.d=0 GROUP BY a;
4126a	AVG(b)	test
41271	4.0000	d
41282	2.0000	g
41293	2.5000	NULL
4130SELECT tt.a,
4131(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4132LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4133FROM t1 as tt;
4134a	test
41351	n
41361	n
41371	n
41381	n
41391	n
41401	n
41411	n
41422	o
41432	o
41442	o
41452	o
41463	p
41473	p
41483	p
41493	p
41503	p
4151SELECT tt.a,
4152(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4153LIMIT 1)
4154FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
4155FROM t1 as tt GROUP BY tt.a;
4156a	test
41571	n
41582	o
41593	p
4160SELECT tt.a, MAX(
4161(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
4162LIMIT 1)
4163FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
4164FROM t1 as tt GROUP BY tt.a;
4165a	test
41661	n
41672	o
41683	p
4169DROP TABLE t1;
4170CREATE TABLE t1 (a int, b int);
4171INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4172SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4173a
41741
41752
4176SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4177a
4178SELECT a FROM t1 t0
4179WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4180a
41811
41822
4183SET @@sql_mode='ansi';
4184SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4185ERROR HY000: Invalid use of group function
4186SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4187ERROR HY000: Invalid use of group function
4188SELECT a FROM t1 t0
4189WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4190ERROR HY000: Invalid use of group function
4191SET @@sql_mode=default;
4192DROP TABLE t1;
4193CREATE TABLE t1 (a INT);
4194INSERT INTO t1 values (1),(1),(1),(1);
4195CREATE TABLE t2 (x INT);
4196INSERT INTO t1 values (1000),(1001),(1002);
4197SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
4198ERROR HY000: Invalid use of group function
4199SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
4200ERROR HY000: Invalid use of group function
4201SELECT COUNT(1) FROM DUAL;
4202COUNT(1)
42031
4204SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
4205ERROR HY000: Invalid use of group function
4206SELECT
4207SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
4208FROM t1;
4209ERROR HY000: Invalid use of group function
4210SELECT t1.a as XXA,
4211SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
4212FROM t1;
4213ERROR HY000: Invalid use of group function
4214DROP TABLE t1,t2;
4215CREATE TABLE t1 (a int, b int, KEY (a));
4216INSERT INTO t1 VALUES (1,1),(2,1);
4217EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
4218id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
42191	PRIMARY	t1	ref	a	a	5	const	1	Using where; Using index
42202	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
4221DROP TABLE t1;
4222CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
4223INSERT INTO t1 VALUES
4224(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
4225CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
4226INSERT INTO t2 VALUES (7), (5), (1), (3);
4227SELECT id, st FROM t1
4228WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4229id	st
42303	FL
42311	GA
42327	FL
4233SELECT id, st FROM t1
4234WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4235GROUP BY id;
4236id	st
42371	GA
42383	FL
42397	FL
4240SELECT id, st FROM t1
4241WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
4242id	st
42432	GA
42444	FL
4245SELECT id, st FROM t1
4246WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
4247GROUP BY id;
4248id	st
42492	GA
42504	FL
4251DROP TABLE t1,t2;
4252CREATE TABLE t1 (a int);
4253INSERT INTO t1 VALUES (1), (2);
4254EXPLAIN EXTENDED
4255SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
4256id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
42571	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	100.00
42582	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary; Using filesort
4259Warnings:
4260Note	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`
4261DROP TABLE t1;
4262CREATE TABLE t1 (
4263a varchar(255) default NULL,
4264b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
4265INDEX idx(a,b)
4266);
4267CREATE TABLE t2 (
4268a varchar(255) default NULL
4269);
4270INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
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 SELECT * FROM t1;
4279INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
4280INSERT INTO `t2` VALUES ('abcdefghijk');
4281INSERT INTO `t2` VALUES ('asdf');
4282SET session sort_buffer_size=8192;
4283SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
4284d1
42851
42861
4287DROP TABLE t1,t2;
4288CREATE TABLE t1 (a INTEGER, b INTEGER);
4289CREATE TABLE t2 (x INTEGER);
4290INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
4291INSERT INTO t2 VALUES (1), (2);
4292SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
4293ERROR 21000: Subquery returns more than 1 row
4294SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
4295ERROR 21000: Subquery returns more than 1 row
4296SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
4297(SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
42983.3333
4299DROP TABLE t1,t2;
4300CREATE TABLE t1 (a INT, b INT);
4301INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
4302SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
4303AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
4304GROUP BY a1.a;
4305a	COUNT(*)
43061	3
4307DROP TABLE t1;
4308CREATE TABLE t1 (a INT);
4309CREATE TABLE t2 (a INT);
4310INSERT INTO t1 VALUES (1),(2);
4311INSERT INTO t2 VALUES (1),(2);
4312SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
4313(SELECT SUM(t1.a) FROM t2 WHERE a=0)
4314NULL
4315SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
4316ERROR 21000: Subquery returns more than 1 row
4317SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
4318(SELECT SUM(t1.a) FROM t2 WHERE a=1)
43193
4320DROP TABLE t1,t2;
4321CREATE TABLE t1 (a1 INT, a2 INT);
4322CREATE TABLE t2 (b1 INT, b2 INT);
4323INSERT INTO t1 VALUES (100, 200);
4324INSERT INTO t1 VALUES (101, 201);
4325INSERT INTO t2 VALUES (101, 201);
4326INSERT INTO t2 VALUES (103, 203);
4327SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
4328((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
43290
43300
4331DROP TABLE t1, t2;
4332CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
4333INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
4334SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4335s1	s2
4336SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4337s1	s2
4338CREATE INDEX I1 ON t1 (s1);
4339CREATE INDEX I2 ON t1 (s2);
4340SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
4341s1	s2
4342SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
4343s1	s2
4344TRUNCATE t1;
4345INSERT INTO t1 VALUES (0x41,0x41);
4346SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
4347s1	s2
4348DROP TABLE t1;
4349CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
4350CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
4351CREATE TABLE t3 (a3 BINARY(2) default '0');
4352INSERT INTO t1 VALUES (1),(2),(3),(4);
4353INSERT INTO t2 VALUES (1),(2),(3);
4354INSERT INTO t3 VALUES (1),(2),(3);
4355SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
4356LEFT(t2.a2, 1)
43571
43582
43593
4360SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
4361a1	t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
43621	0
43632	0
43643	0
43654	0
4366DROP TABLE t1,t2,t3;
4367CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
4368CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
4369CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
4370INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
4371INSERT INTO t2 VALUES (2), (3), (4), (5);
4372INSERT INTO t3 VALUES (10), (20), (30);
4373SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
4374LEFT(t1.a1,1)
43751
43762
43773
4378SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
4379a2
4380DROP TABLE t1, t2, t3;
4381SET @save_optimizer_switch=@@optimizer_switch;
4382SET optimizer_switch='semijoin_with_cache=off';
4383SET optimizer_switch='materialization=off';
4384CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
4385INSERT INTO t1 VALUES ('a', 'aa');
4386INSERT INTO t1 VALUES ('a', 'aaa');
4387SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4388a	b
4389CREATE INDEX I1 ON t1 (a);
4390CREATE INDEX I2 ON t1 (b);
4391EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4392id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
43931	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where
43941	PRIMARY	t1	ref	I1	I1	2	test.t1.b	2	Using where; Using index; FirstMatch(t1)
4395SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
4396a	b
4397CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
4398INSERT INTO t2 SELECT * FROM t1;
4399CREATE INDEX I1 ON t2 (a);
4400CREATE INDEX I2 ON t2 (b);
4401EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4402id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44031	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where
44041	PRIMARY	t2	ref	I1	I1	4	test.t2.b	2	Using where; Using index; FirstMatch(t2)
4405SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4406a	b
4407EXPLAIN
4408SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4409id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44101	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where
44111	PRIMARY	t1	ref	I1	I1	2	test.t1.b	2	Using where; Using index; FirstMatch(t1)
4412SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4413a	b
4414DROP TABLE t1,t2;
4415SET optimizer_switch= @save_optimizer_switch;
4416CREATE TABLE t1(a INT, b INT);
4417INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
4418EXPLAIN
4419SELECT a AS out_a, MIN(b) FROM t1
4420WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4421GROUP BY a;
4422ERROR 42S22: Unknown column 'out_a' in 'where clause'
4423SELECT a AS out_a, MIN(b) FROM t1
4424WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
4425GROUP BY a;
4426ERROR 42S22: Unknown column 'out_a' in 'where clause'
4427EXPLAIN
4428SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4429WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4430GROUP BY a;
4431id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
44321	PRIMARY	t1_outer	ALL	NULL	NULL	NULL	NULL	4	Using where; Using temporary; Using filesort
44332	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	4	Using where
4434SELECT a AS out_a, MIN(b) FROM t1 t1_outer
4435WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
4436GROUP BY a;
4437out_a	MIN(b)
44381	2
44392	4
4440DROP TABLE t1;
4441CREATE TABLE t1 (a INT);
4442CREATE TABLE t2 (a INT);
4443INSERT INTO t1 VALUES (1),(2);
4444INSERT INTO t2 VALUES (1),(2);
4445SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
44462
44472
44482
4449EXPLAIN EXTENDED
4450SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
4451id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44521	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
44531	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	100.00
44542	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	100.00
4455Warnings:
4456Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4457Note	1003	select 2 AS `2` from `test`.`t1` semi join (`test`.`t2`) where 1
4458EXPLAIN EXTENDED
4459SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
4460(SELECT 1 FROM t2 WHERE t1.a = t2.a));
4461id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
44621	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44632	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
44643	DEPENDENT UNION	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where
4465NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	NULL
4466Warnings:
4467Note	1276	Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4468Note	1276	Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4469Note	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))
4470DROP TABLE t1,t2;
4471create table t0(a int);
4472insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
4473create table t1(f11 int, f12 int);
4474create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
4475insert into t1 values(1,1),(2,2), (3, 3);
4476insert ignore into t2
4477select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
4478from t0 A, t0 B, t0 C, t0 D;
4479set session sort_buffer_size= 33*1024;
4480select count(*) from t1 where f12 =
4481(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
4482count(*)
44833
4484drop table t0,t1,t2;
4485CREATE TABLE t4 (
4486f7 varchar(32) collate utf8_bin NOT NULL default '',
4487f10 varchar(32) collate utf8_bin default NULL,
4488PRIMARY KEY  (f7)
4489);
4490INSERT INTO t4 VALUES(1,1), (2,null);
4491CREATE TABLE t2 (
4492f4 varchar(32) collate utf8_bin NOT NULL default '',
4493f2 varchar(50) collate utf8_bin default NULL,
4494f3 varchar(10) collate utf8_bin default NULL,
4495PRIMARY KEY  (f4),
4496UNIQUE KEY uk1 (f2)
4497);
4498INSERT INTO t2 VALUES(1,1,null), (2,2,null);
4499CREATE TABLE t1 (
4500f8 varchar(32) collate utf8_bin NOT NULL default '',
4501f1 varchar(10) collate utf8_bin default NULL,
4502f9 varchar(32) collate utf8_bin default NULL,
4503PRIMARY KEY  (f8)
4504);
4505INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
4506CREATE TABLE t3 (
4507f6 varchar(32) collate utf8_bin NOT NULL default '',
4508f5 varchar(50) collate utf8_bin default NULL,
4509PRIMARY KEY (f6)
4510);
4511INSERT INTO t3 VALUES (1,null), (2,null);
4512SELECT
4513IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
4514IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
4515SUM(
4516IF(
4517(SELECT VPC.f2
4518FROM t2 VPC, t4 a2, t2 a3
4519WHERE
4520VPC.f4 = a2.f10 AND a3.f2 = a4
4521LIMIT 1) IS NULL,
45220,
4523t3.f5
4524)
4525) AS a6
4526FROM
4527t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
4528GROUP BY a4;
4529a4	f3	a6
45301	NULL	NULL
45312	NULL	NULL
4532DROP TABLE t1, t2, t3, t4;
4533create table t1 (a float(5,4) zerofill);
4534create table t2 (a float(5,4),b float(2,0));
4535select t1.a from t1 where
4536t1.a= (select b from t2 limit 1) and not
4537t1.a= (select a from t2 limit 1) ;
4538a
4539drop table t1, t2;
4540CREATE TABLE t1 (a INT);
4541INSERT INTO t1 VALUES (1),(2);
4542SET @save_join_cache_level=@@join_cache_level;
4543SET join_cache_level=0;
4544EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
4545id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45461	PRIMARY	<subquery2>	const	distinct_key	distinct_key	4	const	1	100.00
45471	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
45482	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using temporary
4549Warnings:
4550Note	1003	/* select#1 */ select 1 AS `1` from  <materialize> (/* select#2 */ select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where `<subquery2>`.`min(a)` = 1
4551EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
4552id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45531	PRIMARY	<subquery2>	const	distinct_key	distinct_key	4	const	1	100.00
45541	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
45552	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using temporary
4556Warnings:
4557Note	1003	/* select#1 */ select 1 AS `1` from  <materialize> (/* select#2 */ select min(`test`.`t1`.`a`) from `test`.`t1` where `test`.`t1`.`a` > 3 group by `test`.`t1`.`a`) join `test`.`t1` where `<subquery2>`.`min(a)` = 1
4558SET join_cache_level=@save_join_cache_level;
4559DROP TABLE t1;
4560#
4561# Bug#45061: Incorrectly market field caused wrong result.
4562#
4563CREATE TABLE `C` (
4564`int_nokey` int(11) NOT NULL,
4565`int_key` int(11) NOT NULL,
4566KEY `int_key` (`int_key`)
4567);
4568INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
4569(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
4570(5,2), (1,8), (7,0), (0,9), (9,5);
4571SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4572int_nokey	int_key
45739	9
45740	0
45755	5
45760	0
4577EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
4578id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
45791	SIMPLE	C	ALL	NULL	NULL	NULL	NULL	20	100.00	Using where
4580DROP TABLE C;
4581# End of test for bug#45061.
4582#
4583# Bug #46749: Segfault in add_key_fields() with outer subquery level
4584#   field references
4585#
4586CREATE TABLE t1 (
4587a int,
4588b int,
4589UNIQUE (a), KEY (b)
4590);
4591INSERT INTO t1 VALUES (1,1), (2,1);
4592CREATE TABLE st1 like t1;
4593INSERT INTO st1 VALUES (1,1), (2,1);
4594CREATE TABLE st2 like t1;
4595INSERT INTO st2 VALUES (1,1), (2,1);
4596EXPLAIN
4597SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4598FROM t1
4599WHERE a = 230;
4600id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
46011	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
46022	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
4603SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4604FROM t1
4605WHERE a = 230;
4606MAX(b)	(SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
4607NULL	NULL
4608DROP TABLE t1, st1, st2;
4609#
4610# Bug #48709: Assertion failed in sql_select.cc:11782:
4611#   int join_read_key(JOIN_TAB*)
4612#
4613CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
4614INSERT INTO t1 VALUES (10,1), (14,1);
4615CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
4616INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
4617# should have eq_ref for t1
4618EXPLAIN
4619SELECT * FROM t2 outr
4620WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4621ORDER BY outr.pk;
4622id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
4623x	x	outr	ALL	x	x	x	x	x	x
4624x	x	t1	eq_ref	x	x	x	x	x	x
4625x	x	t2	index	x	x	x	x	x	x
4626# should not crash on debug binaries
4627SELECT * FROM t2 outr
4628WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
4629ORDER BY outr.pk;
4630pk	int_key
46313	3
46327	3
4633DROP TABLE t1,t2;
4634#
4635# Bug#12329653
4636# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
4637#
4638CREATE TABLE t1(a1 int);
4639INSERT INTO t1 VALUES (1),(2);
4640SELECT @@session.sql_mode INTO @old_sql_mode;
4641SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4642EXPLAIN EXTENDED
4643SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
4644id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
46451	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
46462	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00
4647Warnings:
4648Note	1003	/* select#1 */ select 1 AS `1` from `test`.`t1` where 1
4649SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
46501
46511
46521
4653PREPARE stmt FROM
4654'SELECT 1 UNION ALL
4655SELECT 1 FROM t1
4656ORDER BY
4657(SELECT 1 FROM t1 AS t1_0
4658  WHERE 1 < SOME (SELECT a1 FROM t1)
4659)' ;
4660EXECUTE stmt ;
4661ERROR 21000: Subquery returns more than 1 row
4662EXECUTE stmt ;
4663ERROR 21000: Subquery returns more than 1 row
4664SET SESSION sql_mode=@old_sql_mode;
4665DEALLOCATE PREPARE stmt;
4666DROP TABLE t1;
4667#
4668# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4669#
4670CREATE TABLE t1(a1 int);
4671INSERT INTO t1 VALUES (1),(2);
4672CREATE TABLE t2(a1 int);
4673INSERT INTO t2 VALUES (3);
4674SELECT @@session.sql_mode INTO @old_sql_mode;
4675SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4676SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
46771
46781
46791
4680SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
46811
46821
46831
4684SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
46851
4686Warnings:
4687Warning	1292	Truncated incorrect DOUBLE value: 'a'
4688SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
46891
46901
46911
4692SET SESSION sql_mode=@old_sql_mode;
4693DROP TABLE t1, t2;
4694#
4695# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
4696#
4697create table t2(i int);
4698insert into t2 values(0);
4699SELECT @@session.sql_mode INTO @old_sql_mode;
4700SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
4701CREATE VIEW v1 AS
4702SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
4703;
4704CREATE TABLE t1 (
4705pk int NOT NULL,
4706col_varchar_key varchar(1) DEFAULT NULL,
4707PRIMARY KEY (pk),
4708KEY col_varchar_key (col_varchar_key)
4709);
4710SELECT t1.pk
4711FROM t1
4712WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
4713;
4714pk
4715SET SESSION sql_mode=@old_sql_mode;
4716drop table t2, t1;
4717drop view v1;
4718# End of 5.0 tests.
4719create table t_out (subcase char(3),
4720a1 char(2), b1 char(2), c1 char(2));
4721create table t_in  (a2 char(2), b2 char(2), c2 char(2));
4722insert into t_out values ('A.1','2a', NULL, '2a');
4723insert into t_out values ('A.3', '2a', NULL, '2a');
4724insert into t_out values ('A.4', '2a', NULL, 'xx');
4725insert into t_out values ('B.1', '2a', '2a', '2a');
4726insert into t_out values ('B.2', '2a', '2a', '2a');
4727insert into t_out values ('B.3', '3a', 'xx', '3a');
4728insert into t_out values ('B.4', 'xx', '3a', '3a');
4729insert into t_in values ('1a', '1a', '1a');
4730insert into t_in values ('2a', '2a', '2a');
4731insert into t_in values (NULL, '2a', '2a');
4732insert into t_in values ('3a', NULL, '3a');
4733
4734Test general IN semantics (not top-level)
4735
4736case A.1
4737select subcase,
4738(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4739(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4740from t_out where subcase = 'A.1';
4741subcase	pred_in	pred_not_in
4742A.1	0	1
4743case A.2 - impossible
4744case A.3
4745select subcase,
4746(a1, b1, c1)     IN (select * from t_in) pred_in,
4747(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4748from t_out where subcase = 'A.3';
4749subcase	pred_in	pred_not_in
4750A.3	NULL	NULL
4751case A.4
4752select subcase,
4753(a1, b1, c1)     IN (select * from t_in) pred_in,
4754(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4755from t_out where subcase = 'A.4';
4756subcase	pred_in	pred_not_in
4757A.4	0	1
4758case B.1
4759select subcase,
4760(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
4761(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
4762from t_out where subcase = 'B.1';
4763subcase	pred_in	pred_not_in
4764B.1	0	1
4765case B.2
4766select subcase,
4767(a1, b1, c1)     IN (select * from t_in) pred_in,
4768(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4769from t_out where subcase = 'B.2';
4770subcase	pred_in	pred_not_in
4771B.2	1	0
4772case B.3
4773select subcase,
4774(a1, b1, c1)     IN (select * from t_in) pred_in,
4775(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4776from t_out where subcase = 'B.3';
4777subcase	pred_in	pred_not_in
4778B.3	NULL	NULL
4779case B.4
4780select subcase,
4781(a1, b1, c1)     IN (select * from t_in) pred_in,
4782(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
4783from t_out where subcase = 'B.4';
4784subcase	pred_in	pred_not_in
4785B.4	0	1
4786
4787Test IN as top-level predicate, and
4788as non-top level for cases A.3, B.3 (the only cases with NULL result).
4789
4790case A.1
4791select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4792where subcase = 'A.1' and
4793(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4794pred_in
4795F
4796select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4797where subcase = 'A.1' and
4798(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4799pred_not_in
4800T
4801select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4802where subcase = 'A.1' and
4803NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4804not_pred_in
4805T
4806case A.3
4807select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4808where subcase = 'A.3' and
4809(a1, b1, c1) IN (select * from t_in);
4810pred_in
4811F
4812select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4813where subcase = 'A.3' and
4814(a1, b1, c1) NOT IN (select * from t_in);
4815pred_not_in
4816F
4817select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4818where subcase = 'A.3' and
4819NOT((a1, b1, c1) IN (select * from t_in));
4820not_pred_in
4821F
4822select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4823where subcase = 'A.3' and
4824((a1, b1, c1) IN (select * from t_in)) is NULL and
4825((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4826pred_in
4827N
4828case A.4
4829select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4830where subcase = 'A.4' and
4831(a1, b1, c1) IN (select * from t_in);
4832pred_in
4833F
4834select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4835where subcase = 'A.4' and
4836(a1, b1, c1) NOT IN (select * from t_in);
4837pred_not_in
4838T
4839select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4840where subcase = 'A.4' and
4841NOT((a1, b1, c1) IN (select * from t_in));
4842not_pred_in
4843T
4844case B.1
4845select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4846where subcase = 'B.1' and
4847(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
4848pred_in
4849F
4850select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4851where subcase = 'B.1' and
4852(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
4853pred_not_in
4854T
4855select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4856where subcase = 'B.1' and
4857NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
4858not_pred_in
4859T
4860case B.2
4861select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4862where subcase = 'B.2' and
4863(a1, b1, c1) IN (select * from t_in);
4864pred_in
4865T
4866select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4867where subcase = 'B.2' and
4868(a1, b1, c1) NOT IN (select * from t_in);
4869pred_not_in
4870F
4871select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4872where subcase = 'B.2' and
4873NOT((a1, b1, c1) IN (select * from t_in));
4874not_pred_in
4875F
4876case B.3
4877select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4878where subcase = 'B.3' and
4879(a1, b1, c1) IN (select * from t_in);
4880pred_in
4881F
4882select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4883where subcase = 'B.3' and
4884(a1, b1, c1) NOT IN (select * from t_in);
4885pred_not_in
4886F
4887select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4888where subcase = 'B.3' and
4889NOT((a1, b1, c1) IN (select * from t_in));
4890not_pred_in
4891F
4892select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
4893where subcase = 'B.3' and
4894((a1, b1, c1) IN (select * from t_in)) is NULL and
4895((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
4896pred_in
4897N
4898case B.4
4899select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
4900where subcase = 'B.4' and
4901(a1, b1, c1) IN (select * from t_in);
4902pred_in
4903F
4904select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
4905where subcase = 'B.4' and
4906(a1, b1, c1) NOT IN (select * from t_in);
4907pred_not_in
4908T
4909select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
4910where subcase = 'B.4' and
4911NOT((a1, b1, c1) IN (select * from t_in));
4912not_pred_in
4913T
4914drop table t_out;
4915drop table t_in;
4916CREATE TABLE t1 (a INT, b INT);
4917INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
4918SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4919a
49201
49212
4922SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4923a
4924SELECT a FROM t1 t0
4925WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4926a
49271
49282
4929SET @@sql_mode='ansi';
4930SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
4931ERROR HY000: Invalid use of group function
4932SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
4933ERROR HY000: Invalid use of group function
4934SELECT a FROM t1 t0
4935WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
4936ERROR HY000: Invalid use of group function
4937SET @@sql_mode=default;
4938DROP TABLE t1;
4939CREATE TABLE t1 (s1 CHAR(1));
4940INSERT INTO t1 VALUES ('a');
4941SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
4942s1
4943a
4944DROP TABLE t1;
4945CREATE TABLE t1(c INT, KEY(c));
4946CREATE TABLE t2(a INT, b INT);
4947INSERT INTO t2 VALUES (1, 10), (2, NULL);
4948INSERT INTO t1 VALUES (1), (3);
4949SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
4950a	b
4951DROP TABLE t1,t2;
4952CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
4953INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
4954CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
4955INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
4956SELECT * FROM t1
4957WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
4958pk	a
49591	10
4960DROP TABLE t1,t2;
4961CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
4962INSERT INTO t1 VALUES (1,NULL), (9,NULL);
4963CREATE TABLE t2 (
4964a INT,
4965b INT,
4966c INT,
4967d INT,
4968PRIMARY KEY (a),
4969UNIQUE KEY b (b,c,d),
4970KEY b_2 (b),
4971KEY c (c),
4972KEY d (d)
4973);
4974INSERT INTO t2 VALUES
4975(43, 2, 11 ,30),
4976(44, 2, 12 ,30),
4977(45, 1, 1  ,10000),
4978(46, 1, 2  ,10000),
4979(556,1, 32 ,10000);
4980CREATE TABLE t3 (
4981a INT,
4982b INT,
4983c INT,
4984PRIMARY KEY (a),
4985UNIQUE KEY b (b,c),
4986KEY c (c),
4987KEY b_2 (b)
4988);
4989INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2);
4990explain
4991SELECT 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;
4992id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
49931	PRIMARY	t1	index	PRIMARY	PRIMARY	4	NULL	2	Using index
49941	PRIMARY	t3	ref	b,b_2	b	5	test.t1.a	1	Using index
49952	DEPENDENT SUBQUERY	t2	ref	b,b_2,c	b	10	test.t3.c,test.t1.a	1	Using where; Using index
4996SELECT 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;
4997a	incorrect
49981	1
4999DROP TABLE t1,t2,t3;
5000CREATE TABLE t1 (id int);
5001CREATE TABLE t2 (id int, c int);
5002INSERT INTO t1 (id) VALUES (1);
5003INSERT INTO t2 (id) VALUES (1);
5004INSERT INTO t1 (id) VALUES (1);
5005INSERT INTO t2 (id) VALUES (1);
5006CREATE VIEW v1 AS
5007SELECT t2.c AS c FROM t1, t2
5008WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5009UPDATE v1 SET c=1;
5010CREATE VIEW v2 (a,b) AS
5011SELECT t2.id, t2.c AS c FROM t1, t2
5012WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5013INSERT INTO v2(a,b) VALUES (2,2);
5014ERROR 44000: CHECK OPTION failed `test`.`v2`
5015SELECT * FROM v1;
5016c
50171
50181
50191
50201
5021CREATE VIEW v3 AS
5022SELECT t2.c AS c FROM t2
5023WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
5024DELETE FROM v3;
5025DROP VIEW v1,v2,v3;
5026DROP TABLE t1,t2;
5027#
5028# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5029#
5030create table t1(id integer primary key, g integer, v integer, s char(1));
5031create table t2(id integer primary key, g integer, v integer, s char(1));
5032insert into t1 values
5033(10, 10, 10,   'l'),
5034(20, 20, 20,   'l'),
5035(40, 40, 40,   'l'),
5036(41, 40, null, 'l'),
5037(50, 50, 50,   'l'),
5038(51, 50, null, 'l'),
5039(60, 60, 60,   'l'),
5040(61, 60, null, 'l'),
5041(70, 70, 70,   'l'),
5042(90, 90, null, 'l');
5043insert into t2 values
5044(10, 10, 10,   'r'),
5045(30, 30, 30,   'r'),
5046(50, 50, 50,   'r'),
5047(60, 60, 60,   'r'),
5048(61, 60, null, 'r'),
5049(70, 70, 70,   'r'),
5050(71, 70, null, 'r'),
5051(80, 80, 80,   'r'),
5052(81, 80, null, 'r'),
5053(100,100,null, 'r');
5054select *
5055from t1
5056where v in(select v
5057from t2
5058where t1.g=t2.g) is unknown;
5059id	g	v	s
506051	50	NULL	l
506161	60	NULL	l
5062drop table t1, t2;
5063#
5064# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
5065#
5066create table t1(id integer primary key, g integer, v integer, s char(1));
5067create table t2(id integer primary key, g integer, v integer, s char(1));
5068insert into t1 values
5069(10, 10, 10,   'l'),
5070(20, 20, 20,   'l'),
5071(40, 40, 40,   'l'),
5072(41, 40, null, 'l'),
5073(50, 50, 50,   'l'),
5074(51, 50, null, 'l'),
5075(60, 60, 60,   'l'),
5076(61, 60, null, 'l'),
5077(70, 70, 70,   'l'),
5078(90, 90, null, 'l');
5079insert into t2 values
5080(10, 10, 10,   'r'),
5081(30, 30, 30,   'r'),
5082(50, 50, 50,   'r'),
5083(60, 60, 60,   'r'),
5084(61, 60, null, 'r'),
5085(70, 70, 70,   'r'),
5086(71, 70, null, 'r'),
5087(80, 80, 80,   'r'),
5088(81, 80, null, 'r'),
5089(100,100,null, 'r');
5090select *
5091from t1
5092where v in(select v
5093from t2
5094where t1.g=t2.g) is unknown;
5095id	g	v	s
509651	50	NULL	l
509761	60	NULL	l
5098drop table t1, t2;
5099#
5100# Bug#33204: INTO is allowed in subselect, causing inconsistent results
5101#
5102CREATE TABLE t1( a INT );
5103INSERT INTO t1 VALUES (1),(2);
5104CREATE TABLE t2( a INT, b INT );
5105SELECT *
5106FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
5107ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2
5108SELECT *
5109FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
5110ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
5111SELECT *
5112FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
5113ERROR 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
5114SELECT * FROM (
5115SELECT 1 a
5116UNION
5117SELECT a INTO @var FROM t1 WHERE a = 2
5118) t1a;
5119ERROR 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
5120) t1a' at line 4
5121SELECT * FROM (
5122SELECT 1 a
5123UNION
5124SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
5125) t1a;
5126ERROR 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
5127) t1a' at line 4
5128SELECT * FROM (
5129SELECT 1 a
5130UNION
5131SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
5132) t1a;
5133ERROR 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
5134) t1a' at line 4
5135SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
5136a
51372
5138SELECT * FROM (
5139SELECT a FROM t1 WHERE a = 2
5140UNION
5141SELECT a FROM t1 WHERE a = 2
5142) t1a;
5143a
51442
5145SELECT * FROM (
5146SELECT 1 a
5147UNION
5148SELECT a FROM t1 WHERE a = 2
5149UNION
5150SELECT a FROM t1 WHERE a = 2
5151) t1a;
5152a
51531
51542
5155SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
5156a
51571
5158SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
5159a
51601
5161SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
51621
51631
5164SELECT * FROM ((SELECT 1 a INTO @a)) 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 @a)) t1a' at line 1
5166SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) 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 OUTFILE 'file' )) t1a' at line 1
5168SELECT * FROM ((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 @a)) 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 @a)) t1a' at line 1
5172SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) 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 DUMPFILE 'file' )) t1a' at line 1
5174SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE '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 OUTFILE 'file' )) t1a' at line 1
5176SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) 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 @a))) t1a' at line 1
5178SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
5179ERROR 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
5180SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
5181ERROR 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
5182SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
5183a
51841
5185SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
5186a
51871
5188SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
5189a
51901
5191SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
5192a
51931
5194SELECT * FROM t1 JOIN  (SELECT 1 UNION SELECT 1) alias ON 1;
5195a	1
51961	1
51972	1
5198SELECT * FROM t1 JOIN ((SELECT 1 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 'ON 1' at line 1
5200SELECT * FROM t1 JOIN  (t1 t1a UNION SELECT 1)  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 'UNION SELECT 1)  ON 1' at line 1
5202SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) 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 'UNION SELECT 1)) ON 1' at line 1
5204SELECT * FROM t1 JOIN  (t1 t1a)  t1a ON 1;
5205ERROR 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
5206SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
5207ERROR 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
5208SELECT * FROM t1 JOIN  (t1 t1a)  ON 1;
5209a	a
52101	1
52112	1
52121	2
52132	2
5214SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
5215a	a
52161	1
52172	1
52181	2
52192	2
5220SELECT * FROM (t1 t1a);
5221a
52221
52232
5224SELECT * FROM ((t1 t1a));
5225a
52261
52272
5228SELECT * FROM t1 JOIN  (SELECT 1 t1a) alias ON 1;
5229a	t1a
52301	1
52312	1
5232SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
5233a	t1a
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 a)) a ON 1;
5241a	a
52421	1
52432	1
5244SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
5245ERROR 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
5246SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
5247a
52481
5249SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
5250a
52511
5252SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
5253a
52541
5255SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
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 @a)' at line 1
5257SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5258ERROR 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
5259SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5260ERROR 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
5261SELECT * FROM t1 WHERE a = ( SELECT 1 );
5262a
52631
5264SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
5265a
52661
5267SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
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 @a)' at line 1
5269SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
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 OUTFILE 'file' )' at line 1
5271SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE '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 DUMPFILE 'file' )' at line 1
5273SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
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 @a)' at line 1
5275SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
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 OUTFILE 'file' )' at line 1
5277SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE '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 DUMPFILE 'file' )' at line 1
5279SELECT ( SELECT 1 INTO @v );
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 @v )' at line 1
5281SELECT ( SELECT 1 INTO OUTFILE 'file' );
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 OUTFILE 'file' )' at line 1
5283SELECT ( SELECT 1 INTO DUMPFILE '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 DUMPFILE 'file' )' at line 1
5285SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
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 @v )' at line 1
5287SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
5288ERROR 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
5289SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
5290ERROR 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
5291SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
5292( SELECT a FROM t1 WHERE a = 1 )	a
52931	1
52941	2
5295SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
5296( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 )	a
52971	1
52981	2
5299SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
5300a	b
5301SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
53021
53031
5304( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
53051
53061
5307SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5308( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) )
53091
5310SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
5311ERROR 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
5312SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5313( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
53141
5315SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
5316((SELECT 1 UNION SELECT 1 UNION SELECT 1))
53171
5318SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5319ERROR 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
5320SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
5321ERROR 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
5322SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
53231
53241
5325SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
53261
53271
5328SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5329a
53301
5331SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5332a
53331
5334SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5335a
53361
5337SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
5338a
53391
5340SELECT * FROM t1 WHERE a =     ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5341a
53421
5343SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5344a
53451
5346SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5347a
53481
5349SELECT * FROM t1 WHERE a IN    ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
5350a
53511
5352SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5353a
53541
5355SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5356a
53571
5358SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5359a
53601
5361SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
5362a
53631
5364SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
5365ERROR 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
5366SELECT EXISTS(SELECT 1+1);
5367EXISTS(SELECT 1+1)
53681
5369SELECT EXISTS(SELECT 1+1 INTO @test);
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 @test)' at line 1
5371SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION 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
5373SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
5374ERROR 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
5375SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
5376ERROR 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
5377DROP TABLE t1, t2;
5378CREATE TABLE t1 (a ENUM('rainbow'));
5379INSERT INTO t1 VALUES (),(),(),(),();
5380SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
53811
53821
5383DROP TABLE t1;
5384CREATE TABLE t1 (a LONGBLOB);
5385INSERT INTO t1 SET a = 'aaaa';
5386INSERT INTO t1 SET a = 'aaaa';
5387SELECT 1 FROM t1 GROUP BY
5388(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
53891
53901
5391DROP TABLE t1;
5392#
5393# Bug #49512 : subquery with aggregate function crash
5394#   subselect_single_select_engine::exec()
5395CREATE TABLE t1(a INT);
5396INSERT INTO t1 VALUES();
5397# should not crash
5398SELECT 1 FROM t1 WHERE a <> SOME
5399(
5400SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
5401FROM t1,t1 a
5402);
54031
5404DROP TABLE t1;
5405#
5406# Bug #45989 take 2 : memory leak after explain encounters an
5407# error in the query
5408#
5409CREATE TABLE t1(a LONGTEXT);
5410INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
5411INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
5412EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
5413(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
5414WHERE t1.a = d1.a;
5415ERROR 42S22: Unknown column 'd1.a' in 'where clause'
5416DROP TABLE t1;
5417Set up test tables.
5418CREATE TABLE t1 (
5419t1_id INT UNSIGNED,
5420PRIMARY KEY(t1_id)
5421) Engine=MyISAM;
5422INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
5423CREATE TABLE t2 SELECT * FROM t1;
5424CREATE TABLE t3 (
5425t3_id INT UNSIGNED AUTO_INCREMENT,
5426t1_id INT UNSIGNED,
5427amount DECIMAL(16,2),
5428PRIMARY KEY(t3_id),
5429KEY(t1_id)
5430) Engine=MyISAM;
5431INSERT INTO t3 (t1_id, t3_id, amount)
5432VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
5433This is the 'inner query' running by itself.
5434Produces correct results.
5435SELECT
5436t1.t1_id,
5437IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5438FROM
5439t1
5440LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5441GROUP BY
5442t1.t1_id
5443;
5444t1_id	total_amount
54451	100.00
54462	200.00
54473	0.00
54484	400.00
54495	0.00
5450SELECT * FROM (the same inner query)
5451Produces correct results.
5452SELECT * FROM (
5453SELECT
5454t1.t1_id,
5455IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5456FROM
5457t1
5458LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5459GROUP BY
5460t1.t1_id
5461) AS t;
5462t1_id	total_amount
54631	100.00
54642	200.00
54653	0.00
54664	400.00
54675	0.00
5468Now make t2.t1_id part of a key.
5469ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
5470Same inner query by itself.
5471Still correct results.
5472SELECT
5473t1.t1_id,
5474IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5475FROM
5476t1
5477LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5478GROUP BY
5479t1.t1_id;
5480t1_id	total_amount
54811	100.00
54822	200.00
54833	0.00
54844	400.00
54855	0.00
5486SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
5487SELECT * FROM (
5488SELECT
5489t1.t1_id,
5490IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
5491FROM
5492t1
5493LEFT JOIN t2 ON t2.t1_id=t1.t1_id
5494GROUP BY
5495t1.t1_id
5496) AS t;
5497t1_id	total_amount
54981	100.00
54992	200.00
55003	0.00
55014	400.00
55025	0.00
5503DROP TABLE t3;
5504DROP TABLE t2;
5505DROP TABLE t1;
5506#
5507# Bug #52711: Segfault when doing EXPLAIN SELECT with
5508#  union...order by (select... where...)
5509#
5510CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
5511INSERT INTO t1 VALUES (1),(2);
5512CREATE TABLE t2 (b INT);
5513INSERT INTO t2 VALUES (1),(2);
5514# Should not crash
5515EXPLAIN
5516SELECT * FROM t2 UNION SELECT * FROM t2
5517ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5518# Should not crash
5519SELECT * FROM t2 UNION SELECT * FROM t2
5520ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
5521DROP TABLE t1,t2;
5522#
5523# Bug #58818: Incorrect result for IN/ANY subquery
5524# with HAVING condition
5525#
5526CREATE TABLE t1(i INT);
5527INSERT INTO t1 VALUES (1), (2), (3);
5528CREATE TABLE t1s(i INT);
5529INSERT INTO t1s VALUES (10), (20), (30);
5530CREATE TABLE t2s(i INT);
5531INSERT INTO t2s VALUES (100), (200), (300);
5532SELECT * FROM t1
5533WHERE t1.i NOT IN
5534(
5535SELECT STRAIGHT_JOIN t2s.i
5536FROM
5537t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5538HAVING t2s.i = 999
5539);
5540i
55411
55422
55433
5544SELECT * FROM t1
5545WHERE t1.I IN
5546(
5547SELECT STRAIGHT_JOIN t2s.i
5548FROM
5549t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5550HAVING t2s.i = 999
5551) IS UNKNOWN;
5552i
5553SELECT * FROM t1
5554WHERE NOT t1.I = ANY
5555(
5556SELECT STRAIGHT_JOIN t2s.i
5557FROM
5558t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5559HAVING t2s.i = 999
5560);
5561i
55621
55632
55643
5565SELECT * FROM t1
5566WHERE t1.i = ANY (
5567SELECT STRAIGHT_JOIN t2s.i
5568FROM
5569t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
5570HAVING t2s.i = 999
5571) IS UNKNOWN;
5572i
5573DROP TABLE t1,t1s,t2s;
5574# LP BUG#675248 - select->prep_where references on freed memory
5575CREATE TABLE t1 (a int, b int);
5576insert into t1 values (1,1),(0,0);
5577CREATE TABLE t2 (c int);
5578insert into t2 values (1),(2);
5579prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
5580from t1 as table1 group by sub";
5581execute stmt1;
5582sum(a)	sub
55830	NULL
55841	3
5585deallocate prepare stmt1;
5586prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
5587from t1 as table1";
5588execute stmt1;
5589sum(a)	sub
55901	3
5591deallocate prepare stmt1;
5592drop table t1,t2;
5593#
5594# Bug LP#693935/#58727: Assertion failure with
5595# a single row subquery returning more than one row
5596#
5597create table t1 (a char(1) charset utf8);
5598insert into t1 values ('a'), ('b');
5599create table t2 (a binary(1));
5600insert into t2 values ('x'), ('y');
5601select * from t2 where a=(select a from t1) and a='x';
5602ERROR 21000: Subquery returns more than 1 row
5603drop table t1,t2;
5604# End of 5.1 tests
5605#
5606# Bug #11765713 58705:
5607# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5608# CREATED BY OPT_SUM_QUERY
5609#
5610CREATE TABLE t1(a INT NOT NULL, KEY (a));
5611INSERT INTO t1 VALUES (0), (1);
5612SELECT 1 as foo FROM t1 WHERE a < SOME
5613(SELECT a FROM t1 WHERE a <=>
5614(SELECT a FROM t1)
5615);
5616ERROR 21000: Subquery returns more than 1 row
5617SELECT 1 as foo FROM t1 WHERE a < SOME
5618(SELECT a FROM t1 WHERE a <=>
5619(SELECT a FROM t1 where a is null)
5620);
5621foo
5622DROP TABLE t1;
5623#
5624# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
5625#             Assertion `file' failed.
5626#
5627CREATE TABLE t1 (a INT);
5628SELECT 1 FROM
5629(SELECT ROW(
5630(SELECT 1 FROM t1 RIGHT JOIN
5631(SELECT 1 FROM t1, t1 t2) AS d ON 1),
56321) FROM t1) AS e;
5633ERROR 21000: Operand should contain 1 column(s)
5634DROP TABLE t1;
5635#
5636# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
5637#
5638CREATE TABLE t1(a TIME NOT NULL);
5639INSERT INTO t1 VALUES ('00:00:32');
5640SELECT 1 FROM t1 WHERE a >
5641(SELECT timestamp(a) AS a FROM t1);
56421
5643DROP TABLE t1;
5644#
5645# No BUG#, a case brought from 5.2's innodb_mysql_lock.test
5646#
5647create table t1 (i int  not null primary key);
5648insert into t1 values (1),(2),(3),(4),(5);
5649create table t2 (j int not null  primary key);
5650insert into t2 values (1),(2),(3),(4),(5);
5651create table t3 (k int not null primary key);
5652insert into t3 values (1),(2),(3);
5653create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
5654select * from t3 where k in (select j from v2);
5655k
56561
56572
56583
5659drop table t1,t2,t3;
5660drop view v2;
5661#
5662# Bug#52068: Optimizer generates invalid semijoin materialization plan
5663#
5664drop table if exists ot1, ot2, it1, it2;
5665CREATE TABLE ot1(a INTEGER);
5666INSERT INTO ot1 VALUES(5), (8);
5667CREATE TABLE it2(a INTEGER);
5668INSERT INTO it2 VALUES(9), (5), (1), (8);
5669CREATE TABLE it3(a INTEGER);
5670INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
5671CREATE TABLE ot4(a INTEGER);
5672INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
5673SELECT * FROM ot1,ot4
5674WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5675FROM it2,it3);
5676a	a
56775	1
56788	1
56795	5
56808	5
56815	7
56828	7
56835	7
56848	7
56855	1
56868	1
5687explain SELECT * FROM ot1,ot4
5688WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
5689FROM it2,it3);
5690id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
56911	PRIMARY	ot1	ALL	NULL	NULL	NULL	NULL	2
56921	PRIMARY	ot4	ALL	NULL	NULL	NULL	NULL	8	Using join buffer (flat, BNL join)
56931	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1
56942	MATERIALIZED	it2	ALL	NULL	NULL	NULL	NULL	4
56952	MATERIALIZED	it3	ALL	NULL	NULL	NULL	NULL	6	Using join buffer (flat, BNL join)
5696DROP TABLE IF EXISTS ot1, ot4, it2, it3;
5697#
5698# Bug#729039: NULL keys used to evaluate subquery
5699#
5700CREATE TABLE t1 (a int) ;
5701INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
5702CREATE TABLE t2 (a int, INDEX idx(a)) ;
5703INSERT INTO t2 VALUES (NULL), (1), (NULL);
5704SELECT * FROM t1
5705WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
5706a
57071
5708EXPLAIN
5709SELECT * FROM t1
5710WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
5711id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57121	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
57131	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
57142	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	3
5715SELECT * FROM t1
5716WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5717a
57181
5719EXPLAIN
5720SELECT * FROM t1
5721WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
5722id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57231	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
57241	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
57252	MATERIALIZED	t2	index	idx	idx	5	NULL	3	Using index
5726DROP TABLE t1,t2;
5727#
5728# BUG#752992: Wrong results for a subquery with 'semijoin=on'
5729#
5730CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5731INSERT INTO t1 VALUES (11,0);
5732INSERT INTO t1 VALUES (12,5);
5733INSERT INTO t1 VALUES (15,0);
5734CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
5735INSERT INTO t2 VALUES (11,1);
5736INSERT INTO t2 VALUES (12,2);
5737INSERT INTO t2 VALUES (15,4);
5738SET @save_join_cache_level=@@join_cache_level;
5739SET join_cache_level=0;
5740EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5741id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57421	PRIMARY	t1	ALL	PRIMARY	NULL	NULL	NULL	3
57431	PRIMARY	it	eq_ref	PRIMARY	PRIMARY	4	test.t1.pk	1	Using index
57441	PRIMARY	t2	index	NULL	PRIMARY	4	NULL	3	Using index; FirstMatch(it)
5745SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
5746pk	i
574711	0
574812	5
574915	0
5750SET join_cache_level=@save_join_cache_level;
5751DROP table t1,t2;
5752#
5753# Bug#751350: crash with pushed condition for outer references when
5754#             there should be none of such conditions
5755#
5756CREATE TABLE t1 (a int, b int) ;
5757INSERT INTO t1 VALUES (0,0),(0,0);
5758set @optimizer_switch_save=@@optimizer_switch;
5759set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
5760EXPLAIN
5761SELECT b FROM t1
5762WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5763GROUP BY b;
5764id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
57651	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using temporary; Using filesort
57662	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
5767SELECT b FROM t1
5768WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
5769GROUP BY b;
5770b
57710
5772set @@optimizer_switch=@optimizer_switch_save;
5773DROP TABLE t1;
5774#
5775# Bug #11765713 58705:
5776# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
5777# CREATED BY OPT_SUM_QUERY
5778#
5779CREATE TABLE t1(a INT NOT NULL, KEY (a));
5780INSERT INTO t1 VALUES (0), (1);
5781SELECT 1 as foo FROM t1 WHERE a < SOME
5782(SELECT a FROM t1 WHERE a <=>
5783(SELECT a FROM t1)
5784);
5785ERROR 21000: Subquery returns more than 1 row
5786SELECT 1 as foo FROM t1 WHERE a < SOME
5787(SELECT a FROM t1 WHERE a <=>
5788(SELECT a FROM t1 where a is null)
5789);
5790foo
5791DROP TABLE t1;
5792CREATE TABLE t1 (a int(11), b varchar(1));
5793INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5794SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5795a
57965
5797SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5798a
57995
5800SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5801a
58027
5803SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5804a
58057
5806SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5807a
58085
58097
5810SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5811a
58125
58137
5814SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5815a
58165
58177
5818SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5819a
58205
58217
5822SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5823a
58245
58257
5826SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5827a
58285
58297
5830SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5831a
58325
58337
5834SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5835a
58365
58377
5838SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5839a
5840SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5841a
5842SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5843a
5844SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5845a
5846SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5847a
5848SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5849a
5850SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5851a
5852SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5853a
5854SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5855a
5856SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5857a
5858SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5859a
5860SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5861a
5862delete from t1;
5863INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
5864SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
5865a
58665
5867SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
5868a
58695
5870SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
5871a
58727
5873SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
5874a
58757
5876SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
5877a
58785
58797
5880SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
5881a
58825
58837
5884SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
5885a
58865
58877
5888SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
5889a
58905
58917
5892SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
5893a
58945
58957
5896SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
5897a
58985
58997
5900SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
5901a
59025
59037
5904SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
5905a
59065
59077
5908SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
5909a
5910SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
5911a
5912SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
5913a
5914SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
5915a
5916SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
5917a
5918SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
5919a
5920SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
5921a
5922SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
5923a
5924SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
5925a
5926SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
5927a
5928SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
5929a
5930SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
5931a
5932drop table t1;
5933#
5934# Fix of lp:780386 (NULL left part with empty ALL subquery).
5935#
5936CREATE TABLE t1 ( f11 int) ;
5937INSERT IGNORE INTO t1 VALUES (0),(0);
5938CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
5939INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
5940DROP TABLE IF EXISTS t3;
5941Warnings:
5942Note	1051	Unknown table 'test.t3'
5943CREATE TABLE t3 ( f3 int) ;
5944INSERT INTO t3 VALUES (0),(0);
5945SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
5946r
5947NULL
59485
5949NULL
59505
5951DROP TABLE t1, t2, t3;
5952#
5953# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
5954#
5955CREATE TABLE t1(a1 int);
5956INSERT INTO t1 VALUES (1),(2);
5957CREATE TABLE t2(a1 int);
5958INSERT INTO t2 VALUES (3);
5959SELECT @@session.sql_mode INTO @old_sql_mode;
5960SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5961SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
59621
59631
59641
5965SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
59661
59671
59681
5969SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
59701
5971Warnings:
5972Warning	1292	Truncated incorrect DOUBLE value: 'a'
5973SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
59741
59751
59761
5977SET SESSION sql_mode=@old_sql_mode;
5978DROP TABLE t1, t2;
5979create table t2(i int);
5980insert into t2 values(0);
5981SELECT @@session.sql_mode INTO @old_sql_mode;
5982SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
5983CREATE VIEW v1 AS
5984SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
5985;
5986CREATE TABLE t1 (
5987pk int NOT NULL,
5988col_varchar_key varchar(1) DEFAULT NULL,
5989PRIMARY KEY (pk),
5990KEY col_varchar_key (col_varchar_key)
5991);
5992SELECT t1.pk
5993FROM t1
5994WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
5995;
5996pk
5997SET SESSION sql_mode=@old_sql_mode;
5998drop table t2, t1;
5999drop view v1;
6000#
6001# BUG#50257: Missing info in REF column of the EXPLAIN
6002#            lines for subselects
6003#
6004CREATE TABLE t1 (a INT, b INT, INDEX (a));
6005INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
6006EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6007id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60081	SIMPLE	t1	ref	a	a	5	const	1
6009EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6010id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60111	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
60122	SUBQUERY	t1	ref	a	a	5	const	1	Using index
6013DROP TABLE t1;
6014#
6015# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6016# (duplicate of LP bug #888456)
6017#
6018CREATE TABLE t1 (f1 varchar(1));
6019INSERT INTO t1 VALUES ('v'),('s');
6020CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6021INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6022('d'),('y'),('t'),('d'),('s');
6023EXPLAIN
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);
6027id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60281	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
60291	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
60302	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6031SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
6032WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
6033WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
6034f1	f1_key
6035s	c
6036s	d
6037s	d
6038s	d
6039s	j
6040s	m
6041s	s
6042s	t
6043s	v
6044s	y
6045v	c
6046v	d
6047v	d
6048v	d
6049v	j
6050v	m
6051v	s
6052v	t
6053v	v
6054v	y
6055DROP TABLE t1,t2;
6056#
6057# LP bug 919427: EXPLAIN for a query over a single-row table
6058#                with IN subquery in WHERE condition
6059#
6060CREATE TABLE ot (
6061col_int_nokey int(11),
6062col_varchar_nokey varchar(1)
6063) ;
6064INSERT INTO ot VALUES (1,'x');
6065CREATE TABLE it1(
6066col_int_key int(11),
6067col_varchar_key varchar(1),
6068KEY idx_cvk_cik (col_varchar_key,col_int_key)
6069);
6070INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
6071CREATE TABLE it2 (
6072col_int_key int(11),
6073col_varchar_key varchar(1),
6074col_varchar_key2 varchar(1),
6075KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
6076KEY idx_cvk_cik (col_varchar_key, col_int_key)
6077);
6078INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
6079EXPLAIN
6080SELECT col_int_nokey FROM ot
6081WHERE col_varchar_nokey IN
6082(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6083id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60841	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60851	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1
60862	MATERIALIZED	it1	ref	idx_cvk_cik	idx_cvk_cik	9	const,const	1	Using where; Using index
6087SELECT col_int_nokey FROM ot
6088WHERE col_varchar_nokey IN
6089(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
6090col_int_nokey
60911
6092EXPLAIN
6093SELECT col_int_nokey FROM ot
6094WHERE (col_varchar_nokey, 'x') IN
6095(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6096id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
60971	PRIMARY	ot	system	NULL	NULL	NULL	NULL	1
60981	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	8	func,func	1
60992	MATERIALIZED	it2	ref	idx_cvk_cvk2_cik,idx_cvk_cik	idx_cvk_cvk2_cik	8	const,const	1	Using where; Using index
6100SELECT col_int_nokey FROM ot
6101WHERE (col_varchar_nokey, 'x') IN
6102(SELECT col_varchar_key, col_varchar_key2 FROM it2);
6103col_int_nokey
61041
6105DROP TABLE ot,it1,it2;
6106#
6107# MDEV-746
6108# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
6109# HAS AN EMPTY RESULT
6110#
6111CREATE TABLE t1 (
6112pk int NOT NULL,
6113col_int_nokey int NOT NULL,
6114col_int_key int NOT NULL,
6115col_time_key time NOT NULL,
6116col_varchar_key varchar(1) NOT NULL,
6117col_varchar_nokey varchar(1) NOT NULL,
6118PRIMARY KEY (pk),
6119KEY col_int_key (col_int_key),
6120KEY col_time_key (col_time_key),
6121KEY col_varchar_key (col_varchar_key,col_int_key)
6122) ENGINE=MyISAM;
6123CREATE TABLE t2 (
6124pk int NOT NULL AUTO_INCREMENT,
6125col_int_nokey int NOT NULL,
6126col_int_key int NOT NULL,
6127col_time_key time NOT NULL,
6128col_varchar_key varchar(1) NOT NULL,
6129col_varchar_nokey varchar(1) NOT NULL,
6130PRIMARY KEY (pk),
6131KEY col_int_key (col_int_key),
6132KEY col_time_key (col_time_key),
6133KEY col_varchar_key (col_varchar_key,col_int_key)
6134) ENGINE=MyISAM;
6135INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
6136SET @var2:=4, @var3:=8;
6137
6138Testcase without inner subquery
6139EXPLAIN SELECT @var3:=12, sq4_alias1.*
6140FROM t1 AS sq4_alias1
6141WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6142sq4_alias1.col_varchar_key = @var3;
6143id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61441	SIMPLE	sq4_alias1	system	NULL	NULL	NULL	NULL	0	Const row not found
6145SELECT @var3:=12, sq4_alias1.*
6146FROM t1 AS sq4_alias1
6147WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6148sq4_alias1.col_varchar_key = @var3;
6149@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6150SELECT @var3;
6151@var3
61528
6153EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6154FROM t1 AS sq4_alias1
6155WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6156sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6157id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61581	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	Const row not found
61592	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
6160SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
6161FROM t1 AS sq4_alias1
6162WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
6163sq4_alias1.col_varchar_key = @var3 ) AS alias3;
6164@var3:=12	pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6165SELECT @var3;
6166@var3
61678
6168
6169Testcase with inner subquery; crashed WL#6095
6170SET @var3=8;
6171EXPLAIN SELECT sq4_alias1.*
6172FROM t1 AS sq4_alias1
6173WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6174NOT IN
6175(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6176c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6177FROM t2 AS c_sq1_alias1
6178WHERE (c_sq1_alias1.col_int_nokey != @var2
6179OR c_sq1_alias1.pk != @var3));
6180id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
61811	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
61822	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6183SELECT sq4_alias1.*
6184FROM t1 AS sq4_alias1
6185WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6186NOT IN
6187(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6188c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6189FROM t2 AS c_sq1_alias1
6190WHERE (c_sq1_alias1.col_int_nokey != @var2
6191OR c_sq1_alias1.pk != @var3));
6192pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6193EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
6194FROM t1 AS sq4_alias1
6195WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6196NOT IN
6197(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6198c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6199FROM t2 AS c_sq1_alias1
6200WHERE (c_sq1_alias1.col_int_nokey != @var2
6201OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6202id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62031	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
62043	DEPENDENT SUBQUERY	c_sq1_alias1	system	PRIMARY	NULL	NULL	NULL	1
6205SELECT * FROM ( SELECT sq4_alias1.*
6206FROM t1 AS sq4_alias1
6207WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
6208NOT IN
6209(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
6210c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
6211FROM t2 AS c_sq1_alias1
6212WHERE (c_sq1_alias1.col_int_nokey != @var2
6213OR c_sq1_alias1.pk != @var3)) ) AS alias3;
6214pk	col_int_nokey	col_int_key	col_time_key	col_varchar_key	col_varchar_nokey
6215DROP TABLE t1,t2;
6216# End of 5.2 tests
6217#
6218# BUG#779885: Crash in eliminate_item_equal with materialization=on in
6219#
6220CREATE TABLE t1 ( f1 int );
6221INSERT INTO t1 VALUES (19), (20);
6222CREATE TABLE t2 ( f10 varchar(32) );
6223INSERT INTO t2 VALUES ('c'),('d');
6224CREATE TABLE t3 ( f10 varchar(32) );
6225INSERT INTO t3 VALUES ('a'),('b');
6226SELECT *
6227FROM t1
6228WHERE
6229( 't' ) IN (
6230SELECT t3.f10
6231FROM t3
6232JOIN t2
6233ON t2.f10 = t3.f10
6234);
6235f1
6236DROP TABLE t1,t2,t3;
6237#
6238# BUG lp:813473: Wrong result with outer join + NOT IN subquery
6239# This bug is a duplicate of Bug#11764086 whose test case is added below
6240#
6241CREATE TABLE t1 (c int) ;
6242INSERT INTO t1 VALUES (5),(6);
6243CREATE TABLE t2 (a int, b int) ;
6244INSERT INTO t2 VALUES (20,9),(20,9);
6245create table t3 (d int, e int);
6246insert into t3 values (2, 9), (3,10);
6247SET @save_optimizer_switch=@@optimizer_switch;
6248SET optimizer_switch='outer_join_with_cache=off';
6249EXPLAIN
6250SELECT t2.b , t1.c
6251FROM t2 LEFT JOIN t1 ON t1.c < 3
6252WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
6253id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62541	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2
62551	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
62562	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where
6257SELECT t2.b , t1.c
6258FROM t2 LEFT JOIN t1 ON t1.c < 3
6259WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
6260b	c
62619	NULL
62629	NULL
6263SET optimizer_switch=@save_optimizer_switch;
6264drop table t1, t2, t3;
6265#
6266# BUG#50257: Missing info in REF column of the EXPLAIN
6267#            lines for subselects
6268#
6269CREATE TABLE t1 (a INT, b INT, INDEX (a));
6270INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
6271
6272set @tmp_optimizer_switch=@@optimizer_switch;
6273set optimizer_switch='derived_merge=off,derived_with_keys=off';
6274EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
6275id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62761	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2
62772	DERIVED	t1	ref	a	a	5	const	1
6278set optimizer_switch=@tmp_optimizer_switch;
6279
6280EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
6281id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
62821	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	4
62832	SUBQUERY	t1	ref	a	a	5	const	1	Using index
6284
6285DROP TABLE t1;
6286#
6287# Bug#11764086: Null left operand to NOT IN in WHERE clause
6288# behaves differently than real NULL
6289#
6290CREATE TABLE parent (id int);
6291INSERT INTO parent VALUES (1), (2);
6292CREATE TABLE child (parent_id int, other int);
6293INSERT INTO child VALUES (1,NULL);
6294# Offending query (c.parent_id is NULL for null-complemented rows only)
6295SELECT    p.id, c.parent_id
6296FROM      parent p
6297LEFT JOIN child  c
6298ON        p.id = c.parent_id
6299WHERE     c.parent_id NOT IN (
6300SELECT parent_id
6301FROM   child
6302WHERE  parent_id = 3
6303);
6304id	parent_id
63051	1
63062	NULL
6307# Some syntactic variations with IS FALSE and IS NOT TRUE
6308SELECT    p.id, c.parent_id
6309FROM      parent p
6310LEFT JOIN child  c
6311ON        p.id = c.parent_id
6312WHERE     c.parent_id IN (
6313SELECT parent_id
6314FROM   child
6315WHERE  parent_id = 3
6316) IS NOT TRUE;
6317id	parent_id
63181	1
63192	NULL
6320SELECT    p.id, c.parent_id
6321FROM      parent p
6322LEFT JOIN child  c
6323ON        p.id = c.parent_id
6324WHERE     c.parent_id IN (
6325SELECT parent_id
6326FROM   child
6327WHERE  parent_id = 3
6328) IS FALSE;
6329id	parent_id
63301	1
63312	NULL
6332DROP TABLE parent, child;
6333# End of test for bug#11764086.
6334#
6335# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
6336#                BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
6337#
6338CREATE TABLE t1(a INT);
6339INSERT INTO t1 VALUES (0), (1);
6340CREATE TABLE t2(
6341b TEXT,
6342c INT,
6343PRIMARY KEY (b(1))
6344);
6345INSERT INTO t2 VALUES ('a', 2), ('b', 3);
6346SELECT 1 FROM t1 WHERE a =
6347(SELECT 1 FROM t2 WHERE b =
6348(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6349ORDER BY b
6350);
63511
6352Warnings:
6353Warning	1292	Truncated incorrect DOUBLE value: 'a'
6354Warning	1292	Truncated incorrect DOUBLE value: 'b'
6355Warning	1292	Truncated incorrect DOUBLE value: 'a'
6356Warning	1292	Truncated incorrect DOUBLE value: 'b'
6357SELECT 1 FROM t1 WHERE a =
6358(SELECT 1 FROM t2 WHERE b =
6359(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
6360GROUP BY b
6361);
63621
6363Warnings:
6364Warning	1292	Truncated incorrect DOUBLE value: 'a'
6365Warning	1292	Truncated incorrect DOUBLE value: 'b'
6366Warning	1292	Truncated incorrect DOUBLE value: 'a'
6367Warning	1292	Truncated incorrect DOUBLE value: 'b'
6368DROP TABLE t1, t2;
6369#
6370# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
6371#
6372CREATE TABLE t1 (f1 varchar(1));
6373INSERT INTO t1 VALUES ('v'),('s');
6374CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
6375INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
6376('d'),('y'),('t'),('d'),('s');
6377SELECT table1.f1, table2.f1_key
6378FROM t1 AS table1, t2 AS table2
6379WHERE EXISTS
6380(
6381SELECT DISTINCT f1_key
6382FROM t2
6383WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6384f1	f1_key
6385s	c
6386s	d
6387s	d
6388s	d
6389s	j
6390s	m
6391s	s
6392s	t
6393s	v
6394s	y
6395v	c
6396v	d
6397v	d
6398v	d
6399v	j
6400v	m
6401v	s
6402v	t
6403v	v
6404v	y
6405explain SELECT table1.f1, table2.f1_key
6406FROM t1 AS table1, t2 AS table2
6407WHERE EXISTS
6408(
6409SELECT DISTINCT f1_key
6410FROM t2
6411WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
6412id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
64131	PRIMARY	table1	ALL	NULL	NULL	NULL	NULL	2
64141	PRIMARY	table2	index	NULL	f1_key	4	NULL	10	Using where; Using index; Using join buffer (flat, BNL join)
64152	DEPENDENT SUBQUERY	t2	index	f1_key	f1_key	4	NULL	10	Using where; Using index
6416DROP TABLE t1,t2;
6417#
6418# lp:826279: assertion failure with GROUP BY a result of subquery
6419#
6420CREATE TABLE t1 (a int);
6421INSERT INTO t1 VALUES (0), (0);
6422CREATE TABLE t2 (a int, b int, c int);
6423INSERT INTO t2 VALUES (10,7,0), (0,7,0);
6424CREATE TABLE t3 (a int, b int);
6425INSERT INTO t3 VALUES (10,7), (0,7);
6426SELECT SUM(DISTINCT b),
6427(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6428WHERE t.a != 0 AND t2.a != 0)
6429FROM (SELECT * FROM t3) AS t
6430GROUP BY 2;
6431SUM(DISTINCT b)	(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
6432WHERE t.a != 0 AND t2.a != 0)
64337	NULL
6434SELECT SUM(DISTINCT b),
6435(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
6436FROM (SELECT * FROM t3) AS t
6437GROUP BY 2;
6438SUM(DISTINCT b)	(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
64397	NULL
64407	10
6441DROP TABLE t1,t2,t3;
6442#
6443# Bug#12329653
6444# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
6445#
6446CREATE TABLE t1(a1 int);
6447INSERT INTO t1 VALUES (1),(2);
6448SELECT @@session.sql_mode INTO @old_sql_mode;
6449SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
6450SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
64511
64521
64531
6454PREPARE stmt FROM
6455'SELECT 1 UNION ALL
6456SELECT 1 FROM t1
6457ORDER BY
6458(SELECT 1 FROM t1 AS t1_0
6459  WHERE 1 < SOME (SELECT a1 FROM t1)
6460)' ;
6461EXECUTE stmt ;
6462ERROR 21000: Subquery returns more than 1 row
6463EXECUTE stmt ;
6464ERROR 21000: Subquery returns more than 1 row
6465SET SESSION sql_mode=@old_sql_mode;
6466DEALLOCATE PREPARE stmt;
6467DROP TABLE t1;
6468#
6469# LP BUG#833777 Performance regression with deeply nested subqueries
6470#
6471create table t1 (a int not null, b char(10) not null);
6472insert into t1 values (1, 'a');
6473set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
6474select 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)))))))))))))))))))))))))))));
6475a
64761
6477set @@optimizer_switch=@subselect_tmp;
6478drop table t1;
6479#
6480# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
6481#
6482CREATE TABLE t1 (a varchar(3));
6483INSERT INTO t1 VALUES ('AAA'),('BBB');
6484CREATE TABLE t2 (a varchar(3));
6485INSERT INTO t2 VALUES ('CCC');
6486set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
6487SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
6488a
6489set @@optimizer_switch=@subselect_tmp;
6490drop table t1, t2;
6491#
6492# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
6493# with view , UNION and prepared statement (rewriting fake_select
6494# condition).
6495#
6496CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
6497INSERT INTO t1 VALUES (6,'d'),(7,'y');
6498CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
6499INSERT INTO t2 VALUES (10,7);
6500CREATE VIEW v2 AS SELECT * FROM t2;
6501PREPARE st1 FROM "
6502        SELECT *
6503        FROM t1
6504        LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
6505        WHERE v2.f1 NOT IN (
6506                SELECT 1 UNION
6507                SELECT 247
6508        )
6509";
6510EXECUTE st1;
6511f1	f4	f1	f2
65127	y	10	7
6513deallocate prepare st1;
6514DROP VIEW v2;
6515DROP TABLE t1,t2;
6516#
6517# LP bug #887458 Crash in subselect_union_engine::no_rows with
6518# double UNION and join_cache_level=3,8
6519# (IN/ALL/ANY optimizations should not be applied to fake_select)
6520CREATE TABLE t2 ( a int, b varchar(1)) ;
6521INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
6522CREATE TABLE t1 ( b varchar(1)) ;
6523INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
6524set @save_join_cache_level=@@join_cache_level;
6525SET SESSION join_cache_level=3;
6526SELECT *
6527FROM t1, t2
6528WHERE t2.b IN (
6529SELECT 'm' UNION
6530SELECT 'm'
6531) OR t1.b <> SOME (
6532SELECT 'v' UNION
6533SELECT 't'
6534);
6535b	a	b
6536set @@join_cache_level= @save_join_cache_level;
6537drop table t1,t2;
6538#
6539# LP bug #885162 Got error 124 from storage engine with UNION inside
6540# subquery and join_cache_level=3..8
6541# (IN/ALL/ANY optimizations should not be applied to fake_select)
6542#
6543CREATE TABLE t1 (
6544f1 varchar(1) DEFAULT NULL
6545);
6546INSERT INTO t1 VALUES ('c');
6547set @save_join_cache_level=@@join_cache_level;
6548SET SESSION join_cache_level=8;
6549SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
6550f1
6551set @@join_cache_level= @save_join_cache_level;
6552drop table t1;
6553#
6554# LP BUG#747278 incorrect values of the NULL (no rows) single
6555# row subquery requested via element_index() interface
6556#
6557CREATE TABLE t1 (f1a int, f1b int) ;
6558INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
6559CREATE TABLE t2 ( f2 int);
6560INSERT IGNORE INTO t2 VALUES (3),(4);
6561CREATE TABLE t3 (f3a int default 1, f3b int default 2);
6562INSERT INTO t3 VALUES (1,1),(2,2);
6563set @old_optimizer_switch = @@session.optimizer_switch;
6564set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
6565SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6566(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6567NULL
6568NULL
6569SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6570(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6571NULL
6572NULL
6573SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6574(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6575NULL
6576NULL
6577SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6578(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6579NULL
6580SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6581(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6582NULL
6583NULL
6584SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6585(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6586NULL
6587NULL
6588SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6589(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6590NULL
6591NULL
6592SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6593(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6594NULL
6595set @@session.optimizer_switch=@old_optimizer_switch;
6596SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
6597(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
6598NULL
6599NULL
6600SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
6601(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
6602NULL
6603NULL
6604SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
6605(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6606NULL
6607NULL
6608SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
6609(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
6610NULL
6611SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
6612(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
6613NULL
6614NULL
6615SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
6616(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
6617NULL
6618NULL
6619SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
6620(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6621NULL
6622NULL
6623SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
6624(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
6625NULL
6626select (null, null) = (null, null);
6627(null, null) = (null, null)
6628NULL
6629SELECT (SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0);
6630(SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0)
6631NULL
6632drop tables t1,t2,t3;
6633#
6634# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
6635#
6636CREATE TABLE t1 (a date, KEY (a)) ;
6637INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
6638set @old_optimizer_switch = @@optimizer_switch;
6639SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,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	DEPENDENT SUBQUERY	t1	index_subquery	a	a	4	func	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='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
6649EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6650id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
66511	PRIMARY	t1	index	NULL	a	4	NULL	2	Using where; Using index
66522	MATERIALIZED	t1	index	NULL	a	4	NULL	2	Using index
6653SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
6654a
66552009-01-01
66562009-02-02
6657set @@optimizer_switch=@old_optimizer_switch;
6658drop table t1;
6659#
6660# LP BUG#908269 incorrect condition in case of subqueries depending
6661# on constant tables
6662#
6663CREATE TABLE t1 ( a INT );
6664INSERT INTO t1 VALUES (1),(5);
6665CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
6666INSERT INTO t2 VALUES (1);
6667CREATE TABLE t3 ( c INT );
6668INSERT INTO t3 VALUES (4),(5);
6669SET optimizer_switch='subquery_cache=off';
6670SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
6671( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
66721
6673NULL
6674SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
6675( SELECT b FROM t2 WHERE b = a OR b * 0)
66761
6677NULL
6678SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
6679( SELECT b FROM t2 WHERE b = a OR rand() * 0)
66801
6681NULL
6682drop table t1,t2,t3;
6683set optimizer_switch=@subselect_tmp;
6684#
6685# LP BUG#905353 Wrong non-empty result with a constant table,
6686# aggregate function in subquery, MyISAM or Aria
6687#
6688CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
6689INSERT INTO t1 VALUES (1);
6690SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
6691a
6692drop table t1;
6693#
6694# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in
6695# main query and implicit grouping
6696#
6697CREATE TABLE t1 (f1 int) engine=MyISAM;
6698INSERT INTO t1 VALUES (7),(8);
6699CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
6700INSERT INTO t2 VALUES (3,'f');
6701EXPLAIN
6702SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6703id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67041	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67052	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6706SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6707COUNT(f1)	f4
67080	NULL
6709EXPLAIN
6710SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6711id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67121	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67132	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6714SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
6715COUNT(f1)	f4
67160	0
6717EXPLAIN
6718SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6719id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67201	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67212	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6722SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6723COUNT(f1)	f4
67240	1
6725EXPLAIN
6726SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6727id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67281	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE noticed after reading const tables
67292	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6730SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
6731COUNT(f1)	f4
67320	0
6733drop table t1,t2;
6734#
6735# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
6736# HAVING, UNION in subquery
6737#
6738CREATE TABLE t1 (a INT);
6739INSERT INTO t1 VALUES (7),(0);
6740CREATE TABLE t2 (b INT);
6741EXPLAIN
6742SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6743id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67441	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	Const row not found
67451	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2
67462	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67473	UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6748NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6749SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
6750f1	f2
6751drop table t1,t2;
6752#
6753# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
6754# WHERE and UNION in HAVING
6755#
6756CREATE TABLE t1 (a INT);
6757INSERT INTO t1 VALUES (1),(7);
6758EXPLAIN
6759SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) 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	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
67633	DEPENDENT UNION	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
6764NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6765SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
6766min_a	a
6767EXPLAIN
6768SELECT 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;
6769id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
67701	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
67712	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
67723	DEPENDENT UNION	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
6773NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL
6774SELECT 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;
6775min_a	a
6776drop table t1;
6777#
6778# MDEV-367: Different results with and without subquery_cache on
6779# a query with a constant NOT IN condition
6780#
6781CREATE TABLE t1 (a INT) ENGINE=MyISAM;
6782INSERT INTO t1 VALUES (1),(2),(3);
6783set @mdev367_optimizer_switch = @@optimizer_switch;
6784set optimizer_switch = 'subquery_cache=on';
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;
6793set optimizer_switch = 'subquery_cache=off';
6794SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
6795a
6796SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
6797a	( 3, 3 ) NOT IN ( SELECT NULL, NULL )
67981	NULL
67992	NULL
68003	NULL
6801set optimizer_switch=@mdev367_optimizer_switch;
6802DROP TABLE t1;
6803#
6804# MDEV-521 single value subselect transformation problem
6805#
6806CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
6807INSERT INTO t1 VALUES ('u1'),('u2');
6808SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6809f1
6810u1
6811u2
6812FLUSH TABLES;
6813SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
6814f1
6815u1
6816u2
6817DROP TABLE t1;
6818# return optimizer switch changed in the beginning of this test
6819set optimizer_switch=@subselect_tmp;
6820#
6821# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
6822# precomputed and thus not part of optimization
6823#
6824CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
6825INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
6826('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
6827EXPLAIN
6828SELECT MAX( alias2.a ) AS field
6829FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6830WHERE alias1.a = alias2.a OR alias1.a = 'y'
6831HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6832id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68331	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible HAVING
68342	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6835SELECT MAX( alias2.a ) AS field
6836FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6837WHERE alias1.a = alias2.a OR alias1.a = 'y'
6838HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
6839field
6840EXPLAIN
6841SELECT MAX( alias2.a )
6842FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6843WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6844id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68451	PRIMARY	alias1	index	a	a	19	NULL	11	Using where; Using index
68461	PRIMARY	alias2	ref	a	a	19	test.alias1.a	2	Using index
68471	PRIMARY	alias3	index	NULL	a	19	NULL	11	Using index; Using join buffer (flat, BNL join)
68482	SUBQUERY	t1	index_subquery	a	a	19	const	1	Using index; Using where
6849SELECT MAX( alias2.a )
6850FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
6851WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
6852MAX( alias2.a )
6853Arden-Arcade
6854drop table t1;
6855#
6856# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
6857# with semijoin+materialization, IN and = subqueries
6858#
6859CREATE TABLE t1 (a1 INT);
6860INSERT INTO t1 VALUES (4),(6);
6861CREATE TABLE t2 (b1 INT);
6862INSERT INTO t2 VALUES (1),(7);
6863EXPLAIN
6864SELECT * FROM t1
6865WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6866id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68671	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68682	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
68692	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1
68703	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	2
68713	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
6872SELECT * FROM t1
6873WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
6874a1
6875drop table t1, t2;
6876#
6877# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
6878# inner joins takes hundreds times longer
6879#
6880CREATE TABLE t1 (a INT);
6881INSERT INTO t1 VALUES (1),(7);
6882CREATE TABLE t2 (b INT);
6883INSERT INTO t2 VALUES (4),(5);
6884CREATE TABLE t3 (c INT);
6885INSERT INTO t3 VALUES (8),(3);
6886set @@expensive_subquery_limit= 0;
6887EXPLAIN
6888SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6889FROM t2 alias1, t1 alias2, t1 alias3;
6890id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
68911	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
68921	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68931	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
68942	DEPENDENT SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
68952	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
68963	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6897flush status;
6898SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6899FROM t2 alias1, t1 alias2, t1 alias3;
6900(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6901NULL
6902NULL
6903NULL
6904NULL
6905NULL
6906NULL
6907NULL
6908NULL
6909show status like "subquery_cache%";
6910Variable_name	Value
6911Subquery_cache_hit	6
6912Subquery_cache_miss	2
6913show status like '%Handler_read%';
6914Variable_name	Value
6915Handler_read_first	0
6916Handler_read_key	8
6917Handler_read_last	0
6918Handler_read_next	0
6919Handler_read_prev	0
6920Handler_read_retry	0
6921Handler_read_rnd	0
6922Handler_read_rnd_deleted	0
6923Handler_read_rnd_next	22
6924set @@expensive_subquery_limit= default;
6925EXPLAIN
6926SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6927FROM t2 alias1, t1 alias2, t1 alias3;
6928id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69291	PRIMARY	alias1	ALL	NULL	NULL	NULL	NULL	2
69301	PRIMARY	alias2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69311	PRIMARY	alias3	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (flat, BNL join)
69322	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
69332	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (flat, BNL join)
69343	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	2
6935flush status;
6936SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6937FROM t2 alias1, t1 alias2, t1 alias3;
6938(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
6939NULL
6940NULL
6941NULL
6942NULL
6943NULL
6944NULL
6945NULL
6946NULL
6947show status like "subquery_cache%";
6948Variable_name	Value
6949Subquery_cache_hit	0
6950Subquery_cache_miss	0
6951show status like '%Handler_read%';
6952Variable_name	Value
6953Handler_read_first	0
6954Handler_read_key	0
6955Handler_read_last	0
6956Handler_read_next	0
6957Handler_read_prev	0
6958Handler_read_retry	0
6959Handler_read_rnd	0
6960Handler_read_rnd_deleted	0
6961Handler_read_rnd_next	16
6962drop table t1, t2, t3;
6963#
6964# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
6965#
6966CREATE TABLE t1 (a INT);
6967INSERT INTO t1 VALUES (0),(8);
6968CREATE TABLE t2 (b INT PRIMARY KEY);
6969INSERT INTO t2 VALUES (1),(2);
6970EXPLAIN
6971SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6972id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69731	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
69742	MATERIALIZED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
69753	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2
6976SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
6977a
6978drop table t1,t2;
6979#
6980# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
6981#
6982CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
6983INSERT INTO t1 VALUES ('USA');
6984CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
6985INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
6986CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
6987INSERT INTO t3 VALUES (12),(22),(9),(45);
6988create table t4 like t3;
6989insert into t4 select * from t3;
6990# This should not show range access for table t2
6991explain
6992SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
6993WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
6994id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
69951	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1
69961	PRIMARY	t2	ALL	b	NULL	NULL	NULL	2	Using where
69971	PRIMARY	t3	ref	d	d	5	test.t2.b	2	Using index
69983	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
6999set @tmp_mdev410=@@global.userstat;
7000set global userstat=on;
7001flush table_statistics;
7002flush index_statistics;
7003SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
7004WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
7005MIN(b)
7006NULL
7007# The following shows that t2 was indeed scanned with a full scan.
7008show table_statistics;
7009Table_schema	Table_name	Rows_read	Rows_changed	Rows_changed_x_#indexes
7010test	t1	2	0	0
7011test	t2	3	0	0
7012show index_statistics;
7013Table_schema	Table_name	Index_name	Rows_read
7014test	t2	b	1
7015set global userstat=@tmp_mdev410;
7016DROP TABLE t1,t2,t3,t4;
7017#
7018# MDEV-430: Server crashes in select_describe on EXPLAIN with
7019#    materialization+semijoin, 2 nested subqueries, aggregate functions
7020#
7021CREATE TABLE t1 (a INT, KEY(a));
7022INSERT INTO t1 VALUES (1),(8);
7023CREATE TABLE t2 (b INT, KEY(b));
7024INSERT INTO t2 VALUES (45),(17),(20);
7025EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7026id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70271	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70282	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70292	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7030DROP TABLE t1,t2;
7031#
7032# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
7033#
7034CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
7035INSERT INTO t1 VALUES (8),(0);
7036CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
7037INSERT INTO t2 VALUES (4,'j'),(6,'v');
7038CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
7039INSERT INTO t3 VALUES ('b'),('c');
7040EXPLAIN
7041SELECT * FROM t1
7042WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7043id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70441	PRIMARY	t1	index	a	a	5	NULL	2	Using where; Using index
70452	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
70462	SUBQUERY	<subquery3>	eq_ref	distinct_key	distinct_key	4	test.t2.c	1
70473	MATERIALIZED	t3	ALL	NULL	NULL	NULL	NULL	2
7048SELECT * FROM t1
7049WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
7050a
7051drop table t1, t2, t3;
7052#
7053# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
7054#
7055CREATE TABLE t1 (a INT, KEY(a));
7056INSERT INTO t1 VALUES (1),(8);
7057CREATE TABLE t2 (b INT, KEY(b));
7058INSERT INTO t2 VALUES (45),(17),(20);
7059EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
7060id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
70611	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
70622	SUBQUERY	t1	range	a	a	5	NULL	2	Using where; Using index
70632	SUBQUERY	t2	ref	b	b	5	test.t1.a	2	Using index
7064DROP TABLE t1,t2;
7065#
7066# MDEV-5991: crash in Item_field::used_tables
7067#
7068create table t1 (c int);
7069select exists(select 1 from t1 group by `c` in (select `c` from t1));
7070exists(select 1 from t1 group by `c` in (select `c` from t1))
70710
7072drop table t1;
7073#
7074# MDEV-7565: Server crash with Signal 6 (part 2)
7075#
7076Select
7077(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7078Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7079) As `ControlRev`
7080From
7081(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7082Group By  TestCase.Revenue, TestCase.TemplateID;
7083ControlRev
7084NULL
7085#
7086# MDEV-7445:Server crash with Signal 6
7087#
7088CREATE PROCEDURE procedure2()
7089BEGIN
7090Select
7091(Select Sum(`TestCase`.Revenue) From mysql.slow_log E
7092Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
7093) As `ControlRev`
7094  From
7095(Select  3 as Revenue, 4 as TemplateID) As `TestCase`
7096Group By  TestCase.Revenue, TestCase.TemplateID;
7097END |
7098call procedure2();
7099ControlRev
7100NULL
7101call procedure2();
7102ControlRev
7103NULL
7104drop procedure procedure2;
7105#
7106# MDEV-7846:Server crashes in Item_subselect::fix
7107#_fields or fails with Thread stack overrun
7108#
7109CREATE TABLE t1 (column1 INT) ENGINE=MyISAM;
7110INSERT INTO t1 VALUES (3),(9);
7111CREATE TABLE t2 (column2 INT) ENGINE=MyISAM;
7112INSERT INTO t2 VALUES (1),(4);
7113CREATE TABLE t3 (column3 INT) ENGINE=MyISAM;
7114INSERT INTO t3 VALUES (6),(8);
7115CREATE TABLE t4 (column4 INT) ENGINE=MyISAM;
7116INSERT INTO t4 VALUES (2),(5);
7117PREPARE stmt FROM "
7118SELECT (
7119  SELECT MAX( table1.column1 ) AS field1
7120  FROM t1 AS table1
7121  WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 )
7122) AS sq
7123FROM t3 AS table3, t4 AS table4 GROUP BY sq
7124";
7125EXECUTE stmt;
7126sq
7127NULL
7128EXECUTE stmt;
7129sq
7130NULL
7131deallocate prepare stmt;
7132drop table t1,t2,t3,t4;
7133#
7134# MDEV-7122
7135# Assertion `0' failed in subselect_hash_sj_engine::init
7136#
7137SET SESSION big_tables=1;
7138CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
7139INSERT INTO t1 VALUES(0),(0),(0);
7140SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1);
7141a
71420
71430
71440
7145DROP TABLE t1;
7146SET SESSION big_tables=0;
7147#
7148# MDEV-10776: Server crash on query
7149#
7150create table t1 (field1 int);
7151insert into t1 values (1);
7152select round((select 1 from t1 limit 1))
7153from t1
7154group by round((select 1 from t1 limit 1));
7155round((select 1 from t1 limit 1))
71561
7157drop table t1;
7158#
7159# MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE ||
7160# m_lock_type != 2' failed in  handler::ha_index_read_map
7161#
7162CREATE TABLE t1 (f1 INT);
7163INSERT INTO t1 VALUES (1),(2);
7164CREATE TABLE t2 (f2 INT, KEY(f2));
7165INSERT INTO t2 VALUES (3);
7166CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2;
7167SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7168ERROR 42000: Can't group on 'sq'
7169SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7170ERROR 42000: Can't group on 'sq'
7171SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0;
7172f2
71733
7174SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 );
7175count(*)
71761
7177delete from t1;
7178SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
7179ERROR 42000: Can't group on 'sq'
7180SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
7181ERROR 42000: Can't group on 'sq'
7182drop view v2;
7183drop table t1,t2;
7184#
7185# MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*)
7186#
7187CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL);
7188INSERT INTO t1 VALUES ('foo','bar');
7189SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 );
7190f1	f2
7191SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 );
7192f1	f2
7193foo	bar
7194DROP TABLE t1;
7195#
7196# MDEV-10146: Wrong result (or questionable result and behavior)
7197# with aggregate function in uncorrelated SELECT subquery
7198#
7199CREATE TABLE t1 (f1 INT);
7200CREATE VIEW v1 AS SELECT * FROM t1;
7201INSERT INTO t1 VALUES (1),(2);
7202CREATE TABLE t2 (f2 int);
7203INSERT INTO t2 VALUES (3);
7204SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7205( SELECT MAX(f1) FROM t2 )
72062
7207SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7208( SELECT MAX(f1) FROM t2 )
72092
7210INSERT INTO t2 VALUES (4);
7211SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
7212ERROR 21000: Subquery returns more than 1 row
7213SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
7214ERROR 21000: Subquery returns more than 1 row
7215drop view v1;
7216drop table t1,t2;
7217CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM;
7218INSERT t1 VALUES (4),(8);
7219CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM;
7220INSERT t2 VALUES (6);
7221SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7222(SELECT MAX(sq.f2) FROM t1)
7223NULL
7224#
7225# Disable this query till MDEV-13399 is resolved
7226#
7227# INSERT t2 VALUES (9);
7228# --error ER_SUBQUERY_NO_1_ROW
7229# SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
7230#
7231drop table t1, t2;
7232#
7233# MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in
7234# (5.5 test)
7235#
7236SET @optimiser_switch_save= @@optimizer_switch;
7237CREATE TABLE t1 (a INT NOT NULL);
7238INSERT INTO t1 VALUES (1),(1),(1),(5),(5);
7239CREATE TABLE t2 (b INT);
7240INSERT INTO t2 VALUES (5),(1);
7241CREATE TABLE t3 (c INT, KEY(c));
7242INSERT INTO t3 VALUES (5),(5);
7243SET optimizer_switch='semijoin=on';
7244select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7245and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7246a
72475
72485
7249SET optimizer_switch='semijoin=off';
7250select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
7251and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
7252a
72535
72545
7255SET @@optimizer_switch= @optimiser_switch_save;
7256DROP TABLE t1, t2, t3;
7257#
7258# MDEV-16820: impossible where with inexpensive subquery
7259#
7260create table t1 (a int) engine=myisam;
7261insert into t1 values (3), (1), (7);
7262create table t2 (b int, index idx(b));
7263insert into t2 values (2), (5), (3), (2);
7264explain select * from t1 where (select max(b) from t2) = 10;
7265id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72661	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72672	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7268explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3;
7269id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72701	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72712	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
7272drop table t1,t2;
7273#
7274# MDEV-19429: Wrong query result with EXISTS and LIMIT 0
7275#
7276create table t10 (a int);
7277insert into t10 values (1),(2),(3);
7278create table t12 (a int);
7279insert into t12 values (1),(2),(3);
7280select * from t10 where exists (select * from t12 order by a limit 0);
7281a
7282explain select * from t10 where exists (select * from t12 order by a limit 0);
7283id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
72841	PRIMARY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Impossible WHERE
72852	SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Zero limit
7286prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)";
7287set @l=1;
7288execute stmt1 using @l;
7289a
72901
72912
72923
7293set @l=2;
7294execute stmt1 using @l;
7295a
72961
72972
72983
7299set @l=0;
7300execute stmt1 using @l;
7301a
7302deallocate prepare stmt1;
7303drop table t10, t12;
7304End of 5.5 tests
7305# End of 10.0 tests
7306#
7307# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
7308# with UNION in ALL subquery
7309#
7310SET NAMES utf8;
7311CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
7312INSERT INTO t1 VALUES ('foo');
7313SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
7314f
7315foo
7316SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar');
7317f
7318foo
7319drop table t1;
7320SET NAMES default;
7321#
7322# MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops
7323#
7324SET NAMES utf8;
7325CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM;
7326CREATE TABLE t2 (f3 INT) ENGINE=MyISAM;
7327SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' );
7328f1	f2	f3
7329SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo');
7330f1	f2	f3
7331DROP TABLE t1, t2;
7332SET NAMES default;
7333# End of 10.1 tests
7334#
7335# MDEV-12564: IN TO EXISTS transformation for rows after
7336#             conversion an outer join to inner join
7337#
7338CREATE TABLE t (
7339pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
7340) ENGINE=MyISAM;
7341INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
7342SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
7343WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
7344pk	i	c	pk	i	c
73451	10	foo	1	10	foo
7346DROP TABLE t;
7347#
7348# MDEV-25002: Outer reference in ON clause of subselect
7349#
7350create table t1 (
7351pk int primary key,
7352a int
7353) engine=myisam;
7354insert into t1 values (1,1), (2,2);
7355create table t2 (
7356pk int primary key,
7357b int
7358) engine=myisam;
7359insert into t2 values (1,1), (2,3);
7360create table t3 (a int);
7361insert into t3 values (1),(2);
7362select a,
7363(select count(*) from t1, t2
7364where t2.pk=t3.a and t1.pk=1) as sq
7365from t3;
7366a	sq
73671	1
73682	1
7369select a,
7370(select count(*) from t1 join t2 on t2.pk=t3.a
7371where t1.pk=1) as sq
7372from t3;
7373a	sq
73741	1
73752	1
7376select a from t3
7377where a in (select t2.b from t1,t2 where t2.pk=t3.a and t1.pk=1);
7378a
73791
7380select a from t3
7381where a in (select t2.b from t1 join t2 on t2.pk=t3.a where t1.pk=1);
7382a
73831
7384drop table t1,t2,t3;
7385# End of 10.2 tests
7386#
7387# Start of 10.4 tests
7388#
7389#
7390# MDEV-16861 Split Item::update_null_value() into a new virtual method in Type_handler
7391#
7392SELECT ROW(1,2) = EXISTS (SELECT 1);
7393ERROR HY000: Illegal parameter data types row and boolean for operation '='
7394SELECT ROW(1,2) = 1 IN (SELECT 1 UNION SELECT 2);
7395ERROR HY000: Illegal parameter data types row and boolean for operation '='
7396SELECT ROW(1,2) = (1 = ANY (SELECT 1 UNION SELECT 2));
7397ERROR HY000: Illegal parameter data types row and boolean for operation '='
7398#
7399# End of 10.4 tests
7400#
7401