1select @@optimizer_switch like '%exists_to_in=off%'; 2@@optimizer_switch like '%exists_to_in=off%' 30 4set optimizer_switch='exists_to_in=off'; 5drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12; 6drop view if exists v2; 7call mtr.add_suppression("Sort aborted.*"); 8set @subselect_tmp=@@optimizer_switch; 9set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test, 10"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off"); 11set join_cache_level=1; 12SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; 13SET optimizer_use_condition_selectivity=4; 14select (select 2); 15(select 2) 162 17explain extended select (select 2); 18id select_type table type possible_keys key key_len ref rows filtered Extra 191 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used 20Warnings: 21Note 1249 Select 2 was reduced during optimization 22Note 1003 select 2 AS `(select 2)` 23SELECT (SELECT 1) UNION SELECT (SELECT 2); 24(SELECT 1) 251 262 27explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2); 28id select_type table type possible_keys key key_len ref rows filtered Extra 291 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 303 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 31NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 32Warnings: 33Note 1249 Select 2 was reduced during optimization 34Note 1249 Select 4 was reduced during optimization 35Note 1003 /* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)` 36SELECT (SELECT (SELECT 0 UNION SELECT 0)); 37(SELECT (SELECT 0 UNION SELECT 0)) 380 39explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0)); 40id select_type table type possible_keys key key_len ref rows filtered Extra 411 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 423 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 434 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 44NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL 45Warnings: 46Note 1249 Select 2 was reduced during optimization 47Note 1003 /* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))` 48SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a; 49ERROR 42S22: Reference 'a' not supported (forward reference in item list) 50SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b; 51ERROR 42S22: Reference 'b' not supported (forward reference in item list) 52SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a; 53(SELECT 1) MAX(1) 541 1 55SELECT (SELECT a) as a; 56ERROR 42S22: Reference 'a' not supported (forward reference in item list) 57EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 58id select_type table type possible_keys key key_len ref rows filtered Extra 591 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00 603 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 612 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 62Warnings: 63Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 64Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 65Note 1003 /* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1 66SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 671 681 69SELECT (SELECT 1), a; 70ERROR 42S22: Unknown column 'a' in 'field list' 71SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; 72a 731 74SELECT 1 FROM (SELECT (SELECT a) b) c; 75ERROR 42S22: Unknown column 'a' in 'field list' 76SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id); 77id 781 79SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1); 80ERROR 21000: Operand should contain 1 column(s) 81SELECT 1 IN (SELECT 1); 821 IN (SELECT 1) 831 84SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a)); 851 861 87select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1)); 88ERROR 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 89SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1)); 90ERROR 42000: PROCEDURE does not support subqueries or stored functions 91SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL; 92ERROR 42S22: Unknown column 'a' in 'field list' 93SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL; 94ERROR 42S22: Unknown column 'a' in 'field list' 95SELECT (SELECT 1,2,3) = ROW(1,2,3); 96(SELECT 1,2,3) = ROW(1,2,3) 971 98SELECT (SELECT 1,2,3) = ROW(1,2,1); 99(SELECT 1,2,3) = ROW(1,2,1) 1000 101SELECT (SELECT 1,2,3) < ROW(1,2,1); 102(SELECT 1,2,3) < ROW(1,2,1) 1030 104SELECT (SELECT 1,2,3) > ROW(1,2,1); 105(SELECT 1,2,3) > ROW(1,2,1) 1061 107SELECT (SELECT 1,2,3) = ROW(1,2,NULL); 108(SELECT 1,2,3) = ROW(1,2,NULL) 109NULL 110SELECT ROW(1,2,3) = (SELECT 1,2,3); 111ROW(1,2,3) = (SELECT 1,2,3) 1121 113SELECT ROW(1,2,3) = (SELECT 1,2,1); 114ROW(1,2,3) = (SELECT 1,2,1) 1150 116SELECT ROW(1,2,3) < (SELECT 1,2,1); 117ROW(1,2,3) < (SELECT 1,2,1) 1180 119SELECT ROW(1,2,3) > (SELECT 1,2,1); 120ROW(1,2,3) > (SELECT 1,2,1) 1211 122SELECT ROW(1,2,3) = (SELECT 1,2,NULL); 123ROW(1,2,3) = (SELECT 1,2,NULL) 124NULL 125SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a'); 126(SELECT 1.5,2,'a') = ROW(1.5,2,'a') 1271 128SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b'); 129(SELECT 1.5,2,'a') = ROW(1.5,2,'b') 1300 131SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b'); 132(SELECT 1.5,2,'a') = ROW('1.5b',2,'b') 1330 134Warnings: 135Warning 1292 Truncated incorrect DOUBLE value: '1.5b' 136SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a'); 137(SELECT 'b',2,'a') = ROW(1.5,2,'a') 1380 139Warnings: 140Warning 1292 Truncated incorrect DOUBLE value: 'b' 141SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a'); 142(SELECT 1.5,2,'a') = ROW(1.5,'2','a') 1431 144SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a'); 145(SELECT 1.5,'c','a') = ROW(1.5,2,'a') 1460 147Warnings: 148Warning 1292 Truncated incorrect DOUBLE value: 'c' 149SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a); 150ERROR 21000: Operand should contain 1 column(s) 151SELECT 1 as a,(SELECT a+a) b,(SELECT b); 152a b (SELECT b) 1531 2 2 154create table t1 (a int); 155create table t2 (a int, b int); 156create table t3 (a int); 157create table t4 (a int not null, b int not null); 158insert into t1 values (2); 159insert into t2 values (1,7),(2,7); 160insert into t4 values (4,8),(3,8),(5,9); 161select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1; 162ERROR 42S22: Reference 'a1' not supported (forward reference in item list) 163select (select a from t1 where t1.a=t2.a), a from t2; 164(select a from t1 where t1.a=t2.a) a 165NULL 1 1662 2 167select (select a from t1 where t1.a=t2.b), a from t2; 168(select a from t1 where t1.a=t2.b) a 169NULL 1 170NULL 2 171select (select a from t1), a, (select 1 union select 2 limit 1) from t2; 172(select a from t1) a (select 1 union select 2 limit 1) 1732 1 1 1742 2 1 175select (select a from t3), a from t2; 176(select a from t3) a 177NULL 1 178NULL 2 179select * from t2 where t2.a=(select a from t1); 180a b 1812 7 182insert into t3 values (6),(7),(3); 183select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1); 184a b 1851 7 1862 7 187(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) 188union (select * from t4 order by a limit 2) order by a limit 3; 189a b 1901 7 1912 7 1923 8 193(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); 194a b 1951 7 1962 7 1974 8 1983 8 199explain 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); 200id select_type table type possible_keys key key_len ref rows filtered Extra 2011 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 2022 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort 2033 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where 2044 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 205NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 206Warnings: 207Note 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`)) 208select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2; 209(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a 2103 1 2117 2 212select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 213(select * from t2 where a>1) as tt; 214(select t3.a from t3 where a<8 order by 1 desc limit 1) a 2157 2 216set @tmp_optimizer_switch=@@optimizer_switch; 217set optimizer_switch='derived_merge=off,derived_with_keys=off'; 218explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 219(select * from t2 where a>1) as tt; 220id select_type table type possible_keys key key_len ref rows filtered Extra 2211 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00 2223 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where 2232 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort 224Warnings: 225Note 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` 226set optimizer_switch=@tmp_optimizer_switch; 227select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1); 228a 2292 230select * 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); 231a 2322 233select * 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); 234a 235select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 236b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) 2378 7.5000 2388 4.5000 2399 7.5000 240explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 241id select_type table type possible_keys key key_len ref rows filtered Extra 2421 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00 2432 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 2443 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 245Warnings: 246Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1 247Note 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` 248select * from t3 where exists (select * from t2 where t2.b=t3.a); 249a 2507 251select * from t3 where not exists (select * from t2 where t2.b=t3.a); 252a 2536 2543 255select * from t3 where a in (select b from t2); 256a 2577 258select * from t3 where a not in (select b from t2); 259a 2606 2613 262select * from t3 where a = some (select b from t2); 263a 2647 265select * from t3 where a <> any (select b from t2); 266a 2676 2683 269select * from t3 where a = all (select b from t2); 270a 2717 272select * from t3 where a <> all (select b from t2); 273a 2746 2753 276insert into t2 values (100, 5); 277select * from t3 where a < any (select b from t2); 278a 2796 2803 281select * from t3 where a < all (select b from t2); 282a 2833 284select * from t3 where a >= any (select b from t2); 285a 2866 2877 288explain extended select * from t3 where a >= any (select b from t2); 289id select_type table type possible_keys key key_len ref rows filtered Extra 2901 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 2912 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 292Warnings: 293Note 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`))) 294select * from t3 where a >= all (select b from t2); 295a 2967 297delete from t2 where a=100; 298select * from t3 where a in (select a,b from t2); 299ERROR 21000: Operand should contain 1 column(s) 300select * from t3 where a in (select * from t2); 301ERROR 21000: Operand should contain 1 column(s) 302insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10); 303select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b); 304b ma 305insert into t2 values (2,10); 306select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b); 307b ma 30810 1 309delete from t2 where a=2 and b=10; 310select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b); 311b ma 3127 12 313create table t5 (a int); 314select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 315(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 316NULL 1 3172 2 318insert into t5 values (5); 319select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 320(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 321NULL 1 3222 2 323insert into t5 values (2); 324select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 325(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 326NULL 1 3272 2 328explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 329id select_type table type possible_keys key key_len ref rows filtered Extra 3301 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 3312 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 3323 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where 333NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 334Warnings: 335Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1 336Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1 337Note 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` 338select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2; 339ERROR 21000: Subquery returns more than 1 row 340create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq)); 341create table t7( uq int primary key, name char(25)); 342insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta"); 343insert into t6 values (1,1),(1,2),(2,2),(1,3); 344select * from t6 where exists (select * from t7 where uq = clinic_uq); 345patient_uq clinic_uq 3461 1 3471 2 3482 2 349explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq); 350id select_type table type possible_keys key key_len ref rows filtered Extra 3511 PRIMARY t6 ALL NULL NULL NULL NULL 4 100.00 Using where 3522 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index 353Warnings: 354Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 355Note 1003 /* select#1 */ select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(/* select#2 */ select 1 from `test`.`t7` where `test`.`t7`.`uq` = `test`.`t6`.`clinic_uq` limit 1)) 356select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); 357ERROR 23000: Column 'a' in field list is ambiguous 358drop table t1,t2,t3; 359CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0'); 360INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b'); 361CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0'); 362INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2'); 363CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00'); 364INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13'); 365SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1); 366a b 367W 1732-02-22 368SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2); 369a b 370W 1 371SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3); 372a b 373W a 374CREATE TABLE `t8` ( 375`pseudo` varchar(35) character set latin1 NOT NULL default '', 376`email` varchar(60) character set latin1 NOT NULL default '', 377PRIMARY KEY (`pseudo`), 378UNIQUE KEY `email` (`email`) 379) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 380INSERT INTO t8 (pseudo,email) VALUES ('joce','test'); 381INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1'); 382INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1'); 383EXPLAIN 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'); 384id select_type table type possible_keys key key_len ref rows filtered Extra 3851 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3864 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3872 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 3883 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 389Warnings: 390Note 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 391SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM 392t8 WHERE pseudo='joce'); 393ERROR HY000: Illegal parameter data types varchar and row for operation '=' 394SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE 395pseudo='joce'); 396ERROR HY000: Illegal parameter data types varchar and row for operation '=' 397SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); 398pseudo 399joce 400SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%'); 401ERROR 21000: Subquery returns more than 1 row 402drop table if exists t1,t2,t3,t4,t5,t6,t7,t8; 403CREATE TABLE `t1` ( 404`topic` mediumint(8) unsigned NOT NULL default '0', 405`date` date NOT NULL default '0000-00-00', 406`pseudo` varchar(35) character set latin1 NOT NULL default '', 407PRIMARY KEY (`pseudo`,`date`,`topic`), 408KEY `topic` (`topic`) 409) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 410INSERT INTO t1 (topic,date,pseudo) VALUES 411('43506','2002-10-02','joce'),('40143','2002-08-03','joce'); 412EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 413id select_type table type possible_keys key key_len ref rows filtered Extra 4141 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 415Warnings: 416Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03' 417EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 418id select_type table type possible_keys key key_len ref rows filtered Extra 4191 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4202 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 421Warnings: 422Note 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')` 423SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 424date 4252002-08-03 426SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 427(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03') 4282002-08-03 429SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1; 4301 4311 4321 4331 434SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1; 435ERROR 21000: Subquery returns more than 1 row 436EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1); 437id select_type table type possible_keys key key_len ref rows filtered Extra 4381 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index 4392 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4403 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 441NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 442Warnings: 443Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 444drop table t1; 445CREATE TABLE `t1` ( 446`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 447`maxnumrep` int(10) unsigned NOT NULL default '0', 448PRIMARY KEY (`numeropost`), 449UNIQUE KEY `maxnumrep` (`maxnumrep`) 450) ENGINE=MyISAM ROW_FORMAT=FIXED; 451INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2); 452CREATE TABLE `t2` ( 453`mot` varchar(30) NOT NULL default '', 454`topic` mediumint(8) unsigned NOT NULL default '0', 455`date` date NOT NULL default '0000-00-00', 456`pseudo` varchar(35) NOT NULL default '', 457PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`) 458) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 459INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce'); 460select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 461a 46240143 463SELECT 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; 464numeropost maxnumrep 46543506 2 46640143 1 467SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b; 468ERROR 42S22: Unknown column 'a' in 'having clause' 469SELECT 1 IN (SELECT 1 FROM t2 HAVING a); 470ERROR 42S22: Unknown column 'a' in 'having clause' 471SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic); 472mot topic date pseudo 473joce 40143 2002-10-22 joce 474joce 43506 2002-10-22 joce 475SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 476mot topic date pseudo 477SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1); 478mot topic date pseudo 479SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic); 480mot topic date pseudo 481joce 40143 2002-10-22 joce 482joce 43506 2002-10-22 joce 483SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 484mot topic date pseudo 485SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1); 486mot topic date pseudo 487SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic); 488mot topic date pseudo 489SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 490mot topic date pseudo 491joce 40143 2002-10-22 joce 492joce 43506 2002-10-22 joce 493SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2; 494mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) 495joce 40143 2002-10-22 joce 1 496joce 43506 2002-10-22 joce 1 497SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2); 498mot topic date pseudo 499SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2); 500mot topic date pseudo 501joce 40143 2002-10-22 joce 502joce 43506 2002-10-22 joce 503SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 504mot topic date pseudo 505joce 40143 2002-10-22 joce 506SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 507mot topic date pseudo 508joce 40143 2002-10-22 joce 509SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 510mot topic date pseudo 511joce 40143 2002-10-22 joce 512SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2; 513mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) 514joce 40143 2002-10-22 joce 1 515joce 43506 2002-10-22 joce 0 516drop table t1,t2; 517CREATE TABLE `t1` ( 518`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 519`maxnumrep` int(10) unsigned NOT NULL default '0', 520PRIMARY KEY (`numeropost`), 521UNIQUE KEY `maxnumrep` (`maxnumrep`) 522) ENGINE=MyISAM ROW_FORMAT=FIXED; 523INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1); 524select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 525ERROR 21000: Subquery returns more than 1 row 526select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1); 527ERROR 21000: Subquery returns more than 1 row 528show warnings; 529Level Code Message 530Error 1242 Subquery returns more than 1 row 531Error 1028 Sort aborted: Subquery returns more than 1 row 532drop table t1; 533create table t1 (a int); 534insert into t1 values (1),(2),(3); 535(select * from t1) union (select * from t1) order by (select a from t1 limit 1); 536a 5371 5382 5393 540drop table t1; 541CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b'); 542INSERT INTO t1 VALUES (); 543SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b'); 544ERROR 21000: Subquery returns more than 1 row 545drop table t1; 546CREATE TABLE `t1` ( 547`numeropost` mediumint(8) unsigned NOT NULL default '0', 548`numreponse` int(10) unsigned NOT NULL auto_increment, 549`pseudo` varchar(35) NOT NULL default '', 550PRIMARY KEY (`numeropost`,`numreponse`), 551UNIQUE KEY `numreponse` (`numreponse`), 552KEY `pseudo` (`pseudo`,`numeropost`) 553) ENGINE=MyISAM; 554SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a; 555ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list) 556SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a; 557ERROR 42S22: Unknown column 'a' in 'having clause' 558SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a; 559numreponse (SELECT numeropost FROM t1 HAVING numreponse=1) 560INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test'); 561EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 562ERROR 21000: Subquery returns more than 1 row 563SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 564ERROR 21000: Subquery returns more than 1 row 565EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'; 566id select_type table type possible_keys key key_len ref rows filtered Extra 5671 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 568Warnings: 569Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1' 570EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'); 571id select_type table type possible_keys key key_len ref rows filtered Extra 5721 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using index 5732 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 574Warnings: 575Note 1003 /* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1 576drop table t1; 577CREATE TABLE t1 (a int(1)); 578INSERT INTO t1 VALUES (1); 579SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1; 5801 5811 582drop table t1; 583create table t1 (a int NOT NULL, b int, primary key (a)); 584create table t2 (a int NOT NULL, b int, primary key (a)); 585insert into t1 values (0, 10),(1, 11),(2, 12); 586insert into t2 values (1, 21),(2, 22),(3, 23); 587select * from t1; 588a b 5890 10 5901 11 5912 12 592update t1 set b= (select b from t2); 593ERROR 21000: Subquery returns more than 1 row 594update t1 set b= (select b from t2 where t1.a = t2.a); 595select * from t1; 596a b 5970 NULL 5981 21 5992 22 600drop table t1, t2; 601create table t1 (a int NOT NULL, b int, primary key (a)); 602create table t2 (a int NOT NULL, b int, primary key (a)); 603insert into t1 values (0, 10),(1, 11),(2, 12); 604insert into t2 values (1, 21),(2, 12),(3, 23); 605select * from t1; 606a b 6070 10 6081 11 6092 12 610select * from t1 where b = (select b from t2 where t1.a = t2.a); 611a b 6122 12 613delete from t1 where b in (select b from t1); 614affected rows: 3 615insert into t1 values (0, 10),(1, 11),(2, 12); 616delete from t1 where b = (select b from t2); 617ERROR 21000: Subquery returns more than 1 row 618delete from t1 where b = (select b from t2 where t1.a = t2.a); 619select * from t1 order by b; 620a b 6210 10 6221 11 623drop table t1, t2; 624create table t11 (a int NOT NULL, b int, primary key (a)); 625create table t12 (a int NOT NULL, b int, primary key (a)); 626create table t2 (a int NOT NULL, b int, primary key (a)); 627insert into t11 values (0, 10),(1, 11),(2, 12); 628insert into t12 values (33, 10),(22, 11),(2, 12); 629insert into t2 values (1, 21),(2, 12),(3, 23); 630select * from t11; 631a b 6320 10 6331 11 6342 12 635select * from t12; 636a b 63733 10 63822 11 6392 12 640delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); 641ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data 642delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2); 643ERROR 21000: Subquery returns more than 1 row 644delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); 645select * from t11; 646a b 6470 10 6481 11 649select * from t12; 650a b 65133 10 65222 11 653drop table t11, t12, t2; 654CREATE TABLE t1 (x int) ENGINE=MyISAM; 655create table t2 (a int) ENGINE=MyISAM; 656create table t3 (b int); 657insert into t2 values (1); 658insert into t3 values (1),(2); 659INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); 660ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data 661INSERT INTO t1 (x) VALUES ((SELECT b FROM t3)); 662ERROR 21000: Subquery returns more than 1 row 663INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); 664select * from t1; 665x 6661 667insert into t2 values (1); 668INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); 669select * from t1; 670x 6711 6722 673INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2; 674select * from t1; 675x 6761 6772 6783 6793 680INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; 681select * from t1; 682x 6831 6842 6853 6863 68711 68811 689INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2)); 690ERROR 42S22: Unknown column 'x' in 'field list' 691INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); 692select * from t1; 693x 6941 6952 6963 6973 69811 69911 7002 701drop table t1, t2, t3; 702CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM; 703create table t2 (a int); 704create table t3 (a int); 705insert into t2 values (1); 706insert into t3 values (1),(2); 707select * from t1; 708x y 709replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2)); 710ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data 711replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2)); 712ERROR 21000: Subquery returns more than 1 row 713replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2)); 714select * from t1; 715x y 7161 2 717replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2)); 718select * from t1; 719x y 7201 3 721replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2)); 722select * from t1; 723x y 7241 3 7254 1 726replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2)); 727select * from t1; 728x y 7291 3 7304 2 731replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2)); 732select * from t1; 733x y 7341 3 7354 2 7362 1 737drop table t1, t2, t3; 738SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *); 739ERROR HY000: No tables used 740CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; 741INSERT INTO t2 VALUES (1),(2); 742SELECT * FROM t2 WHERE id IN (SELECT 1); 743id 7441 745EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1); 746id select_type table type possible_keys key key_len ref rows filtered Extra 7471 SIMPLE t2 ref id id 5 const 1 100.00 Using index 748Warnings: 749Note 1249 Select 2 was reduced during optimization 750Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1 751SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); 752id 7531 754SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); 755id 7562 757EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); 758id select_type table type possible_keys key key_len ref rows filtered Extra 7591 SIMPLE t2 ref id id 5 const 1 100.00 Using where; Using index 760Warnings: 761Note 1249 Select 3 was reduced during optimization 762Note 1249 Select 2 was reduced during optimization 763Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1) 764EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); 765id select_type table type possible_keys key key_len ref rows filtered Extra 7661 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index 7672 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 7683 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 769NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 770Warnings: 771Note 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)))) 772SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3); 773id 774SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2); 775id 7762 777INSERT INTO t2 VALUES ((SELECT * FROM t2)); 778ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data 779INSERT INTO t2 VALUES ((SELECT id FROM t2)); 780ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data 781SELECT * FROM t2; 782id 7831 7842 785CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; 786INSERT INTO t1 values (1),(1); 787UPDATE t2 SET id=(SELECT * FROM t1); 788ERROR 21000: Subquery returns more than 1 row 789drop table t2, t1; 790create table t1 (a int); 791insert into t1 values (1),(2),(3); 792select 1 IN (SELECT * from t1); 7931 IN (SELECT * from t1) 7941 795select 10 IN (SELECT * from t1); 79610 IN (SELECT * from t1) 7970 798select NULL IN (SELECT * from t1); 799NULL IN (SELECT * from t1) 800NULL 801update t1 set a=NULL where a=2; 802select 1 IN (SELECT * from t1); 8031 IN (SELECT * from t1) 8041 805select 3 IN (SELECT * from t1); 8063 IN (SELECT * from t1) 8071 808select 10 IN (SELECT * from t1); 80910 IN (SELECT * from t1) 810NULL 811select 1 > ALL (SELECT * from t1); 8121 > ALL (SELECT * from t1) 8130 814select 10 > ALL (SELECT * from t1); 81510 > ALL (SELECT * from t1) 816NULL 817select 1 > ANY (SELECT * from t1); 8181 > ANY (SELECT * from t1) 819NULL 820select 10 > ANY (SELECT * from t1); 82110 > ANY (SELECT * from t1) 8221 823drop table t1; 824create table t1 (a varchar(20)); 825insert into t1 values ('A'),('BC'),('DEF'); 826select 'A' IN (SELECT * from t1); 827'A' IN (SELECT * from t1) 8281 829select 'XYZS' IN (SELECT * from t1); 830'XYZS' IN (SELECT * from t1) 8310 832select NULL IN (SELECT * from t1); 833NULL IN (SELECT * from t1) 834NULL 835update t1 set a=NULL where a='BC'; 836select 'A' IN (SELECT * from t1); 837'A' IN (SELECT * from t1) 8381 839select 'DEF' IN (SELECT * from t1); 840'DEF' IN (SELECT * from t1) 8411 842select 'XYZS' IN (SELECT * from t1); 843'XYZS' IN (SELECT * from t1) 844NULL 845select 'A' > ALL (SELECT * from t1); 846'A' > ALL (SELECT * from t1) 8470 848select 'XYZS' > ALL (SELECT * from t1); 849'XYZS' > ALL (SELECT * from t1) 850NULL 851select 'A' > ANY (SELECT * from t1); 852'A' > ANY (SELECT * from t1) 853NULL 854select 'XYZS' > ANY (SELECT * from t1); 855'XYZS' > ANY (SELECT * from t1) 8561 857drop table t1; 858create table t1 (a float); 859insert into t1 values (1.5),(2.5),(3.5); 860select 1.5 IN (SELECT * from t1); 8611.5 IN (SELECT * from t1) 8621 863select 10.5 IN (SELECT * from t1); 86410.5 IN (SELECT * from t1) 8650 866select NULL IN (SELECT * from t1); 867NULL IN (SELECT * from t1) 868NULL 869update t1 set a=NULL where a=2.5; 870select 1.5 IN (SELECT * from t1); 8711.5 IN (SELECT * from t1) 8721 873select 3.5 IN (SELECT * from t1); 8743.5 IN (SELECT * from t1) 8751 876select 10.5 IN (SELECT * from t1); 87710.5 IN (SELECT * from t1) 878NULL 879select 1.5 > ALL (SELECT * from t1); 8801.5 > ALL (SELECT * from t1) 8810 882select 10.5 > ALL (SELECT * from t1); 88310.5 > ALL (SELECT * from t1) 884NULL 885select 1.5 > ANY (SELECT * from t1); 8861.5 > ANY (SELECT * from t1) 887NULL 888select 10.5 > ANY (SELECT * from t1); 88910.5 > ANY (SELECT * from t1) 8901 891explain extended select (select a+1) from t1; 892id select_type table type possible_keys key key_len ref rows filtered Extra 8931 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 894Warnings: 895Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 896Note 1249 Select 2 was reduced during optimization 897Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1` 898select (select a+1) from t1; 899(select a+1) 9002.5 901NULL 9024.5 903drop table t1; 904CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a)); 905CREATE TABLE t2 (a int(11) default '0', INDEX (a)); 906INSERT INTO t1 VALUES (1),(2),(3),(4); 907INSERT INTO t2 VALUES (1),(2),(3); 908SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; 909a t1.a in (select t2.a from t2) 9101 1 9112 1 9123 1 9134 0 914explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; 915id select_type table type possible_keys key key_len ref rows filtered Extra 9161 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index 9172 SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index 918Warnings: 919Note 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` 920CREATE TABLE t3 (a int(11) default '0'); 921INSERT INTO t3 VALUES (1),(2),(3); 922SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; 923a t1.a in (select t2.a from t2,t3 where t3.a=t2.a) 9241 1 9252 1 9263 1 9274 0 928explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; 929id select_type table type possible_keys key key_len ref rows filtered Extra 9301 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index 9312 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index 9322 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) 933Warnings: 934Note 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` 935drop table t1,t2,t3; 936# check correct NULL Processing for normal IN/ALL/ANY 937# and 2 ways of max/min optimization 938create table t1 (a int); 939insert into t1 values (1), (100), (NULL), (1000); 940create table t2 (a int not null); 941# subselect returns empty set (for NULL and non-NULL left part) 942select a, a in (select * from t2) from t1; 943a a in (select * from t2) 9441 0 945100 0 946NULL 0 9471000 0 948select a, a > any (select * from t2) from t1; 949a a > any (select * from t2) 9501 0 951100 0 952NULL 0 9531000 0 954select a, a > all (select * from t2) from t1; 955a a > all (select * from t2) 9561 1 957100 1 958NULL 1 9591000 1 960select a from t1 where a in (select * from t2); 961a 962select a from t1 where a > any (select * from t2); 963a 964select a from t1 where a > all (select * from t2); 965a 9661 967100 968NULL 9691000 970select a from t1 where a in (select * from t2 group by a); 971a 972select a from t1 where a > any (select * from t2 group by a); 973a 974select a from t1 where a > all (select * from t2 group by a); 975a 9761 977100 978NULL 9791000 980insert into t2 values (1),(200); 981# sebselect returns non-empty set without NULLs 982select a, a in (select * from t2) from t1; 983a a in (select * from t2) 9841 1 985100 0 986NULL NULL 9871000 0 988select a, a > any (select * from t2) from t1; 989a a > any (select * from t2) 9901 0 991100 1 992NULL NULL 9931000 1 994select a, a > all (select * from t2) from t1; 995a a > all (select * from t2) 9961 0 997100 0 998NULL NULL 9991000 1 1000select a from t1 where a in (select * from t2); 1001a 10021 1003select a from t1 where a > any (select * from t2); 1004a 1005100 10061000 1007select a from t1 where a > all (select * from t2); 1008a 10091000 1010select a from t1 where a in (select * from t2 group by a); 1011a 10121 1013select a from t1 where a > any (select * from t2 group by a); 1014a 1015100 10161000 1017select a from t1 where a > all (select * from t2 group by a); 1018a 10191000 1020drop table t2; 1021create table t2 (a int); 1022insert into t2 values (1),(NULL),(200); 1023# sebselect returns non-empty set with NULLs 1024select a, a in (select * from t2) from t1; 1025a a in (select * from t2) 10261 1 1027100 NULL 1028NULL NULL 10291000 NULL 1030select a, a > any (select * from t2) from t1; 1031a a > any (select * from t2) 10321 NULL 1033100 1 1034NULL NULL 10351000 1 1036select a, a > all (select * from t2) from t1; 1037a a > all (select * from t2) 10381 0 1039100 0 1040NULL NULL 10411000 NULL 1042select a from t1 where a in (select * from t2); 1043a 10441 1045select a from t1 where a > any (select * from t2); 1046a 1047100 10481000 1049select a from t1 where a > all (select * from t2); 1050a 1051select a from t1 where a in (select * from t2 group by a); 1052a 10531 1054select a from t1 where a > any (select * from t2 group by a); 1055a 1056100 10571000 1058select a from t1 where a > all (select * from t2 group by a); 1059a 1060drop table t1, t2; 1061create table t1 (a float); 1062select 10.5 IN (SELECT * from t1 LIMIT 1); 1063ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1064select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5); 1065ERROR 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 1066select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1); 1067ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1068drop table t1; 1069create table t1 (a int, b int, c varchar(10)); 1070create table t2 (a int); 1071insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c'); 1072insert into t2 values (1),(2),(NULL); 1073select 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; 1074a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a) 10751 1 a 10762 0 b 1077NULL NULL NULL 1078select 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; 1079a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a) 10801 0 a 10812 1 b 1082NULL NULL NULL 1083select 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; 1084a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a) 10851 0 a 10862 0 b 1087NULL NULL NULL 1088drop table t1,t2; 1089create table t1 (a int, b real, c varchar(10)); 1090insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b'); 1091select ROW(1, 1, 'a') IN (select a,b,c from t1); 1092ROW(1, 1, 'a') IN (select a,b,c from t1) 10931 1094select ROW(1, 2, 'a') IN (select a,b,c from t1); 1095ROW(1, 2, 'a') IN (select a,b,c from t1) 10960 1097select ROW(1, 1, 'a') IN (select b,a,c from t1); 1098ROW(1, 1, 'a') IN (select b,a,c from t1) 10991 1100select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null); 1101ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null) 11021 1103select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null); 1104ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null) 11050 1106select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null); 1107ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null) 11081 1109select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 1110ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a') 11111 1112select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 1113ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a') 11140 1115select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a'); 1116ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a') 11171 1118select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2); 1119ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1120drop table t1; 1121create table t1 (a int); 1122insert into t1 values (1); 1123do @a:=(SELECT a from t1); 1124select @a; 1125@a 11261 1127set @a:=2; 1128set @a:=(SELECT a from t1); 1129select @a; 1130@a 11311 1132drop table t1; 1133do (SELECT a from t1); 1134ERROR 42S02: Table 'test.t1' doesn't exist 1135set @a:=(SELECT a from t1); 1136ERROR 42S02: Table 'test.t1' doesn't exist 1137CREATE TABLE t1 (a int, KEY(a)); 1138HANDLER t1 OPEN; 1139HANDLER t1 READ a=((SELECT 1)); 1140ERROR 42000: HANDLER..READ does not support subqueries or stored functions 1141HANDLER t1 CLOSE; 1142drop table t1; 1143create table t1 (a int); 1144create table t2 (b int); 1145insert into t1 values (1),(2); 1146insert into t2 values (1); 1147select a from t1 where a in (select a from t1 where a in (select b from t2)); 1148a 11491 1150drop table t1, t2; 1151create table t1 (a int, b int); 1152create table t2 like t1; 1153insert into t1 values (1,2),(1,3),(1,4),(1,5); 1154insert into t2 values (1,2),(1,3); 1155select * from t1 where row(a,b) in (select a,b from t2); 1156a b 11571 2 11581 3 1159drop table t1, t2; 1160CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; 1161INSERT INTO t1 VALUES (1); 1162UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); 1163select * from t1; 1164i 11652 1166drop table t1; 1167CREATE TABLE t1 (a int(1)); 1168EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1; 1169id select_type table type possible_keys key key_len ref rows filtered Extra 11701 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 11712 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 1172Warnings: 1173Note 1003 /* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1` 1174EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1; 1175id select_type table type possible_keys key key_len ref rows filtered Extra 11761 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 11772 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 1178Warnings: 1179Note 1003 /* select#1 */ select (/* select#2 */ select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1` 1180EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1; 1181id select_type table type possible_keys key key_len ref rows filtered Extra 11821 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 11832 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 1184Warnings: 1185Note 1003 /* select#1 */ select (/* select#2 */ select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1` 1186drop table t1; 1187CREATE TABLE `t1` ( 1188`mot` varchar(30) character set latin1 NOT NULL default '', 1189`topic` mediumint(8) unsigned NOT NULL default '0', 1190`date` date NOT NULL default '0000-00-00', 1191`pseudo` varchar(35) character set latin1 NOT NULL default '', 1192PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), 1193KEY `pseudo` (`pseudo`,`date`,`topic`), 1194KEY `topic` (`topic`) 1195) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 1196CREATE TABLE `t2` ( 1197`mot` varchar(30) character set latin1 NOT NULL default '', 1198`topic` mediumint(8) unsigned NOT NULL default '0', 1199`date` date NOT NULL default '0000-00-00', 1200`pseudo` varchar(35) character set latin1 NOT NULL default '', 1201PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), 1202KEY `pseudo` (`pseudo`,`date`,`topic`), 1203KEY `topic` (`topic`) 1204) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 1205CREATE TABLE `t3` ( 1206`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 1207`maxnumrep` int(10) unsigned NOT NULL default '0', 1208PRIMARY KEY (`numeropost`), 1209UNIQUE KEY `maxnumrep` (`maxnumrep`) 1210) ENGINE=MyISAM CHARSET=latin1; 1211INSERT IGNORE INTO t1 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 IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test'); 1216Warnings: 1217Warning 1265 Data truncated for column 'date' at row 1 1218Warning 1265 Data truncated for column 'date' at row 2 1219INSERT INTO t3 VALUES (1,1); 1220SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE 1221numeropost=topic); 1222topic 12232 1224select * from t1; 1225mot topic date pseudo 1226joce 1 0000-00-00 joce 1227test 2 0000-00-00 test 1228DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT 1229EXISTS(SELECT * FROM t3 WHERE numeropost=topic)); 1230select * from t1; 1231mot topic date pseudo 1232joce 1 0000-00-00 joce 1233drop table t1, t2, t3; 1234SELECT * FROM (SELECT 1 as a,(SELECT a)) a; 1235a (SELECT a) 12361 1 1237CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a; 1238SHOW CREATE TABLE t1; 1239Table Create Table 1240t1 CREATE TABLE `t1` ( 1241 `a` int(1) NOT NULL DEFAULT 0, 1242 `(SELECT 1)` int(1) NOT NULL DEFAULT 0 1243) ENGINE=MyISAM DEFAULT CHARSET=latin1 1244drop table t1; 1245CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a; 1246SHOW CREATE TABLE t1; 1247Table Create Table 1248t1 CREATE TABLE `t1` ( 1249 `a` int(1) NOT NULL DEFAULT 0, 1250 `(SELECT a)` int(1) NOT NULL DEFAULT 0 1251) ENGINE=MyISAM DEFAULT CHARSET=latin1 1252drop table t1; 1253CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; 1254SHOW CREATE TABLE t1; 1255Table Create Table 1256t1 CREATE TABLE `t1` ( 1257 `a` int(1) NOT NULL DEFAULT 0, 1258 `(SELECT a+0)` int(3) NOT NULL DEFAULT 0 1259) ENGINE=MyISAM DEFAULT CHARSET=latin1 1260drop table t1; 1261CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a; 1262select * from t1; 1263a 12642 1265SHOW CREATE TABLE t1; 1266Table Create Table 1267t1 CREATE TABLE `t1` ( 1268 `a` int(3) NOT NULL 1269) ENGINE=MyISAM DEFAULT CHARSET=latin1 1270drop table t1; 1271create table t1 (a int); 1272insert into t1 values (1), (2), (3); 1273explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1) 1274from t1; 1275id select_type table type possible_keys key key_len ref rows filtered Extra 12761 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 12772 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 12783 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 1279Warnings: 1280Note 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` 1281drop table t1; 1282select 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); 1283ERROR 42S02: Table 'test.t1' doesn't exist 1284CREATE TABLE t1 ( 1285ID int(11) NOT NULL auto_increment, 1286name char(35) NOT NULL default '', 1287t2 char(3) NOT NULL default '', 1288District char(20) NOT NULL default '', 1289Population int(11) NOT NULL default '0', 1290PRIMARY KEY (ID) 1291) ENGINE=MyISAM; 1292INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207); 1293INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329); 1294INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117); 1295CREATE TABLE t2 ( 1296Code char(3) NOT NULL default '', 1297Name char(52) NOT NULL default '', 1298Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', 1299Region char(26) NOT NULL default '', 1300SurfaceArea float(10,2) NOT NULL default '0.00', 1301IndepYear smallint(6) default NULL, 1302Population int(11) NOT NULL default '0', 1303LifeExpectancy float(3,1) default NULL, 1304GNP float(10,2) default NULL, 1305GNPOld float(10,2) default NULL, 1306LocalName char(45) NOT NULL default '', 1307GovernmentForm char(45) NOT NULL default '', 1308HeadOfState char(60) default NULL, 1309Capital int(11) default NULL, 1310Code2 char(2) NOT NULL default '', 1311PRIMARY KEY (Code) 1312) ENGINE=MyISAM; 1313INSERT 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'); 1314INSERT 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'); 1315select 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); 1316Continent Name Population 1317Oceania Sydney 3276207 1318drop table t1, t2; 1319CREATE TABLE `t1` ( 1320`id` mediumint(8) unsigned NOT NULL auto_increment, 1321`pseudo` varchar(35) character set latin1 NOT NULL default '', 1322PRIMARY KEY (`id`), 1323UNIQUE KEY `pseudo` (`pseudo`) 1324) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC; 1325INSERT INTO t1 (pseudo) VALUES ('test'); 1326SELECT 0 IN (SELECT 1 FROM t1 a); 13270 IN (SELECT 1 FROM t1 a) 13280 1329EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); 1330id select_type table type possible_keys key key_len ref rows filtered Extra 13311 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 13322 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1333Warnings: 1334Note 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)` 1335INSERT INTO t1 (pseudo) VALUES ('test1'); 1336SELECT 0 IN (SELECT 1 FROM t1 a); 13370 IN (SELECT 1 FROM t1 a) 13380 1339EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); 1340id select_type table type possible_keys key key_len ref rows filtered Extra 13411 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 13422 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1343Warnings: 1344Note 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)` 1345drop table t1; 1346CREATE TABLE `t1` ( 1347`i` int(11) NOT NULL default '0', 1348PRIMARY KEY (`i`) 1349) ENGINE=MyISAM CHARSET=latin1; 1350INSERT INTO t1 VALUES (1); 1351UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); 1352UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); 1353UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); 1354ERROR 42S22: Unknown column 't.i' in 'field list' 1355select * from t1; 1356i 13573 1358drop table t1; 1359CREATE TABLE t1 ( 1360id int(11) default NULL 1361) ENGINE=MyISAM CHARSET=latin1; 1362INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3); 1363CREATE TABLE t2 ( 1364id int(11) default NULL, 1365name varchar(15) default NULL 1366) ENGINE=MyISAM CHARSET=latin1; 1367INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita'); 1368update t1, t2 set t2.name='lenka' where t2.id in (select id from t1); 1369select * from t2; 1370id name 13714 vita 13721 lenka 13732 lenka 13741 lenka 1375drop table t1,t2; 1376create table t1 (a int, unique index indexa (a)); 1377insert into t1 values (-1), (-4), (-2), (NULL); 1378select -10 IN (select a from t1 FORCE INDEX (indexa)); 1379-10 IN (select a from t1 FORCE INDEX (indexa)) 1380NULL 1381drop table t1; 1382create table t1 (id int not null auto_increment primary key, salary int, key(salary)); 1383insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000); 1384explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1); 1385id select_type table type possible_keys key key_len ref rows filtered Extra 13861 PRIMARY t1 ref salary salary 5 const 1 100.00 Using where 13872 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 1388Warnings: 1389Note 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`) 1390drop table t1; 1391CREATE TABLE t1 ( 1392ID int(10) unsigned NOT NULL auto_increment, 1393SUB_ID int(3) unsigned NOT NULL default '0', 1394REF_ID int(10) unsigned default NULL, 1395REF_SUB int(3) unsigned default '0', 1396PRIMARY KEY (ID,SUB_ID), 1397UNIQUE KEY t1_PK (ID,SUB_ID), 1398KEY t1_FK (REF_ID,REF_SUB), 1399KEY t1_REFID (REF_ID) 1400) ENGINE=MyISAM CHARSET=cp1251; 1401INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL); 1402SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2); 1403REF_ID 1404DROP TABLE t1; 1405create table t1 (a int, b int); 1406create table t2 (a int, b int); 1407insert into t1 values (1,0), (2,0), (3,0); 1408insert into t2 values (1,1), (2,1), (3,1), (2,2); 1409update ignore t1 set b=(select b from t2 where t1.a=t2.a); 1410Warnings: 1411Warning 1242 Subquery returns more than 1 row 1412select * from t1; 1413a b 14141 1 14152 NULL 14163 1 1417drop table t1, t2; 1418CREATE TABLE `t1` ( 1419`id` mediumint(8) unsigned NOT NULL auto_increment, 1420`pseudo` varchar(35) NOT NULL default '', 1421`email` varchar(60) NOT NULL default '', 1422PRIMARY KEY (`id`), 1423UNIQUE KEY `email` (`email`), 1424UNIQUE KEY `pseudo` (`pseudo`) 1425) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC; 1426INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1'); 1427SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1); 1428a b 1429test test 1430test1 test1 1431drop table if exists t1; 1432(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0); 1433a 14341 1435create table t1 (a int not null, b int, primary key (a)); 1436create table t2 (a int not null, primary key (a)); 1437create table t3 (a int not null, b int, primary key (a)); 1438insert into t1 values (1,10), (2,20), (3,30), (4,40); 1439insert into t2 values (2), (3), (4), (5); 1440insert into t3 values (10,3), (20,4), (30,5); 1441select * from t2 where t2.a in (select a from t1); 1442a 14432 14443 14454 1446explain extended select * from t2 where t2.a in (select a from t1); 1447id select_type table type possible_keys key key_len ref rows filtered Extra 14481 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14491 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 Using index 1450Warnings: 1451Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a` 1452select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1453a 14542 14554 1456explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1457id select_type table type possible_keys key key_len ref rows filtered Extra 14581 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14591 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 Using where 1460Warnings: 1461Note 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 1462select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1463a 14642 14653 1466explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1467id select_type table type possible_keys key key_len ref rows filtered Extra 14681 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14691 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 14701 PRIMARY t3 index PRIMARY PRIMARY 4 NULL 3 100.00 Using where; Using index; Using join buffer (flat, BNL join) 1471Warnings: 1472Note 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` 1473drop table t1, t2, t3; 1474create table t1 (a int, b int, index a (a,b)); 1475create table t2 (a int, index a (a)); 1476create table t3 (a int, b int, index a (a)); 1477insert into t1 values (1,10), (2,20), (3,30), (4,40); 1478create table t0(a int); 1479insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 1480insert into t1 1481select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D; 1482insert into t2 values (2), (3), (4), (5); 1483insert into t3 values (10,3), (20,4), (30,5); 1484select * from t2 where t2.a in (select a from t1); 1485a 14862 14873 14884 1489explain extended select * from t2 where t2.a in (select a from t1); 1490id select_type table type possible_keys key key_len ref rows filtered Extra 14911 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 14921 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using index; FirstMatch(t2) 1493Warnings: 1494Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a` 1495select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1496a 14972 14984 1499explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1500id select_type table type possible_keys key key_len ref rows filtered Extra 15011 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 15021 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) 1503Warnings: 1504Note 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 1505select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1506a 15072 15083 1509explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1510id select_type table type possible_keys key key_len ref rows filtered Extra 15111 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 15121 PRIMARY t3 index a a 5 NULL 3 100.00 Using where; Using index 15131 PRIMARY t1 ref a a 10 test.t2.a,test.t3.a 116 100.00 Using index; FirstMatch(t2) 1514Warnings: 1515Note 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` 1516insert into t1 values (3,31); 1517select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1518a 15192 15203 15214 1522select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31); 1523a 15242 15254 1526explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1527id select_type table type possible_keys key key_len ref rows filtered Extra 15281 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 15291 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) 1530Warnings: 1531Note 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 1532drop table t0, t1, t2, t3; 1533create table t1 (a int, b int); 1534create table t2 (a int, b int); 1535create table t3 (a int, b int); 1536insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10); 1537insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1); 1538insert into t3 values (3,3), (2,2), (1,1); 1539select 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; 1540a (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) 15413 1 15422 2 15431 2 1544drop table t1,t2,t3; 1545create table t1 (s1 int); 1546create table t2 (s1 int); 1547insert into t1 values (1); 1548insert into t2 values (1); 1549select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1); 1550s1 15511 1552drop table t1,t2; 1553create table t1 (s1 int); 1554create table t2 (s1 int); 1555insert into t1 values (1); 1556insert into t2 values (1); 1557update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); 1558ERROR 42S22: Unknown column 'x.s1' in 'field list' 1559DROP TABLE t1, t2; 1560CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci, 1561s2 CHAR(5) COLLATE latin1_swedish_ci); 1562INSERT INTO t1 VALUES ('z','?'); 1563select * from t1 where s1 > (select max(s2) from t1); 1564ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>' 1565select * from t1 where s1 > any (select max(s2) from t1); 1566ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<' 1567drop table t1; 1568create table t1(toid int,rd int); 1569create table t2(userid int,pmnew int,pmtotal int); 1570insert into t2 values(1,0,0),(2,0,0); 1571insert 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); 1572select 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); 1573userid pmtotal pmnew calc_total calc_new 15741 0 0 9 3 15752 0 0 4 2 1576drop table t1, t2; 1577create table t1 (s1 char(5)); 1578select (select 'a','b' from t1 union select 'a','b' from t1) from t1; 1579ERROR 21000: Operand should contain 1 column(s) 1580insert into t1 values ('tttt'); 1581select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1); 1582s1 1583tttt 1584explain extended (select * from t1); 1585id select_type table type possible_keys key key_len ref rows filtered Extra 15861 SIMPLE t1 system NULL NULL NULL NULL 1 100.00 1587Warnings: 1588Note 1003 (select 'tttt' AS `s1` from dual) 1589(select * from t1); 1590s1 1591tttt 1592drop table t1; 1593create table t1 (s1 char(5), index s1(s1)); 1594create table t2 (s1 char(5), index s1(s1)); 1595insert into t1 values ('a1'),('a2'),('a3'); 1596insert into t2 values ('a1'),('a2'); 1597select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; 1598s1 s1 NOT IN (SELECT s1 FROM t2) 1599a1 0 1600a2 0 1601a3 1 1602select s1, s1 = ANY (SELECT s1 FROM t2) from t1; 1603s1 s1 = ANY (SELECT s1 FROM t2) 1604a1 1 1605a2 1 1606a3 0 1607select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; 1608s1 s1 <> ALL (SELECT s1 FROM t2) 1609a1 0 1610a2 0 1611a3 1 1612select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; 1613s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') 1614a1 0 1615a2 1 1616a3 1 1617explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; 1618id select_type table type possible_keys key key_len ref rows filtered Extra 16191 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16202 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1621Warnings: 1622Note 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` 1623explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1; 1624id select_type table type possible_keys key key_len ref rows filtered Extra 16251 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16262 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1627Warnings: 1628Note 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` 1629explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; 1630id select_type table type possible_keys key key_len ref rows filtered Extra 16311 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16322 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1633Warnings: 1634Note 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` 1635explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; 1636id select_type table type possible_keys key key_len ref rows filtered Extra 16371 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16382 SUBQUERY t2 index_subquery s1 s1 6 func 2 50.00 Using index; Using where; Full scan on NULL key 1639Warnings: 1640Note 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` 1641drop table t1,t2; 1642create table t2 (a int, b int not null); 1643create table t3 (a int); 1644insert into t3 values (6),(7),(3); 1645select * from t3 where a >= all (select b from t2); 1646a 16476 16487 16493 1650explain extended select * from t3 where a >= all (select b from t2); 1651id select_type table type possible_keys key key_len ref rows filtered Extra 16521 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16532 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1654Warnings: 1655Note 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`))) 1656select * from t3 where a >= some (select b from t2); 1657a 1658explain extended select * from t3 where a >= some (select b from t2); 1659id select_type table type possible_keys key key_len ref rows filtered Extra 16601 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16612 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1662Warnings: 1663Note 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`))) 1664select * from t3 where a >= all (select b from t2 group by 1); 1665a 16666 16677 16683 1669explain extended select * from t3 where a >= all (select b from t2 group by 1); 1670id select_type table type possible_keys key key_len ref rows filtered Extra 16711 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16722 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1673Warnings: 1674Note 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`))) 1675select * from t3 where a >= some (select b from t2 group by 1); 1676a 1677explain extended select * from t3 where a >= some (select b from t2 group by 1); 1678id select_type table type possible_keys key key_len ref rows filtered Extra 16791 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16802 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1681Warnings: 1682Note 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`))) 1683select * from t3 where NULL >= any (select b from t2); 1684a 1685explain extended select * from t3 where NULL >= any (select b from t2); 1686id select_type table type possible_keys key key_len ref rows filtered Extra 16871 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 16882 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1689Warnings: 1690Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1691select * from t3 where NULL >= any (select b from t2 group by 1); 1692a 1693explain extended select * from t3 where NULL >= any (select b from t2 group by 1); 1694id select_type table type possible_keys key key_len ref rows filtered Extra 16951 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 16962 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1697Warnings: 1698Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1699select * from t3 where NULL >= some (select b from t2); 1700a 1701explain extended select * from t3 where NULL >= some (select b from t2); 1702id select_type table type possible_keys key key_len ref rows filtered Extra 17031 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 17042 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1705Warnings: 1706Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1707select * from t3 where NULL >= some (select b from t2 group by 1); 1708a 1709explain extended select * from t3 where NULL >= some (select b from t2 group by 1); 1710id select_type table type possible_keys key key_len ref rows filtered Extra 17111 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 17122 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1713Warnings: 1714Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1715insert into t2 values (2,2), (2,1), (3,3), (3,1); 1716select * from t3 where a > all (select max(b) from t2 group by a); 1717a 17186 17197 1720explain extended select * from t3 where a > all (select max(b) from t2 group by a); 1721id select_type table type possible_keys key key_len ref rows filtered Extra 17221 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 17232 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary 1724Warnings: 1725Note 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`))) 1726drop table t2, t3; 1727CREATE 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 ; 1728INSERT 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()); 1729CREATE 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; 1730INSERT 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); 1731CREATE 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 ; 1732INSERT 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); 1733CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; 1734INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status'); 1735select 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; 1736dbid 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') 1737-1 Valid 1 1738-1 Valid 2 1 1739-1 Should Not Return 0 1740SELECT 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; 1741dbid name 1742-1 Valid 1743-1 Valid 2 1744drop table t1,t2,t3,t4; 1745CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; 1746INSERT INTO t1 VALUES (1),(5); 1747CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; 1748INSERT INTO t2 VALUES (2),(6); 1749select * from t1 where (1,2,6) in (select * from t2); 1750ERROR 21000: Operand should contain 3 column(s) 1751DROP TABLE t1,t2; 1752create table t1 (s1 int); 1753insert into t1 values (1); 1754insert into t1 values (2); 1755set sort_buffer_size = (select s1 from t1); 1756ERROR 21000: Subquery returns more than 1 row 1757do (select * from t1); 1758Warnings: 1759Error 1242 Subquery returns more than 1 row 1760drop table t1; 1761create table t1 (s1 char); 1762insert into t1 values ('e'); 1763select * from t1 where 'f' > any (select s1 from t1); 1764s1 1765e 1766select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); 1767s1 1768e 1769explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); 1770id select_type table type possible_keys key key_len ref rows filtered Extra 17711 PRIMARY t1 system NULL NULL NULL NULL 1 100.00 17722 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 17733 UNION t1 system NULL NULL NULL NULL 1 100.00 1774NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 1775Warnings: 1776Note 1003 /* select#1 */ select 'e' AS `s1` from dual where 1 1777drop table t1; 1778CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; 1779INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874'); 1780CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1; 1781INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6'); 1782select 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; 1783phone code 178469294728265 6 178518621828126 1862 178689356874041 NULL 178795895001874 NULL 1788drop table t1, t2; 1789create table t1 (s1 int); 1790create table t2 (s1 int); 1791select * from t1 where (select count(*) from t2 where t1.s2) = 1; 1792ERROR 42S22: Unknown column 't1.s2' in 'where clause' 1793select * from t1 where (select count(*) from t2 group by t1.s2) = 1; 1794ERROR 42S22: Unknown column 't1.s2' in 'group statement' 1795select count(*) from t2 group by t1.s2; 1796ERROR 42S22: Unknown column 't1.s2' in 'group statement' 1797drop table t1, t2; 1798CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB)); 1799CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA)); 1800INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365'); 1801INSERT INTO t2 VALUES (100, 200, 'C'); 1802SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1); 1803COLC 1804DROP TABLE t1, t2; 1805CREATE TABLE t1 (a int(1)); 1806INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5); 1807SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100; 1808(SELECT a) 18091 18102 18113 18124 18135 1814DROP TABLE t1; 1815create table t1 (a int, b decimal(13, 3)); 1816insert into t1 values (1, 0.123); 1817select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1; 1818delete from t1; 1819load data infile "../../tmp/subselect.out.file.1" into table t1; 1820select * from t1; 1821a b 18221 0.123 1823drop table t1; 1824CREATE TABLE `t1` ( 1825`id` int(11) NOT NULL auto_increment, 1826`id_cns` tinyint(3) unsigned NOT NULL default '0', 1827`tipo` enum('','UNO','DUE') NOT NULL default '', 1828`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000', 1829`particolare` mediumint(8) unsigned NOT NULL default '0', 1830`generale` mediumint(8) unsigned NOT NULL default '0', 1831`bis` tinyint(3) unsigned NOT NULL default '0', 1832PRIMARY KEY (`id`), 1833UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`), 1834UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`) 1835); 1836INSERT 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); 1837CREATE TABLE `t2` ( 1838`id` tinyint(3) unsigned NOT NULL auto_increment, 1839`max_anno_dep` smallint(6) unsigned NOT NULL default '0', 1840PRIMARY KEY (`id`) 1841); 1842INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990); 1843SELECT 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; 1844id max_anno_dep PIPPO 184516 1987 1 184650 1990 0 184751 1990 NULL 1848DROP TABLE t1, t2; 1849create table t1 (a int); 1850insert into t1 values (1), (2), (3); 1851SET SQL_SELECT_LIMIT=1; 1852select sum(a) from (select * from t1) as a; 1853sum(a) 18546 1855select 2 in (select * from t1); 18562 in (select * from t1) 18571 1858SET SQL_SELECT_LIMIT=default; 1859drop table t1; 1860CREATE TABLE t1 (a int, b int, INDEX (a)); 1861INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3); 1862SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b; 1863a b 18641 1 18651 2 18661 3 1867DROP TABLE t1; 1868create table t1(val varchar(10)); 1869insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp'); 1870select 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%'); 1871count(*) 18720 1873drop table t1; 1874create table t1 (id int not null, text varchar(20) not null default '', primary key (id)); 1875insert 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'); 1876select * from t1 where id not in (select id from t1 where id < 8); 1877id text 18788 text8 18799 text9 188010 text10 188111 text11 188212 text12 1883select * 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); 1884id text 18858 text8 18869 text9 188710 text10 188811 text11 188912 text12 1890explain extended select * from t1 where id not in (select id from t1 where id < 8); 1891id select_type table type possible_keys key key_len ref rows filtered Extra 18921 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where 18932 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where 1894Warnings: 1895Note 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`)))) 1896explain 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); 1897id select_type table type possible_keys key key_len ref rows filtered Extra 18981 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where 18992 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index 1900Warnings: 1901Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 1902Note 1003 /* select#1 */ select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where !<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)) 1903insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); 1904create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); 1905insert 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'); 1906select * 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); 1907id text id text id text 19081 text1 1 text1 1 text1 19092 text2 2 text2 2 text2 19103 text3 3 text3 3 text3 19114 text4 4 text4 4 text4 19125 text5 5 text5 5 text5 19136 text6 6 text6 6 text6 19147 text7 7 text7 7 text7 19158 text8 8 text8 8 text8 19169 text9 9 text9 9 text9 191710 text10 10 text10 10 text10 191811 text11 11 text1 11 text11 191912 text12 12 text2 12 text12 19201000 text1000 NULL NULL 1000 text1000 19211001 text1001 NULL NULL 1000 text1000 1922explain 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); 1923id select_type table type possible_keys key key_len ref rows filtered Extra 19241 SIMPLE a ALL NULL NULL NULL NULL 14 100.00 19251 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00 19261 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition 1927Warnings: 1928Note 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` 1929drop table t1,t2; 1930create table t1 (a int); 1931insert into t1 values (1); 1932explain select benchmark(1000, (select a from t1 where a=sha(rand()))); 1933id select_type table type possible_keys key key_len ref rows Extra 19341 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used 19352 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1 1936drop table t1; 1937create table t1(id int); 1938create table t2(id int); 1939create table t3(flag int); 1940select (select * from t3 where id not null) from t1, t2; 1941ERROR 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 1942drop table t1,t2,t3; 1943CREATE TABLE t1 (id INT); 1944CREATE TABLE t2 (id INT); 1945INSERT INTO t1 VALUES (1), (2); 1946INSERT INTO t2 VALUES (1); 1947SELECT t1.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 id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); 1952id c 19531 1 19542 0 1955SELECT 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; 1956id c 19571 1 19582 0 1959SELECT 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; 1960id c 19611 1 19622 0 1963DROP TABLE t1,t2; 1964CREATE TABLE t1 ( a int, b int ); 1965INSERT INTO t1 VALUES (1,1),(2,2),(3,3); 1966SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); 1967a 19683 1969SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); 1970a 19711 1972SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); 1973a 19742 1975SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); 1976a 19772 19783 1979SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); 1980a 19811 19822 1983SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); 1984a 19851 19863 1987SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); 1988a 19893 1990SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); 1991a 19921 1993SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); 1994a 19952 1996SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); 1997a 19982 19993 2000SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); 2001a 20021 20032 2004SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); 2005a 20061 20073 2008ALTER TABLE t1 ADD INDEX (a); 2009SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); 2010a 20113 2012SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); 2013a 20141 2015SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); 2016a 20172 2018SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); 2019a 20202 20213 2022SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); 2023a 20241 20252 2026SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); 2027a 20281 20293 2030SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); 2031a 20323 2033SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); 2034a 20351 2036SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); 2037a 20382 2039SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); 2040a 20412 20423 2043SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); 2044a 20451 20462 2047SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); 2048a 20491 20503 2051SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2); 2052a 20533 2054SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2); 2055a 20561 2057SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2); 2058a 20592 2060SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2); 2061a 20622 20633 2064SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2); 2065a 20661 20672 2068SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2); 2069a 20701 20713 2072SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2); 2073a 20743 2075SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2); 2076a 20771 2078SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2); 2079a 20802 2081SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2); 2082a 20832 20843 2085SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2); 2086a 20871 20882 2089SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2); 2090a 20911 20923 2093SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2094a 20953 2096SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2097a 20981 2099SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2100a 21012 2102SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2103a 21042 21053 2106SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2107a 21081 21092 2110SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2111a 21121 21133 2114SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2115a 21163 2117SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2118a 21191 2120SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2121a 21222 2123SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2124a 21252 21263 2127SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2128a 21291 21302 2131SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2132a 21331 21343 2135SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2136a 21373 2138SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2139a 21401 2141SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2142a 21432 2144SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2145a 21462 21473 2148SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2149a 21501 21512 2152SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2153a 21541 21553 2156SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2157a 21583 2159SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2160a 21611 2162SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2163a 21642 2165SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2166a 21672 21683 2169SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2170a 21711 21722 2173SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2174a 21751 21763 2177SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2); 2178ERROR 21000: Operand should contain 1 column(s) 2179SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2); 2180ERROR 21000: Operand should contain 1 column(s) 2181SELECT a FROM t1 WHERE (1,2) > ANY (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 FROM t1 WHERE b = 2); 2184ERROR 21000: Operand should contain 1 column(s) 2185SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2); 2186ERROR 21000: Operand should contain 1 column(s) 2187SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2); 2188ERROR 21000: Operand should contain 1 column(s) 2189SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2); 2190ERROR 21000: Operand should contain 1 column(s) 2191SELECT a FROM t1 WHERE (1,2) <> 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 FROM t1 WHERE b = 2); 2194ERROR 21000: Operand should contain 2 column(s) 2195SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2); 2196ERROR 21000: Operand should contain 1 column(s) 2197SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2); 2198a 2199SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2); 2200ERROR 21000: Operand should contain 2 column(s) 2201SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2); 2202ERROR 21000: Operand should contain 1 column(s) 2203SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2); 2204a 22051 22062 22073 2208SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2); 2209a 22102 2211SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2); 2212a 22131 22143 2215SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2); 2216a 22172 2218SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2); 2219a 22201 22213 2222SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); 2223a 22242 2225SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); 2226a 22271 22283 2229SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); 2230a 22312 2232SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); 2233a 22341 22353 2236SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2237a 22383 2239SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2240a 22411 2242SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2243a 22442 2245SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2246a 22472 22483 2249SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2250a 22511 22522 2253SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2254a 22551 22563 2257SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2258a 22593 2260SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2261a 22621 2263SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2264a 22652 2266SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2267a 22682 22693 2270SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2271a 22721 22732 2274SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2275a 22761 22773 2278SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2); 2279a 22803 2281SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2); 2282a 22831 2284SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2); 2285a 22862 2287SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2); 2288a 22892 22903 2291SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2); 2292a 22931 22942 2295SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2); 2296a 22971 22983 2299SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2); 2300a 23013 2302SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2); 2303a 23041 2305SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2); 2306a 23072 2308SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2); 2309a 23102 23113 2312SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2); 2313a 23141 23152 2316SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2); 2317a 23181 23193 2320SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a; 2321concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') 23220- 23230- 23241- 2325SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a; 2326concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') 23271- 23280- 23290- 2330SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a; 2331concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') 23320- 23331- 23340- 2335DROP TABLE t1; 2336CREATE TABLE t1 ( a double, b double ); 2337INSERT INTO t1 VALUES (1,1),(2,2),(3,3); 2338SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0); 2339a 23403 2341SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0); 2342a 23431 2344SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0); 2345a 23462 2347SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0); 2348a 23492 23503 2351SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0); 2352a 23531 23542 2355SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0); 2356a 23571 23583 2359SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0); 2360a 23613 2362SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0); 2363a 23641 2365SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0); 2366a 23672 2368SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0); 2369a 23702 23713 2372SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0); 2373a 23741 23752 2376SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0); 2377a 23781 23793 2380DROP TABLE t1; 2381CREATE TABLE t1 ( a char(1), b char(1)); 2382INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3'); 2383SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2'); 2384a 23853 2386SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2'); 2387a 23881 2389SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2'); 2390a 23912 2392SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2'); 2393a 23942 23953 2396SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2'); 2397a 23981 23992 2400SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2'); 2401a 24021 24033 2404SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2'); 2405a 24063 2407SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2'); 2408a 24091 2410SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2'); 2411a 24122 2413SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2'); 2414a 24152 24163 2417SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2'); 2418a 24191 24202 2421SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2'); 2422a 24231 24243 2425DROP TABLE t1; 2426create table t1 (a int, b int); 2427insert into t1 values (1,2),(3,4); 2428select * from t1 up where exists (select * from t1 where t1.a=up.a); 2429a b 24301 2 24313 4 2432explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a); 2433id select_type table type possible_keys key key_len ref rows filtered Extra 24341 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 Using where 24352 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where 2436Warnings: 2437Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 2438Note 1003 /* select#1 */ select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(/* select#2 */ select 1 from `test`.`t1` where `test`.`t1`.`a` = `test`.`up`.`a` limit 1)) 2439drop table t1; 2440CREATE TABLE t1 (t1_a int); 2441INSERT INTO t1 VALUES (1); 2442CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b)); 2443INSERT INTO t2 VALUES (1, 1), (1, 2); 2444SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1 2445HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a); 2446t1_a t2_a t2_b 24471 1 2 2448DROP TABLE t1, t2; 2449CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL); 2450INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL); 2451CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL); 2452INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix'); 2453SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id; 2454id name id pet 24551 Tim 1 Fido 24562 Rebecca 2 Spot 24573 NULL 3 Felix 2458drop table t1,t2; 2459CREATE TABLE t1 ( a int, b int ); 2460CREATE TABLE t2 ( c int, d int ); 2461INSERT INTO t1 VALUES (1,2), (2,3), (3,4); 2462SELECT a AS abc, b FROM t1 outr WHERE b = 2463(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2464abc b 24651 2 24662 3 24673 4 2468INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = 2469(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2470select * from t2; 2471c d 24721 2 24732 3 24743 4 2475CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = 2476(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2477select * from t3; 2478abc b 24791 2 24802 3 24813 4 2482prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2483execute stmt1; 2484deallocate prepare stmt1; 2485select * from t2; 2486c d 24871 2 24882 3 24893 4 24901 2 24912 3 24923 4 2493drop table t3; 2494prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2495execute stmt1; 2496select * from t3; 2497abc b 24981 2 24992 3 25003 4 2501deallocate prepare stmt1; 2502DROP TABLE t1, t2, t3; 2503CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2504insert into t1 values (1); 2505CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2506insert into t2 values (1,2); 2507select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1); 2508a C 25091 1 2510drop table t1,t2; 2511create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b)); 2512insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan'); 2513create table t2 (a int); 2514insert into t2 values (1),(3),(2),(7); 2515select a,b from t1 where match(b) against ('Ball') > 0; 2516a b 25171 ball 25182 ball games 2519select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0); 2520a 25211 25222 2523drop table t1,t2; 2524CREATE 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); 2525CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel); 2526INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001'); 2527INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001'); 2528INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001'); 2529INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001'); 2530SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001'); 2531IZAVORGANG_ID 2532D0000000001 2533drop table t1; 2534CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2535CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2536insert into t1 values (1,1),(1,2),(2,1),(2,2); 2537insert into t2 values (1,2),(2,2); 2538select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2539aid bid 25401 1 25412 1 2542alter table t2 drop primary key; 2543alter table t2 add key KEY1 (aid, bid); 2544select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2545aid bid 25461 1 25472 1 2548alter table t2 drop key KEY1; 2549alter table t2 add primary key (bid, aid); 2550select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2551aid bid 25521 1 25532 1 2554drop table t1,t2; 2555CREATE TABLE t1 (howmanyvalues bigint, avalue int); 2556INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4); 2557SELECT howmanyvalues, count(*) from t1 group by howmanyvalues; 2558howmanyvalues count(*) 25591 1 25602 2 25613 3 25624 4 2563SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; 2564howmanyvalues mycount 25651 1 25662 2 25673 3 25684 4 2569CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues); 2570SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues; 2571howmanyvalues mycount 25721 1 25732 2 25743 3 25754 4 2576SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; 2577howmanyvalues mycount 25781 1 25792 2 25803 3 25814 4 2582SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues; 2583howmanyvalues mycount 25841 1 25852 1 25863 1 25874 1 2588drop table t1; 2589create table t1 (x int); 2590select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x; 2591(select b.x from t1 as b where b.x=a.x) 2592drop table t1; 2593CREATE 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`)); 2594INSERT 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); 2595CREATE 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`)) ; 2596INSERT 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'); 2597SELECT 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; 2598ERROR 42S22: Unknown column 'b.sc' in 'field list' 2599SELECT 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; 2600ac 2601700 2602NULL 2603drop tables t1,t2; 2604create table t1 (a int not null, b int not null, c int, primary key (a,b)); 2605insert into t1 values (1,1,1), (2,2,2), (3,3,3); 2606set @b:= 0; 2607explain select sum(a) from t1 where b > @b; 2608id select_type table type possible_keys key key_len ref rows Extra 26091 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index 2610set @a:= (select sum(a) from t1 where b > @b); 2611explain select a from t1 where c=2; 2612id select_type table type possible_keys key key_len ref rows Extra 26131 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2614do @a:= (select sum(a) from t1 where b > @b); 2615explain select a from t1 where c=2; 2616id select_type table type possible_keys key key_len ref rows Extra 26171 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2618drop table t1; 2619connect root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK; 2620connection root; 2621set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; 2622disconnect root; 2623connection default; 2624create table t1 (a int, b int); 2625create table t2 (a int, b int); 2626insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); 2627insert into t2 values (1,3),(2,1); 2628select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; 2629a b (select max(b) from t2 where t1.b=t2.a) 26301 1 3 26311 2 1 26321 3 NULL 26332 4 NULL 26342 5 NULL 2635drop table t1, t2; 2636create table t1 (id int); 2637create table t2 (id int, body text, fulltext (body)); 2638insert into t1 values(1),(2),(3); 2639insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test'); 2640select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode)); 2641count(distinct id) 26421 2643drop table t2,t1; 2644create table t1 (s1 int,s2 int); 2645insert into t1 values (20,15); 2646select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0)); 2647s1 s2 2648drop table t1; 2649create table t1 (s1 int); 2650insert into t1 values (1),(null); 2651select * from t1 where s1 < all (select s1 from t1); 2652s1 2653select s1, s1 < all (select s1 from t1) from t1; 2654s1 s1 < all (select s1 from t1) 26551 0 2656NULL NULL 2657drop table t1; 2658CREATE TABLE t1 ( 2659Code char(3) NOT NULL default '', 2660Name char(52) NOT NULL default '', 2661Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', 2662Region char(26) NOT NULL default '', 2663SurfaceArea float(10,2) NOT NULL default '0.00', 2664IndepYear smallint(6) default NULL, 2665Population int(11) NOT NULL default '0', 2666LifeExpectancy float(3,1) default NULL, 2667GNP float(10,2) default NULL, 2668GNPOld float(10,2) default NULL, 2669LocalName char(45) NOT NULL default '', 2670GovernmentForm char(45) NOT NULL default '', 2671HeadOfState char(60) default NULL, 2672Capital int(11) default NULL, 2673Code2 char(2) NOT NULL default '' 2674) ENGINE=MyISAM; 2675INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX'); 2676INSERT 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'); 2677INSERT 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'); 2678INSERT 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'); 2679/*!40000 ALTER TABLE t1 ENABLE KEYS */; 2680SELECT DISTINCT Continent AS c FROM t1 outr WHERE 2681Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND 2682Population < 200); 2683c 2684Oceania 2685drop table t1; 2686create table t1 (a1 int); 2687create table t2 (b1 int); 2688select * from t1 where a2 > any(select b1 from t2); 2689ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery' 2690select * from t1 where a1 > any(select b1 from t2); 2691a1 2692drop table t1,t2; 2693create table t1 (a integer, b integer); 2694select (select * from t1) = (select 1,2); 2695(select * from t1) = (select 1,2) 2696NULL 2697select (select 1,2) = (select * from t1); 2698(select 1,2) = (select * from t1) 2699NULL 2700select row(1,2) = ANY (select * from t1); 2701row(1,2) = ANY (select * from t1) 27020 2703select row(1,2) != ALL (select * from t1); 2704row(1,2) != ALL (select * from t1) 27051 2706drop table t1; 2707create table t1 (a integer, b integer); 2708select row(1,(2,2)) in (select * from t1 ); 2709ERROR 21000: Operand should contain 2 column(s) 2710select row(1,(2,2)) = (select * from t1 ); 2711ERROR 21000: Operand should contain 2 column(s) 2712select (select * from t1) = row(1,(2,2)); 2713ERROR 21000: Operand should contain 1 column(s) 2714drop table t1; 2715create table t1 (a integer); 2716insert into t1 values (1); 2717select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ; 2718ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2719select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2720ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2721select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx ); 2722xx 1 = ALL ( select 1 from t1 where 1 = xx ) 27231 1 2724select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2725ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2726select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL; 2727ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2728drop table t1; 2729CREATE TABLE t1 ( 2730categoryId int(11) NOT NULL, 2731courseId int(11) NOT NULL, 2732startDate datetime NOT NULL, 2733endDate datetime NOT NULL, 2734createDate datetime NOT NULL, 2735modifyDate timestamp NOT NULL, 2736attributes text NOT NULL 2737); 2738INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2739(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2740(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2741(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''), 2742(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''), 2743(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2744(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2745(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2746(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18',''); 2747CREATE TABLE t2 ( 2748userId int(11) NOT NULL, 2749courseId int(11) NOT NULL, 2750date datetime NOT NULL 2751); 2752INSERT INTO t2 VALUES (5141,71,'2003-11-18'), 2753(5141,72,'2003-11-25'),(5141,41,'2004-08-06'), 2754(5141,52,'2004-08-06'),(5141,53,'2004-08-06'), 2755(5141,12,'2004-08-06'),(5141,86,'2004-10-21'), 2756(5141,87,'2004-10-21'),(5141,88,'2004-10-21'), 2757(5141,89,'2004-10-22'),(5141,51,'2004-10-26'); 2758CREATE TABLE t3 ( 2759groupId int(11) NOT NULL, 2760parentId int(11) NOT NULL, 2761startDate datetime NOT NULL, 2762endDate datetime NOT NULL, 2763createDate datetime NOT NULL, 2764modifyDate timestamp NOT NULL, 2765ordering int(11) 2766); 2767INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL); 2768CREATE TABLE t4 ( 2769id int(11) NOT NULL, 2770groupTypeId int(11) NOT NULL, 2771groupKey varchar(50) NOT NULL, 2772name text, 2773ordering int(11), 2774description text, 2775createDate datetime NOT NULL, 2776modifyDate timestamp NOT NULL 2777); 2778INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'), 2779(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29'); 2780CREATE TABLE t5 ( 2781userId int(11) NOT NULL, 2782groupId int(11) NOT NULL, 2783createDate datetime NOT NULL, 2784modifyDate timestamp NOT NULL 2785); 2786INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06'); 2787select 2788count(distinct t2.userid) pass, 2789groupstuff.*, 2790count(t2.courseid) crse, 2791t1.categoryid, 2792t2.courseid, 2793date_format(date, '%b%y') as colhead 2794from t2 2795join t1 on t2.courseid=t1.courseid 2796join 2797( 2798select 2799t5.userid, 2800parentid, 2801parentgroup, 2802childid, 2803groupname, 2804grouptypeid 2805from t5 2806join 2807( 2808select t4.id as parentid, 2809t4.name as parentgroup, 2810t4.id as childid, 2811t4.name as groupname, 2812t4.grouptypeid 2813from t4 2814) as gin on t5.groupid=gin.childid 2815) as groupstuff on t2.userid = groupstuff.userid 2816group by 2817groupstuff.groupname, colhead , t2.courseid; 2818pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead 28191 5141 12 group2 12 group2 5 1 5 12 Aug04 28201 5141 12 group2 12 group2 5 1 1 41 Aug04 28211 5141 12 group2 12 group2 5 1 2 52 Aug04 28221 5141 12 group2 12 group2 5 1 2 53 Aug04 28231 5141 12 group2 12 group2 5 1 3 51 Oct04 28241 5141 12 group2 12 group2 5 1 1 86 Oct04 28251 5141 12 group2 12 group2 5 1 1 87 Oct04 28261 5141 12 group2 12 group2 5 1 2 88 Oct04 28271 5141 12 group2 12 group2 5 1 2 89 Oct04 2828drop table t1, t2, t3, t4, t5; 2829create table t1 (a int); 2830insert into t1 values (1), (2), (3); 2831SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1); 28321 28331 28341 28351 2836drop table t1; 2837create table t1 (a int); 2838create table t2 (a int); 2839insert into t1 values (1),(2); 2840insert into t2 values (0),(1),(2),(3); 2841select a from t2 where a in (select a from t1); 2842a 28431 28442 2845select a from t2 having a in (select a from t1); 2846a 28471 28482 2849prepare stmt1 from "select a from t2 where a in (select a from t1)"; 2850execute stmt1; 2851a 28521 28532 2854execute stmt1; 2855a 28561 28572 2858deallocate prepare stmt1; 2859prepare stmt1 from "select a from t2 having a in (select a from t1)"; 2860execute stmt1; 2861a 28621 28632 2864execute stmt1; 2865a 28661 28672 2868deallocate prepare stmt1; 2869drop table t1, t2; 2870create table t1 (a int, b int); 2871insert into t1 values (1,2); 2872select 1 = (select * from t1); 2873ERROR HY000: Illegal parameter data types int and row for operation '=' 2874select (select * from t1) = 1; 2875ERROR HY000: Illegal parameter data types row and int for operation '=' 2876select (1,2) = (select a from t1); 2877ERROR HY000: Illegal parameter data types row and int for operation '=' 2878select (select a from t1) = (1,2); 2879ERROR HY000: Illegal parameter data types int and row for operation '=' 2880select (1,2,3) = (select * from t1); 2881ERROR 21000: Operand should contain 3 column(s) 2882select (select * from t1) = (1,2,3); 2883ERROR 21000: Operand should contain 2 column(s) 2884drop table t1; 2885CREATE TABLE `t1` ( 2886`itemid` bigint(20) unsigned NOT NULL auto_increment, 2887`sessionid` bigint(20) unsigned default NULL, 2888`time` int(10) unsigned NOT NULL default '0', 2889`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 2890NULL default '', 2891`data` text collate latin1_general_ci NOT NULL, 2892PRIMARY KEY (`itemid`) 2893) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2894INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 2895CREATE TABLE `t2` ( 2896`sessionid` bigint(20) unsigned NOT NULL auto_increment, 2897`pid` int(10) unsigned NOT NULL default '0', 2898`date` int(10) unsigned NOT NULL default '0', 2899`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 2900PRIMARY KEY (`sessionid`) 2901) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2902INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 2903SELECT 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; 2904ip count( e.itemid ) 290510.10.10.1 1 2906drop tables t1,t2; 2907create table t1 (fld enum('0','1')); 2908insert into t1 values ('1'); 2909select * from (select max(fld) from t1) as foo; 2910max(fld) 29111 2912drop table t1; 2913set @tmp11867_optimizer_switch=@@optimizer_switch; 2914set optimizer_switch='semijoin_with_cache=off'; 2915CREATE TABLE t1 (one int, two int, flag char(1)); 2916CREATE TABLE t2 (one int, two int, flag char(1)); 2917INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2918INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2919SELECT * FROM t1 2920WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N'); 2921one two flag 29225 6 N 29237 8 N 2924SELECT * FROM t1 2925WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N'); 2926one two flag 29275 6 N 29287 8 N 2929insert into t2 values (null,null,'N'); 2930insert into t2 values (null,3,'0'); 2931insert into t2 values (null,5,'0'); 2932insert into t2 values (10,null,'0'); 2933insert into t1 values (10,3,'0'); 2934insert into t1 values (10,5,'0'); 2935insert into t1 values (10,10,'0'); 2936SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1; 2937one two test 29381 2 NULL 29392 3 NULL 29403 4 NULL 29415 6 1 29427 8 1 294310 3 NULL 294410 5 NULL 294510 10 NULL 2946SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 2947one two 29485 6 29497 8 2950SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1; 2951one two test 29521 2 NULL 29532 3 NULL 29543 4 NULL 29555 6 1 29567 8 1 295710 3 NULL 295810 5 NULL 295910 10 NULL 2960SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2961one two test 29621 2 0 29632 3 NULL 29643 4 0 29655 6 0 29667 8 0 296710 3 NULL 296810 5 NULL 296910 10 NULL 2970SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2971one two test 29721 2 0 29732 3 NULL 29743 4 0 29755 6 0 29767 8 0 297710 3 NULL 297810 5 NULL 297910 10 NULL 2980explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2981id select_type table type possible_keys key key_len ref rows filtered Extra 29821 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29832 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2984Warnings: 2985Note 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` 2986explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 2987id select_type table type possible_keys key key_len ref rows filtered Extra 29881 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29891 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 29902 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2991Warnings: 2992Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`flag` = 'N' 2993explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2994id select_type table type possible_keys key key_len ref rows filtered Extra 29951 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29962 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2997Warnings: 2998Note 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` 2999DROP TABLE t1,t2; 3000set optimizer_switch=@tmp11867_optimizer_switch; 3001CREATE TABLE t1 (a char(5), b char(5)); 3002INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa'); 3003SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb')); 3004a b 3005aaa aaa 3006DROP TABLE t1; 3007CREATE TABLE t1 (a int); 3008CREATE TABLE t2 (a int, b int); 3009CREATE TABLE t3 (b int NOT NULL); 3010INSERT INTO t1 VALUES (1), (2), (3), (4); 3011INSERT INTO t2 VALUES (1,10), (3,30); 3012SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3013WHERE t3.b IS NOT NULL OR t2.a > 10; 3014a b b 3015SELECT * FROM t1 3016WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3017WHERE t3.b IS NOT NULL OR t2.a > 10); 3018a 30191 30202 30213 30224 3023DROP TABLE t1,t2,t3; 3024CREATE TABLE t1 (f1 INT); 3025CREATE TABLE t2 (f2 INT); 3026INSERT INTO t1 VALUES (1); 3027SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2); 3028f1 30291 3030SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0); 3031f1 30321 3033INSERT INTO t2 VALUES (1); 3034INSERT INTO t2 VALUES (2); 3035SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0); 3036f1 30371 3038DROP TABLE t1, t2; 3039select 1 from dual where 1 < any (select 2); 30401 30411 3042select 1 from dual where 1 < all (select 2); 30431 30441 3045select 1 from dual where 2 > any (select 1); 30461 30471 3048select 1 from dual where 2 > all (select 1); 30491 30501 3051select 1 from dual where 1 < any (select 2 from dual); 30521 30531 3054select 1 from dual where 1 < all (select 2 from dual where 1!=1); 30551 30561 3057create table t1 (s1 char); 3058insert into t1 values (1),(2); 3059select * from t1 where (s1 < any (select s1 from t1)); 3060s1 30611 3062select * from t1 where not (s1 < any (select s1 from t1)); 3063s1 30642 3065select * from t1 where (s1 < ALL (select s1+1 from t1)); 3066s1 30671 3068select * from t1 where not(s1 < ALL (select s1+1 from t1)); 3069s1 30702 3071select * from t1 where (s1+1 = ANY (select s1 from t1)); 3072s1 30731 3074select * from t1 where NOT(s1+1 = ANY (select s1 from t1)); 3075s1 30762 3077select * from t1 where (s1 = ALL (select s1/s1 from t1)); 3078s1 30791 3080select * from t1 where NOT(s1 = ALL (select s1/s1 from t1)); 3081s1 30822 3083drop table t1; 3084create table t1 ( 3085retailerID varchar(8) NOT NULL, 3086statusID int(10) unsigned NOT NULL, 3087changed datetime NOT NULL, 3088UNIQUE KEY retailerID (retailerID, statusID, changed) 3089); 3090INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56"); 3091INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53"); 3092INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56"); 3093INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53"); 3094INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50"); 3095INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50"); 3096select * from t1 r1 3097where (r1.retailerID,(r1.changed)) in 3098(SELECT r2.retailerId,(max(changed)) from t1 r2 3099group by r2.retailerId); 3100retailerID statusID changed 31010026 2 2006-01-06 12:25:53 31020037 2 2006-01-06 12:25:53 31030048 1 2006-01-06 12:37:50 31040059 1 2006-01-06 12:37:50 3105drop table t1; 3106create table t1(a int, primary key (a)); 3107insert into t1 values (10); 3108create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b)); 3109insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989'); 3110insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999'); 3111analyze table t1; 3112Table Op Msg_type Msg_text 3113test.t1 analyze status Engine-independent statistics collected 3114test.t1 analyze status OK 3115explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3116ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3117 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3118id select_type table type possible_keys key key_len ref rows Extra 31191 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31201 PRIMARY r const PRIMARY PRIMARY 4 const 1 31212 SUBQUERY t2 range b b 40 NULL 3 Using where 3122SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3123ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3124 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3125a a b 312610 3 35989 3127explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3128ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3129 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3130id select_type table type possible_keys key key_len ref rows Extra 31311 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31321 PRIMARY r const PRIMARY PRIMARY 4 const 1 31332 SUBQUERY t2 range b b 40 NULL 3 Using index condition 3134SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3135ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3136 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3137a a b 313810 1 359 3139drop table t1,t2; 3140CREATE TABLE t1 ( 3141field1 int NOT NULL, 3142field2 int NOT NULL, 3143field3 int NOT NULL, 3144PRIMARY KEY (field1,field2,field3) 3145); 3146CREATE TABLE t2 ( 3147fieldA int NOT NULL, 3148fieldB int NOT NULL, 3149PRIMARY KEY (fieldA,fieldB) 3150); 3151INSERT INTO t1 VALUES 3152(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1); 3153INSERT INTO t2 VALUES (1,1), (1,2), (1,3); 3154SELECT field1, field2, COUNT(*) 3155FROM t1 GROUP BY field1, field2; 3156field1 field2 COUNT(*) 31571 1 2 31581 2 3 31591 3 1 3160SELECT field1, field2 3161FROM t1 3162GROUP BY field1, field2 3163HAVING COUNT(*) >= ALL (SELECT fieldB 3164FROM t2 WHERE fieldA = field1); 3165field1 field2 31661 2 3167SELECT field1, field2 3168FROM t1 3169GROUP BY field1, field2 3170HAVING COUNT(*) < ANY (SELECT fieldB 3171FROM t2 WHERE fieldA = field1); 3172field1 field2 31731 1 31741 3 3175DROP TABLE t1, t2; 3176CREATE TABLE t1(a int, INDEX (a)); 3177INSERT INTO t1 VALUES (1), (3), (5), (7); 3178INSERT INTO t1 VALUES (NULL); 3179CREATE TABLE t2(a int); 3180INSERT INTO t2 VALUES (1),(2),(3); 3181EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2; 3182id select_type table type possible_keys key key_len ref rows Extra 31831 PRIMARY t2 ALL NULL NULL NULL NULL 3 31842 SUBQUERY t1 index_subquery a a 5 func 2 Using index; Full scan on NULL key 3185SELECT a, a IN (SELECT a FROM t1) FROM t2; 3186a a IN (SELECT a FROM t1) 31871 1 31882 NULL 31893 1 3190DROP TABLE t1,t2; 3191CREATE TABLE t1 (a DATETIME); 3192INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25'); 3193CREATE TABLE t2 AS SELECT 3194(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a 3195FROM t1 WHERE a > '2000-01-01'; 3196SHOW CREATE TABLE t2; 3197Table Create Table 3198t2 CREATE TABLE `t2` ( 3199 `sub_a` datetime DEFAULT NULL 3200) ENGINE=MyISAM DEFAULT CHARSET=latin1 3201CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01'); 3202SHOW CREATE TABLE t3; 3203Table Create Table 3204t3 CREATE TABLE `t3` ( 3205 `a` datetime DEFAULT NULL 3206) ENGINE=MyISAM DEFAULT CHARSET=latin1 3207DROP TABLE t1,t2,t3; 3208CREATE TABLE t1 (a int); 3209INSERT INTO t1 VALUES (1), (2); 3210SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; 3211a 3212SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3213a 32141 32152 3216EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3217id select_type table type possible_keys key key_len ref rows Extra 32181 PRIMARY t1 ALL NULL NULL NULL NULL 2 32192 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 3220DROP TABLE t1; 3221CREATE TABLE t1 (a int); 3222INSERT INTO t1 VALUES (2), (4), (1), (3); 3223CREATE TABLE t2 (b int, c int); 3224INSERT INTO t2 VALUES 3225(2,1), (1,3), (2,1), (4,4), (2,2), (1,4); 3226SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); 3227a 32282 32294 32301 32313 3232SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); 3233ERROR 21000: Subquery returns more than 1 row 3234SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; 3235a 32361 32372 32383 32394 3240SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; 3241ERROR 21000: Subquery returns more than 1 row 3242SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); 3243b MAX(c) 32441 4 32452 2 32464 4 3247SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); 3248ERROR 21000: Subquery returns more than 1 row 3249SELECT a FROM t1 GROUP BY a 3250HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), 3251(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3252a 32531 32542 32553 32564 3257SELECT a FROM t1 GROUP BY a 3258HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), 3259(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3260ERROR 21000: Subquery returns more than 1 row 3261SELECT a FROM t1 GROUP BY a 3262HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3263(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3264a 32654 3266SELECT a FROM t1 GROUP BY a 3267HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3268(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; 3269ERROR 21000: Subquery returns more than 1 row 3270SELECT a FROM t1 3271ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), 3272(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3273a 32741 32752 32763 32774 3278SELECT a FROM t1 3279ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), 3280(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3281ERROR 21000: Subquery returns more than 1 row 3282SELECT a FROM t1 3283ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3284(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3285a 32861 32872 32883 32894 3290SELECT a FROM t1 3291ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3292(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3293ERROR 21000: Subquery returns more than 1 row 3294DROP TABLE t1,t2; 3295create table t1 (df decimal(5,1)); 3296insert into t1 values(1.1); 3297insert into t1 values(2.2); 3298select * from t1 where df <= all (select avg(df) from t1 group by df); 3299df 33001.1 3301select * from t1 where df >= all (select avg(df) from t1 group by df); 3302df 33032.2 3304drop table t1; 3305create table t1 (df decimal(5,1)); 3306insert into t1 values(1.1); 3307select 1.1 * exists(select * from t1); 33081.1 * exists(select * from t1) 33091.1 3310drop table t1; 3311CREATE TABLE t1 ( 3312grp int(11) default NULL, 3313a decimal(10,2) default NULL); 3314insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL); 3315select * from t1; 3316grp a 33171 1.00 33182 2.00 33192 3.00 33203 4.00 33213 5.00 33223 6.00 3323NULL NULL 3324select min(a) from t1 group by grp; 3325min(a) 3326NULL 33271.00 33282.00 33294.00 3330drop table t1; 3331CREATE table t1 ( c1 integer ); 3332INSERT INTO t1 VALUES ( 1 ); 3333INSERT INTO t1 VALUES ( 2 ); 3334INSERT INTO t1 VALUES ( 3 ); 3335CREATE TABLE t2 ( c2 integer ); 3336INSERT INTO t2 VALUES ( 1 ); 3337INSERT INTO t2 VALUES ( 4 ); 3338INSERT INTO t2 VALUES ( 5 ); 3339SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1); 3340c1 c2 33411 1 3342SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 3343WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) ); 3344c1 c2 33451 1 3346DROP TABLE t1,t2; 3347CREATE TABLE t1 ( c1 integer ); 3348INSERT INTO t1 VALUES ( 1 ); 3349INSERT INTO t1 VALUES ( 2 ); 3350INSERT INTO t1 VALUES ( 3 ); 3351INSERT INTO t1 VALUES ( 6 ); 3352CREATE TABLE t2 ( c2 integer ); 3353INSERT INTO t2 VALUES ( 1 ); 3354INSERT INTO t2 VALUES ( 4 ); 3355INSERT INTO t2 VALUES ( 5 ); 3356INSERT INTO t2 VALUES ( 6 ); 3357CREATE TABLE t3 ( c3 integer ); 3358INSERT INTO t3 VALUES ( 7 ); 3359INSERT INTO t3 VALUES ( 8 ); 3360SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2 3361WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL ); 3362c1 c2 33632 NULL 33643 NULL 3365DROP TABLE t1,t2,t3; 3366CREATE TABLE `t1` ( 3367`itemid` bigint(20) unsigned NOT NULL auto_increment, 3368`sessionid` bigint(20) unsigned default NULL, 3369`time` int(10) unsigned NOT NULL default '0', 3370`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 3371NULL default '', 3372`data` text collate latin1_general_ci NOT NULL, 3373PRIMARY KEY (`itemid`) 3374) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3375INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 3376CREATE TABLE `t2` ( 3377`sessionid` bigint(20) unsigned NOT NULL auto_increment, 3378`pid` int(10) unsigned NOT NULL default '0', 3379`date` int(10) unsigned NOT NULL default '0', 3380`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 3381PRIMARY KEY (`sessionid`) 3382) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3383INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 3384SELECT 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; 3385ip count( e.itemid ) 338610.10.10.1 1 3387drop tables t1,t2; 3388CREATE TABLE t1 (EMPNUM CHAR(3)); 3389CREATE TABLE t2 (EMPNUM CHAR(3) ); 3390INSERT INTO t1 VALUES ('E1'),('E2'); 3391INSERT INTO t2 VALUES ('E1'); 3392DELETE FROM t1 3393WHERE t1.EMPNUM NOT IN 3394(SELECT t2.EMPNUM 3395FROM t2 3396WHERE t1.EMPNUM = t2.EMPNUM); 3397select * from t1; 3398EMPNUM 3399E1 3400DROP TABLE t1,t2; 3401CREATE TABLE t1(select_id BIGINT, values_id BIGINT); 3402INSERT INTO t1 VALUES (1, 1); 3403CREATE TABLE t2 (select_id BIGINT, values_id BIGINT, 3404PRIMARY KEY(select_id,values_id)); 3405INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5); 3406SELECT values_id FROM t1 3407WHERE values_id IN (SELECT values_id FROM t2 3408WHERE select_id IN (1, 0)); 3409values_id 34101 3411SELECT values_id FROM t1 3412WHERE values_id IN (SELECT values_id FROM t2 3413WHERE select_id BETWEEN 0 AND 1); 3414values_id 34151 3416SELECT values_id FROM t1 3417WHERE values_id IN (SELECT values_id FROM t2 3418WHERE select_id = 0 OR select_id = 1); 3419values_id 34201 3421DROP TABLE t1, t2; 3422create table t1 (fld enum('0','1')); 3423insert into t1 values ('1'); 3424select * from (select max(fld) from t1) as foo; 3425max(fld) 34261 3427drop table t1; 3428CREATE TABLE t1 (a int, b int); 3429CREATE TABLE t2 (c int, d int); 3430CREATE TABLE t3 (e int); 3431INSERT INTO t1 VALUES 3432(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40); 3433INSERT INTO t2 VALUES 3434(2,10), (2,20), (4,10), (5,10), (3,20), (2,40); 3435INSERT INTO t3 VALUES (10), (30), (10), (20) ; 3436SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a; 3437a MAX(b) MIN(b) 34381 20 10 34392 30 10 34403 20 20 34414 40 40 3442SELECT * FROM t2; 3443c d 34442 10 34452 20 34464 10 34475 10 34483 20 34492 40 3450SELECT * FROM t3; 3451e 345210 345330 345410 345520 3456SELECT a FROM t1 GROUP BY a 3457HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20); 3458a 34592 34604 3461SELECT a FROM t1 GROUP BY a 3462HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d); 3463a 34642 3465SELECT a FROM t1 GROUP BY a 3466HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d); 3467a 34682 34694 3470SELECT a FROM t1 GROUP BY a 3471HAVING a IN (SELECT c FROM t2 3472WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3473a 34742 34753 3476SELECT a FROM t1 GROUP BY a 3477HAVING a IN (SELECT c FROM t2 3478WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3479a 34802 34813 3482SELECT a FROM t1 GROUP BY a 3483HAVING a IN (SELECT c FROM t2 3484WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3485a 34862 3487SELECT a FROM t1 GROUP BY a 3488HAVING a IN (SELECT c FROM t2 3489WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d)); 3490a 34912 3492SELECT a FROM t1 GROUP BY a 3493HAVING a IN (SELECT c FROM t2 3494WHERE MIN(b) < d AND 3495EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3496a 34972 3498SELECT a, SUM(a) FROM t1 GROUP BY a; 3499a SUM(a) 35001 2 35012 6 35023 3 35034 4 3504SELECT a FROM t1 3505WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a; 3506a 35073 35084 3509SELECT a FROM t1 GROUP BY a 3510HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c); 3511a 35121 35133 35144 3515SELECT a FROM t1 3516WHERE a < 3 AND 3517EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a; 3518a 35191 35202 3521SELECT a FROM t1 3522WHERE a < 3 AND 3523EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c); 3524a 35251 35262 35271 35282 35292 3530SELECT t1.a FROM t1 GROUP BY t1.a 3531HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c 3532HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3533HAVING SUM(t1.a+t2.c) < t3.e/4)); 3534a 35351 35362 3537SELECT t1.a FROM t1 GROUP BY t1.a 3538HAVING t1.a > ALL(SELECT t2.c FROM t2 3539WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3540HAVING SUM(t1.a+t2.c) < t3.e/4)); 3541a 35424 3543SELECT t1.a FROM t1 GROUP BY t1.a 3544HAVING t1.a > ALL(SELECT t2.c FROM t2 3545WHERE EXISTS(SELECT t3.e FROM t3 3546WHERE SUM(t1.a+t2.c) < t3.e/4)); 3547ERROR HY000: Invalid use of group function 3548SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20; 3549ERROR HY000: Invalid use of group function 3550SELECT t1.a FROM t1 GROUP BY t1.a 3551HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3552HAVING AVG(t2.c+SUM(t1.b)) > 20); 3553a 35542 35553 35564 3557SELECT t1.a FROM t1 GROUP BY t1.a 3558HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3559HAVING AVG(SUM(t1.b)) > 20); 3560a 35612 35624 3563SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a 3564HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3565HAVING t2.c+sum > 20); 3566a sum 35672 60 35683 20 35694 40 3570DROP TABLE t1,t2,t3; 3571CREATE TABLE t1 (a varchar(5), b varchar(10)); 3572INSERT INTO t1 VALUES 3573('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2), 3574('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8); 3575SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3576a b 3577BBB 4 3578CCC 7 3579AAA 8 3580EXPLAIN 3581SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3582id select_type table type possible_keys key key_len ref rows Extra 35831 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where 35841 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35852 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3586ALTER TABLE t1 ADD INDEX(a); 3587SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3588a b 3589BBB 4 3590CCC 7 3591AAA 8 3592EXPLAIN 3593SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3594id select_type table type possible_keys key key_len ref rows Extra 35951 PRIMARY t1 ALL a NULL NULL NULL 9 Using where 35961 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35972 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3598DROP TABLE t1; 3599create table t1( f1 int,f2 int); 3600insert into t1 values (1,1),(2,2); 3601select 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'; 3602t 3603crash1 3604crash1 3605drop table t1; 3606create table t1 (c int, key(c)); 3607insert into t1 values (1142477582), (1142455969); 3608create table t2 (a int, b int); 3609insert into t2 values (2, 1), (1, 0); 3610delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1; 3611drop table t1, t2; 3612CREATE TABLE t1 (a INT); 3613CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3614ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3615CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1); 3616ERROR 42S22: Unknown column 'no_such_column' in 'where clause' 3617SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3618ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3619DROP TABLE t1; 3620create table t1 (i int, j bigint); 3621insert into t1 values (1, 2), (2, 2), (3, 2); 3622select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3; 3623min(i) 36241 3625drop table t1; 3626CREATE TABLE t1 (i BIGINT UNSIGNED); 3627INSERT INTO t1 VALUES (10000000000000000000); 3628INSERT INTO t1 VALUES (1); 3629CREATE TABLE t2 (i BIGINT UNSIGNED); 3630INSERT INTO t2 VALUES (10000000000000000000); 3631INSERT INTO t2 VALUES (1); 3632/* simple test */ 3633SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i; 3634i 363510000000000000000000 36361 3637/* subquery test */ 3638SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2); 3639i 364010000000000000000000 3641/* subquery test with cast*/ 3642SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED); 3643i 364410000000000000000000 3645DROP TABLE t1; 3646DROP TABLE t2; 3647CREATE TABLE t1 ( 3648id bigint(20) unsigned NOT NULL auto_increment, 3649name varchar(255) NOT NULL, 3650PRIMARY KEY (id) 3651); 3652INSERT INTO t1 VALUES 3653(1, 'Balazs'), (2, 'Joe'), (3, 'Frank'); 3654CREATE TABLE t2 ( 3655id bigint(20) unsigned NOT NULL auto_increment, 3656mid bigint(20) unsigned NOT NULL, 3657date date NOT NULL, 3658PRIMARY KEY (id) 3659); 3660INSERT INTO t2 VALUES 3661(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'), 3662(4, 2, '2006-04-20'), (5, 1, '2006-05-01'); 3663SELECT *, 3664(SELECT date FROM t2 WHERE mid = t1.id 3665ORDER BY date DESC LIMIT 0, 1) AS date_last, 3666(SELECT date FROM t2 WHERE mid = t1.id 3667ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last 3668FROM t1; 3669id name date_last date_next_to_last 36701 Balazs 2006-05-01 NULL 36712 Joe 2006-04-20 NULL 36723 Frank 2006-04-13 NULL 3673SELECT *, 3674(SELECT COUNT(*) FROM t2 WHERE mid = t1.id 3675ORDER BY date DESC LIMIT 1, 1) AS date_count 3676FROM t1; 3677id name date_count 36781 Balazs NULL 36792 Joe NULL 36803 Frank NULL 3681SELECT *, 3682(SELECT date FROM t2 WHERE mid = t1.id 3683ORDER BY date DESC LIMIT 0, 1) AS date_last, 3684(SELECT date FROM t2 WHERE mid = t1.id 3685ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last 3686FROM t1; 3687id name date_last date_next_to_last 36881 Balazs 2006-05-01 2006-03-30 36892 Joe 2006-04-20 2006-04-06 36903 Frank 2006-04-13 NULL 3691DROP TABLE t1,t2; 3692CREATE TABLE t1 ( 3693i1 int(11) NOT NULL default '0', 3694i2 int(11) NOT NULL default '0', 3695t datetime NOT NULL default '0000-00-00 00:00:00', 3696PRIMARY KEY (i1,i2,t) 3697); 3698INSERT INTO t1 VALUES 3699(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'), 3700(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'), 3701(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'), 3702(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'), 3703(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'), 3704(24,2,'2005-05-27 12:40:06'); 3705CREATE TABLE t2 ( 3706i1 int(11) NOT NULL default '0', 3707i2 int(11) NOT NULL default '0', 3708t datetime default NULL, 3709PRIMARY KEY (i1) 3710); 3711INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40'); 3712EXPLAIN 3713SELECT * FROM t1,t2 3714WHERE t1.t = (SELECT t1.t FROM t1 3715WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3716ORDER BY t1.t DESC LIMIT 1); 3717id select_type table type possible_keys key key_len ref rows Extra 37181 PRIMARY t2 system NULL NULL NULL NULL 1 37191 PRIMARY t1 index NULL PRIMARY 13 NULL 11 Using where; Using index 37202 SUBQUERY t1 range PRIMARY PRIMARY 13 NULL 6 Using where; Using index 3721SELECT * FROM t1,t2 3722WHERE t1.t = (SELECT t1.t FROM t1 3723WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3724ORDER BY t1.t DESC LIMIT 1); 3725i1 i2 t i1 i2 t 372624 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40 3727DROP TABLE t1, t2; 3728CREATE TABLE t1 (i INT); 3729(SELECT i FROM t1) UNION (SELECT i FROM t1); 3730i 3731SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS 3732( 3733(SELECT i FROM t1) UNION 3734(SELECT i FROM t1) 3735); 3736i 3737SELECT * FROM t1 3738WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1))); 3739i 3740explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12)) 3741from t1; 3742id select_type table type possible_keys key key_len ref rows Extra 37431 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37442 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37453 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3746NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3747explain select * from t1 where not exists 3748((select t11.i from t1 t11) union (select t12.i from t1 t12)); 3749id select_type table type possible_keys key key_len ref rows Extra 37501 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37512 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37523 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3753NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3754DROP TABLE t1; 3755CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b)); 3756insert into t1 (a) values (FLOOR(rand() * 100)); 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; 3767insert into t1 (a) select FLOOR(rand() * 100) from t1; 3768insert into t1 (a) select FLOOR(rand() * 100) from t1; 3769insert into t1 (a) select FLOOR(rand() * 100) from t1; 3770SELECT a, 3771(SELECT REPEAT(' ',250) FROM t1 i1 3772WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a 3773FROM t1 ORDER BY a LIMIT 5; 3774a a 37750 NULL 37760 NULL 37770 NULL 37780 NULL 37790 NULL 3780DROP TABLE t1; 3781CREATE TABLE t1 (a INT, b INT); 3782CREATE TABLE t2 (a INT); 3783INSERT INTO t2 values (1); 3784INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4); 3785SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a; 3786(SELECT COUNT(DISTINCT t1.b) from t2) 37872 37881 37891 3790SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 3791FROM t1 GROUP BY t1.a; 3792(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 37932 37941 37951 3796SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a; 3797COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b)) 37982 2 37991 1 38001 1 3801SELECT COUNT(DISTINCT t1.b), 3802(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 3803FROM t1 GROUP BY t1.a; 3804COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 38052 2 38061 1 38071 1 3808SELECT ( 3809SELECT ( 3810SELECT COUNT(DISTINCT t1.b) 3811) 3812) 3813FROM t1 GROUP BY t1.a; 3814( 3815SELECT ( 3816SELECT COUNT(DISTINCT t1.b) 3817) 3818) 38192 38201 38211 3822SELECT ( 3823SELECT ( 3824SELECT ( 3825SELECT COUNT(DISTINCT t1.b) 3826) 3827) 3828FROM t1 GROUP BY t1.a LIMIT 1) 3829FROM t1 t2 3830GROUP BY t2.a; 3831( 3832SELECT ( 3833SELECT ( 3834SELECT COUNT(DISTINCT t1.b) 3835) 3836) 3837FROM t1 GROUP BY t1.a LIMIT 1) 38382 38392 38402 3841DROP TABLE t1,t2; 3842CREATE TABLE t1 (a int, b int, PRIMARY KEY (b)); 3843CREATE TABLE t2 (x int auto_increment, y int, z int, 3844PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b)); 3845create table t3 (a int); 3846insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 3847insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a)) 3848from t3 A, t3 B, t3 C, t3 D where D.a<3; 3849insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3; 3850SET SESSION sort_buffer_size = 32 * 1024; 3851SELECT SQL_NO_CACHE COUNT(*) 3852FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c 3853FROM t1) t; 3854COUNT(*) 38553000 3856SET SESSION sort_buffer_size = 8 * 1024 * 1024; 3857SELECT SQL_NO_CACHE COUNT(*) 3858FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c 3859FROM t1) t; 3860COUNT(*) 38613000 3862DROP TABLE t1,t2,t3; 3863CREATE TABLE t1 (id char(4) PRIMARY KEY, c int); 3864CREATE TABLE t2 (c int); 3865INSERT INTO t1 VALUES ('aa', 1); 3866INSERT INTO t2 VALUES (1); 3867SELECT * FROM t1 3868WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3869UNION 3870SELECT c from t2 WHERE c=t1.c); 3871id c 3872aa 1 3873INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1); 3874SELECT * FROM t1 3875WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3876UNION 3877SELECT c from t2 WHERE c=t1.c); 3878id c 3879aa 1 3880bb 2 3881cc 3 3882dd 1 3883INSERT INTO t2 VALUES (2); 3884CREATE TABLE t3 (c int); 3885INSERT INTO t3 VALUES (1); 3886SELECT * FROM t1 3887WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1 3888UNION 3889SELECT c from t2 WHERE c=t1.c); 3890id c 3891aa 1 3892bb 2 3893cc 3 3894dd 1 3895DROP TABLE t1,t2,t3; 3896CREATE TABLE t1(f1 int); 3897CREATE TABLE t2(f2 int, f21 int, f3 timestamp); 3898INSERT INTO t1 VALUES (1),(1),(2),(2); 3899INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11"); 3900SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1; 3901sq 39022 39034 3904SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2; 3905tt 39062 39072 3908PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1'; 3909EXECUTE stmt1; 3910sq 39112 39124 3913EXECUTE stmt1; 3914sq 39152 39164 3917DEALLOCATE PREPARE stmt1; 3918SELECT f2, AVG(f21), 3919(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test 3920FROM t2 GROUP BY f2; 3921f2 AVG(f21) test 39221 1.0000 2004-02-29 11:11:11 39232 2.0000 2004-02-29 11:11:11 3924DROP TABLE t1,t2; 3925CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL); 3926INSERT INTO t1 VALUES 3927(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'), 3928(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'), 3929(3,2,'k'), (3,1,'l'), (1,9,'m'); 3930SELECT a, MAX(b), 3931(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test 3932FROM t1 GROUP BY a; 3933a MAX(b) test 39341 9 m 39352 3 h 39363 4 i 3937DROP TABLE t1; 3938DROP TABLE IF EXISTS t1; 3939DROP TABLE IF EXISTS t2; 3940DROP TABLE IF EXISTS t1xt2; 3941CREATE TABLE t1 ( 3942id_1 int(5) NOT NULL, 3943t varchar(4) DEFAULT NULL 3944); 3945CREATE TABLE t2 ( 3946id_2 int(5) NOT NULL, 3947t varchar(4) DEFAULT NULL 3948); 3949CREATE TABLE t1xt2 ( 3950id_1 int(5) NOT NULL, 3951id_2 int(5) NOT NULL 3952); 3953INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'); 3954INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa'); 3955INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4); 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 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 3964id_1 3965SELECT DISTINCT t1.id_1 FROM t1 WHERE 3966(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3967id_1 39681 39692 39703 39714 3972SELECT DISTINCT t1.id_1 FROM t1 WHERE 3973(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))); 3974id_1 39751 39762 39773 39784 3979SELECT DISTINCT t1.id_1 FROM t1 WHERE 3980(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)))); 3981id_1 39821 39832 39843 39854 3986insert INTO t1xt2 VALUES (1, 12); 3987SELECT DISTINCT t1.id_1 FROM t1 WHERE 3988(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3989id_1 39901 3991SELECT DISTINCT t1.id_1 FROM t1 WHERE 3992(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 3993id_1 39941 3995SELECT DISTINCT t1.id_1 FROM t1 WHERE 3996(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 3997id_1 39981 3999SELECT DISTINCT t1.id_1 FROM t1 WHERE 4000(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4001id_1 40022 40033 40044 4005SELECT DISTINCT t1.id_1 FROM t1 WHERE 4006(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4007id_1 40082 40093 40104 4011SELECT DISTINCT t1.id_1 FROM t1 WHERE 4012(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4013id_1 40142 40153 40164 4017insert INTO t1xt2 VALUES (2, 12); 4018SELECT DISTINCT t1.id_1 FROM t1 WHERE 4019(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4020id_1 40211 40222 4023SELECT DISTINCT t1.id_1 FROM t1 WHERE 4024(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4025id_1 40261 40272 4028SELECT DISTINCT t1.id_1 FROM t1 WHERE 4029(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4030id_1 40311 40322 4033SELECT DISTINCT t1.id_1 FROM t1 WHERE 4034(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4035id_1 40363 40374 4038SELECT DISTINCT t1.id_1 FROM t1 WHERE 4039(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4040id_1 40413 40424 4043SELECT DISTINCT t1.id_1 FROM t1 WHERE 4044(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4045id_1 40463 40474 4048DROP TABLE t1; 4049DROP TABLE t2; 4050DROP TABLE t1xt2; 4051CREATE TABLE t1 (a int); 4052INSERT INTO t1 VALUES (3), (1), (2); 4053SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1; 4054col1 col2 4055this is a test. 3 4056this is a test. 1 4057this is a test. 2 4058SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t; 4059col1 t2 4060this is a test. 3 4061this is a test. 1 4062this is a test. 2 4063DROP table t1; 4064CREATE TABLE t1 (a int, b int); 4065CREATE TABLE t2 (m int, n int); 4066INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4067INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4068SELECT COUNT(*), a, 4069(SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 4070FROM t1 GROUP BY a; 4071COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 40722 2 2 40733 3 3 40741 4 1 4075SELECT COUNT(*), a, 4076(SELECT MIN(m) FROM t2 WHERE m = count(*)) 4077FROM t1 GROUP BY a; 4078COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*)) 40792 2 2 40803 3 3 40811 4 1 4082SELECT COUNT(*), a 4083FROM t1 GROUP BY a 4084HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1; 4085COUNT(*) a 40862 2 40873 3 4088DROP TABLE t1,t2; 4089CREATE TABLE t1 (a int, b int); 4090CREATE TABLE t2 (m int, n int); 4091INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4092INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4093SELECT COUNT(*) c, a, 4094(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 4095FROM t1 GROUP BY a; 4096c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 40972 2 2 40983 3 3 40991 4 1,1 4100SELECT COUNT(*) c, a, 4101(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 4102FROM t1 GROUP BY a; 4103c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 41042 2 3 41053 3 4 41061 4 2,2 4107DROP table t1,t2; 4108CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b)); 4109INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'), 4110(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'), 4111(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p'); 4112SELECT a, MAX(b), 4113(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test 4114FROM t1 GROUP BY a; 4115a MAX(b) test 41161 9 m 41172 3 h 41183 4 i 4119SELECT a x, MAX(b), 4120(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test 4121FROM t1 GROUP BY a; 4122x MAX(b) test 41231 9 m 41242 3 h 41253 4 i 4126SELECT a, AVG(b), 4127(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test 4128FROM t1 WHERE t1.d=0 GROUP BY a; 4129a AVG(b) test 41301 4.0000 d 41312 2.0000 g 41323 2.5000 NULL 4133SELECT tt.a, 4134(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4135LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4136FROM t1 as tt; 4137a test 41381 n 41391 n 41401 n 41411 n 41421 n 41431 n 41441 n 41452 o 41462 o 41472 o 41482 o 41493 p 41503 p 41513 p 41523 p 41533 p 4154SELECT tt.a, 4155(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4156LIMIT 1) 4157FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4158FROM t1 as tt GROUP BY tt.a; 4159a test 41601 n 41612 o 41623 p 4163SELECT tt.a, MAX( 4164(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4165LIMIT 1) 4166FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test 4167FROM t1 as tt GROUP BY tt.a; 4168a test 41691 n 41702 o 41713 p 4172DROP TABLE t1; 4173CREATE TABLE t1 (a int, b int); 4174INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4175SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4176a 41771 41782 4179SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4180a 4181SELECT a FROM t1 t0 4182WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4183a 41841 41852 4186SET @@sql_mode='ansi'; 4187SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4188ERROR HY000: Invalid use of group function 4189SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4190ERROR HY000: Invalid use of group function 4191SELECT a FROM t1 t0 4192WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4193ERROR HY000: Invalid use of group function 4194SET @@sql_mode=default; 4195DROP TABLE t1; 4196CREATE TABLE t1 (a INT); 4197INSERT INTO t1 values (1),(1),(1),(1); 4198CREATE TABLE t2 (x INT); 4199INSERT INTO t1 values (1000),(1001),(1002); 4200SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1; 4201ERROR HY000: Invalid use of group function 4202SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1; 4203ERROR HY000: Invalid use of group function 4204SELECT COUNT(1) FROM DUAL; 4205COUNT(1) 42061 4207SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1; 4208ERROR HY000: Invalid use of group function 4209SELECT 4210SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) ) 4211FROM t1; 4212ERROR HY000: Invalid use of group function 4213SELECT t1.a as XXA, 4214SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) ) 4215FROM t1; 4216ERROR HY000: Invalid use of group function 4217DROP TABLE t1,t2; 4218CREATE TABLE t1 (a int, b int, KEY (a)); 4219INSERT INTO t1 VALUES (1,1),(2,1); 4220EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b); 4221id select_type table type possible_keys key key_len ref rows Extra 42221 PRIMARY t1 ref a a 5 const 1 Using where; Using index 42232 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 4224DROP TABLE t1; 4225CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id)); 4226INSERT INTO t1 VALUES 4227(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY'); 4228CREATE TABLE t2 (id int NOT NULL, INDEX idx(id)); 4229INSERT INTO t2 VALUES (7), (5), (1), (3); 4230SELECT id, st FROM t1 4231WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4232id st 42333 FL 42341 GA 42357 FL 4236SELECT id, st FROM t1 4237WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4238GROUP BY id; 4239id st 42401 GA 42413 FL 42427 FL 4243SELECT id, st FROM t1 4244WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4245id st 42462 GA 42474 FL 4248SELECT id, st FROM t1 4249WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4250GROUP BY id; 4251id st 42522 GA 42534 FL 4254DROP TABLE t1,t2; 4255CREATE TABLE t1 (a int); 4256INSERT INTO t1 VALUES (1), (2); 4257EXPLAIN EXTENDED 4258SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res; 4259id select_type table type possible_keys key key_len ref rows filtered Extra 42601 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00 42612 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort 4262Warnings: 4263Note 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` 4264DROP TABLE t1; 4265CREATE TABLE t1 ( 4266a varchar(255) default NULL, 4267b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 4268INDEX idx(a,b) 4269); 4270CREATE TABLE t2 ( 4271a varchar(255) default NULL 4272); 4273INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24'); 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 SELECT * FROM t1; 4280INSERT INTO t1 SELECT * FROM t1; 4281INSERT INTO t1 SELECT * FROM t1; 4282INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26'); 4283INSERT INTO `t2` VALUES ('abcdefghijk'); 4284INSERT INTO `t2` VALUES ('asdf'); 4285SET session sort_buffer_size=8192; 4286SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2; 4287d1 42881 42891 4290DROP TABLE t1,t2; 4291CREATE TABLE t1 (a INTEGER, b INTEGER); 4292CREATE TABLE t2 (x INTEGER); 4293INSERT INTO t1 VALUES (1,11), (2,22), (2,22); 4294INSERT INTO t2 VALUES (1), (2); 4295SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a; 4296ERROR 21000: Subquery returns more than 1 row 4297SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a; 4298ERROR 21000: Subquery returns more than 1 row 4299SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1; 4300(SELECT SUM(t1.a)/AVG(t2.x) FROM t2) 43013.3333 4302DROP TABLE t1,t2; 4303CREATE TABLE t1 (a INT, b INT); 4304INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2); 4305SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1 4306AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a) 4307GROUP BY a1.a; 4308a COUNT(*) 43091 3 4310DROP TABLE t1; 4311CREATE TABLE t1 (a INT); 4312CREATE TABLE t2 (a INT); 4313INSERT INTO t1 VALUES (1),(2); 4314INSERT INTO t2 VALUES (1),(2); 4315SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1; 4316(SELECT SUM(t1.a) FROM t2 WHERE a=0) 4317NULL 4318SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1; 4319ERROR 21000: Subquery returns more than 1 row 4320SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1; 4321(SELECT SUM(t1.a) FROM t2 WHERE a=1) 43223 4323DROP TABLE t1,t2; 4324CREATE TABLE t1 (a1 INT, a2 INT); 4325CREATE TABLE t2 (b1 INT, b2 INT); 4326INSERT INTO t1 VALUES (100, 200); 4327INSERT INTO t1 VALUES (101, 201); 4328INSERT INTO t2 VALUES (101, 201); 4329INSERT INTO t2 VALUES (103, 203); 4330SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1; 4331((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL 43320 43330 4334DROP TABLE t1, t2; 4335CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5)); 4336INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43); 4337SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); 4338s1 s2 4339SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); 4340s1 s2 4341CREATE INDEX I1 ON t1 (s1); 4342CREATE INDEX I2 ON t1 (s2); 4343SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); 4344s1 s2 4345SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); 4346s1 s2 4347TRUNCATE t1; 4348INSERT INTO t1 VALUES (0x41,0x41); 4349SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1); 4350s1 s2 4351DROP TABLE t1; 4352CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1)); 4353CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2)); 4354CREATE TABLE t3 (a3 BINARY(2) default '0'); 4355INSERT INTO t1 VALUES (1),(2),(3),(4); 4356INSERT INTO t2 VALUES (1),(2),(3); 4357INSERT INTO t3 VALUES (1),(2),(3); 4358SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2; 4359LEFT(t2.a2, 1) 43601 43612 43623 4363SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1; 4364a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) 43651 0 43662 0 43673 0 43684 0 4369DROP TABLE t1,t2,t3; 4370CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3)); 4371CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY); 4372CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY); 4373INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40); 4374INSERT INTO t2 VALUES (2), (3), (4), (5); 4375INSERT INTO t3 VALUES (10), (20), (30); 4376SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3; 4377LEFT(t1.a1,1) 43781 43792 43803 4381SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3); 4382a2 4383DROP TABLE t1, t2, t3; 4384SET @save_optimizer_switch=@@optimizer_switch; 4385SET optimizer_switch='semijoin_with_cache=off'; 4386SET optimizer_switch='materialization=off'; 4387CREATE TABLE t1 (a CHAR(1), b VARCHAR(10)); 4388INSERT INTO t1 VALUES ('a', 'aa'); 4389INSERT INTO t1 VALUES ('a', 'aaa'); 4390SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4391a b 4392CREATE INDEX I1 ON t1 (a); 4393CREATE INDEX I2 ON t1 (b); 4394EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4395id select_type table type possible_keys key key_len ref rows Extra 43961 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 43971 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4398SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4399a b 4400CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10)); 4401INSERT INTO t2 SELECT * FROM t1; 4402CREATE INDEX I1 ON t2 (a); 4403CREATE INDEX I2 ON t2 (b); 4404EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4405id select_type table type possible_keys key key_len ref rows Extra 44061 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where 44071 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2) 4408SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4409a b 4410EXPLAIN 4411SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4412id select_type table type possible_keys key key_len ref rows Extra 44131 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 44141 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4415SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4416a b 4417DROP TABLE t1,t2; 4418SET optimizer_switch= @save_optimizer_switch; 4419CREATE TABLE t1(a INT, b INT); 4420INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4); 4421EXPLAIN 4422SELECT a AS out_a, MIN(b) FROM t1 4423WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4424GROUP BY a; 4425ERROR 42S22: Unknown column 'out_a' in 'where clause' 4426SELECT a AS out_a, MIN(b) FROM t1 4427WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4428GROUP BY a; 4429ERROR 42S22: Unknown column 'out_a' in 'where clause' 4430EXPLAIN 4431SELECT a AS out_a, MIN(b) FROM t1 t1_outer 4432WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) 4433GROUP BY a; 4434id select_type table type possible_keys key key_len ref rows Extra 44351 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort 44362 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where 4437SELECT a AS out_a, MIN(b) FROM t1 t1_outer 4438WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) 4439GROUP BY a; 4440out_a MIN(b) 44411 2 44422 4 4443DROP TABLE t1; 4444CREATE TABLE t1 (a INT); 4445CREATE TABLE t2 (a INT); 4446INSERT INTO t1 VALUES (1),(2); 4447INSERT INTO t2 VALUES (1),(2); 4448SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 44492 44502 44512 4452EXPLAIN EXTENDED 4453SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 4454id select_type table type possible_keys key key_len ref rows filtered Extra 44551 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where 44562 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 4457Warnings: 4458Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4459Note 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` limit 1)) 4460EXPLAIN EXTENDED 4461SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION 4462(SELECT 1 FROM t2 WHERE t1.a = t2.a)); 4463id select_type table type possible_keys key key_len ref rows filtered Extra 44641 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where 44652 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 44663 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where 4467NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 4468Warnings: 4469Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4470Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 4471Note 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)) 4472DROP TABLE t1,t2; 4473create table t0(a int); 4474insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 4475create table t1(f11 int, f12 int); 4476create table t2(f21 int unsigned not null, f22 int, f23 varchar(10)); 4477insert into t1 values(1,1),(2,2), (3, 3); 4478insert ignore into t2 4479select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a 4480from t0 A, t0 B, t0 C, t0 D; 4481set session sort_buffer_size= 33*1024; 4482select count(*) from t1 where f12 = 4483(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1); 4484count(*) 44853 4486drop table t0,t1,t2; 4487CREATE TABLE t4 ( 4488f7 varchar(32) collate utf8_bin NOT NULL default '', 4489f10 varchar(32) collate utf8_bin default NULL, 4490PRIMARY KEY (f7) 4491); 4492INSERT INTO t4 VALUES(1,1), (2,null); 4493CREATE TABLE t2 ( 4494f4 varchar(32) collate utf8_bin NOT NULL default '', 4495f2 varchar(50) collate utf8_bin default NULL, 4496f3 varchar(10) collate utf8_bin default NULL, 4497PRIMARY KEY (f4), 4498UNIQUE KEY uk1 (f2) 4499); 4500INSERT INTO t2 VALUES(1,1,null), (2,2,null); 4501CREATE TABLE t1 ( 4502f8 varchar(32) collate utf8_bin NOT NULL default '', 4503f1 varchar(10) collate utf8_bin default NULL, 4504f9 varchar(32) collate utf8_bin default NULL, 4505PRIMARY KEY (f8) 4506); 4507INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2); 4508CREATE TABLE t3 ( 4509f6 varchar(32) collate utf8_bin NOT NULL default '', 4510f5 varchar(50) collate utf8_bin default NULL, 4511PRIMARY KEY (f6) 4512); 4513INSERT INTO t3 VALUES (1,null), (2,null); 4514SELECT 4515IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4, 4516IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3, 4517SUM( 4518IF( 4519(SELECT VPC.f2 4520FROM t2 VPC, t4 a2, t2 a3 4521WHERE 4522VPC.f4 = a2.f10 AND a3.f2 = a4 4523LIMIT 1) IS NULL, 45240, 4525t3.f5 4526) 4527) AS a6 4528FROM 4529t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4 4530GROUP BY a4; 4531a4 f3 a6 45321 NULL NULL 45332 NULL NULL 4534DROP TABLE t1, t2, t3, t4; 4535create table t1 (a float(5,4) zerofill); 4536create table t2 (a float(5,4),b float(2,0)); 4537select t1.a from t1 where 4538t1.a= (select b from t2 limit 1) and not 4539t1.a= (select a from t2 limit 1) ; 4540a 4541drop table t1, t2; 4542CREATE TABLE t1 (a INT); 4543INSERT INTO t1 VALUES (1),(2); 4544SET @save_join_cache_level=@@join_cache_level; 4545SET join_cache_level=0; 4546EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a); 4547id select_type table type possible_keys key key_len ref rows filtered Extra 45481 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45491 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45502 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary 4551Warnings: 4552Note 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 4553EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a); 4554id select_type table type possible_keys key key_len ref rows filtered Extra 45551 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45561 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45572 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary 4558Warnings: 4559Note 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 4560SET join_cache_level=@save_join_cache_level; 4561DROP TABLE t1; 4562# 4563# Bug#45061: Incorrectly market field caused wrong result. 4564# 4565CREATE TABLE `C` ( 4566`int_nokey` int(11) NOT NULL, 4567`int_key` int(11) NOT NULL, 4568KEY `int_key` (`int_key`) 4569); 4570INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), 4571(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), 4572(5,2), (1,8), (7,0), (0,9), (9,5); 4573SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4574int_nokey int_key 45759 9 45760 0 45775 5 45780 0 4579EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4580id select_type table type possible_keys key key_len ref rows filtered Extra 45811 SIMPLE C ALL NULL NULL NULL NULL 20 100.00 Using where 4582DROP TABLE C; 4583# End of test for bug#45061. 4584# 4585# Bug #46749: Segfault in add_key_fields() with outer subquery level 4586# field references 4587# 4588CREATE TABLE t1 ( 4589a int, 4590b int, 4591UNIQUE (a), KEY (b) 4592); 4593INSERT INTO t1 VALUES (1,1), (2,1); 4594CREATE TABLE st1 like t1; 4595INSERT INTO st1 VALUES (1,1), (2,1); 4596CREATE TABLE st2 like t1; 4597INSERT INTO st2 VALUES (1,1), (2,1); 4598EXPLAIN 4599SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4600FROM t1 4601WHERE a = 230; 4602id select_type table type possible_keys key key_len ref rows Extra 46031 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 46042 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 4605SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4606FROM t1 4607WHERE a = 230; 4608MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4609NULL NULL 4610DROP TABLE t1, st1, st2; 4611# 4612# Bug #48709: Assertion failed in sql_select.cc:11782: 4613# int join_read_key(JOIN_TAB*) 4614# 4615CREATE TABLE t1 (pk int PRIMARY KEY, int_key int); 4616INSERT INTO t1 VALUES (10,1), (14,1); 4617CREATE TABLE t2 (pk int PRIMARY KEY, int_key int); 4618INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3); 4619# should have eq_ref for t1 4620EXPLAIN 4621SELECT * FROM t2 outr 4622WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4623ORDER BY outr.pk; 4624id select_type table type possible_keys key key_len ref rows Extra 4625x x outr ALL x x x x x x 4626x x t1 eq_ref x x x x x x 4627x x t2 index x x x x x x 4628# should not crash on debug binaries 4629SELECT * FROM t2 outr 4630WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4631ORDER BY outr.pk; 4632pk int_key 46333 3 46347 3 4635DROP TABLE t1,t2; 4636# 4637# Bug#12329653 4638# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY 4639# 4640CREATE TABLE t1(a1 int); 4641INSERT INTO t1 VALUES (1),(2); 4642SELECT @@session.sql_mode INTO @old_sql_mode; 4643SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4644EXPLAIN EXTENDED 4645SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 4646id select_type table type possible_keys key key_len ref rows filtered Extra 46471 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 46482 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 4649Warnings: 4650Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 4651SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 46521 46531 46541 4655PREPARE stmt FROM 4656'SELECT 1 UNION ALL 4657SELECT 1 FROM t1 4658ORDER BY 4659(SELECT 1 FROM t1 AS t1_0 4660 WHERE 1 < SOME (SELECT a1 FROM t1) 4661)' ; 4662EXECUTE stmt ; 4663ERROR 21000: Subquery returns more than 1 row 4664EXECUTE stmt ; 4665ERROR 21000: Subquery returns more than 1 row 4666SET SESSION sql_mode=@old_sql_mode; 4667DEALLOCATE PREPARE stmt; 4668DROP TABLE t1; 4669# 4670# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4671# 4672CREATE TABLE t1(a1 int); 4673INSERT INTO t1 VALUES (1),(2); 4674CREATE TABLE t2(a1 int); 4675INSERT INTO t2 VALUES (3); 4676SELECT @@session.sql_mode INTO @old_sql_mode; 4677SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4678SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); 46791 46801 46811 4682SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); 46831 46841 46851 4686SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); 46871 4688Warnings: 4689Warning 1292 Truncated incorrect DOUBLE value: 'a' 4690SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); 46911 46921 46931 4694SET SESSION sql_mode=@old_sql_mode; 4695DROP TABLE t1, t2; 4696# 4697# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4698# 4699create table t2(i int); 4700insert into t2 values(0); 4701SELECT @@session.sql_mode INTO @old_sql_mode; 4702SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4703CREATE VIEW v1 AS 4704SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 4705; 4706CREATE TABLE t1 ( 4707pk int NOT NULL, 4708col_varchar_key varchar(1) DEFAULT NULL, 4709PRIMARY KEY (pk), 4710KEY col_varchar_key (col_varchar_key) 4711); 4712SELECT t1.pk 4713FROM t1 4714WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) 4715; 4716pk 4717SET SESSION sql_mode=@old_sql_mode; 4718drop table t2, t1; 4719drop view v1; 4720# End of 5.0 tests. 4721create table t_out (subcase char(3), 4722a1 char(2), b1 char(2), c1 char(2)); 4723create table t_in (a2 char(2), b2 char(2), c2 char(2)); 4724insert into t_out values ('A.1','2a', NULL, '2a'); 4725insert into t_out values ('A.3', '2a', NULL, '2a'); 4726insert into t_out values ('A.4', '2a', NULL, 'xx'); 4727insert into t_out values ('B.1', '2a', '2a', '2a'); 4728insert into t_out values ('B.2', '2a', '2a', '2a'); 4729insert into t_out values ('B.3', '3a', 'xx', '3a'); 4730insert into t_out values ('B.4', 'xx', '3a', '3a'); 4731insert into t_in values ('1a', '1a', '1a'); 4732insert into t_in values ('2a', '2a', '2a'); 4733insert into t_in values (NULL, '2a', '2a'); 4734insert into t_in values ('3a', NULL, '3a'); 4735 4736Test general IN semantics (not top-level) 4737 4738case A.1 4739select subcase, 4740(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4741(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4742from t_out where subcase = 'A.1'; 4743subcase pred_in pred_not_in 4744A.1 0 1 4745case A.2 - impossible 4746case A.3 4747select subcase, 4748(a1, b1, c1) IN (select * from t_in) pred_in, 4749(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4750from t_out where subcase = 'A.3'; 4751subcase pred_in pred_not_in 4752A.3 NULL NULL 4753case A.4 4754select subcase, 4755(a1, b1, c1) IN (select * from t_in) pred_in, 4756(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4757from t_out where subcase = 'A.4'; 4758subcase pred_in pred_not_in 4759A.4 0 1 4760case B.1 4761select subcase, 4762(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4763(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4764from t_out where subcase = 'B.1'; 4765subcase pred_in pred_not_in 4766B.1 0 1 4767case B.2 4768select subcase, 4769(a1, b1, c1) IN (select * from t_in) pred_in, 4770(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4771from t_out where subcase = 'B.2'; 4772subcase pred_in pred_not_in 4773B.2 1 0 4774case B.3 4775select subcase, 4776(a1, b1, c1) IN (select * from t_in) pred_in, 4777(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4778from t_out where subcase = 'B.3'; 4779subcase pred_in pred_not_in 4780B.3 NULL NULL 4781case B.4 4782select subcase, 4783(a1, b1, c1) IN (select * from t_in) pred_in, 4784(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4785from t_out where subcase = 'B.4'; 4786subcase pred_in pred_not_in 4787B.4 0 1 4788 4789Test IN as top-level predicate, and 4790as non-top level for cases A.3, B.3 (the only cases with NULL result). 4791 4792case A.1 4793select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4794where subcase = 'A.1' and 4795(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4796pred_in 4797F 4798select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4799where subcase = 'A.1' and 4800(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4801pred_not_in 4802T 4803select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4804where subcase = 'A.1' and 4805NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4806not_pred_in 4807T 4808case A.3 4809select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4810where subcase = 'A.3' and 4811(a1, b1, c1) IN (select * from t_in); 4812pred_in 4813F 4814select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4815where subcase = 'A.3' and 4816(a1, b1, c1) NOT IN (select * from t_in); 4817pred_not_in 4818F 4819select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4820where subcase = 'A.3' and 4821NOT((a1, b1, c1) IN (select * from t_in)); 4822not_pred_in 4823F 4824select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4825where subcase = 'A.3' and 4826((a1, b1, c1) IN (select * from t_in)) is NULL and 4827((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4828pred_in 4829N 4830case A.4 4831select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4832where subcase = 'A.4' and 4833(a1, b1, c1) IN (select * from t_in); 4834pred_in 4835F 4836select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4837where subcase = 'A.4' and 4838(a1, b1, c1) NOT IN (select * from t_in); 4839pred_not_in 4840T 4841select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4842where subcase = 'A.4' and 4843NOT((a1, b1, c1) IN (select * from t_in)); 4844not_pred_in 4845T 4846case B.1 4847select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4848where subcase = 'B.1' and 4849(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4850pred_in 4851F 4852select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4853where subcase = 'B.1' and 4854(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4855pred_not_in 4856T 4857select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4858where subcase = 'B.1' and 4859NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4860not_pred_in 4861T 4862case B.2 4863select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4864where subcase = 'B.2' and 4865(a1, b1, c1) IN (select * from t_in); 4866pred_in 4867T 4868select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4869where subcase = 'B.2' and 4870(a1, b1, c1) NOT IN (select * from t_in); 4871pred_not_in 4872F 4873select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4874where subcase = 'B.2' and 4875NOT((a1, b1, c1) IN (select * from t_in)); 4876not_pred_in 4877F 4878case B.3 4879select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4880where subcase = 'B.3' and 4881(a1, b1, c1) IN (select * from t_in); 4882pred_in 4883F 4884select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4885where subcase = 'B.3' and 4886(a1, b1, c1) NOT IN (select * from t_in); 4887pred_not_in 4888F 4889select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4890where subcase = 'B.3' and 4891NOT((a1, b1, c1) IN (select * from t_in)); 4892not_pred_in 4893F 4894select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4895where subcase = 'B.3' and 4896((a1, b1, c1) IN (select * from t_in)) is NULL and 4897((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4898pred_in 4899N 4900case B.4 4901select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4902where subcase = 'B.4' and 4903(a1, b1, c1) IN (select * from t_in); 4904pred_in 4905F 4906select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4907where subcase = 'B.4' and 4908(a1, b1, c1) NOT IN (select * from t_in); 4909pred_not_in 4910T 4911select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4912where subcase = 'B.4' and 4913NOT((a1, b1, c1) IN (select * from t_in)); 4914not_pred_in 4915T 4916drop table t_out; 4917drop table t_in; 4918CREATE TABLE t1 (a INT, b INT); 4919INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4920SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4921a 49221 49232 4924SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4925a 4926SELECT a FROM t1 t0 4927WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4928a 49291 49302 4931SET @@sql_mode='ansi'; 4932SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4933ERROR HY000: Invalid use of group function 4934SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4935ERROR HY000: Invalid use of group function 4936SELECT a FROM t1 t0 4937WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4938ERROR HY000: Invalid use of group function 4939SET @@sql_mode=default; 4940DROP TABLE t1; 4941CREATE TABLE t1 (s1 CHAR(1)); 4942INSERT INTO t1 VALUES ('a'); 4943SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1); 4944s1 4945a 4946DROP TABLE t1; 4947CREATE TABLE t1(c INT, KEY(c)); 4948CREATE TABLE t2(a INT, b INT); 4949INSERT INTO t2 VALUES (1, 10), (2, NULL); 4950INSERT INTO t1 VALUES (1), (3); 4951SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10); 4952a b 4953DROP TABLE t1,t2; 4954CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a)); 4955INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20); 4956CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a)); 4957INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100); 4958SELECT * FROM t1 4959WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b); 4960pk a 49611 10 4962DROP TABLE t1,t2; 4963CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b)); 4964INSERT INTO t1 VALUES (1,NULL), (9,NULL); 4965CREATE TABLE t2 ( 4966a INT, 4967b INT, 4968c INT, 4969d INT, 4970PRIMARY KEY (a), 4971UNIQUE KEY b (b,c,d), 4972KEY b_2 (b), 4973KEY c (c), 4974KEY d (d) 4975); 4976INSERT INTO t2 VALUES 4977(43, 2, 11 ,30), 4978(44, 2, 12 ,30), 4979(45, 1, 1 ,10000), 4980(46, 1, 2 ,10000), 4981(556,1, 32 ,10000); 4982CREATE TABLE t3 ( 4983a INT, 4984b INT, 4985c INT, 4986PRIMARY KEY (a), 4987UNIQUE KEY b (b,c), 4988KEY c (c), 4989KEY b_2 (b) 4990); 4991INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2); 4992explain 4993SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a; 4994id select_type table type possible_keys key key_len ref rows Extra 49951 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index 49961 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index 49972 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index 4998SELECT 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; 4999a incorrect 50001 1 5001DROP TABLE t1,t2,t3; 5002CREATE TABLE t1 (id int); 5003CREATE TABLE t2 (id int, c int); 5004INSERT INTO t1 (id) VALUES (1); 5005INSERT INTO t2 (id) VALUES (1); 5006INSERT INTO t1 (id) VALUES (1); 5007INSERT INTO t2 (id) VALUES (1); 5008CREATE VIEW v1 AS 5009SELECT t2.c AS c FROM t1, t2 5010WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5011UPDATE v1 SET c=1; 5012CREATE VIEW v2 (a,b) AS 5013SELECT t2.id, t2.c AS c FROM t1, t2 5014WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5015INSERT INTO v2(a,b) VALUES (2,2); 5016ERROR 44000: CHECK OPTION failed `test`.`v2` 5017SELECT * FROM v1; 5018c 50191 50201 50211 50221 5023CREATE VIEW v3 AS 5024SELECT t2.c AS c FROM t2 5025WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5026DELETE FROM v3; 5027DROP VIEW v1,v2,v3; 5028DROP TABLE t1,t2; 5029# 5030# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5031# 5032create table t1(id integer primary key, g integer, v integer, s char(1)); 5033create table t2(id integer primary key, g integer, v integer, s char(1)); 5034insert into t1 values 5035(10, 10, 10, 'l'), 5036(20, 20, 20, 'l'), 5037(40, 40, 40, 'l'), 5038(41, 40, null, 'l'), 5039(50, 50, 50, 'l'), 5040(51, 50, null, 'l'), 5041(60, 60, 60, 'l'), 5042(61, 60, null, 'l'), 5043(70, 70, 70, 'l'), 5044(90, 90, null, 'l'); 5045insert into t2 values 5046(10, 10, 10, 'r'), 5047(30, 30, 30, 'r'), 5048(50, 50, 50, 'r'), 5049(60, 60, 60, 'r'), 5050(61, 60, null, 'r'), 5051(70, 70, 70, 'r'), 5052(71, 70, null, 'r'), 5053(80, 80, 80, 'r'), 5054(81, 80, null, 'r'), 5055(100,100,null, 'r'); 5056select * 5057from t1 5058where v in(select v 5059from t2 5060where t1.g=t2.g) is unknown; 5061id g v s 506251 50 NULL l 506361 60 NULL l 5064drop table t1, t2; 5065# 5066# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5067# 5068create table t1(id integer primary key, g integer, v integer, s char(1)); 5069create table t2(id integer primary key, g integer, v integer, s char(1)); 5070insert into t1 values 5071(10, 10, 10, 'l'), 5072(20, 20, 20, 'l'), 5073(40, 40, 40, 'l'), 5074(41, 40, null, 'l'), 5075(50, 50, 50, 'l'), 5076(51, 50, null, 'l'), 5077(60, 60, 60, 'l'), 5078(61, 60, null, 'l'), 5079(70, 70, 70, 'l'), 5080(90, 90, null, 'l'); 5081insert into t2 values 5082(10, 10, 10, 'r'), 5083(30, 30, 30, 'r'), 5084(50, 50, 50, 'r'), 5085(60, 60, 60, 'r'), 5086(61, 60, null, 'r'), 5087(70, 70, 70, 'r'), 5088(71, 70, null, 'r'), 5089(80, 80, 80, 'r'), 5090(81, 80, null, 'r'), 5091(100,100,null, 'r'); 5092select * 5093from t1 5094where v in(select v 5095from t2 5096where t1.g=t2.g) is unknown; 5097id g v s 509851 50 NULL l 509961 60 NULL l 5100drop table t1, t2; 5101# 5102# Bug#33204: INTO is allowed in subselect, causing inconsistent results 5103# 5104CREATE TABLE t1( a INT ); 5105INSERT INTO t1 VALUES (1),(2); 5106CREATE TABLE t2( a INT, b INT ); 5107SELECT * 5108FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a; 5109ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2 5110SELECT * 5111FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a; 5112ERROR 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 5113SELECT * 5114FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a; 5115ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2 5116SELECT * FROM ( 5117SELECT 1 a 5118UNION 5119SELECT a INTO @var FROM t1 WHERE a = 2 5120) t1a; 5121ERROR 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 5122) t1a' at line 4 5123SELECT * FROM ( 5124SELECT 1 a 5125UNION 5126SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 5127) t1a; 5128ERROR 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 5129) t1a' at line 4 5130SELECT * FROM ( 5131SELECT 1 a 5132UNION 5133SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 5134) t1a; 5135ERROR 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 5136) t1a' at line 4 5137SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a; 5138a 51392 5140SELECT * FROM ( 5141SELECT a FROM t1 WHERE a = 2 5142UNION 5143SELECT a FROM t1 WHERE a = 2 5144) t1a; 5145a 51462 5147SELECT * FROM ( 5148SELECT 1 a 5149UNION 5150SELECT a FROM t1 WHERE a = 2 5151UNION 5152SELECT a FROM t1 WHERE a = 2 5153) t1a; 5154a 51551 51562 5157SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q; 5158a 51591 5160SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; 5161a 51621 5163SELECT * FROM (SELECT 1 UNION SELECT 1) t1a; 51641 51651 5166SELECT * FROM ((SELECT 1 a INTO @a)) t1a; 5167ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1 5168SELECT * FROM ((SELECT 1 a INTO OUTFILE '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 OUTFILE 'file' )) t1a' at line 1 5170SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a; 5171ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1 5172SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a; 5173ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1 5174SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a; 5175ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1 5176SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a; 5177ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1 5178SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) 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 @a))) t1a' at line 1 5180SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE '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 DUMPFILE 'file' ))) t1a' at line 1 5182SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a; 5183ERROR 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 5184SELECT * FROM (SELECT 1 a ORDER BY a) t1a; 5185a 51861 5187SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a; 5188a 51891 5190SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a; 5191a 51921 5193SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a; 5194a 51951 5196SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1; 5197a 1 51981 1 51992 1 5200SELECT * FROM t1 JOIN ((SELECT 1 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 '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 UNION SELECT 1)) 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 'UNION SELECT 1)) 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)) t1a ON 1; 5209ERROR 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 5210SELECT * FROM t1 JOIN (t1 t1a) ON 1; 5211a a 52121 1 52132 1 52141 2 52152 2 5216SELECT * FROM t1 JOIN ((t1 t1a)) ON 1; 5217a a 52181 1 52192 1 52201 2 52212 2 5222SELECT * FROM (t1 t1a); 5223a 52241 52252 5226SELECT * FROM ((t1 t1a)); 5227a 52281 52292 5230SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1; 5231a t1a 52321 1 52332 1 5234SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1; 5235a t1a 52361 1 52372 1 5238SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1; 5239a a 52401 1 52412 1 5242SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1; 5243a a 52441 1 52452 1 5246SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2; 5247ERROR 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 5248SELECT * FROM t1 WHERE a = ALL ( SELECT 1 ); 5249a 52501 5251SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 ); 5252a 52531 5254SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 ); 5255a 52561 5257SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a); 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 @a)' at line 1 5259SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5261SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5262ERROR 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 5263SELECT * FROM t1 WHERE a = ( SELECT 1 ); 5264a 52651 5266SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 ); 5267a 52681 5269SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a); 5270ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1 5271SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' ); 5272ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1 5273SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' ); 5274ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5275SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a); 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 @a)' at line 1 5277SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' ); 5278ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1 5279SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5280ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5281SELECT ( SELECT 1 INTO @v ); 5282ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1 5283SELECT ( SELECT 1 INTO OUTFILE 'file' ); 5284ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1 5285SELECT ( SELECT 1 INTO DUMPFILE 'file' ); 5286ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5287SELECT ( SELECT 1 UNION SELECT 1 INTO @v ); 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 @v )' at line 1 5289SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5291SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5292ERROR 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 5293SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1; 5294( SELECT a FROM t1 WHERE a = 1 ) a 52951 1 52961 2 5297SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1; 5298( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a 52991 1 53001 2 5301SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2); 5302a b 5303SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ); 53041 53051 5306( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 53071 53081 5309SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5310( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ) 53111 5312SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 5313ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5314SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5315( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) 53161 5317SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1)); 5318((SELECT 1 UNION SELECT 1 UNION SELECT 1)) 53191 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 ); 5323ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5324SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a; 53251 53261 5327SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a; 53281 53291 5330SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5331a 53321 5333SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5334a 53351 5336SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5337a 53381 5339SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5340a 53411 5342SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5343a 53441 5345SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5346a 53471 5348SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5349a 53501 5351SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5352a 53531 5354SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5355a 53561 5357SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5358a 53591 5360SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5361a 53621 5363SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5364a 53651 5366SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v ); 5367ERROR 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 5368SELECT EXISTS(SELECT 1+1); 5369EXISTS(SELECT 1+1) 53701 5371SELECT EXISTS(SELECT 1+1 INTO @test); 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 @test)' at line 1 5373SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION 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 EXISTS ( 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 5377SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v ); 5378ERROR 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 5379DROP TABLE t1, t2; 5380CREATE TABLE t1 (a ENUM('rainbow')); 5381INSERT INTO t1 VALUES (),(),(),(),(); 5382SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID())); 53831 53841 5385DROP TABLE t1; 5386CREATE TABLE t1 (a LONGBLOB); 5387INSERT INTO t1 SET a = 'aaaa'; 5388INSERT INTO t1 SET a = 'aaaa'; 5389SELECT 1 FROM t1 GROUP BY 5390(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1); 53911 53921 5393DROP TABLE t1; 5394# 5395# Bug #49512 : subquery with aggregate function crash 5396# subselect_single_select_engine::exec() 5397CREATE TABLE t1(a INT); 5398INSERT INTO t1 VALUES(); 5399# should not crash 5400SELECT 1 FROM t1 WHERE a <> SOME 5401( 5402SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d 5403FROM t1,t1 a 5404); 54051 5406DROP TABLE t1; 5407# 5408# Bug #45989 take 2 : memory leak after explain encounters an 5409# error in the query 5410# 5411CREATE TABLE t1(a LONGTEXT); 5412INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet)); 5413INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet)); 5414EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1, 5415(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1 5416WHERE t1.a = d1.a; 5417ERROR 42S22: Unknown column 'd1.a' in 'where clause' 5418DROP TABLE t1; 5419Set up test tables. 5420CREATE TABLE t1 ( 5421t1_id INT UNSIGNED, 5422PRIMARY KEY(t1_id) 5423) Engine=MyISAM; 5424INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5); 5425CREATE TABLE t2 SELECT * FROM t1; 5426CREATE TABLE t3 ( 5427t3_id INT UNSIGNED AUTO_INCREMENT, 5428t1_id INT UNSIGNED, 5429amount DECIMAL(16,2), 5430PRIMARY KEY(t3_id), 5431KEY(t1_id) 5432) Engine=MyISAM; 5433INSERT INTO t3 (t1_id, t3_id, amount) 5434VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00); 5435This is the 'inner query' running by itself. 5436Produces correct results. 5437SELECT 5438t1.t1_id, 5439IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5440FROM 5441t1 5442LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5443GROUP BY 5444t1.t1_id 5445; 5446t1_id total_amount 54471 100.00 54482 200.00 54493 0.00 54504 400.00 54515 0.00 5452SELECT * FROM (the same inner query) 5453Produces correct results. 5454SELECT * FROM ( 5455SELECT 5456t1.t1_id, 5457IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5458FROM 5459t1 5460LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5461GROUP BY 5462t1.t1_id 5463) AS t; 5464t1_id total_amount 54651 100.00 54662 200.00 54673 0.00 54684 400.00 54695 0.00 5470Now make t2.t1_id part of a key. 5471ALTER TABLE t2 ADD PRIMARY KEY(t1_id); 5472Same inner query by itself. 5473Still correct results. 5474SELECT 5475t1.t1_id, 5476IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5477FROM 5478t1 5479LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5480GROUP BY 5481t1.t1_id; 5482t1_id total_amount 54831 100.00 54842 200.00 54853 0.00 54864 400.00 54875 0.00 5488SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN 5489SELECT * FROM ( 5490SELECT 5491t1.t1_id, 5492IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5493FROM 5494t1 5495LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5496GROUP BY 5497t1.t1_id 5498) AS t; 5499t1_id total_amount 55001 100.00 55012 200.00 55023 0.00 55034 400.00 55045 0.00 5505DROP TABLE t3; 5506DROP TABLE t2; 5507DROP TABLE t1; 5508# 5509# Bug #52711: Segfault when doing EXPLAIN SELECT with 5510# union...order by (select... where...) 5511# 5512CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a)); 5513INSERT INTO t1 VALUES (1),(2); 5514CREATE TABLE t2 (b INT); 5515INSERT INTO t2 VALUES (1),(2); 5516# Should not crash 5517EXPLAIN 5518SELECT * FROM t2 UNION SELECT * FROM t2 5519ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5520# Should not crash 5521SELECT * FROM t2 UNION SELECT * FROM t2 5522ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5523DROP TABLE t1,t2; 5524# 5525# Bug #58818: Incorrect result for IN/ANY subquery 5526# with HAVING condition 5527# 5528CREATE TABLE t1(i INT); 5529INSERT INTO t1 VALUES (1), (2), (3); 5530CREATE TABLE t1s(i INT); 5531INSERT INTO t1s VALUES (10), (20), (30); 5532CREATE TABLE t2s(i INT); 5533INSERT INTO t2s VALUES (100), (200), (300); 5534SELECT * FROM t1 5535WHERE t1.i NOT IN 5536( 5537SELECT STRAIGHT_JOIN t2s.i 5538FROM 5539t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5540HAVING t2s.i = 999 5541); 5542i 55431 55442 55453 5546SELECT * FROM t1 5547WHERE t1.I IN 5548( 5549SELECT STRAIGHT_JOIN t2s.i 5550FROM 5551t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5552HAVING t2s.i = 999 5553) IS UNKNOWN; 5554i 5555SELECT * FROM t1 5556WHERE NOT t1.I = ANY 5557( 5558SELECT STRAIGHT_JOIN t2s.i 5559FROM 5560t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5561HAVING t2s.i = 999 5562); 5563i 55641 55652 55663 5567SELECT * FROM t1 5568WHERE t1.i = ANY ( 5569SELECT STRAIGHT_JOIN t2s.i 5570FROM 5571t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5572HAVING t2s.i = 999 5573) IS UNKNOWN; 5574i 5575DROP TABLE t1,t1s,t2s; 5576# LP BUG#675248 - select->prep_where references on freed memory 5577CREATE TABLE t1 (a int, b int); 5578insert into t1 values (1,1),(0,0); 5579CREATE TABLE t2 (c int); 5580insert into t2 values (1),(2); 5581prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub 5582from t1 as table1 group by sub"; 5583execute stmt1; 5584sum(a) sub 55850 NULL 55861 3 5587deallocate prepare stmt1; 5588prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub 5589from t1 as table1"; 5590execute stmt1; 5591sum(a) sub 55921 3 5593deallocate prepare stmt1; 5594drop table t1,t2; 5595# 5596# Bug LP#693935/#58727: Assertion failure with 5597# a single row subquery returning more than one row 5598# 5599create table t1 (a char(1) charset utf8); 5600insert into t1 values ('a'), ('b'); 5601create table t2 (a binary(1)); 5602insert into t2 values ('x'), ('y'); 5603select * from t2 where a=(select a from t1) and a='x'; 5604ERROR 21000: Subquery returns more than 1 row 5605drop table t1,t2; 5606# End of 5.1 tests 5607# 5608# Bug #11765713 58705: 5609# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES 5610# CREATED BY OPT_SUM_QUERY 5611# 5612CREATE TABLE t1(a INT NOT NULL, KEY (a)); 5613INSERT INTO t1 VALUES (0), (1); 5614SELECT 1 as foo FROM t1 WHERE a < SOME 5615(SELECT a FROM t1 WHERE a <=> 5616(SELECT a FROM t1) 5617); 5618ERROR 21000: Subquery returns more than 1 row 5619SELECT 1 as foo FROM t1 WHERE a < SOME 5620(SELECT a FROM t1 WHERE a <=> 5621(SELECT a FROM t1 where a is null) 5622); 5623foo 5624DROP TABLE t1; 5625# 5626# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool): 5627# Assertion `file' failed. 5628# 5629CREATE TABLE t1 (a INT); 5630SELECT 1 FROM 5631(SELECT ROW( 5632(SELECT 1 FROM t1 RIGHT JOIN 5633(SELECT 1 FROM t1, t1 t2) AS d ON 1), 56341) FROM t1) AS e; 5635ERROR 21000: Operand should contain 1 column(s) 5636DROP TABLE t1; 5637# 5638# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY 5639# 5640CREATE TABLE t1(a TIME NOT NULL); 5641INSERT INTO t1 VALUES ('00:00:32'); 5642SELECT 1 FROM t1 WHERE a > 5643(SELECT timestamp(a) AS a FROM t1); 56441 5645DROP TABLE t1; 5646# 5647# No BUG#, a case brought from 5.2's innodb_mysql_lock.test 5648# 5649create table t1 (i int not null primary key); 5650insert into t1 values (1),(2),(3),(4),(5); 5651create table t2 (j int not null primary key); 5652insert into t2 values (1),(2),(3),(4),(5); 5653create table t3 (k int not null primary key); 5654insert into t3 values (1),(2),(3); 5655create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1); 5656select * from t3 where k in (select j from v2); 5657k 56581 56592 56603 5661drop table t1,t2,t3; 5662drop view v2; 5663# 5664# Bug#52068: Optimizer generates invalid semijoin materialization plan 5665# 5666drop table if exists ot1, ot2, it1, it2; 5667CREATE TABLE ot1(a INTEGER); 5668INSERT INTO ot1 VALUES(5), (8); 5669CREATE TABLE it2(a INTEGER); 5670INSERT INTO it2 VALUES(9), (5), (1), (8); 5671CREATE TABLE it3(a INTEGER); 5672INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4); 5673CREATE TABLE ot4(a INTEGER); 5674INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1); 5675SELECT * FROM ot1,ot4 5676WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5677FROM it2,it3); 5678a a 56795 1 56808 1 56815 5 56828 5 56835 7 56848 7 56855 7 56868 7 56875 1 56888 1 5689explain SELECT * FROM ot1,ot4 5690WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5691FROM it2,it3); 5692id select_type table type possible_keys key key_len ref rows Extra 56931 PRIMARY ot1 ALL NULL NULL NULL NULL 2 56941 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using join buffer (flat, BNL join) 56951 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 56962 MATERIALIZED it2 ALL NULL NULL NULL NULL 4 56972 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join) 5698DROP TABLE IF EXISTS ot1, ot4, it2, it3; 5699# 5700# Bug#729039: NULL keys used to evaluate subquery 5701# 5702CREATE TABLE t1 (a int) ; 5703INSERT INTO t1 VALUES (NULL), (1), (NULL), (2); 5704CREATE TABLE t2 (a int, INDEX idx(a)) ; 5705INSERT INTO t2 VALUES (NULL), (1), (NULL); 5706SELECT * FROM t1 5707WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a); 5708a 57091 5710EXPLAIN 5711SELECT * FROM t1 5712WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a); 5713id select_type table type possible_keys key key_len ref rows Extra 57141 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where 57152 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 Using where 5716SELECT * FROM t1 5717WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5718a 57191 5720EXPLAIN 5721SELECT * FROM t1 5722WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5723id select_type table type possible_keys key key_len ref rows Extra 57241 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where 57252 DEPENDENT SUBQUERY t2 ref idx idx 5 test.t1.a 2 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# 7401set optimizer_switch=default; 7402select @@optimizer_switch like '%exists_to_in=off%'; 7403@@optimizer_switch like '%exists_to_in=off%' 74040 7405