1select @@optimizer_switch like '%subquery_cache=on%'; 2@@optimizer_switch like '%subquery_cache=on%' 31 4set optimizer_switch='subquery_cache=off'; 5set join_cache_level=1; 6set @join_cache_level_for_subselect_test=@@join_cache_level; 7drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12; 8drop view if exists v2; 9call mtr.add_suppression("Sort aborted.*"); 10set @subselect_tmp=@@optimizer_switch; 11set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test, 12"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off"); 13set join_cache_level=@join_cache_level_for_subselect_test; 14SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; 15SET optimizer_use_condition_selectivity=4; 16select (select 2); 17(select 2) 182 19explain extended select (select 2); 20id select_type table type possible_keys key key_len ref rows filtered Extra 211 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used 22Warnings: 23Note 1249 Select 2 was reduced during optimization 24Note 1003 select 2 AS `(select 2)` 25SELECT (SELECT 1) UNION SELECT (SELECT 2); 26(SELECT 1) 271 282 29explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2); 30id select_type table type possible_keys key key_len ref rows filtered Extra 311 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 323 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 33NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 34Warnings: 35Note 1249 Select 2 was reduced during optimization 36Note 1249 Select 4 was reduced during optimization 37Note 1003 /* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)` 38SELECT (SELECT (SELECT 0 UNION SELECT 0)); 39(SELECT (SELECT 0 UNION SELECT 0)) 400 41explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0)); 42id select_type table type possible_keys key key_len ref rows filtered Extra 431 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 443 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 454 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 46NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL 47Warnings: 48Note 1249 Select 2 was reduced during optimization 49Note 1003 /* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))` 50SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a; 51ERROR 42S22: Reference 'a' not supported (forward reference in item list) 52SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b; 53ERROR 42S22: Reference 'b' not supported (forward reference in item list) 54SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a; 55(SELECT 1) MAX(1) 561 1 57SELECT (SELECT a) as a; 58ERROR 42S22: Reference 'a' not supported (forward reference in item list) 59EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 60id select_type table type possible_keys key key_len ref rows filtered Extra 611 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00 623 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 632 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 64Warnings: 65Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 66Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 67Note 1003 /* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1 68SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 691 701 71SELECT (SELECT 1), a; 72ERROR 42S22: Unknown column 'a' in 'field list' 73SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; 74a 751 76SELECT 1 FROM (SELECT (SELECT a) b) c; 77ERROR 42S22: Unknown column 'a' in 'field list' 78SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id); 79id 801 81SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1); 82ERROR 21000: Operand should contain 1 column(s) 83SELECT 1 IN (SELECT 1); 841 IN (SELECT 1) 851 86SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a)); 871 881 89select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1)); 90ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PROCEDURE ANALYSE(1))' at line 1 91SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1)); 92ERROR 42000: PROCEDURE does not support subqueries or stored functions 93SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL; 94ERROR 42S22: Unknown column 'a' in 'field list' 95SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL; 96ERROR 42S22: Unknown column 'a' in 'field list' 97SELECT (SELECT 1,2,3) = ROW(1,2,3); 98(SELECT 1,2,3) = ROW(1,2,3) 991 100SELECT (SELECT 1,2,3) = ROW(1,2,1); 101(SELECT 1,2,3) = ROW(1,2,1) 1020 103SELECT (SELECT 1,2,3) < ROW(1,2,1); 104(SELECT 1,2,3) < ROW(1,2,1) 1050 106SELECT (SELECT 1,2,3) > ROW(1,2,1); 107(SELECT 1,2,3) > ROW(1,2,1) 1081 109SELECT (SELECT 1,2,3) = ROW(1,2,NULL); 110(SELECT 1,2,3) = ROW(1,2,NULL) 111NULL 112SELECT ROW(1,2,3) = (SELECT 1,2,3); 113ROW(1,2,3) = (SELECT 1,2,3) 1141 115SELECT ROW(1,2,3) = (SELECT 1,2,1); 116ROW(1,2,3) = (SELECT 1,2,1) 1170 118SELECT ROW(1,2,3) < (SELECT 1,2,1); 119ROW(1,2,3) < (SELECT 1,2,1) 1200 121SELECT ROW(1,2,3) > (SELECT 1,2,1); 122ROW(1,2,3) > (SELECT 1,2,1) 1231 124SELECT ROW(1,2,3) = (SELECT 1,2,NULL); 125ROW(1,2,3) = (SELECT 1,2,NULL) 126NULL 127SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a'); 128(SELECT 1.5,2,'a') = ROW(1.5,2,'a') 1291 130SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b'); 131(SELECT 1.5,2,'a') = ROW(1.5,2,'b') 1320 133SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b'); 134(SELECT 1.5,2,'a') = ROW('1.5b',2,'b') 1350 136Warnings: 137Warning 1292 Truncated incorrect DOUBLE value: '1.5b' 138SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a'); 139(SELECT 'b',2,'a') = ROW(1.5,2,'a') 1400 141Warnings: 142Warning 1292 Truncated incorrect DOUBLE value: 'b' 143SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a'); 144(SELECT 1.5,2,'a') = ROW(1.5,'2','a') 1451 146SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a'); 147(SELECT 1.5,'c','a') = ROW(1.5,2,'a') 1480 149Warnings: 150Warning 1292 Truncated incorrect DOUBLE value: 'c' 151SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a); 152ERROR 21000: Operand should contain 1 column(s) 153SELECT 1 as a,(SELECT a+a) b,(SELECT b); 154a b (SELECT b) 1551 2 2 156create table t1 (a int); 157create table t2 (a int, b int); 158create table t3 (a int); 159create table t4 (a int not null, b int not null); 160insert into t1 values (2); 161insert into t2 values (1,7),(2,7); 162insert into t4 values (4,8),(3,8),(5,9); 163select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1; 164ERROR 42S22: Reference 'a1' not supported (forward reference in item list) 165select (select a from t1 where t1.a=t2.a), a from t2; 166(select a from t1 where t1.a=t2.a) a 167NULL 1 1682 2 169select (select a from t1 where t1.a=t2.b), a from t2; 170(select a from t1 where t1.a=t2.b) a 171NULL 1 172NULL 2 173select (select a from t1), a, (select 1 union select 2 limit 1) from t2; 174(select a from t1) a (select 1 union select 2 limit 1) 1752 1 1 1762 2 1 177select (select a from t3), a from t2; 178(select a from t3) a 179NULL 1 180NULL 2 181select * from t2 where t2.a=(select a from t1); 182a b 1832 7 184insert into t3 values (6),(7),(3); 185select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1); 186a b 1871 7 1882 7 189(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) 190union (select * from t4 order by a limit 2) order by a limit 3; 191a b 1921 7 1932 7 1943 8 195(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); 196a b 1971 7 1982 7 1994 8 2003 8 201explain 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); 202id select_type table type possible_keys key key_len ref rows filtered Extra 2031 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 2042 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort 2053 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where 2064 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 207NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 208Warnings: 209Note 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`)) 210select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2; 211(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a 2123 1 2137 2 214select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 215(select * from t2 where a>1) as tt; 216(select t3.a from t3 where a<8 order by 1 desc limit 1) a 2177 2 218set @tmp_optimizer_switch=@@optimizer_switch; 219set optimizer_switch='derived_merge=off,derived_with_keys=off'; 220explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 221(select * from t2 where a>1) as tt; 222id select_type table type possible_keys key key_len ref rows filtered Extra 2231 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00 2243 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where 2252 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort 226Warnings: 227Note 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` 228set optimizer_switch=@tmp_optimizer_switch; 229select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1); 230a 2312 232select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1); 233a 2342 235select * 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); 236a 237select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 238b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) 2398 7.5000 2408 4.5000 2419 7.5000 242explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 243id select_type table type possible_keys key key_len ref rows filtered Extra 2441 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00 2452 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 2463 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 247Warnings: 248Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1 249Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,(/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4` 250select * from t3 where exists (select * from t2 where t2.b=t3.a); 251a 2527 253select * from t3 where not exists (select * from t2 where t2.b=t3.a); 254a 2556 2563 257select * from t3 where a in (select b from t2); 258a 2597 260select * from t3 where a not in (select b from t2); 261a 2626 2633 264select * from t3 where a = some (select b from t2); 265a 2667 267select * from t3 where a <> any (select b from t2); 268a 2696 2703 271select * from t3 where a = all (select b from t2); 272a 2737 274select * from t3 where a <> all (select b from t2); 275a 2766 2773 278insert into t2 values (100, 5); 279select * from t3 where a < any (select b from t2); 280a 2816 2823 283select * from t3 where a < all (select b from t2); 284a 2853 286select * from t3 where a >= any (select b from t2); 287a 2886 2897 290explain extended select * from t3 where a >= any (select b from t2); 291id select_type table type possible_keys key key_len ref rows filtered Extra 2921 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 2932 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 294Warnings: 295Note 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`))) 296select * from t3 where a >= all (select b from t2); 297a 2987 299delete from t2 where a=100; 300select * from t3 where a in (select a,b from t2); 301ERROR 21000: Operand should contain 1 column(s) 302select * from t3 where a in (select * from t2); 303ERROR 21000: Operand should contain 1 column(s) 304insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10); 305select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b); 306b ma 307insert into t2 values (2,10); 308select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b); 309b ma 31010 1 311delete from t2 where a=2 and b=10; 312select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b); 313b ma 3147 12 315create table t5 (a int); 316select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 317(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 318NULL 1 3192 2 320insert into t5 values (5); 321select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 322(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 323NULL 1 3242 2 325insert into t5 values (2); 326select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 327(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 328NULL 1 3292 2 330explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 331id select_type table type possible_keys key key_len ref rows filtered Extra 3321 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 3332 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 3343 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where 335NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 336Warnings: 337Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1 338Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1 339Note 1003 /* select#1 */ select (/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2` 340select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2; 341ERROR 21000: Subquery returns more than 1 row 342create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq)); 343create table t7( uq int primary key, name char(25)); 344insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta"); 345insert into t6 values (1,1),(1,2),(2,2),(1,3); 346select * from t6 where exists (select * from t7 where uq = clinic_uq); 347patient_uq clinic_uq 3481 1 3491 2 3502 2 351explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq); 352id select_type table type possible_keys key key_len ref rows filtered Extra 3531 PRIMARY t7 index PRIMARY PRIMARY 4 NULL 2 100.00 Using index 3541 PRIMARY t6 ALL i1 NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) 355Warnings: 356Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 357Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t7` join `test`.`t6` where `test`.`t6`.`clinic_uq` = `test`.`t7`.`uq` 358select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); 359ERROR 23000: Column 'a' in field list is ambiguous 360drop table t1,t2,t3; 361CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0'); 362INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b'); 363CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0'); 364INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2'); 365CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00'); 366INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13'); 367SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1); 368a b 369W 1732-02-22 370SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2); 371a b 372W 1 373SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3); 374a b 375W a 376CREATE TABLE `t8` ( 377`pseudo` varchar(35) character set latin1 NOT NULL default '', 378`email` varchar(60) character set latin1 NOT NULL default '', 379PRIMARY KEY (`pseudo`), 380UNIQUE KEY `email` (`email`) 381) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 382INSERT INTO t8 (pseudo,email) VALUES ('joce','test'); 383INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1'); 384INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1'); 385EXPLAIN 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'); 386id select_type table type possible_keys key key_len ref rows filtered Extra 3871 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3884 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3892 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 3903 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 391Warnings: 392Note 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 393SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM 394t8 WHERE pseudo='joce'); 395ERROR HY000: Illegal parameter data types varchar and row for operation '=' 396SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE 397pseudo='joce'); 398ERROR HY000: Illegal parameter data types varchar and row for operation '=' 399SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); 400pseudo 401joce 402SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%'); 403ERROR 21000: Subquery returns more than 1 row 404drop table if exists t1,t2,t3,t4,t5,t6,t7,t8; 405CREATE TABLE `t1` ( 406`topic` mediumint(8) unsigned NOT NULL default '0', 407`date` date NOT NULL default '0000-00-00', 408`pseudo` varchar(35) character set latin1 NOT NULL default '', 409PRIMARY KEY (`pseudo`,`date`,`topic`), 410KEY `topic` (`topic`) 411) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 412INSERT INTO t1 (topic,date,pseudo) VALUES 413('43506','2002-10-02','joce'),('40143','2002-08-03','joce'); 414EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 415id select_type table type possible_keys key key_len ref rows filtered Extra 4161 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 417Warnings: 418Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03' 419EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 420id select_type table type possible_keys key key_len ref rows filtered Extra 4211 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4222 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 423Warnings: 424Note 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')` 425SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 426date 4272002-08-03 428SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 429(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03') 4302002-08-03 431SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1; 4321 4331 4341 4351 436SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1; 437ERROR 21000: Subquery returns more than 1 row 438EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1); 439id select_type table type possible_keys key key_len ref rows filtered Extra 4401 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index 4412 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4423 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 443NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 444Warnings: 445Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 446drop table t1; 447CREATE TABLE `t1` ( 448`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 449`maxnumrep` int(10) unsigned NOT NULL default '0', 450PRIMARY KEY (`numeropost`), 451UNIQUE KEY `maxnumrep` (`maxnumrep`) 452) ENGINE=MyISAM ROW_FORMAT=FIXED; 453INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2); 454CREATE TABLE `t2` ( 455`mot` varchar(30) NOT NULL default '', 456`topic` mediumint(8) unsigned NOT NULL default '0', 457`date` date NOT NULL default '0000-00-00', 458`pseudo` varchar(35) NOT NULL default '', 459PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`) 460) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 461INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce'); 462select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 463a 46440143 465SELECT 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; 466numeropost maxnumrep 46743506 2 46840143 1 469SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b; 470ERROR 42S22: Unknown column 'a' in 'having clause' 471SELECT 1 IN (SELECT 1 FROM t2 HAVING a); 472ERROR 42S22: Unknown column 'a' in 'having clause' 473SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic); 474mot topic date pseudo 475joce 40143 2002-10-22 joce 476joce 43506 2002-10-22 joce 477SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 478mot topic date pseudo 479SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1); 480mot topic date pseudo 481SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic); 482mot topic date pseudo 483joce 40143 2002-10-22 joce 484joce 43506 2002-10-22 joce 485SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 486mot topic date pseudo 487SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1); 488mot topic date pseudo 489SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic); 490mot topic date pseudo 491SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 492mot topic date pseudo 493joce 40143 2002-10-22 joce 494joce 43506 2002-10-22 joce 495SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2; 496mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) 497joce 40143 2002-10-22 joce 1 498joce 43506 2002-10-22 joce 1 499SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2); 500mot topic date pseudo 501SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2); 502mot topic date pseudo 503joce 40143 2002-10-22 joce 504joce 43506 2002-10-22 joce 505SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 506mot topic date pseudo 507joce 40143 2002-10-22 joce 508SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 509mot topic date pseudo 510joce 40143 2002-10-22 joce 511SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 512mot topic date pseudo 513joce 40143 2002-10-22 joce 514SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2; 515mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) 516joce 40143 2002-10-22 joce 1 517joce 43506 2002-10-22 joce 0 518drop table t1,t2; 519CREATE TABLE `t1` ( 520`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 521`maxnumrep` int(10) unsigned NOT NULL default '0', 522PRIMARY KEY (`numeropost`), 523UNIQUE KEY `maxnumrep` (`maxnumrep`) 524) ENGINE=MyISAM ROW_FORMAT=FIXED; 525INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1); 526select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 527ERROR 21000: Subquery returns more than 1 row 528select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1); 529ERROR 21000: Subquery returns more than 1 row 530show warnings; 531Level Code Message 532Error 1242 Subquery returns more than 1 row 533Error 1028 Sort aborted: Subquery returns more than 1 row 534drop table t1; 535create table t1 (a int); 536insert into t1 values (1),(2),(3); 537(select * from t1) union (select * from t1) order by (select a from t1 limit 1); 538a 5391 5402 5413 542drop table t1; 543CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b'); 544INSERT INTO t1 VALUES (); 545SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b'); 546ERROR 21000: Subquery returns more than 1 row 547drop table t1; 548CREATE TABLE `t1` ( 549`numeropost` mediumint(8) unsigned NOT NULL default '0', 550`numreponse` int(10) unsigned NOT NULL auto_increment, 551`pseudo` varchar(35) NOT NULL default '', 552PRIMARY KEY (`numeropost`,`numreponse`), 553UNIQUE KEY `numreponse` (`numreponse`), 554KEY `pseudo` (`pseudo`,`numeropost`) 555) ENGINE=MyISAM; 556SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a; 557ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list) 558SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a; 559ERROR 42S22: Unknown column 'a' in 'having clause' 560SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a; 561numreponse (SELECT numeropost FROM t1 HAVING numreponse=1) 562INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test'); 563EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 564ERROR 21000: Subquery returns more than 1 row 565SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 566ERROR 21000: Subquery returns more than 1 row 567EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'; 568id select_type table type possible_keys key key_len ref rows filtered Extra 5691 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 570Warnings: 571Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1' 572EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'); 573id select_type table type possible_keys key key_len ref rows filtered Extra 5741 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using index 5752 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 576Warnings: 577Note 1003 /* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1 578drop table t1; 579CREATE TABLE t1 (a int(1)); 580INSERT INTO t1 VALUES (1); 581SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1; 5821 5831 584drop table t1; 585create table t1 (a int NOT NULL, b int, primary key (a)); 586create table t2 (a int NOT NULL, b int, primary key (a)); 587insert into t1 values (0, 10),(1, 11),(2, 12); 588insert into t2 values (1, 21),(2, 22),(3, 23); 589select * from t1; 590a b 5910 10 5921 11 5932 12 594update t1 set b= (select b from t2); 595ERROR 21000: Subquery returns more than 1 row 596update t1 set b= (select b from t2 where t1.a = t2.a); 597select * from t1; 598a b 5990 NULL 6001 21 6012 22 602drop table t1, t2; 603create table t1 (a int NOT NULL, b int, primary key (a)); 604create table t2 (a int NOT NULL, b int, primary key (a)); 605insert into t1 values (0, 10),(1, 11),(2, 12); 606insert into t2 values (1, 21),(2, 12),(3, 23); 607select * from t1; 608a b 6090 10 6101 11 6112 12 612select * from t1 where b = (select b from t2 where t1.a = t2.a); 613a b 6142 12 615delete from t1 where b in (select b from t1); 616affected rows: 3 617insert into t1 values (0, 10),(1, 11),(2, 12); 618delete from t1 where b = (select b from t2); 619ERROR 21000: Subquery returns more than 1 row 620delete from t1 where b = (select b from t2 where t1.a = t2.a); 621select * from t1 order by b; 622a b 6230 10 6241 11 625drop table t1, t2; 626create table t11 (a int NOT NULL, b int, primary key (a)); 627create table t12 (a int NOT NULL, b int, primary key (a)); 628create table t2 (a int NOT NULL, b int, primary key (a)); 629insert into t11 values (0, 10),(1, 11),(2, 12); 630insert into t12 values (33, 10),(22, 11),(2, 12); 631insert into t2 values (1, 21),(2, 12),(3, 23); 632select * from t11; 633a b 6340 10 6351 11 6362 12 637select * from t12; 638a b 63933 10 64022 11 6412 12 642delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); 643ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data 644delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2); 645ERROR 21000: Subquery returns more than 1 row 646delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); 647select * from t11; 648a b 6490 10 6501 11 651select * from t12; 652a b 65333 10 65422 11 655drop table t11, t12, t2; 656CREATE TABLE t1 (x int) ENGINE=MyISAM; 657create table t2 (a int) ENGINE=MyISAM; 658create table t3 (b int); 659insert into t2 values (1); 660insert into t3 values (1),(2); 661INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); 662ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data 663INSERT INTO t1 (x) VALUES ((SELECT b FROM t3)); 664ERROR 21000: Subquery returns more than 1 row 665INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); 666select * from t1; 667x 6681 669insert into t2 values (1); 670INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); 671select * from t1; 672x 6731 6742 675INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2; 676select * from t1; 677x 6781 6792 6803 6813 682INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; 683select * from t1; 684x 6851 6862 6873 6883 68911 69011 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 <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`,<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`,<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 range 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`,!(<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`,<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`,!(<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`,!(<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 !(<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 !<in_optimizer>(1,exists(/* select#2 */ select `test`.`t1`.`id` from `test`.`t1` where `test`.`t1`.`id` < 8 and `test`.`t1`.`id` = `test`.`tt`.`id` having `test`.`t1`.`id` is not null limit 1)) 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 24351 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 24362 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 2437Warnings: 2438Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 2439Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` semi join (`test`.`t1`) where 1 2440drop table t1; 2441CREATE TABLE t1 (t1_a int); 2442INSERT INTO t1 VALUES (1); 2443CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b)); 2444INSERT INTO t2 VALUES (1, 1), (1, 2); 2445SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1 2446HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a); 2447t1_a t2_a t2_b 24481 1 2 2449DROP TABLE t1, t2; 2450CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL); 2451INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL); 2452CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL); 2453INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix'); 2454SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id; 2455id name id pet 24561 Tim 1 Fido 24572 Rebecca 2 Spot 24583 NULL 3 Felix 2459drop table t1,t2; 2460CREATE TABLE t1 ( a int, b int ); 2461CREATE TABLE t2 ( c int, d int ); 2462INSERT INTO t1 VALUES (1,2), (2,3), (3,4); 2463SELECT a AS abc, b FROM t1 outr WHERE b = 2464(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2465abc b 24661 2 24672 3 24683 4 2469INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = 2470(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2471select * from t2; 2472c d 24731 2 24742 3 24753 4 2476CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = 2477(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2478select * from t3; 2479abc b 24801 2 24812 3 24823 4 2483prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2484execute stmt1; 2485deallocate prepare stmt1; 2486select * from t2; 2487c d 24881 2 24892 3 24903 4 24911 2 24922 3 24933 4 2494drop table t3; 2495prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2496execute stmt1; 2497select * from t3; 2498abc b 24991 2 25002 3 25013 4 2502deallocate prepare stmt1; 2503DROP TABLE t1, t2, t3; 2504CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2505insert into t1 values (1); 2506CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2507insert into t2 values (1,2); 2508select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1); 2509a C 25101 1 2511drop table t1,t2; 2512create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b)); 2513insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan'); 2514create table t2 (a int); 2515insert into t2 values (1),(3),(2),(7); 2516select a,b from t1 where match(b) against ('Ball') > 0; 2517a b 25181 ball 25192 ball games 2520select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0); 2521a 25221 25232 2524drop table t1,t2; 2525CREATE 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); 2526CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel); 2527INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001'); 2528INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001'); 2529INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001'); 2530INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001'); 2531SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001'); 2532IZAVORGANG_ID 2533D0000000001 2534drop table t1; 2535CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2536CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2537insert into t1 values (1,1),(1,2),(2,1),(2,2); 2538insert into t2 values (1,2),(2,2); 2539select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2540aid bid 25411 1 25422 1 2543alter table t2 drop primary key; 2544alter table t2 add key KEY1 (aid, bid); 2545select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2546aid bid 25471 1 25482 1 2549alter table t2 drop key KEY1; 2550alter table t2 add primary key (bid, aid); 2551select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2552aid bid 25531 1 25542 1 2555drop table t1,t2; 2556CREATE TABLE t1 (howmanyvalues bigint, avalue int); 2557INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4); 2558SELECT howmanyvalues, count(*) from t1 group by howmanyvalues; 2559howmanyvalues count(*) 25601 1 25612 2 25623 3 25634 4 2564SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; 2565howmanyvalues mycount 25661 1 25672 2 25683 3 25694 4 2570CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues); 2571SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues; 2572howmanyvalues mycount 25731 1 25742 2 25753 3 25764 4 2577SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; 2578howmanyvalues mycount 25791 1 25802 2 25813 3 25824 4 2583SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues; 2584howmanyvalues mycount 25851 1 25862 1 25873 1 25884 1 2589drop table t1; 2590create table t1 (x int); 2591select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x; 2592(select b.x from t1 as b where b.x=a.x) 2593drop table t1; 2594CREATE 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`)); 2595INSERT 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); 2596CREATE 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`)) ; 2597INSERT 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'); 2598SELECT 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; 2599ERROR 42S22: Unknown column 'b.sc' in 'field list' 2600SELECT 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; 2601ac 2602700 2603NULL 2604drop tables t1,t2; 2605create table t1 (a int not null, b int not null, c int, primary key (a,b)); 2606insert into t1 values (1,1,1), (2,2,2), (3,3,3); 2607set @b:= 0; 2608explain select sum(a) from t1 where b > @b; 2609id select_type table type possible_keys key key_len ref rows Extra 26101 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index 2611set @a:= (select sum(a) from t1 where b > @b); 2612explain select a from t1 where c=2; 2613id select_type table type possible_keys key key_len ref rows Extra 26141 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2615do @a:= (select sum(a) from t1 where b > @b); 2616explain select a from t1 where c=2; 2617id select_type table type possible_keys key key_len ref rows Extra 26181 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2619drop table t1; 2620connect root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK; 2621connection root; 2622set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; 2623disconnect root; 2624connection default; 2625create table t1 (a int, b int); 2626create table t2 (a int, b int); 2627insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); 2628insert into t2 values (1,3),(2,1); 2629select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; 2630a b (select max(b) from t2 where t1.b=t2.a) 26311 1 3 26321 2 1 26331 3 NULL 26342 4 NULL 26352 5 NULL 2636drop table t1, t2; 2637create table t1 (id int); 2638create table t2 (id int, body text, fulltext (body)); 2639insert into t1 values(1),(2),(3); 2640insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test'); 2641select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode)); 2642count(distinct id) 26431 2644drop table t2,t1; 2645create table t1 (s1 int,s2 int); 2646insert into t1 values (20,15); 2647select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0)); 2648s1 s2 2649drop table t1; 2650create table t1 (s1 int); 2651insert into t1 values (1),(null); 2652select * from t1 where s1 < all (select s1 from t1); 2653s1 2654select s1, s1 < all (select s1 from t1) from t1; 2655s1 s1 < all (select s1 from t1) 26561 0 2657NULL NULL 2658drop table t1; 2659CREATE TABLE t1 ( 2660Code char(3) NOT NULL default '', 2661Name char(52) NOT NULL default '', 2662Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', 2663Region char(26) NOT NULL default '', 2664SurfaceArea float(10,2) NOT NULL default '0.00', 2665IndepYear smallint(6) default NULL, 2666Population int(11) NOT NULL default '0', 2667LifeExpectancy float(3,1) default NULL, 2668GNP float(10,2) default NULL, 2669GNPOld float(10,2) default NULL, 2670LocalName char(45) NOT NULL default '', 2671GovernmentForm char(45) NOT NULL default '', 2672HeadOfState char(60) default NULL, 2673Capital int(11) default NULL, 2674Code2 char(2) NOT NULL default '' 2675) ENGINE=MyISAM; 2676INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX'); 2677INSERT 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'); 2678INSERT 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'); 2679INSERT 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'); 2680/*!40000 ALTER TABLE t1 ENABLE KEYS */; 2681SELECT DISTINCT Continent AS c FROM t1 outr WHERE 2682Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND 2683Population < 200); 2684c 2685Oceania 2686drop table t1; 2687create table t1 (a1 int); 2688create table t2 (b1 int); 2689select * from t1 where a2 > any(select b1 from t2); 2690ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery' 2691select * from t1 where a1 > any(select b1 from t2); 2692a1 2693drop table t1,t2; 2694create table t1 (a integer, b integer); 2695select (select * from t1) = (select 1,2); 2696(select * from t1) = (select 1,2) 2697NULL 2698select (select 1,2) = (select * from t1); 2699(select 1,2) = (select * from t1) 2700NULL 2701select row(1,2) = ANY (select * from t1); 2702row(1,2) = ANY (select * from t1) 27030 2704select row(1,2) != ALL (select * from t1); 2705row(1,2) != ALL (select * from t1) 27061 2707drop table t1; 2708create table t1 (a integer, b integer); 2709select row(1,(2,2)) in (select * from t1 ); 2710ERROR 21000: Operand should contain 2 column(s) 2711select row(1,(2,2)) = (select * from t1 ); 2712ERROR 21000: Operand should contain 2 column(s) 2713select (select * from t1) = row(1,(2,2)); 2714ERROR 21000: Operand should contain 1 column(s) 2715drop table t1; 2716create table t1 (a integer); 2717insert into t1 values (1); 2718select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ; 2719ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2720select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2721ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2722select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx ); 2723xx 1 = ALL ( select 1 from t1 where 1 = xx ) 27241 1 2725select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2726ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2727select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL; 2728ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2729drop table t1; 2730CREATE TABLE t1 ( 2731categoryId int(11) NOT NULL, 2732courseId int(11) NOT NULL, 2733startDate datetime NOT NULL, 2734endDate datetime NOT NULL, 2735createDate datetime NOT NULL, 2736modifyDate timestamp NOT NULL, 2737attributes text NOT NULL 2738); 2739INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2740(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2741(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2742(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''), 2743(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''), 2744(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2745(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2746(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2747(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18',''); 2748CREATE TABLE t2 ( 2749userId int(11) NOT NULL, 2750courseId int(11) NOT NULL, 2751date datetime NOT NULL 2752); 2753INSERT INTO t2 VALUES (5141,71,'2003-11-18'), 2754(5141,72,'2003-11-25'),(5141,41,'2004-08-06'), 2755(5141,52,'2004-08-06'),(5141,53,'2004-08-06'), 2756(5141,12,'2004-08-06'),(5141,86,'2004-10-21'), 2757(5141,87,'2004-10-21'),(5141,88,'2004-10-21'), 2758(5141,89,'2004-10-22'),(5141,51,'2004-10-26'); 2759CREATE TABLE t3 ( 2760groupId int(11) NOT NULL, 2761parentId int(11) NOT NULL, 2762startDate datetime NOT NULL, 2763endDate datetime NOT NULL, 2764createDate datetime NOT NULL, 2765modifyDate timestamp NOT NULL, 2766ordering int(11) 2767); 2768INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL); 2769CREATE TABLE t4 ( 2770id int(11) NOT NULL, 2771groupTypeId int(11) NOT NULL, 2772groupKey varchar(50) NOT NULL, 2773name text, 2774ordering int(11), 2775description text, 2776createDate datetime NOT NULL, 2777modifyDate timestamp NOT NULL 2778); 2779INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'), 2780(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29'); 2781CREATE TABLE t5 ( 2782userId int(11) NOT NULL, 2783groupId int(11) NOT NULL, 2784createDate datetime NOT NULL, 2785modifyDate timestamp NOT NULL 2786); 2787INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06'); 2788select 2789count(distinct t2.userid) pass, 2790groupstuff.*, 2791count(t2.courseid) crse, 2792t1.categoryid, 2793t2.courseid, 2794date_format(date, '%b%y') as colhead 2795from t2 2796join t1 on t2.courseid=t1.courseid 2797join 2798( 2799select 2800t5.userid, 2801parentid, 2802parentgroup, 2803childid, 2804groupname, 2805grouptypeid 2806from t5 2807join 2808( 2809select t4.id as parentid, 2810t4.name as parentgroup, 2811t4.id as childid, 2812t4.name as groupname, 2813t4.grouptypeid 2814from t4 2815) as gin on t5.groupid=gin.childid 2816) as groupstuff on t2.userid = groupstuff.userid 2817group by 2818groupstuff.groupname, colhead , t2.courseid; 2819pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead 28201 5141 12 group2 12 group2 5 1 5 12 Aug04 28211 5141 12 group2 12 group2 5 1 1 41 Aug04 28221 5141 12 group2 12 group2 5 1 2 52 Aug04 28231 5141 12 group2 12 group2 5 1 2 53 Aug04 28241 5141 12 group2 12 group2 5 1 3 51 Oct04 28251 5141 12 group2 12 group2 5 1 1 86 Oct04 28261 5141 12 group2 12 group2 5 1 1 87 Oct04 28271 5141 12 group2 12 group2 5 1 2 88 Oct04 28281 5141 12 group2 12 group2 5 1 2 89 Oct04 2829drop table t1, t2, t3, t4, t5; 2830create table t1 (a int); 2831insert into t1 values (1), (2), (3); 2832SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1); 28331 28341 28351 28361 2837drop table t1; 2838create table t1 (a int); 2839create table t2 (a int); 2840insert into t1 values (1),(2); 2841insert into t2 values (0),(1),(2),(3); 2842select a from t2 where a in (select a from t1); 2843a 28441 28452 2846select a from t2 having a in (select a from t1); 2847a 28481 28492 2850prepare stmt1 from "select a from t2 where a in (select a from t1)"; 2851execute stmt1; 2852a 28531 28542 2855execute stmt1; 2856a 28571 28582 2859deallocate prepare stmt1; 2860prepare stmt1 from "select a from t2 having a in (select a from t1)"; 2861execute stmt1; 2862a 28631 28642 2865execute stmt1; 2866a 28671 28682 2869deallocate prepare stmt1; 2870drop table t1, t2; 2871create table t1 (a int, b int); 2872insert into t1 values (1,2); 2873select 1 = (select * from t1); 2874ERROR HY000: Illegal parameter data types int and row for operation '=' 2875select (select * from t1) = 1; 2876ERROR HY000: Illegal parameter data types row and int for operation '=' 2877select (1,2) = (select a from t1); 2878ERROR HY000: Illegal parameter data types row and int for operation '=' 2879select (select a from t1) = (1,2); 2880ERROR HY000: Illegal parameter data types int and row for operation '=' 2881select (1,2,3) = (select * from t1); 2882ERROR 21000: Operand should contain 3 column(s) 2883select (select * from t1) = (1,2,3); 2884ERROR 21000: Operand should contain 2 column(s) 2885drop table t1; 2886CREATE TABLE `t1` ( 2887`itemid` bigint(20) unsigned NOT NULL auto_increment, 2888`sessionid` bigint(20) unsigned default NULL, 2889`time` int(10) unsigned NOT NULL default '0', 2890`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 2891NULL default '', 2892`data` text collate latin1_general_ci NOT NULL, 2893PRIMARY KEY (`itemid`) 2894) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2895INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 2896CREATE TABLE `t2` ( 2897`sessionid` bigint(20) unsigned NOT NULL auto_increment, 2898`pid` int(10) unsigned NOT NULL default '0', 2899`date` int(10) unsigned NOT NULL default '0', 2900`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 2901PRIMARY KEY (`sessionid`) 2902) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2903INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 2904SELECT 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; 2905ip count( e.itemid ) 290610.10.10.1 1 2907drop tables t1,t2; 2908create table t1 (fld enum('0','1')); 2909insert into t1 values ('1'); 2910select * from (select max(fld) from t1) as foo; 2911max(fld) 29121 2913drop table t1; 2914set @tmp11867_optimizer_switch=@@optimizer_switch; 2915set optimizer_switch='semijoin_with_cache=off'; 2916CREATE TABLE t1 (one int, two int, flag char(1)); 2917CREATE TABLE t2 (one int, two int, flag char(1)); 2918INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2919INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2920SELECT * FROM t1 2921WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N'); 2922one two flag 29235 6 N 29247 8 N 2925SELECT * FROM t1 2926WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N'); 2927one two flag 29285 6 N 29297 8 N 2930insert into t2 values (null,null,'N'); 2931insert into t2 values (null,3,'0'); 2932insert into t2 values (null,5,'0'); 2933insert into t2 values (10,null,'0'); 2934insert into t1 values (10,3,'0'); 2935insert into t1 values (10,5,'0'); 2936insert into t1 values (10,10,'0'); 2937SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1; 2938one two test 29391 2 NULL 29402 3 NULL 29413 4 NULL 29425 6 1 29437 8 1 294410 3 NULL 294510 5 NULL 294610 10 NULL 2947SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 2948one two 29495 6 29507 8 2951SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1; 2952one two test 29531 2 NULL 29542 3 NULL 29553 4 NULL 29565 6 1 29577 8 1 295810 3 NULL 295910 5 NULL 296010 10 NULL 2961SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2962one two test 29631 2 0 29642 3 NULL 29653 4 0 29665 6 0 29677 8 0 296810 3 NULL 296910 5 NULL 297010 10 NULL 2971SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2972one two test 29731 2 0 29742 3 NULL 29753 4 0 29765 6 0 29777 8 0 297810 3 NULL 297910 5 NULL 298010 10 NULL 2981explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2982id select_type table type possible_keys key key_len ref rows filtered Extra 29831 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29842 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2985Warnings: 2986Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null))) AS `test` from `test`.`t1` 2987explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 2988id select_type table type possible_keys key key_len ref rows filtered Extra 29891 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29901 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 29912 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2992Warnings: 2993Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`flag` = 'N' 2994explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2995id select_type table type possible_keys key key_len ref rows filtered Extra 29961 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29972 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2998Warnings: 2999Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null))) AS `test` from `test`.`t1` 3000DROP TABLE t1,t2; 3001set optimizer_switch=@tmp11867_optimizer_switch; 3002CREATE TABLE t1 (a char(5), b char(5)); 3003INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa'); 3004SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb')); 3005a b 3006aaa aaa 3007DROP TABLE t1; 3008CREATE TABLE t1 (a int); 3009CREATE TABLE t2 (a int, b int); 3010CREATE TABLE t3 (b int NOT NULL); 3011INSERT INTO t1 VALUES (1), (2), (3), (4); 3012INSERT INTO t2 VALUES (1,10), (3,30); 3013SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3014WHERE t3.b IS NOT NULL OR t2.a > 10; 3015a b b 3016SELECT * FROM t1 3017WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3018WHERE t3.b IS NOT NULL OR t2.a > 10); 3019a 30201 30212 30223 30234 3024DROP TABLE t1,t2,t3; 3025CREATE TABLE t1 (f1 INT); 3026CREATE TABLE t2 (f2 INT); 3027INSERT INTO t1 VALUES (1); 3028SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2); 3029f1 30301 3031SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0); 3032f1 30331 3034INSERT INTO t2 VALUES (1); 3035INSERT INTO t2 VALUES (2); 3036SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0); 3037f1 30381 3039DROP TABLE t1, t2; 3040select 1 from dual where 1 < any (select 2); 30411 30421 3043select 1 from dual where 1 < all (select 2); 30441 30451 3046select 1 from dual where 2 > any (select 1); 30471 30481 3049select 1 from dual where 2 > all (select 1); 30501 30511 3052select 1 from dual where 1 < any (select 2 from dual); 30531 30541 3055select 1 from dual where 1 < all (select 2 from dual where 1!=1); 30561 30571 3058create table t1 (s1 char); 3059insert into t1 values (1),(2); 3060select * from t1 where (s1 < any (select s1 from t1)); 3061s1 30621 3063select * from t1 where not (s1 < any (select s1 from t1)); 3064s1 30652 3066select * from t1 where (s1 < ALL (select s1+1 from t1)); 3067s1 30681 3069select * from t1 where not(s1 < ALL (select s1+1 from t1)); 3070s1 30712 3072select * from t1 where (s1+1 = ANY (select s1 from t1)); 3073s1 30741 3075select * from t1 where NOT(s1+1 = ANY (select s1 from t1)); 3076s1 30772 3078select * from t1 where (s1 = ALL (select s1/s1 from t1)); 3079s1 30801 3081select * from t1 where NOT(s1 = ALL (select s1/s1 from t1)); 3082s1 30832 3084drop table t1; 3085create table t1 ( 3086retailerID varchar(8) NOT NULL, 3087statusID int(10) unsigned NOT NULL, 3088changed datetime NOT NULL, 3089UNIQUE KEY retailerID (retailerID, statusID, changed) 3090); 3091INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56"); 3092INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53"); 3093INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56"); 3094INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53"); 3095INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50"); 3096INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50"); 3097select * from t1 r1 3098where (r1.retailerID,(r1.changed)) in 3099(SELECT r2.retailerId,(max(changed)) from t1 r2 3100group by r2.retailerId); 3101retailerID statusID changed 31020026 2 2006-01-06 12:25:53 31030037 2 2006-01-06 12:25:53 31040048 1 2006-01-06 12:37:50 31050059 1 2006-01-06 12:37:50 3106drop table t1; 3107create table t1(a int, primary key (a)); 3108insert into t1 values (10); 3109create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b)); 3110insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989'); 3111insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999'); 3112analyze table t1; 3113Table Op Msg_type Msg_text 3114test.t1 analyze status Engine-independent statistics collected 3115test.t1 analyze status OK 3116explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3117ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3118 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3119id select_type table type possible_keys key key_len ref rows Extra 31201 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31211 PRIMARY r const PRIMARY PRIMARY 4 const 1 31222 SUBQUERY t2 range b b 40 NULL 3 Using where 3123SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3124ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3125 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3126a a b 312710 3 35989 3128explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3129ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3130 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3131id select_type table type possible_keys key key_len ref rows Extra 31321 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31331 PRIMARY r const PRIMARY PRIMARY 4 const 1 31342 SUBQUERY t2 range b b 40 NULL 3 Using index condition 3135SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3136ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3137 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3138a a b 313910 1 359 3140drop table t1,t2; 3141CREATE TABLE t1 ( 3142field1 int NOT NULL, 3143field2 int NOT NULL, 3144field3 int NOT NULL, 3145PRIMARY KEY (field1,field2,field3) 3146); 3147CREATE TABLE t2 ( 3148fieldA int NOT NULL, 3149fieldB int NOT NULL, 3150PRIMARY KEY (fieldA,fieldB) 3151); 3152INSERT INTO t1 VALUES 3153(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1); 3154INSERT INTO t2 VALUES (1,1), (1,2), (1,3); 3155SELECT field1, field2, COUNT(*) 3156FROM t1 GROUP BY field1, field2; 3157field1 field2 COUNT(*) 31581 1 2 31591 2 3 31601 3 1 3161SELECT field1, field2 3162FROM t1 3163GROUP BY field1, field2 3164HAVING COUNT(*) >= ALL (SELECT fieldB 3165FROM t2 WHERE fieldA = field1); 3166field1 field2 31671 2 3168SELECT field1, field2 3169FROM t1 3170GROUP BY field1, field2 3171HAVING COUNT(*) < ANY (SELECT fieldB 3172FROM t2 WHERE fieldA = field1); 3173field1 field2 31741 1 31751 3 3176DROP TABLE t1, t2; 3177CREATE TABLE t1(a int, INDEX (a)); 3178INSERT INTO t1 VALUES (1), (3), (5), (7); 3179INSERT INTO t1 VALUES (NULL); 3180CREATE TABLE t2(a int); 3181INSERT INTO t2 VALUES (1),(2),(3); 3182EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2; 3183id select_type table type possible_keys key key_len ref rows Extra 31841 PRIMARY t2 ALL NULL NULL NULL NULL 3 31852 SUBQUERY t1 index_subquery a a 5 func 3 Using index; Full scan on NULL key 3186SELECT a, a IN (SELECT a FROM t1) FROM t2; 3187a a IN (SELECT a FROM t1) 31881 1 31892 NULL 31903 1 3191DROP TABLE t1,t2; 3192CREATE TABLE t1 (a DATETIME); 3193INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25'); 3194CREATE TABLE t2 AS SELECT 3195(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a 3196FROM t1 WHERE a > '2000-01-01'; 3197SHOW CREATE TABLE t2; 3198Table Create Table 3199t2 CREATE TABLE `t2` ( 3200 `sub_a` datetime DEFAULT NULL 3201) ENGINE=MyISAM DEFAULT CHARSET=latin1 3202CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01'); 3203SHOW CREATE TABLE t3; 3204Table Create Table 3205t3 CREATE TABLE `t3` ( 3206 `a` datetime DEFAULT NULL 3207) ENGINE=MyISAM DEFAULT CHARSET=latin1 3208DROP TABLE t1,t2,t3; 3209CREATE TABLE t1 (a int); 3210INSERT INTO t1 VALUES (1), (2); 3211SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; 3212a 3213SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3214a 32151 32162 3217EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3218id select_type table type possible_keys key key_len ref rows Extra 32191 PRIMARY t1 ALL NULL NULL NULL NULL 2 32202 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 3221DROP TABLE t1; 3222CREATE TABLE t1 (a int); 3223INSERT INTO t1 VALUES (2), (4), (1), (3); 3224CREATE TABLE t2 (b int, c int); 3225INSERT INTO t2 VALUES 3226(2,1), (1,3), (2,1), (4,4), (2,2), (1,4); 3227SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); 3228a 32292 32304 32311 32323 3233SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); 3234ERROR 21000: Subquery returns more than 1 row 3235SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; 3236a 32371 32382 32393 32404 3241SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; 3242ERROR 21000: Subquery returns more than 1 row 3243SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); 3244b MAX(c) 32451 4 32462 2 32474 4 3248SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); 3249ERROR 21000: Subquery returns more than 1 row 3250SELECT a FROM t1 GROUP BY a 3251HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), 3252(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3253a 32541 32552 32563 32574 3258SELECT a FROM t1 GROUP BY a 3259HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), 3260(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3261ERROR 21000: Subquery returns more than 1 row 3262SELECT a FROM t1 GROUP BY a 3263HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3264(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3265a 32664 3267SELECT a FROM t1 GROUP BY a 3268HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3269(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; 3270ERROR 21000: Subquery returns more than 1 row 3271SELECT a FROM t1 3272ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), 3273(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3274a 32751 32762 32773 32784 3279SELECT a FROM t1 3280ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), 3281(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3282ERROR 21000: Subquery returns more than 1 row 3283SELECT a FROM t1 3284ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3285(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3286a 32871 32882 32893 32904 3291SELECT a FROM t1 3292ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3293(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3294ERROR 21000: Subquery returns more than 1 row 3295DROP TABLE t1,t2; 3296create table t1 (df decimal(5,1)); 3297insert into t1 values(1.1); 3298insert into t1 values(2.2); 3299select * from t1 where df <= all (select avg(df) from t1 group by df); 3300df 33011.1 3302select * from t1 where df >= all (select avg(df) from t1 group by df); 3303df 33042.2 3305drop table t1; 3306create table t1 (df decimal(5,1)); 3307insert into t1 values(1.1); 3308select 1.1 * exists(select * from t1); 33091.1 * exists(select * from t1) 33101.1 3311drop table t1; 3312CREATE TABLE t1 ( 3313grp int(11) default NULL, 3314a decimal(10,2) default NULL); 3315insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL); 3316select * from t1; 3317grp a 33181 1.00 33192 2.00 33202 3.00 33213 4.00 33223 5.00 33233 6.00 3324NULL NULL 3325select min(a) from t1 group by grp; 3326min(a) 3327NULL 33281.00 33292.00 33304.00 3331drop table t1; 3332CREATE table t1 ( c1 integer ); 3333INSERT INTO t1 VALUES ( 1 ); 3334INSERT INTO t1 VALUES ( 2 ); 3335INSERT INTO t1 VALUES ( 3 ); 3336CREATE TABLE t2 ( c2 integer ); 3337INSERT INTO t2 VALUES ( 1 ); 3338INSERT INTO t2 VALUES ( 4 ); 3339INSERT INTO t2 VALUES ( 5 ); 3340SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1); 3341c1 c2 33421 1 3343SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 3344WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) ); 3345c1 c2 33461 1 3347DROP TABLE t1,t2; 3348CREATE TABLE t1 ( c1 integer ); 3349INSERT INTO t1 VALUES ( 1 ); 3350INSERT INTO t1 VALUES ( 2 ); 3351INSERT INTO t1 VALUES ( 3 ); 3352INSERT INTO t1 VALUES ( 6 ); 3353CREATE TABLE t2 ( c2 integer ); 3354INSERT INTO t2 VALUES ( 1 ); 3355INSERT INTO t2 VALUES ( 4 ); 3356INSERT INTO t2 VALUES ( 5 ); 3357INSERT INTO t2 VALUES ( 6 ); 3358CREATE TABLE t3 ( c3 integer ); 3359INSERT INTO t3 VALUES ( 7 ); 3360INSERT INTO t3 VALUES ( 8 ); 3361SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2 3362WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL ); 3363c1 c2 33642 NULL 33653 NULL 3366DROP TABLE t1,t2,t3; 3367CREATE TABLE `t1` ( 3368`itemid` bigint(20) unsigned NOT NULL auto_increment, 3369`sessionid` bigint(20) unsigned default NULL, 3370`time` int(10) unsigned NOT NULL default '0', 3371`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 3372NULL default '', 3373`data` text collate latin1_general_ci NOT NULL, 3374PRIMARY KEY (`itemid`) 3375) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3376INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 3377CREATE TABLE `t2` ( 3378`sessionid` bigint(20) unsigned NOT NULL auto_increment, 3379`pid` int(10) unsigned NOT NULL default '0', 3380`date` int(10) unsigned NOT NULL default '0', 3381`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 3382PRIMARY KEY (`sessionid`) 3383) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3384INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 3385SELECT 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; 3386ip count( e.itemid ) 338710.10.10.1 1 3388drop tables t1,t2; 3389CREATE TABLE t1 (EMPNUM CHAR(3)); 3390CREATE TABLE t2 (EMPNUM CHAR(3) ); 3391INSERT INTO t1 VALUES ('E1'),('E2'); 3392INSERT INTO t2 VALUES ('E1'); 3393DELETE FROM t1 3394WHERE t1.EMPNUM NOT IN 3395(SELECT t2.EMPNUM 3396FROM t2 3397WHERE t1.EMPNUM = t2.EMPNUM); 3398select * from t1; 3399EMPNUM 3400E1 3401DROP TABLE t1,t2; 3402CREATE TABLE t1(select_id BIGINT, values_id BIGINT); 3403INSERT INTO t1 VALUES (1, 1); 3404CREATE TABLE t2 (select_id BIGINT, values_id BIGINT, 3405PRIMARY KEY(select_id,values_id)); 3406INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5); 3407SELECT values_id FROM t1 3408WHERE values_id IN (SELECT values_id FROM t2 3409WHERE select_id IN (1, 0)); 3410values_id 34111 3412SELECT values_id FROM t1 3413WHERE values_id IN (SELECT values_id FROM t2 3414WHERE select_id BETWEEN 0 AND 1); 3415values_id 34161 3417SELECT values_id FROM t1 3418WHERE values_id IN (SELECT values_id FROM t2 3419WHERE select_id = 0 OR select_id = 1); 3420values_id 34211 3422DROP TABLE t1, t2; 3423create table t1 (fld enum('0','1')); 3424insert into t1 values ('1'); 3425select * from (select max(fld) from t1) as foo; 3426max(fld) 34271 3428drop table t1; 3429CREATE TABLE t1 (a int, b int); 3430CREATE TABLE t2 (c int, d int); 3431CREATE TABLE t3 (e int); 3432INSERT INTO t1 VALUES 3433(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40); 3434INSERT INTO t2 VALUES 3435(2,10), (2,20), (4,10), (5,10), (3,20), (2,40); 3436INSERT INTO t3 VALUES (10), (30), (10), (20) ; 3437SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a; 3438a MAX(b) MIN(b) 34391 20 10 34402 30 10 34413 20 20 34424 40 40 3443SELECT * FROM t2; 3444c d 34452 10 34462 20 34474 10 34485 10 34493 20 34502 40 3451SELECT * FROM t3; 3452e 345310 345430 345510 345620 3457SELECT a FROM t1 GROUP BY a 3458HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20); 3459a 34602 34614 3462SELECT a FROM t1 GROUP BY a 3463HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d); 3464a 34652 3466SELECT a FROM t1 GROUP BY a 3467HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d); 3468a 34692 34704 3471SELECT a FROM t1 GROUP BY a 3472HAVING a IN (SELECT c FROM t2 3473WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3474a 34752 34763 3477SELECT a FROM t1 GROUP BY a 3478HAVING a IN (SELECT c FROM t2 3479WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3480a 34812 34823 3483SELECT a FROM t1 GROUP BY a 3484HAVING a IN (SELECT c FROM t2 3485WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3486a 34872 3488SELECT a FROM t1 GROUP BY a 3489HAVING a IN (SELECT c FROM t2 3490WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d)); 3491a 34922 3493SELECT a FROM t1 GROUP BY a 3494HAVING a IN (SELECT c FROM t2 3495WHERE MIN(b) < d AND 3496EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3497a 34982 3499SELECT a, SUM(a) FROM t1 GROUP BY a; 3500a SUM(a) 35011 2 35022 6 35033 3 35044 4 3505SELECT a FROM t1 3506WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a; 3507a 35083 35094 3510SELECT a FROM t1 GROUP BY a 3511HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c); 3512a 35131 35143 35154 3516SELECT a FROM t1 3517WHERE a < 3 AND 3518EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a; 3519a 35201 35212 3522SELECT a FROM t1 3523WHERE a < 3 AND 3524EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c); 3525a 35261 35272 35281 35292 35302 3531SELECT t1.a FROM t1 GROUP BY t1.a 3532HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c 3533HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3534HAVING SUM(t1.a+t2.c) < t3.e/4)); 3535a 35361 35372 3538SELECT t1.a FROM t1 GROUP BY t1.a 3539HAVING t1.a > ALL(SELECT t2.c FROM t2 3540WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3541HAVING SUM(t1.a+t2.c) < t3.e/4)); 3542a 35434 3544SELECT t1.a FROM t1 GROUP BY t1.a 3545HAVING t1.a > ALL(SELECT t2.c FROM t2 3546WHERE EXISTS(SELECT t3.e FROM t3 3547WHERE SUM(t1.a+t2.c) < t3.e/4)); 3548ERROR HY000: Invalid use of group function 3549SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20; 3550ERROR HY000: Invalid use of group function 3551SELECT t1.a FROM t1 GROUP BY t1.a 3552HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3553HAVING AVG(t2.c+SUM(t1.b)) > 20); 3554a 35552 35563 35574 3558SELECT t1.a FROM t1 GROUP BY t1.a 3559HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3560HAVING AVG(SUM(t1.b)) > 20); 3561a 35622 35634 3564SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a 3565HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3566HAVING t2.c+sum > 20); 3567a sum 35682 60 35693 20 35704 40 3571DROP TABLE t1,t2,t3; 3572CREATE TABLE t1 (a varchar(5), b varchar(10)); 3573INSERT INTO t1 VALUES 3574('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2), 3575('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8); 3576SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3577a b 3578BBB 4 3579CCC 7 3580AAA 8 3581EXPLAIN 3582SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3583id select_type table type possible_keys key key_len ref rows Extra 35841 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where 35851 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35862 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3587ALTER TABLE t1 ADD INDEX(a); 3588SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3589a b 3590BBB 4 3591CCC 7 3592AAA 8 3593EXPLAIN 3594SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3595id select_type table type possible_keys key key_len ref rows Extra 35961 PRIMARY t1 ALL a NULL NULL NULL 9 Using where 35971 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35982 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3599DROP TABLE t1; 3600create table t1( f1 int,f2 int); 3601insert into t1 values (1,1),(2,2); 3602select 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'; 3603t 3604crash1 3605crash1 3606drop table t1; 3607create table t1 (c int, key(c)); 3608insert into t1 values (1142477582), (1142455969); 3609create table t2 (a int, b int); 3610insert into t2 values (2, 1), (1, 0); 3611delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1; 3612drop table t1, t2; 3613CREATE TABLE t1 (a INT); 3614CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3615ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3616CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1); 3617ERROR 42S22: Unknown column 'no_such_column' in 'where clause' 3618SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3619ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3620DROP TABLE t1; 3621create table t1 (i int, j bigint); 3622insert into t1 values (1, 2), (2, 2), (3, 2); 3623select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3; 3624min(i) 36251 3626drop table t1; 3627CREATE TABLE t1 (i BIGINT UNSIGNED); 3628INSERT INTO t1 VALUES (10000000000000000000); 3629INSERT INTO t1 VALUES (1); 3630CREATE TABLE t2 (i BIGINT UNSIGNED); 3631INSERT INTO t2 VALUES (10000000000000000000); 3632INSERT INTO t2 VALUES (1); 3633/* simple test */ 3634SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i; 3635i 363610000000000000000000 36371 3638/* subquery test */ 3639SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2); 3640i 364110000000000000000000 3642/* subquery test with cast*/ 3643SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED); 3644i 364510000000000000000000 3646DROP TABLE t1; 3647DROP TABLE t2; 3648CREATE TABLE t1 ( 3649id bigint(20) unsigned NOT NULL auto_increment, 3650name varchar(255) NOT NULL, 3651PRIMARY KEY (id) 3652); 3653INSERT INTO t1 VALUES 3654(1, 'Balazs'), (2, 'Joe'), (3, 'Frank'); 3655CREATE TABLE t2 ( 3656id bigint(20) unsigned NOT NULL auto_increment, 3657mid bigint(20) unsigned NOT NULL, 3658date date NOT NULL, 3659PRIMARY KEY (id) 3660); 3661INSERT INTO t2 VALUES 3662(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'), 3663(4, 2, '2006-04-20'), (5, 1, '2006-05-01'); 3664SELECT *, 3665(SELECT date FROM t2 WHERE mid = t1.id 3666ORDER BY date DESC LIMIT 0, 1) AS date_last, 3667(SELECT date FROM t2 WHERE mid = t1.id 3668ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last 3669FROM t1; 3670id name date_last date_next_to_last 36711 Balazs 2006-05-01 NULL 36722 Joe 2006-04-20 NULL 36733 Frank 2006-04-13 NULL 3674SELECT *, 3675(SELECT COUNT(*) FROM t2 WHERE mid = t1.id 3676ORDER BY date DESC LIMIT 1, 1) AS date_count 3677FROM t1; 3678id name date_count 36791 Balazs NULL 36802 Joe NULL 36813 Frank NULL 3682SELECT *, 3683(SELECT date FROM t2 WHERE mid = t1.id 3684ORDER BY date DESC LIMIT 0, 1) AS date_last, 3685(SELECT date FROM t2 WHERE mid = t1.id 3686ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last 3687FROM t1; 3688id name date_last date_next_to_last 36891 Balazs 2006-05-01 2006-03-30 36902 Joe 2006-04-20 2006-04-06 36913 Frank 2006-04-13 NULL 3692DROP TABLE t1,t2; 3693CREATE TABLE t1 ( 3694i1 int(11) NOT NULL default '0', 3695i2 int(11) NOT NULL default '0', 3696t datetime NOT NULL default '0000-00-00 00:00:00', 3697PRIMARY KEY (i1,i2,t) 3698); 3699INSERT INTO t1 VALUES 3700(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'), 3701(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'), 3702(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'), 3703(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'), 3704(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'), 3705(24,2,'2005-05-27 12:40:06'); 3706CREATE TABLE t2 ( 3707i1 int(11) NOT NULL default '0', 3708i2 int(11) NOT NULL default '0', 3709t datetime default NULL, 3710PRIMARY KEY (i1) 3711); 3712INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40'); 3713EXPLAIN 3714SELECT * FROM t1,t2 3715WHERE t1.t = (SELECT t1.t FROM t1 3716WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3717ORDER BY t1.t DESC LIMIT 1); 3718id select_type table type possible_keys key key_len ref rows Extra 37191 PRIMARY t2 system NULL NULL NULL NULL 1 37201 PRIMARY t1 index NULL PRIMARY 13 NULL 11 Using where; Using index 37212 SUBQUERY t1 range PRIMARY PRIMARY 13 NULL 6 Using where; Using index 3722SELECT * FROM t1,t2 3723WHERE t1.t = (SELECT t1.t FROM t1 3724WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3725ORDER BY t1.t DESC LIMIT 1); 3726i1 i2 t i1 i2 t 372724 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40 3728DROP TABLE t1, t2; 3729CREATE TABLE t1 (i INT); 3730(SELECT i FROM t1) UNION (SELECT i FROM t1); 3731i 3732SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS 3733( 3734(SELECT i FROM t1) UNION 3735(SELECT i FROM t1) 3736); 3737i 3738SELECT * FROM t1 3739WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1))); 3740i 3741explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12)) 3742from t1; 3743id select_type table type possible_keys key key_len ref rows Extra 37441 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37452 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37463 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3747NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3748explain select * from t1 where not exists 3749((select t11.i from t1 t11) union (select t12.i from t1 t12)); 3750id select_type table type possible_keys key key_len ref rows Extra 37511 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37522 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37533 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3754NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3755DROP TABLE t1; 3756CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b)); 3757insert into t1 (a) values (FLOOR(rand() * 100)); 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; 3770insert into t1 (a) select FLOOR(rand() * 100) from t1; 3771SELECT a, 3772(SELECT REPEAT(' ',250) FROM t1 i1 3773WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a 3774FROM t1 ORDER BY a LIMIT 5; 3775a a 37760 NULL 37770 NULL 37780 NULL 37790 NULL 37800 NULL 3781DROP TABLE t1; 3782CREATE TABLE t1 (a INT, b INT); 3783CREATE TABLE t2 (a INT); 3784INSERT INTO t2 values (1); 3785INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4); 3786SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a; 3787(SELECT COUNT(DISTINCT t1.b) from t2) 37882 37891 37901 3791SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 3792FROM t1 GROUP BY t1.a; 3793(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 37942 37951 37961 3797SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a; 3798COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b)) 37992 2 38001 1 38011 1 3802SELECT COUNT(DISTINCT t1.b), 3803(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 3804FROM t1 GROUP BY t1.a; 3805COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 38062 2 38071 1 38081 1 3809SELECT ( 3810SELECT ( 3811SELECT COUNT(DISTINCT t1.b) 3812) 3813) 3814FROM t1 GROUP BY t1.a; 3815( 3816SELECT ( 3817SELECT COUNT(DISTINCT t1.b) 3818) 3819) 38202 38211 38221 3823SELECT ( 3824SELECT ( 3825SELECT ( 3826SELECT COUNT(DISTINCT t1.b) 3827) 3828) 3829FROM t1 GROUP BY t1.a LIMIT 1) 3830FROM t1 t2 3831GROUP BY t2.a; 3832( 3833SELECT ( 3834SELECT ( 3835SELECT COUNT(DISTINCT t1.b) 3836) 3837) 3838FROM t1 GROUP BY t1.a LIMIT 1) 38392 38402 38412 3842DROP TABLE t1,t2; 3843CREATE TABLE t1 (a int, b int, PRIMARY KEY (b)); 3844CREATE TABLE t2 (x int auto_increment, y int, z int, 3845PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b)); 3846create table t3 (a int); 3847insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 3848insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a)) 3849from t3 A, t3 B, t3 C, t3 D where D.a<3; 3850insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3; 3851SET SESSION sort_buffer_size = 32 * 1024; 3852SELECT SQL_NO_CACHE COUNT(*) 3853FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c 3854FROM t1) t; 3855COUNT(*) 38563000 3857SET SESSION sort_buffer_size = 8 * 1024 * 1024; 3858SELECT SQL_NO_CACHE COUNT(*) 3859FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c 3860FROM t1) t; 3861COUNT(*) 38623000 3863DROP TABLE t1,t2,t3; 3864CREATE TABLE t1 (id char(4) PRIMARY KEY, c int); 3865CREATE TABLE t2 (c int); 3866INSERT INTO t1 VALUES ('aa', 1); 3867INSERT INTO t2 VALUES (1); 3868SELECT * FROM t1 3869WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3870UNION 3871SELECT c from t2 WHERE c=t1.c); 3872id c 3873aa 1 3874INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1); 3875SELECT * FROM t1 3876WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3877UNION 3878SELECT c from t2 WHERE c=t1.c); 3879id c 3880aa 1 3881bb 2 3882cc 3 3883dd 1 3884INSERT INTO t2 VALUES (2); 3885CREATE TABLE t3 (c int); 3886INSERT INTO t3 VALUES (1); 3887SELECT * FROM t1 3888WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1 3889UNION 3890SELECT c from t2 WHERE c=t1.c); 3891id c 3892aa 1 3893bb 2 3894cc 3 3895dd 1 3896DROP TABLE t1,t2,t3; 3897CREATE TABLE t1(f1 int); 3898CREATE TABLE t2(f2 int, f21 int, f3 timestamp); 3899INSERT INTO t1 VALUES (1),(1),(2),(2); 3900INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11"); 3901SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1; 3902sq 39032 39044 3905SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2; 3906tt 39072 39082 3909PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1'; 3910EXECUTE stmt1; 3911sq 39122 39134 3914EXECUTE stmt1; 3915sq 39162 39174 3918DEALLOCATE PREPARE stmt1; 3919SELECT f2, AVG(f21), 3920(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test 3921FROM t2 GROUP BY f2; 3922f2 AVG(f21) test 39231 1.0000 2004-02-29 11:11:11 39242 2.0000 2004-02-29 11:11:11 3925DROP TABLE t1,t2; 3926CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL); 3927INSERT INTO t1 VALUES 3928(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'), 3929(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'), 3930(3,2,'k'), (3,1,'l'), (1,9,'m'); 3931SELECT a, MAX(b), 3932(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test 3933FROM t1 GROUP BY a; 3934a MAX(b) test 39351 9 m 39362 3 h 39373 4 i 3938DROP TABLE t1; 3939DROP TABLE IF EXISTS t1; 3940DROP TABLE IF EXISTS t2; 3941DROP TABLE IF EXISTS t1xt2; 3942CREATE TABLE t1 ( 3943id_1 int(5) NOT NULL, 3944t varchar(4) DEFAULT NULL 3945); 3946CREATE TABLE t2 ( 3947id_2 int(5) NOT NULL, 3948t varchar(4) DEFAULT NULL 3949); 3950CREATE TABLE t1xt2 ( 3951id_1 int(5) NOT NULL, 3952id_2 int(5) NOT NULL 3953); 3954INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'); 3955INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa'); 3956INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4); 3957SELECT DISTINCT t1.id_1 FROM t1 WHERE 3958(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3959id_1 3960SELECT DISTINCT t1.id_1 FROM t1 WHERE 3961(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 3962id_1 3963SELECT DISTINCT t1.id_1 FROM t1 WHERE 3964(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 3965id_1 3966SELECT DISTINCT t1.id_1 FROM t1 WHERE 3967(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3968id_1 39691 39702 39713 39724 3973SELECT DISTINCT t1.id_1 FROM t1 WHERE 3974(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))); 3975id_1 39761 39772 39783 39794 3980SELECT DISTINCT t1.id_1 FROM t1 WHERE 3981(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)))); 3982id_1 39831 39842 39853 39864 3987insert INTO t1xt2 VALUES (1, 12); 3988SELECT DISTINCT t1.id_1 FROM t1 WHERE 3989(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3990id_1 39911 3992SELECT DISTINCT t1.id_1 FROM t1 WHERE 3993(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 3994id_1 39951 3996SELECT DISTINCT t1.id_1 FROM t1 WHERE 3997(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 3998id_1 39991 4000SELECT DISTINCT t1.id_1 FROM t1 WHERE 4001(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4002id_1 40032 40043 40054 4006SELECT DISTINCT t1.id_1 FROM t1 WHERE 4007(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4008id_1 40092 40103 40114 4012SELECT DISTINCT t1.id_1 FROM t1 WHERE 4013(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4014id_1 40152 40163 40174 4018insert INTO t1xt2 VALUES (2, 12); 4019SELECT DISTINCT t1.id_1 FROM t1 WHERE 4020(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4021id_1 40221 40232 4024SELECT DISTINCT t1.id_1 FROM t1 WHERE 4025(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4026id_1 40271 40282 4029SELECT DISTINCT t1.id_1 FROM t1 WHERE 4030(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4031id_1 40321 40332 4034SELECT DISTINCT t1.id_1 FROM t1 WHERE 4035(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4036id_1 40373 40384 4039SELECT DISTINCT t1.id_1 FROM t1 WHERE 4040(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4041id_1 40423 40434 4044SELECT DISTINCT t1.id_1 FROM t1 WHERE 4045(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4046id_1 40473 40484 4049DROP TABLE t1; 4050DROP TABLE t2; 4051DROP TABLE t1xt2; 4052CREATE TABLE t1 (a int); 4053INSERT INTO t1 VALUES (3), (1), (2); 4054SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1; 4055col1 col2 4056this is a test. 3 4057this is a test. 1 4058this is a test. 2 4059SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t; 4060col1 t2 4061this is a test. 3 4062this is a test. 1 4063this is a test. 2 4064DROP table t1; 4065CREATE TABLE t1 (a int, b int); 4066CREATE TABLE t2 (m int, n int); 4067INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4068INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4069SELECT COUNT(*), a, 4070(SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 4071FROM t1 GROUP BY a; 4072COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 40732 2 2 40743 3 3 40751 4 1 4076SELECT COUNT(*), a, 4077(SELECT MIN(m) FROM t2 WHERE m = count(*)) 4078FROM t1 GROUP BY a; 4079COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*)) 40802 2 2 40813 3 3 40821 4 1 4083SELECT COUNT(*), a 4084FROM t1 GROUP BY a 4085HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1; 4086COUNT(*) a 40872 2 40883 3 4089DROP TABLE t1,t2; 4090CREATE TABLE t1 (a int, b int); 4091CREATE TABLE t2 (m int, n int); 4092INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4093INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4094SELECT COUNT(*) c, a, 4095(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 4096FROM t1 GROUP BY a; 4097c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 40982 2 2 40993 3 3 41001 4 1,1 4101SELECT COUNT(*) c, a, 4102(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 4103FROM t1 GROUP BY a; 4104c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 41052 2 3 41063 3 4 41071 4 2,2 4108DROP table t1,t2; 4109CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b)); 4110INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'), 4111(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'), 4112(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p'); 4113SELECT a, MAX(b), 4114(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test 4115FROM t1 GROUP BY a; 4116a MAX(b) test 41171 9 m 41182 3 h 41193 4 i 4120SELECT a x, MAX(b), 4121(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test 4122FROM t1 GROUP BY a; 4123x MAX(b) test 41241 9 m 41252 3 h 41263 4 i 4127SELECT a, AVG(b), 4128(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test 4129FROM t1 WHERE t1.d=0 GROUP BY a; 4130a AVG(b) test 41311 4.0000 d 41322 2.0000 g 41333 2.5000 NULL 4134SELECT tt.a, 4135(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4136LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4137FROM t1 as tt; 4138a test 41391 n 41401 n 41411 n 41421 n 41431 n 41441 n 41451 n 41462 o 41472 o 41482 o 41492 o 41503 p 41513 p 41523 p 41533 p 41543 p 4155SELECT tt.a, 4156(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4157LIMIT 1) 4158FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4159FROM t1 as tt GROUP BY tt.a; 4160a test 41611 n 41622 o 41633 p 4164SELECT tt.a, MAX( 4165(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4166LIMIT 1) 4167FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test 4168FROM t1 as tt GROUP BY tt.a; 4169a test 41701 n 41712 o 41723 p 4173DROP TABLE t1; 4174CREATE TABLE t1 (a int, b int); 4175INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4176SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4177a 41781 41792 4180SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4181a 4182SELECT a FROM t1 t0 4183WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4184a 41851 41862 4187SET @@sql_mode='ansi'; 4188SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4189ERROR HY000: Invalid use of group function 4190SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4191ERROR HY000: Invalid use of group function 4192SELECT a FROM t1 t0 4193WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4194ERROR HY000: Invalid use of group function 4195SET @@sql_mode=default; 4196DROP TABLE t1; 4197CREATE TABLE t1 (a INT); 4198INSERT INTO t1 values (1),(1),(1),(1); 4199CREATE TABLE t2 (x INT); 4200INSERT INTO t1 values (1000),(1001),(1002); 4201SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1; 4202ERROR HY000: Invalid use of group function 4203SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1; 4204ERROR HY000: Invalid use of group function 4205SELECT COUNT(1) FROM DUAL; 4206COUNT(1) 42071 4208SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1; 4209ERROR HY000: Invalid use of group function 4210SELECT 4211SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) ) 4212FROM t1; 4213ERROR HY000: Invalid use of group function 4214SELECT t1.a as XXA, 4215SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) ) 4216FROM t1; 4217ERROR HY000: Invalid use of group function 4218DROP TABLE t1,t2; 4219CREATE TABLE t1 (a int, b int, KEY (a)); 4220INSERT INTO t1 VALUES (1,1),(2,1); 4221EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b); 4222id select_type table type possible_keys key key_len ref rows Extra 42231 PRIMARY t1 ref a a 5 const 1 Using where; Using index 42242 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 4225DROP TABLE t1; 4226CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id)); 4227INSERT INTO t1 VALUES 4228(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY'); 4229CREATE TABLE t2 (id int NOT NULL, INDEX idx(id)); 4230INSERT INTO t2 VALUES (7), (5), (1), (3); 4231SELECT id, st FROM t1 4232WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4233id st 42343 FL 42351 GA 42367 FL 4237SELECT id, st FROM t1 4238WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4239GROUP BY id; 4240id st 42411 GA 42423 FL 42437 FL 4244SELECT id, st FROM t1 4245WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4246id st 42472 GA 42484 FL 4249SELECT id, st FROM t1 4250WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4251GROUP BY id; 4252id st 42532 GA 42544 FL 4255DROP TABLE t1,t2; 4256CREATE TABLE t1 (a int); 4257INSERT INTO t1 VALUES (1), (2); 4258EXPLAIN EXTENDED 4259SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res; 4260id select_type table type possible_keys key key_len ref rows filtered Extra 42611 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00 42622 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort 4263Warnings: 4264Note 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` 4265DROP TABLE t1; 4266CREATE TABLE t1 ( 4267a varchar(255) default NULL, 4268b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 4269INDEX idx(a,b) 4270); 4271CREATE TABLE t2 ( 4272a varchar(255) default NULL 4273); 4274INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24'); 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 SELECT * FROM t1; 4283INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26'); 4284INSERT INTO `t2` VALUES ('abcdefghijk'); 4285INSERT INTO `t2` VALUES ('asdf'); 4286SET session sort_buffer_size=8192; 4287SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2; 4288d1 42891 42901 4291DROP TABLE t1,t2; 4292CREATE TABLE t1 (a INTEGER, b INTEGER); 4293CREATE TABLE t2 (x INTEGER); 4294INSERT INTO t1 VALUES (1,11), (2,22), (2,22); 4295INSERT INTO t2 VALUES (1), (2); 4296SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a; 4297ERROR 21000: Subquery returns more than 1 row 4298SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a; 4299ERROR 21000: Subquery returns more than 1 row 4300SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1; 4301(SELECT SUM(t1.a)/AVG(t2.x) FROM t2) 43023.3333 4303DROP TABLE t1,t2; 4304CREATE TABLE t1 (a INT, b INT); 4305INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2); 4306SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1 4307AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a) 4308GROUP BY a1.a; 4309a COUNT(*) 43101 3 4311DROP TABLE t1; 4312CREATE TABLE t1 (a INT); 4313CREATE TABLE t2 (a INT); 4314INSERT INTO t1 VALUES (1),(2); 4315INSERT INTO t2 VALUES (1),(2); 4316SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1; 4317(SELECT SUM(t1.a) FROM t2 WHERE a=0) 4318NULL 4319SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1; 4320ERROR 21000: Subquery returns more than 1 row 4321SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1; 4322(SELECT SUM(t1.a) FROM t2 WHERE a=1) 43233 4324DROP TABLE t1,t2; 4325CREATE TABLE t1 (a1 INT, a2 INT); 4326CREATE TABLE t2 (b1 INT, b2 INT); 4327INSERT INTO t1 VALUES (100, 200); 4328INSERT INTO t1 VALUES (101, 201); 4329INSERT INTO t2 VALUES (101, 201); 4330INSERT INTO t2 VALUES (103, 203); 4331SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1; 4332((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL 43330 43340 4335DROP TABLE t1, t2; 4336CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5)); 4337INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43); 4338SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); 4339s1 s2 4340SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); 4341s1 s2 4342CREATE INDEX I1 ON t1 (s1); 4343CREATE INDEX I2 ON t1 (s2); 4344SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); 4345s1 s2 4346SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); 4347s1 s2 4348TRUNCATE t1; 4349INSERT INTO t1 VALUES (0x41,0x41); 4350SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1); 4351s1 s2 4352DROP TABLE t1; 4353CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1)); 4354CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2)); 4355CREATE TABLE t3 (a3 BINARY(2) default '0'); 4356INSERT INTO t1 VALUES (1),(2),(3),(4); 4357INSERT INTO t2 VALUES (1),(2),(3); 4358INSERT INTO t3 VALUES (1),(2),(3); 4359SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2; 4360LEFT(t2.a2, 1) 43611 43622 43633 4364SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1; 4365a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) 43661 0 43672 0 43683 0 43694 0 4370DROP TABLE t1,t2,t3; 4371CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3)); 4372CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY); 4373CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY); 4374INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40); 4375INSERT INTO t2 VALUES (2), (3), (4), (5); 4376INSERT INTO t3 VALUES (10), (20), (30); 4377SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3; 4378LEFT(t1.a1,1) 43791 43802 43813 4382SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3); 4383a2 4384DROP TABLE t1, t2, t3; 4385SET @save_optimizer_switch=@@optimizer_switch; 4386SET optimizer_switch='semijoin_with_cache=off'; 4387SET optimizer_switch='materialization=off'; 4388CREATE TABLE t1 (a CHAR(1), b VARCHAR(10)); 4389INSERT INTO t1 VALUES ('a', 'aa'); 4390INSERT INTO t1 VALUES ('a', 'aaa'); 4391SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4392a b 4393CREATE INDEX I1 ON t1 (a); 4394CREATE INDEX I2 ON t1 (b); 4395EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4396id select_type table type possible_keys key key_len ref rows Extra 43971 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 43981 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4399SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4400a b 4401CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10)); 4402INSERT INTO t2 SELECT * FROM t1; 4403CREATE INDEX I1 ON t2 (a); 4404CREATE INDEX I2 ON t2 (b); 4405EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4406id select_type table type possible_keys key key_len ref rows Extra 44071 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where 44081 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2) 4409SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4410a b 4411EXPLAIN 4412SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4413id select_type table type possible_keys key key_len ref rows Extra 44141 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 44151 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4416SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4417a b 4418DROP TABLE t1,t2; 4419SET optimizer_switch= @save_optimizer_switch; 4420CREATE TABLE t1(a INT, b INT); 4421INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4); 4422EXPLAIN 4423SELECT a AS out_a, MIN(b) FROM t1 4424WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4425GROUP BY a; 4426ERROR 42S22: Unknown column 'out_a' in 'where clause' 4427SELECT a AS out_a, MIN(b) FROM t1 4428WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4429GROUP BY a; 4430ERROR 42S22: Unknown column 'out_a' in 'where clause' 4431EXPLAIN 4432SELECT a AS out_a, MIN(b) FROM t1 t1_outer 4433WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) 4434GROUP BY a; 4435id select_type table type possible_keys key key_len ref rows Extra 44361 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort 44372 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where 4438SELECT a AS out_a, MIN(b) FROM t1 t1_outer 4439WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) 4440GROUP BY a; 4441out_a MIN(b) 44421 2 44432 4 4444DROP TABLE t1; 4445CREATE TABLE t1 (a INT); 4446CREATE TABLE t2 (a INT); 4447INSERT INTO t1 VALUES (1),(2); 4448INSERT INTO t2 VALUES (1),(2); 4449SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 44502 44512 44522 4453EXPLAIN EXTENDED 4454SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 4455id select_type table type possible_keys key key_len ref rows filtered Extra 44561 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 44571 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 44582 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00 4459Warnings: 4460Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4461Note 1003 select 2 AS `2` from `test`.`t1` semi join (`test`.`t2`) where 1 4462EXPLAIN EXTENDED 4463SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION 4464(SELECT 1 FROM t2 WHERE t1.a = t2.a)); 4465id select_type table type possible_keys key key_len ref rows filtered Extra 44661 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where 44672 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 44683 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where 4469NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 4470Warnings: 4471Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4472Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 4473Note 1003 /* select#1 */ select 2 AS `2` from `test`.`t1` where exists((/* select#2 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) union (/* select#3 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) limit 1) 4474DROP TABLE t1,t2; 4475create table t0(a int); 4476insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 4477create table t1(f11 int, f12 int); 4478create table t2(f21 int unsigned not null, f22 int, f23 varchar(10)); 4479insert into t1 values(1,1),(2,2), (3, 3); 4480insert ignore into t2 4481select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a 4482from t0 A, t0 B, t0 C, t0 D; 4483set session sort_buffer_size= 33*1024; 4484select count(*) from t1 where f12 = 4485(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1); 4486count(*) 44873 4488drop table t0,t1,t2; 4489CREATE TABLE t4 ( 4490f7 varchar(32) collate utf8_bin NOT NULL default '', 4491f10 varchar(32) collate utf8_bin default NULL, 4492PRIMARY KEY (f7) 4493); 4494INSERT INTO t4 VALUES(1,1), (2,null); 4495CREATE TABLE t2 ( 4496f4 varchar(32) collate utf8_bin NOT NULL default '', 4497f2 varchar(50) collate utf8_bin default NULL, 4498f3 varchar(10) collate utf8_bin default NULL, 4499PRIMARY KEY (f4), 4500UNIQUE KEY uk1 (f2) 4501); 4502INSERT INTO t2 VALUES(1,1,null), (2,2,null); 4503CREATE TABLE t1 ( 4504f8 varchar(32) collate utf8_bin NOT NULL default '', 4505f1 varchar(10) collate utf8_bin default NULL, 4506f9 varchar(32) collate utf8_bin default NULL, 4507PRIMARY KEY (f8) 4508); 4509INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2); 4510CREATE TABLE t3 ( 4511f6 varchar(32) collate utf8_bin NOT NULL default '', 4512f5 varchar(50) collate utf8_bin default NULL, 4513PRIMARY KEY (f6) 4514); 4515INSERT INTO t3 VALUES (1,null), (2,null); 4516SELECT 4517IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4, 4518IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3, 4519SUM( 4520IF( 4521(SELECT VPC.f2 4522FROM t2 VPC, t4 a2, t2 a3 4523WHERE 4524VPC.f4 = a2.f10 AND a3.f2 = a4 4525LIMIT 1) IS NULL, 45260, 4527t3.f5 4528) 4529) AS a6 4530FROM 4531t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4 4532GROUP BY a4; 4533a4 f3 a6 45341 NULL NULL 45352 NULL NULL 4536DROP TABLE t1, t2, t3, t4; 4537create table t1 (a float(5,4) zerofill); 4538create table t2 (a float(5,4),b float(2,0)); 4539select t1.a from t1 where 4540t1.a= (select b from t2 limit 1) and not 4541t1.a= (select a from t2 limit 1) ; 4542a 4543drop table t1, t2; 4544CREATE TABLE t1 (a INT); 4545INSERT INTO t1 VALUES (1),(2); 4546SET @save_join_cache_level=@@join_cache_level; 4547SET join_cache_level=0; 4548EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a); 4549id select_type table type possible_keys key key_len ref rows filtered Extra 45501 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45511 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45522 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary 4553Warnings: 4554Note 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 4555EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a); 4556id select_type table type possible_keys key key_len ref rows filtered Extra 45571 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45581 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45592 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary 4560Warnings: 4561Note 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 4562SET join_cache_level=@save_join_cache_level; 4563DROP TABLE t1; 4564# 4565# Bug#45061: Incorrectly market field caused wrong result. 4566# 4567CREATE TABLE `C` ( 4568`int_nokey` int(11) NOT NULL, 4569`int_key` int(11) NOT NULL, 4570KEY `int_key` (`int_key`) 4571); 4572INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), 4573(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), 4574(5,2), (1,8), (7,0), (0,9), (9,5); 4575SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4576int_nokey int_key 45779 9 45780 0 45795 5 45800 0 4581EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4582id select_type table type possible_keys key key_len ref rows filtered Extra 45831 SIMPLE C ALL NULL NULL NULL NULL 20 100.00 Using where 4584DROP TABLE C; 4585# End of test for bug#45061. 4586# 4587# Bug #46749: Segfault in add_key_fields() with outer subquery level 4588# field references 4589# 4590CREATE TABLE t1 ( 4591a int, 4592b int, 4593UNIQUE (a), KEY (b) 4594); 4595INSERT INTO t1 VALUES (1,1), (2,1); 4596CREATE TABLE st1 like t1; 4597INSERT INTO st1 VALUES (1,1), (2,1); 4598CREATE TABLE st2 like t1; 4599INSERT INTO st2 VALUES (1,1), (2,1); 4600EXPLAIN 4601SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4602FROM t1 4603WHERE a = 230; 4604id select_type table type possible_keys key key_len ref rows Extra 46051 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 46062 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 4607SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4608FROM t1 4609WHERE a = 230; 4610MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4611NULL NULL 4612DROP TABLE t1, st1, st2; 4613# 4614# Bug #48709: Assertion failed in sql_select.cc:11782: 4615# int join_read_key(JOIN_TAB*) 4616# 4617CREATE TABLE t1 (pk int PRIMARY KEY, int_key int); 4618INSERT INTO t1 VALUES (10,1), (14,1); 4619CREATE TABLE t2 (pk int PRIMARY KEY, int_key int); 4620INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3); 4621# should have eq_ref for t1 4622EXPLAIN 4623SELECT * FROM t2 outr 4624WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4625ORDER BY outr.pk; 4626id select_type table type possible_keys key key_len ref rows Extra 4627x x outr ALL x x x x x x 4628x x t1 eq_ref x x x x x x 4629x x t2 index x x x x x x 4630# should not crash on debug binaries 4631SELECT * FROM t2 outr 4632WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4633ORDER BY outr.pk; 4634pk int_key 46353 3 46367 3 4637DROP TABLE t1,t2; 4638# 4639# Bug#12329653 4640# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY 4641# 4642CREATE TABLE t1(a1 int); 4643INSERT INTO t1 VALUES (1),(2); 4644SELECT @@session.sql_mode INTO @old_sql_mode; 4645SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4646EXPLAIN EXTENDED 4647SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 4648id select_type table type possible_keys key key_len ref rows filtered Extra 46491 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 46502 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 4651Warnings: 4652Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 4653SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 46541 46551 46561 4657PREPARE stmt FROM 4658'SELECT 1 UNION ALL 4659SELECT 1 FROM t1 4660ORDER BY 4661(SELECT 1 FROM t1 AS t1_0 4662 WHERE 1 < SOME (SELECT a1 FROM t1) 4663)' ; 4664EXECUTE stmt ; 4665ERROR 21000: Subquery returns more than 1 row 4666EXECUTE stmt ; 4667ERROR 21000: Subquery returns more than 1 row 4668SET SESSION sql_mode=@old_sql_mode; 4669DEALLOCATE PREPARE stmt; 4670DROP TABLE t1; 4671# 4672# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4673# 4674CREATE TABLE t1(a1 int); 4675INSERT INTO t1 VALUES (1),(2); 4676CREATE TABLE t2(a1 int); 4677INSERT INTO t2 VALUES (3); 4678SELECT @@session.sql_mode INTO @old_sql_mode; 4679SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4680SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); 46811 46821 46831 4684SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); 46851 46861 46871 4688SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); 46891 4690Warnings: 4691Warning 1292 Truncated incorrect DOUBLE value: 'a' 4692SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); 46931 46941 46951 4696SET SESSION sql_mode=@old_sql_mode; 4697DROP TABLE t1, t2; 4698# 4699# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4700# 4701create table t2(i int); 4702insert into t2 values(0); 4703SELECT @@session.sql_mode INTO @old_sql_mode; 4704SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4705CREATE VIEW v1 AS 4706SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 4707; 4708CREATE TABLE t1 ( 4709pk int NOT NULL, 4710col_varchar_key varchar(1) DEFAULT NULL, 4711PRIMARY KEY (pk), 4712KEY col_varchar_key (col_varchar_key) 4713); 4714SELECT t1.pk 4715FROM t1 4716WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) 4717; 4718pk 4719SET SESSION sql_mode=@old_sql_mode; 4720drop table t2, t1; 4721drop view v1; 4722# End of 5.0 tests. 4723create table t_out (subcase char(3), 4724a1 char(2), b1 char(2), c1 char(2)); 4725create table t_in (a2 char(2), b2 char(2), c2 char(2)); 4726insert into t_out values ('A.1','2a', NULL, '2a'); 4727insert into t_out values ('A.3', '2a', NULL, '2a'); 4728insert into t_out values ('A.4', '2a', NULL, 'xx'); 4729insert into t_out values ('B.1', '2a', '2a', '2a'); 4730insert into t_out values ('B.2', '2a', '2a', '2a'); 4731insert into t_out values ('B.3', '3a', 'xx', '3a'); 4732insert into t_out values ('B.4', 'xx', '3a', '3a'); 4733insert into t_in values ('1a', '1a', '1a'); 4734insert into t_in values ('2a', '2a', '2a'); 4735insert into t_in values (NULL, '2a', '2a'); 4736insert into t_in values ('3a', NULL, '3a'); 4737 4738Test general IN semantics (not top-level) 4739 4740case A.1 4741select subcase, 4742(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4743(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4744from t_out where subcase = 'A.1'; 4745subcase pred_in pred_not_in 4746A.1 0 1 4747case A.2 - impossible 4748case A.3 4749select subcase, 4750(a1, b1, c1) IN (select * from t_in) pred_in, 4751(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4752from t_out where subcase = 'A.3'; 4753subcase pred_in pred_not_in 4754A.3 NULL NULL 4755case A.4 4756select subcase, 4757(a1, b1, c1) IN (select * from t_in) pred_in, 4758(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4759from t_out where subcase = 'A.4'; 4760subcase pred_in pred_not_in 4761A.4 0 1 4762case B.1 4763select subcase, 4764(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4765(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4766from t_out where subcase = 'B.1'; 4767subcase pred_in pred_not_in 4768B.1 0 1 4769case B.2 4770select subcase, 4771(a1, b1, c1) IN (select * from t_in) pred_in, 4772(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4773from t_out where subcase = 'B.2'; 4774subcase pred_in pred_not_in 4775B.2 1 0 4776case B.3 4777select subcase, 4778(a1, b1, c1) IN (select * from t_in) pred_in, 4779(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4780from t_out where subcase = 'B.3'; 4781subcase pred_in pred_not_in 4782B.3 NULL NULL 4783case B.4 4784select subcase, 4785(a1, b1, c1) IN (select * from t_in) pred_in, 4786(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4787from t_out where subcase = 'B.4'; 4788subcase pred_in pred_not_in 4789B.4 0 1 4790 4791Test IN as top-level predicate, and 4792as non-top level for cases A.3, B.3 (the only cases with NULL result). 4793 4794case A.1 4795select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4796where subcase = 'A.1' and 4797(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4798pred_in 4799F 4800select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4801where subcase = 'A.1' and 4802(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4803pred_not_in 4804T 4805select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4806where subcase = 'A.1' and 4807NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4808not_pred_in 4809T 4810case A.3 4811select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4812where subcase = 'A.3' and 4813(a1, b1, c1) IN (select * from t_in); 4814pred_in 4815F 4816select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4817where subcase = 'A.3' and 4818(a1, b1, c1) NOT IN (select * from t_in); 4819pred_not_in 4820F 4821select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4822where subcase = 'A.3' and 4823NOT((a1, b1, c1) IN (select * from t_in)); 4824not_pred_in 4825F 4826select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4827where subcase = 'A.3' and 4828((a1, b1, c1) IN (select * from t_in)) is NULL and 4829((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4830pred_in 4831N 4832case A.4 4833select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4834where subcase = 'A.4' and 4835(a1, b1, c1) IN (select * from t_in); 4836pred_in 4837F 4838select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4839where subcase = 'A.4' and 4840(a1, b1, c1) NOT IN (select * from t_in); 4841pred_not_in 4842T 4843select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4844where subcase = 'A.4' and 4845NOT((a1, b1, c1) IN (select * from t_in)); 4846not_pred_in 4847T 4848case B.1 4849select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4850where subcase = 'B.1' and 4851(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4852pred_in 4853F 4854select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4855where subcase = 'B.1' and 4856(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4857pred_not_in 4858T 4859select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4860where subcase = 'B.1' and 4861NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4862not_pred_in 4863T 4864case B.2 4865select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4866where subcase = 'B.2' and 4867(a1, b1, c1) IN (select * from t_in); 4868pred_in 4869T 4870select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4871where subcase = 'B.2' and 4872(a1, b1, c1) NOT IN (select * from t_in); 4873pred_not_in 4874F 4875select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4876where subcase = 'B.2' and 4877NOT((a1, b1, c1) IN (select * from t_in)); 4878not_pred_in 4879F 4880case B.3 4881select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4882where subcase = 'B.3' and 4883(a1, b1, c1) IN (select * from t_in); 4884pred_in 4885F 4886select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4887where subcase = 'B.3' and 4888(a1, b1, c1) NOT IN (select * from t_in); 4889pred_not_in 4890F 4891select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4892where subcase = 'B.3' and 4893NOT((a1, b1, c1) IN (select * from t_in)); 4894not_pred_in 4895F 4896select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4897where subcase = 'B.3' and 4898((a1, b1, c1) IN (select * from t_in)) is NULL and 4899((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4900pred_in 4901N 4902case B.4 4903select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4904where subcase = 'B.4' and 4905(a1, b1, c1) IN (select * from t_in); 4906pred_in 4907F 4908select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4909where subcase = 'B.4' and 4910(a1, b1, c1) NOT IN (select * from t_in); 4911pred_not_in 4912T 4913select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4914where subcase = 'B.4' and 4915NOT((a1, b1, c1) IN (select * from t_in)); 4916not_pred_in 4917T 4918drop table t_out; 4919drop table t_in; 4920CREATE TABLE t1 (a INT, b INT); 4921INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4922SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4923a 49241 49252 4926SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4927a 4928SELECT a FROM t1 t0 4929WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4930a 49311 49322 4933SET @@sql_mode='ansi'; 4934SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4935ERROR HY000: Invalid use of group function 4936SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4937ERROR HY000: Invalid use of group function 4938SELECT a FROM t1 t0 4939WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4940ERROR HY000: Invalid use of group function 4941SET @@sql_mode=default; 4942DROP TABLE t1; 4943CREATE TABLE t1 (s1 CHAR(1)); 4944INSERT INTO t1 VALUES ('a'); 4945SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1); 4946s1 4947a 4948DROP TABLE t1; 4949CREATE TABLE t1(c INT, KEY(c)); 4950CREATE TABLE t2(a INT, b INT); 4951INSERT INTO t2 VALUES (1, 10), (2, NULL); 4952INSERT INTO t1 VALUES (1), (3); 4953SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10); 4954a b 4955DROP TABLE t1,t2; 4956CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a)); 4957INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20); 4958CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a)); 4959INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100); 4960SELECT * FROM t1 4961WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b); 4962pk a 49631 10 4964DROP TABLE t1,t2; 4965CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b)); 4966INSERT INTO t1 VALUES (1,NULL), (9,NULL); 4967CREATE TABLE t2 ( 4968a INT, 4969b INT, 4970c INT, 4971d INT, 4972PRIMARY KEY (a), 4973UNIQUE KEY b (b,c,d), 4974KEY b_2 (b), 4975KEY c (c), 4976KEY d (d) 4977); 4978INSERT INTO t2 VALUES 4979(43, 2, 11 ,30), 4980(44, 2, 12 ,30), 4981(45, 1, 1 ,10000), 4982(46, 1, 2 ,10000), 4983(556,1, 32 ,10000); 4984CREATE TABLE t3 ( 4985a INT, 4986b INT, 4987c INT, 4988PRIMARY KEY (a), 4989UNIQUE KEY b (b,c), 4990KEY c (c), 4991KEY b_2 (b) 4992); 4993INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2); 4994explain 4995SELECT 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; 4996id select_type table type possible_keys key key_len ref rows Extra 49971 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index 49981 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index 49992 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index 5000SELECT 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; 5001a incorrect 50021 1 5003DROP TABLE t1,t2,t3; 5004CREATE TABLE t1 (id int); 5005CREATE TABLE t2 (id int, c int); 5006INSERT INTO t1 (id) VALUES (1); 5007INSERT INTO t2 (id) VALUES (1); 5008INSERT INTO t1 (id) VALUES (1); 5009INSERT INTO t2 (id) VALUES (1); 5010CREATE VIEW v1 AS 5011SELECT t2.c AS c FROM t1, t2 5012WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5013UPDATE v1 SET c=1; 5014CREATE VIEW v2 (a,b) AS 5015SELECT t2.id, t2.c AS c FROM t1, t2 5016WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5017INSERT INTO v2(a,b) VALUES (2,2); 5018ERROR 44000: CHECK OPTION failed `test`.`v2` 5019SELECT * FROM v1; 5020c 50211 50221 50231 50241 5025CREATE VIEW v3 AS 5026SELECT t2.c AS c FROM t2 5027WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5028DELETE FROM v3; 5029DROP VIEW v1,v2,v3; 5030DROP TABLE t1,t2; 5031# 5032# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5033# 5034create table t1(id integer primary key, g integer, v integer, s char(1)); 5035create table t2(id integer primary key, g integer, v integer, s char(1)); 5036insert into t1 values 5037(10, 10, 10, 'l'), 5038(20, 20, 20, 'l'), 5039(40, 40, 40, 'l'), 5040(41, 40, null, 'l'), 5041(50, 50, 50, 'l'), 5042(51, 50, null, 'l'), 5043(60, 60, 60, 'l'), 5044(61, 60, null, 'l'), 5045(70, 70, 70, 'l'), 5046(90, 90, null, 'l'); 5047insert into t2 values 5048(10, 10, 10, 'r'), 5049(30, 30, 30, 'r'), 5050(50, 50, 50, 'r'), 5051(60, 60, 60, 'r'), 5052(61, 60, null, 'r'), 5053(70, 70, 70, 'r'), 5054(71, 70, null, 'r'), 5055(80, 80, 80, 'r'), 5056(81, 80, null, 'r'), 5057(100,100,null, 'r'); 5058select * 5059from t1 5060where v in(select v 5061from t2 5062where t1.g=t2.g) is unknown; 5063id g v s 506451 50 NULL l 506561 60 NULL l 5066drop table t1, t2; 5067# 5068# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5069# 5070create table t1(id integer primary key, g integer, v integer, s char(1)); 5071create table t2(id integer primary key, g integer, v integer, s char(1)); 5072insert into t1 values 5073(10, 10, 10, 'l'), 5074(20, 20, 20, 'l'), 5075(40, 40, 40, 'l'), 5076(41, 40, null, 'l'), 5077(50, 50, 50, 'l'), 5078(51, 50, null, 'l'), 5079(60, 60, 60, 'l'), 5080(61, 60, null, 'l'), 5081(70, 70, 70, 'l'), 5082(90, 90, null, 'l'); 5083insert into t2 values 5084(10, 10, 10, 'r'), 5085(30, 30, 30, 'r'), 5086(50, 50, 50, 'r'), 5087(60, 60, 60, 'r'), 5088(61, 60, null, 'r'), 5089(70, 70, 70, 'r'), 5090(71, 70, null, 'r'), 5091(80, 80, 80, 'r'), 5092(81, 80, null, 'r'), 5093(100,100,null, 'r'); 5094select * 5095from t1 5096where v in(select v 5097from t2 5098where t1.g=t2.g) is unknown; 5099id g v s 510051 50 NULL l 510161 60 NULL l 5102drop table t1, t2; 5103# 5104# Bug#33204: INTO is allowed in subselect, causing inconsistent results 5105# 5106CREATE TABLE t1( a INT ); 5107INSERT INTO t1 VALUES (1),(2); 5108CREATE TABLE t2( a INT, b INT ); 5109SELECT * 5110FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a; 5111ERROR 42000: You 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 5112SELECT * 5113FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a; 5114ERROR 42000: You 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 5115SELECT * 5116FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a; 5117ERROR 42000: You 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 5118SELECT * FROM ( 5119SELECT 1 a 5120UNION 5121SELECT a INTO @var FROM t1 WHERE a = 2 5122) t1a; 5123ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2 5124) t1a' at line 4 5125SELECT * FROM ( 5126SELECT 1 a 5127UNION 5128SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 5129) t1a; 5130ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2 5131) t1a' at line 4 5132SELECT * FROM ( 5133SELECT 1 a 5134UNION 5135SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 5136) t1a; 5137ERROR 42000: You 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 5138) t1a' at line 4 5139SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a; 5140a 51412 5142SELECT * FROM ( 5143SELECT a FROM t1 WHERE a = 2 5144UNION 5145SELECT a FROM t1 WHERE a = 2 5146) t1a; 5147a 51482 5149SELECT * FROM ( 5150SELECT 1 a 5151UNION 5152SELECT a FROM t1 WHERE a = 2 5153UNION 5154SELECT a FROM t1 WHERE a = 2 5155) t1a; 5156a 51571 51582 5159SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q; 5160a 51611 5162SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; 5163a 51641 5165SELECT * FROM (SELECT 1 UNION SELECT 1) t1a; 51661 51671 5168SELECT * FROM ((SELECT 1 a INTO @a)) 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 @a)) t1a' at line 1 5170SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a; 5171ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1 5172SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a; 5173ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1 5174SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) 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 @a)) t1a' at line 1 5176SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE '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 DUMPFILE 'file' )) t1a' at line 1 5178SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a; 5179ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1 5180SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) 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 @a))) t1a' at line 1 5182SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE '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 DUMPFILE 'file' ))) t1a' at line 1 5184SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a; 5185ERROR 42000: You 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 5186SELECT * FROM (SELECT 1 a ORDER BY a) t1a; 5187a 51881 5189SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a; 5190a 51911 5192SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a; 5193a 51941 5195SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a; 5196a 51971 5198SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1; 5199a 1 52001 1 52012 1 5202SELECT * FROM t1 JOIN ((SELECT 1 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 '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 UNION SELECT 1)) 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 'UNION SELECT 1)) 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)) t1a ON 1; 5211ERROR 42000: You 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 5212SELECT * FROM t1 JOIN (t1 t1a) ON 1; 5213a a 52141 1 52152 1 52161 2 52172 2 5218SELECT * FROM t1 JOIN ((t1 t1a)) ON 1; 5219a a 52201 1 52212 1 52221 2 52232 2 5224SELECT * FROM (t1 t1a); 5225a 52261 52272 5228SELECT * FROM ((t1 t1a)); 5229a 52301 52312 5232SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1; 5233a t1a 52341 1 52352 1 5236SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1; 5237a t1a 52381 1 52392 1 5240SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1; 5241a a 52421 1 52432 1 5244SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1; 5245a a 52461 1 52472 1 5248SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2; 5249ERROR 42000: You 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 5250SELECT * FROM t1 WHERE a = ALL ( SELECT 1 ); 5251a 52521 5253SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 ); 5254a 52551 5256SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 ); 5257a 52581 5259SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a); 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 @a)' at line 1 5261SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5263SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5264ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5265SELECT * FROM t1 WHERE a = ( SELECT 1 ); 5266a 52671 5268SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 ); 5269a 52701 5271SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a); 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 @a)' at line 1 5273SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5275SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' ); 5276ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5277SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a); 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 @a)' at line 1 5279SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5281SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5282ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5283SELECT ( SELECT 1 INTO @v ); 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 @v )' at line 1 5285SELECT ( SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5287SELECT ( SELECT 1 INTO DUMPFILE 'file' ); 5288ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5289SELECT ( SELECT 1 UNION SELECT 1 INTO @v ); 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 @v )' at line 1 5291SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE '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 OUTFILE 'file' )' at line 1 5293SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5294ERROR 42000: You 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 5295SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1; 5296( SELECT a FROM t1 WHERE a = 1 ) a 52971 1 52981 2 5299SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1; 5300( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a 53011 1 53021 2 5303SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2); 5304a b 5305SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ); 53061 53071 5308( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 53091 53101 5311SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5312( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ) 53131 5314SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 5315ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5316SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5317( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) 53181 5319SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1)); 5320((SELECT 1 UNION SELECT 1 UNION SELECT 1)) 53211 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 ); 5325ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5326SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a; 53271 53281 5329SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a; 53301 53311 5332SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5333a 53341 5335SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5336a 53371 5338SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5339a 53401 5341SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5342a 53431 5344SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5345a 53461 5347SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5348a 53491 5350SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5351a 53521 5353SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5354a 53551 5356SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5357a 53581 5359SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5360a 53611 5362SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5363a 53641 5365SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5366a 53671 5368SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v ); 5369ERROR 42000: You 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 5370SELECT EXISTS(SELECT 1+1); 5371EXISTS(SELECT 1+1) 53721 5373SELECT EXISTS(SELECT 1+1 INTO @test); 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 @test)' at line 1 5375SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION 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 EXISTS ( 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 5379SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v ); 5380ERROR 42000: You 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 5381DROP TABLE t1, t2; 5382CREATE TABLE t1 (a ENUM('rainbow')); 5383INSERT INTO t1 VALUES (),(),(),(),(); 5384SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID())); 53851 53861 5387DROP TABLE t1; 5388CREATE TABLE t1 (a LONGBLOB); 5389INSERT INTO t1 SET a = 'aaaa'; 5390INSERT INTO t1 SET a = 'aaaa'; 5391SELECT 1 FROM t1 GROUP BY 5392(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1); 53931 53941 5395DROP TABLE t1; 5396# 5397# Bug #49512 : subquery with aggregate function crash 5398# subselect_single_select_engine::exec() 5399CREATE TABLE t1(a INT); 5400INSERT INTO t1 VALUES(); 5401# should not crash 5402SELECT 1 FROM t1 WHERE a <> SOME 5403( 5404SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d 5405FROM t1,t1 a 5406); 54071 5408DROP TABLE t1; 5409# 5410# Bug #45989 take 2 : memory leak after explain encounters an 5411# error in the query 5412# 5413CREATE TABLE t1(a LONGTEXT); 5414INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet)); 5415INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet)); 5416EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1, 5417(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1 5418WHERE t1.a = d1.a; 5419ERROR 42S22: Unknown column 'd1.a' in 'where clause' 5420DROP TABLE t1; 5421Set up test tables. 5422CREATE TABLE t1 ( 5423t1_id INT UNSIGNED, 5424PRIMARY KEY(t1_id) 5425) Engine=MyISAM; 5426INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5); 5427CREATE TABLE t2 SELECT * FROM t1; 5428CREATE TABLE t3 ( 5429t3_id INT UNSIGNED AUTO_INCREMENT, 5430t1_id INT UNSIGNED, 5431amount DECIMAL(16,2), 5432PRIMARY KEY(t3_id), 5433KEY(t1_id) 5434) Engine=MyISAM; 5435INSERT INTO t3 (t1_id, t3_id, amount) 5436VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00); 5437This is the 'inner query' running by itself. 5438Produces correct results. 5439SELECT 5440t1.t1_id, 5441IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5442FROM 5443t1 5444LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5445GROUP BY 5446t1.t1_id 5447; 5448t1_id total_amount 54491 100.00 54502 200.00 54513 0.00 54524 400.00 54535 0.00 5454SELECT * FROM (the same inner query) 5455Produces correct results. 5456SELECT * FROM ( 5457SELECT 5458t1.t1_id, 5459IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5460FROM 5461t1 5462LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5463GROUP BY 5464t1.t1_id 5465) AS t; 5466t1_id total_amount 54671 100.00 54682 200.00 54693 0.00 54704 400.00 54715 0.00 5472Now make t2.t1_id part of a key. 5473ALTER TABLE t2 ADD PRIMARY KEY(t1_id); 5474Same inner query by itself. 5475Still correct results. 5476SELECT 5477t1.t1_id, 5478IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5479FROM 5480t1 5481LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5482GROUP BY 5483t1.t1_id; 5484t1_id total_amount 54851 100.00 54862 200.00 54873 0.00 54884 400.00 54895 0.00 5490SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN 5491SELECT * FROM ( 5492SELECT 5493t1.t1_id, 5494IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5495FROM 5496t1 5497LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5498GROUP BY 5499t1.t1_id 5500) AS t; 5501t1_id total_amount 55021 100.00 55032 200.00 55043 0.00 55054 400.00 55065 0.00 5507DROP TABLE t3; 5508DROP TABLE t2; 5509DROP TABLE t1; 5510# 5511# Bug #52711: Segfault when doing EXPLAIN SELECT with 5512# union...order by (select... where...) 5513# 5514CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a)); 5515INSERT INTO t1 VALUES (1),(2); 5516CREATE TABLE t2 (b INT); 5517INSERT INTO t2 VALUES (1),(2); 5518# Should not crash 5519EXPLAIN 5520SELECT * FROM t2 UNION SELECT * FROM t2 5521ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5522# Should not crash 5523SELECT * FROM t2 UNION SELECT * FROM t2 5524ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5525DROP TABLE t1,t2; 5526# 5527# Bug #58818: Incorrect result for IN/ANY subquery 5528# with HAVING condition 5529# 5530CREATE TABLE t1(i INT); 5531INSERT INTO t1 VALUES (1), (2), (3); 5532CREATE TABLE t1s(i INT); 5533INSERT INTO t1s VALUES (10), (20), (30); 5534CREATE TABLE t2s(i INT); 5535INSERT INTO t2s VALUES (100), (200), (300); 5536SELECT * FROM t1 5537WHERE t1.i NOT IN 5538( 5539SELECT STRAIGHT_JOIN t2s.i 5540FROM 5541t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5542HAVING t2s.i = 999 5543); 5544i 55451 55462 55473 5548SELECT * FROM t1 5549WHERE t1.I IN 5550( 5551SELECT STRAIGHT_JOIN t2s.i 5552FROM 5553t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5554HAVING t2s.i = 999 5555) IS UNKNOWN; 5556i 5557SELECT * FROM t1 5558WHERE NOT t1.I = ANY 5559( 5560SELECT STRAIGHT_JOIN t2s.i 5561FROM 5562t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5563HAVING t2s.i = 999 5564); 5565i 55661 55672 55683 5569SELECT * FROM t1 5570WHERE t1.i = ANY ( 5571SELECT STRAIGHT_JOIN t2s.i 5572FROM 5573t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5574HAVING t2s.i = 999 5575) IS UNKNOWN; 5576i 5577DROP TABLE t1,t1s,t2s; 5578# LP BUG#675248 - select->prep_where references on freed memory 5579CREATE TABLE t1 (a int, b int); 5580insert into t1 values (1,1),(0,0); 5581CREATE TABLE t2 (c int); 5582insert into t2 values (1),(2); 5583prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub 5584from t1 as table1 group by sub"; 5585execute stmt1; 5586sum(a) sub 55870 NULL 55881 3 5589deallocate prepare stmt1; 5590prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub 5591from t1 as table1"; 5592execute stmt1; 5593sum(a) sub 55941 3 5595deallocate prepare stmt1; 5596drop table t1,t2; 5597# 5598# Bug LP#693935/#58727: Assertion failure with 5599# a single row subquery returning more than one row 5600# 5601create table t1 (a char(1) charset utf8); 5602insert into t1 values ('a'), ('b'); 5603create table t2 (a binary(1)); 5604insert into t2 values ('x'), ('y'); 5605select * from t2 where a=(select a from t1) and a='x'; 5606ERROR 21000: Subquery returns more than 1 row 5607drop table t1,t2; 5608# End of 5.1 tests 5609# 5610# Bug #11765713 58705: 5611# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES 5612# CREATED BY OPT_SUM_QUERY 5613# 5614CREATE TABLE t1(a INT NOT NULL, KEY (a)); 5615INSERT INTO t1 VALUES (0), (1); 5616SELECT 1 as foo FROM t1 WHERE a < SOME 5617(SELECT a FROM t1 WHERE a <=> 5618(SELECT a FROM t1) 5619); 5620ERROR 21000: Subquery returns more than 1 row 5621SELECT 1 as foo FROM t1 WHERE a < SOME 5622(SELECT a FROM t1 WHERE a <=> 5623(SELECT a FROM t1 where a is null) 5624); 5625foo 5626DROP TABLE t1; 5627# 5628# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool): 5629# Assertion `file' failed. 5630# 5631CREATE TABLE t1 (a INT); 5632SELECT 1 FROM 5633(SELECT ROW( 5634(SELECT 1 FROM t1 RIGHT JOIN 5635(SELECT 1 FROM t1, t1 t2) AS d ON 1), 56361) FROM t1) AS e; 5637ERROR 21000: Operand should contain 1 column(s) 5638DROP TABLE t1; 5639# 5640# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY 5641# 5642CREATE TABLE t1(a TIME NOT NULL); 5643INSERT INTO t1 VALUES ('00:00:32'); 5644SELECT 1 FROM t1 WHERE a > 5645(SELECT timestamp(a) AS a FROM t1); 56461 5647DROP TABLE t1; 5648# 5649# No BUG#, a case brought from 5.2's innodb_mysql_lock.test 5650# 5651create table t1 (i int not null primary key); 5652insert into t1 values (1),(2),(3),(4),(5); 5653create table t2 (j int not null primary key); 5654insert into t2 values (1),(2),(3),(4),(5); 5655create table t3 (k int not null primary key); 5656insert into t3 values (1),(2),(3); 5657create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1); 5658select * from t3 where k in (select j from v2); 5659k 56601 56612 56623 5663drop table t1,t2,t3; 5664drop view v2; 5665# 5666# Bug#52068: Optimizer generates invalid semijoin materialization plan 5667# 5668drop table if exists ot1, ot2, it1, it2; 5669CREATE TABLE ot1(a INTEGER); 5670INSERT INTO ot1 VALUES(5), (8); 5671CREATE TABLE it2(a INTEGER); 5672INSERT INTO it2 VALUES(9), (5), (1), (8); 5673CREATE TABLE it3(a INTEGER); 5674INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4); 5675CREATE TABLE ot4(a INTEGER); 5676INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1); 5677SELECT * FROM ot1,ot4 5678WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5679FROM it2,it3); 5680a a 56815 1 56828 1 56835 5 56848 5 56855 7 56868 7 56875 7 56888 7 56895 1 56908 1 5691explain SELECT * FROM ot1,ot4 5692WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5693FROM it2,it3); 5694id select_type table type possible_keys key key_len ref rows Extra 56951 PRIMARY ot1 ALL NULL NULL NULL NULL 2 56961 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using join buffer (flat, BNL join) 56971 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 56982 MATERIALIZED it2 ALL NULL NULL NULL NULL 4 56992 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join) 5700DROP TABLE IF EXISTS ot1, ot4, it2, it3; 5701# 5702# Bug#729039: NULL keys used to evaluate subquery 5703# 5704CREATE TABLE t1 (a int) ; 5705INSERT INTO t1 VALUES (NULL), (1), (NULL), (2); 5706CREATE TABLE t2 (a int, INDEX idx(a)) ; 5707INSERT INTO t2 VALUES (NULL), (1), (NULL); 5708SELECT * FROM t1 5709WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a); 5710a 57111 5712EXPLAIN 5713SELECT * FROM t1 5714WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a); 5715id select_type table type possible_keys key key_len ref rows Extra 57161 PRIMARY t1 ALL NULL NULL NULL NULL 4 57171 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 57182 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 5719SELECT * FROM t1 5720WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5721a 57221 5723EXPLAIN 5724SELECT * FROM t1 5725WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5726id select_type table type possible_keys key key_len ref rows Extra 57271 PRIMARY t1 ALL NULL NULL NULL NULL 4 57281 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 57292 MATERIALIZED t2 index idx idx 5 NULL 3 Using index 5730DROP TABLE t1,t2; 5731# 5732# BUG#752992: Wrong results for a subquery with 'semijoin=on' 5733# 5734CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); 5735INSERT INTO t1 VALUES (11,0); 5736INSERT INTO t1 VALUES (12,5); 5737INSERT INTO t1 VALUES (15,0); 5738CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); 5739INSERT INTO t2 VALUES (11,1); 5740INSERT INTO t2 VALUES (12,2); 5741INSERT INTO t2 VALUES (15,4); 5742SET @save_join_cache_level=@@join_cache_level; 5743SET join_cache_level=0; 5744EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); 5745id select_type table type possible_keys key key_len ref rows Extra 57461 PRIMARY t1 ALL PRIMARY NULL NULL NULL 3 57471 PRIMARY it eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 Using index 57481 PRIMARY t2 index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(it) 5749SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); 5750pk i 575111 0 575212 5 575315 0 5754SET join_cache_level=@save_join_cache_level; 5755DROP table t1,t2; 5756# 5757# Bug#751350: crash with pushed condition for outer references when 5758# there should be none of such conditions 5759# 5760CREATE TABLE t1 (a int, b int) ; 5761INSERT INTO t1 VALUES (0,0),(0,0); 5762set @optimizer_switch_save=@@optimizer_switch; 5763set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on'; 5764EXPLAIN 5765SELECT b FROM t1 5766WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) 5767GROUP BY b; 5768id select_type table type possible_keys key key_len ref rows Extra 57691 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 57702 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 5771SELECT b FROM t1 5772WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) 5773GROUP BY b; 5774b 57750 5776set @@optimizer_switch=@optimizer_switch_save; 5777DROP TABLE t1; 5778# 5779# Bug #11765713 58705: 5780# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES 5781# CREATED BY OPT_SUM_QUERY 5782# 5783CREATE TABLE t1(a INT NOT NULL, KEY (a)); 5784INSERT INTO t1 VALUES (0), (1); 5785SELECT 1 as foo FROM t1 WHERE a < SOME 5786(SELECT a FROM t1 WHERE a <=> 5787(SELECT a FROM t1) 5788); 5789ERROR 21000: Subquery returns more than 1 row 5790SELECT 1 as foo FROM t1 WHERE a < SOME 5791(SELECT a FROM t1 WHERE a <=> 5792(SELECT a FROM t1 where a is null) 5793); 5794foo 5795DROP TABLE t1; 5796CREATE TABLE t1 (a int(11), b varchar(1)); 5797INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); 5798SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); 5799a 58005 5801SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); 5802a 58035 5804SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); 5805a 58067 5807SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); 5808a 58097 5810SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); 5811a 58125 58137 5814SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); 5815a 58165 58177 5818SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); 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 ); 5827a 58285 58297 5830SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); 5831a 58325 58337 5834SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); 5835a 58365 58377 5838SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); 5839a 58405 58417 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 GROUP BY b ); 5855a 5856SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); 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 5862SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 ); 5863a 5864SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b ); 5865a 5866delete from t1; 5867INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); 5868SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); 5869a 58705 5871SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); 5872a 58735 5874SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); 5875a 58767 5877SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); 5878a 58797 5880SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); 5881a 58825 58837 5884SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); 5885a 58865 58877 5888SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); 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 ); 5897a 58985 58997 5900SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); 5901a 59025 59037 5904SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); 5905a 59065 59077 5908SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); 5909a 59105 59117 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 GROUP BY b ); 5925a 5926SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); 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 5932SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 ); 5933a 5934SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b ); 5935a 5936drop table t1; 5937# 5938# Fix of lp:780386 (NULL left part with empty ALL subquery). 5939# 5940CREATE TABLE t1 ( f11 int) ; 5941INSERT IGNORE INTO t1 VALUES (0),(0); 5942CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ; 5943INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0); 5944DROP TABLE IF EXISTS t3; 5945Warnings: 5946Note 1051 Unknown table 'test.t3' 5947CREATE TABLE t3 ( f3 int) ; 5948INSERT INTO t3 VALUES (0),(0); 5949SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ; 5950r 5951NULL 59525 5953NULL 59545 5955DROP TABLE t1, t2, t3; 5956# 5957# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 5958# 5959CREATE TABLE t1(a1 int); 5960INSERT INTO t1 VALUES (1),(2); 5961CREATE TABLE t2(a1 int); 5962INSERT INTO t2 VALUES (3); 5963SELECT @@session.sql_mode INTO @old_sql_mode; 5964SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 5965SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); 59661 59671 59681 5969SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); 59701 59711 59721 5973SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); 59741 5975Warnings: 5976Warning 1292 Truncated incorrect DOUBLE value: 'a' 5977SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); 59781 59791 59801 5981SET SESSION sql_mode=@old_sql_mode; 5982DROP TABLE t1, t2; 5983create table t2(i int); 5984insert into t2 values(0); 5985SELECT @@session.sql_mode INTO @old_sql_mode; 5986SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 5987CREATE VIEW v1 AS 5988SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 5989; 5990CREATE TABLE t1 ( 5991pk int NOT NULL, 5992col_varchar_key varchar(1) DEFAULT NULL, 5993PRIMARY KEY (pk), 5994KEY col_varchar_key (col_varchar_key) 5995); 5996SELECT t1.pk 5997FROM t1 5998WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) 5999; 6000pk 6001SET SESSION sql_mode=@old_sql_mode; 6002drop table t2, t1; 6003drop view v1; 6004# 6005# BUG#50257: Missing info in REF column of the EXPLAIN 6006# lines for subselects 6007# 6008CREATE TABLE t1 (a INT, b INT, INDEX (a)); 6009INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); 6010EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; 6011id select_type table type possible_keys key key_len ref rows Extra 60121 SIMPLE t1 ref a a 5 const 1 6013EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); 6014id select_type table type possible_keys key key_len ref rows Extra 60151 PRIMARY t1 ALL NULL NULL NULL NULL 4 60162 SUBQUERY t1 ref a a 5 const 1 Using index 6017DROP TABLE t1; 6018# 6019# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) 6020# (duplicate of LP bug #888456) 6021# 6022CREATE TABLE t1 (f1 varchar(1)); 6023INSERT INTO t1 VALUES ('v'),('s'); 6024CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); 6025INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), 6026('d'),('y'),('t'),('d'),('s'); 6027EXPLAIN 6028SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 6029WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 6030WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); 6031id select_type table type possible_keys key key_len ref rows Extra 60321 PRIMARY table1 ALL NULL NULL NULL NULL 2 60331 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) 60342 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index 6035SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 6036WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 6037WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); 6038f1 f1_key 6039s c 6040s d 6041s d 6042s d 6043s j 6044s m 6045s s 6046s t 6047s v 6048s y 6049v c 6050v d 6051v d 6052v d 6053v j 6054v m 6055v s 6056v t 6057v v 6058v y 6059DROP TABLE t1,t2; 6060# 6061# LP bug 919427: EXPLAIN for a query over a single-row table 6062# with IN subquery in WHERE condition 6063# 6064CREATE TABLE ot ( 6065col_int_nokey int(11), 6066col_varchar_nokey varchar(1) 6067) ; 6068INSERT INTO ot VALUES (1,'x'); 6069CREATE TABLE it1( 6070col_int_key int(11), 6071col_varchar_key varchar(1), 6072KEY idx_cvk_cik (col_varchar_key,col_int_key) 6073); 6074INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f'); 6075CREATE TABLE it2 ( 6076col_int_key int(11), 6077col_varchar_key varchar(1), 6078col_varchar_key2 varchar(1), 6079KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key), 6080KEY idx_cvk_cik (col_varchar_key, col_int_key) 6081); 6082INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f'); 6083EXPLAIN 6084SELECT col_int_nokey FROM ot 6085WHERE col_varchar_nokey IN 6086(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); 6087id select_type table type possible_keys key key_len ref rows Extra 60881 PRIMARY ot system NULL NULL NULL NULL 1 60891 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 60902 MATERIALIZED it1 ref idx_cvk_cik idx_cvk_cik 9 const,const 1 Using where; Using index 6091SELECT col_int_nokey FROM ot 6092WHERE col_varchar_nokey IN 6093(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); 6094col_int_nokey 60951 6096EXPLAIN 6097SELECT col_int_nokey FROM ot 6098WHERE (col_varchar_nokey, 'x') IN 6099(SELECT col_varchar_key, col_varchar_key2 FROM it2); 6100id select_type table type possible_keys key key_len ref rows Extra 61011 PRIMARY ot system NULL NULL NULL NULL 1 61021 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 61032 MATERIALIZED it2 ref idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 const,const 1 Using where; Using index 6104SELECT col_int_nokey FROM ot 6105WHERE (col_varchar_nokey, 'x') IN 6106(SELECT col_varchar_key, col_varchar_key2 FROM it2); 6107col_int_nokey 61081 6109DROP TABLE ot,it1,it2; 6110# 6111# MDEV-746 6112# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY 6113# HAS AN EMPTY RESULT 6114# 6115CREATE TABLE t1 ( 6116pk int NOT NULL, 6117col_int_nokey int NOT NULL, 6118col_int_key int NOT NULL, 6119col_time_key time NOT NULL, 6120col_varchar_key varchar(1) NOT NULL, 6121col_varchar_nokey varchar(1) NOT NULL, 6122PRIMARY KEY (pk), 6123KEY col_int_key (col_int_key), 6124KEY col_time_key (col_time_key), 6125KEY col_varchar_key (col_varchar_key,col_int_key) 6126) ENGINE=MyISAM; 6127CREATE TABLE t2 ( 6128pk int NOT NULL AUTO_INCREMENT, 6129col_int_nokey int NOT NULL, 6130col_int_key int NOT NULL, 6131col_time_key time NOT NULL, 6132col_varchar_key varchar(1) NOT NULL, 6133col_varchar_nokey varchar(1) NOT NULL, 6134PRIMARY KEY (pk), 6135KEY col_int_key (col_int_key), 6136KEY col_time_key (col_time_key), 6137KEY col_varchar_key (col_varchar_key,col_int_key) 6138) ENGINE=MyISAM; 6139INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b'); 6140SET @var2:=4, @var3:=8; 6141 6142Testcase without inner subquery 6143EXPLAIN SELECT @var3:=12, sq4_alias1.* 6144FROM t1 AS sq4_alias1 6145WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6146sq4_alias1.col_varchar_key = @var3; 6147id select_type table type possible_keys key key_len ref rows Extra 61481 SIMPLE sq4_alias1 system NULL NULL NULL NULL 0 Const row not found 6149SELECT @var3:=12, sq4_alias1.* 6150FROM t1 AS sq4_alias1 6151WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6152sq4_alias1.col_varchar_key = @var3; 6153@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6154SELECT @var3; 6155@var3 61568 6157EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* 6158FROM t1 AS sq4_alias1 6159WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6160sq4_alias1.col_varchar_key = @var3 ) AS alias3; 6161id select_type table type possible_keys key key_len ref rows Extra 61621 PRIMARY <derived2> system NULL NULL NULL NULL 0 Const row not found 61632 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table 6164SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* 6165FROM t1 AS sq4_alias1 6166WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6167sq4_alias1.col_varchar_key = @var3 ) AS alias3; 6168@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6169SELECT @var3; 6170@var3 61718 6172 6173Testcase with inner subquery; crashed WL#6095 6174SET @var3=8; 6175EXPLAIN SELECT sq4_alias1.* 6176FROM t1 AS sq4_alias1 6177WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6178NOT IN 6179(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6180c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6181FROM t2 AS c_sq1_alias1 6182WHERE (c_sq1_alias1.col_int_nokey != @var2 6183OR c_sq1_alias1.pk != @var3)); 6184id select_type table type possible_keys key key_len ref rows Extra 61851 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 61862 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 6187SELECT sq4_alias1.* 6188FROM t1 AS sq4_alias1 6189WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6190NOT IN 6191(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6192c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6193FROM t2 AS c_sq1_alias1 6194WHERE (c_sq1_alias1.col_int_nokey != @var2 6195OR c_sq1_alias1.pk != @var3)); 6196pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6197EXPLAIN SELECT * FROM ( SELECT sq4_alias1.* 6198FROM t1 AS sq4_alias1 6199WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6200NOT IN 6201(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6202c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6203FROM t2 AS c_sq1_alias1 6204WHERE (c_sq1_alias1.col_int_nokey != @var2 6205OR c_sq1_alias1.pk != @var3)) ) AS alias3; 6206id select_type table type possible_keys key key_len ref rows Extra 62071 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 62083 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 6209SELECT * FROM ( SELECT sq4_alias1.* 6210FROM t1 AS sq4_alias1 6211WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6212NOT IN 6213(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6214c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6215FROM t2 AS c_sq1_alias1 6216WHERE (c_sq1_alias1.col_int_nokey != @var2 6217OR c_sq1_alias1.pk != @var3)) ) AS alias3; 6218pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6219DROP TABLE t1,t2; 6220# End of 5.2 tests 6221# 6222# BUG#779885: Crash in eliminate_item_equal with materialization=on in 6223# 6224CREATE TABLE t1 ( f1 int ); 6225INSERT INTO t1 VALUES (19), (20); 6226CREATE TABLE t2 ( f10 varchar(32) ); 6227INSERT INTO t2 VALUES ('c'),('d'); 6228CREATE TABLE t3 ( f10 varchar(32) ); 6229INSERT INTO t3 VALUES ('a'),('b'); 6230SELECT * 6231FROM t1 6232WHERE 6233( 't' ) IN ( 6234SELECT t3.f10 6235FROM t3 6236JOIN t2 6237ON t2.f10 = t3.f10 6238); 6239f1 6240DROP TABLE t1,t2,t3; 6241# 6242# BUG lp:813473: Wrong result with outer join + NOT IN subquery 6243# This bug is a duplicate of Bug#11764086 whose test case is added below 6244# 6245CREATE TABLE t1 (c int) ; 6246INSERT INTO t1 VALUES (5),(6); 6247CREATE TABLE t2 (a int, b int) ; 6248INSERT INTO t2 VALUES (20,9),(20,9); 6249create table t3 (d int, e int); 6250insert into t3 values (2, 9), (3,10); 6251SET @save_optimizer_switch=@@optimizer_switch; 6252SET optimizer_switch='outer_join_with_cache=off'; 6253EXPLAIN 6254SELECT t2.b , t1.c 6255FROM t2 LEFT JOIN t1 ON t1.c < 3 6256WHERE (t2.b , t1.c) NOT IN (SELECT * from t3); 6257id select_type table type possible_keys key key_len ref rows Extra 62581 PRIMARY t2 ALL NULL NULL NULL NULL 2 62591 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 62602 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where 6261SELECT t2.b , t1.c 6262FROM t2 LEFT JOIN t1 ON t1.c < 3 6263WHERE (t2.b, t1.c) NOT IN (SELECT * from t3); 6264b c 62659 NULL 62669 NULL 6267SET optimizer_switch=@save_optimizer_switch; 6268drop table t1, t2, t3; 6269# 6270# BUG#50257: Missing info in REF column of the EXPLAIN 6271# lines for subselects 6272# 6273CREATE TABLE t1 (a INT, b INT, INDEX (a)); 6274INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); 6275 6276set @tmp_optimizer_switch=@@optimizer_switch; 6277set optimizer_switch='derived_merge=off,derived_with_keys=off'; 6278EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; 6279id select_type table type possible_keys key key_len ref rows Extra 62801 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 62812 DERIVED t1 ref a a 5 const 1 6282set optimizer_switch=@tmp_optimizer_switch; 6283 6284EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); 6285id select_type table type possible_keys key key_len ref rows Extra 62861 PRIMARY t1 ALL NULL NULL NULL NULL 4 62872 SUBQUERY t1 ref a a 5 const 1 Using index 6288 6289DROP TABLE t1; 6290# 6291# Bug#11764086: Null left operand to NOT IN in WHERE clause 6292# behaves differently than real NULL 6293# 6294CREATE TABLE parent (id int); 6295INSERT INTO parent VALUES (1), (2); 6296CREATE TABLE child (parent_id int, other int); 6297INSERT INTO child VALUES (1,NULL); 6298# Offending query (c.parent_id is NULL for null-complemented rows only) 6299SELECT p.id, c.parent_id 6300FROM parent p 6301LEFT JOIN child c 6302ON p.id = c.parent_id 6303WHERE c.parent_id NOT IN ( 6304SELECT parent_id 6305FROM child 6306WHERE parent_id = 3 6307); 6308id parent_id 63091 1 63102 NULL 6311# Some syntactic variations with IS FALSE and IS NOT TRUE 6312SELECT p.id, c.parent_id 6313FROM parent p 6314LEFT JOIN child c 6315ON p.id = c.parent_id 6316WHERE c.parent_id IN ( 6317SELECT parent_id 6318FROM child 6319WHERE parent_id = 3 6320) IS NOT TRUE; 6321id parent_id 63221 1 63232 NULL 6324SELECT p.id, c.parent_id 6325FROM parent p 6326LEFT JOIN child c 6327ON p.id = c.parent_id 6328WHERE c.parent_id IN ( 6329SELECT parent_id 6330FROM child 6331WHERE parent_id = 3 6332) IS FALSE; 6333id parent_id 63341 1 63352 NULL 6336DROP TABLE parent, child; 6337# End of test for bug#11764086. 6338# 6339# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET || 6340# BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX 6341# 6342CREATE TABLE t1(a INT); 6343INSERT INTO t1 VALUES (0), (1); 6344CREATE TABLE t2( 6345b TEXT, 6346c INT, 6347PRIMARY KEY (b(1)) 6348); 6349INSERT INTO t2 VALUES ('a', 2), ('b', 3); 6350SELECT 1 FROM t1 WHERE a = 6351(SELECT 1 FROM t2 WHERE b = 6352(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) 6353ORDER BY b 6354); 63551 6356Warnings: 6357Warning 1292 Truncated incorrect DOUBLE value: 'a' 6358Warning 1292 Truncated incorrect DOUBLE value: 'b' 6359Warning 1292 Truncated incorrect DOUBLE value: 'a' 6360Warning 1292 Truncated incorrect DOUBLE value: 'b' 6361SELECT 1 FROM t1 WHERE a = 6362(SELECT 1 FROM t2 WHERE b = 6363(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) 6364GROUP BY b 6365); 63661 6367Warnings: 6368Warning 1292 Truncated incorrect DOUBLE value: 'a' 6369Warning 1292 Truncated incorrect DOUBLE value: 'b' 6370Warning 1292 Truncated incorrect DOUBLE value: 'a' 6371Warning 1292 Truncated incorrect DOUBLE value: 'b' 6372DROP TABLE t1, t2; 6373# 6374# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) 6375# 6376CREATE TABLE t1 (f1 varchar(1)); 6377INSERT INTO t1 VALUES ('v'),('s'); 6378CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); 6379INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), 6380('d'),('y'),('t'),('d'),('s'); 6381SELECT table1.f1, table2.f1_key 6382FROM t1 AS table1, t2 AS table2 6383WHERE EXISTS 6384( 6385SELECT DISTINCT f1_key 6386FROM t2 6387WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); 6388f1 f1_key 6389s c 6390s d 6391s d 6392s d 6393s j 6394s m 6395s s 6396s t 6397s v 6398s y 6399v c 6400v d 6401v d 6402v d 6403v j 6404v m 6405v s 6406v t 6407v v 6408v y 6409explain SELECT table1.f1, table2.f1_key 6410FROM t1 AS table1, t2 AS table2 6411WHERE EXISTS 6412( 6413SELECT DISTINCT f1_key 6414FROM t2 6415WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); 6416id select_type table type possible_keys key key_len ref rows Extra 64171 PRIMARY table1 ALL NULL NULL NULL NULL 2 64181 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) 64192 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index 6420DROP TABLE t1,t2; 6421# 6422# lp:826279: assertion failure with GROUP BY a result of subquery 6423# 6424CREATE TABLE t1 (a int); 6425INSERT INTO t1 VALUES (0), (0); 6426CREATE TABLE t2 (a int, b int, c int); 6427INSERT INTO t2 VALUES (10,7,0), (0,7,0); 6428CREATE TABLE t3 (a int, b int); 6429INSERT INTO t3 VALUES (10,7), (0,7); 6430SELECT SUM(DISTINCT b), 6431(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 6432WHERE t.a != 0 AND t2.a != 0) 6433FROM (SELECT * FROM t3) AS t 6434GROUP BY 2; 6435SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 6436WHERE t.a != 0 AND t2.a != 0) 64377 NULL 6438SELECT SUM(DISTINCT b), 6439(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) 6440FROM (SELECT * FROM t3) AS t 6441GROUP BY 2; 6442SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) 64437 NULL 64447 10 6445DROP TABLE t1,t2,t3; 6446# 6447# Bug#12329653 6448# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY 6449# 6450CREATE TABLE t1(a1 int); 6451INSERT INTO t1 VALUES (1),(2); 6452SELECT @@session.sql_mode INTO @old_sql_mode; 6453SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 6454SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 64551 64561 64571 6458PREPARE stmt FROM 6459'SELECT 1 UNION ALL 6460SELECT 1 FROM t1 6461ORDER BY 6462(SELECT 1 FROM t1 AS t1_0 6463 WHERE 1 < SOME (SELECT a1 FROM t1) 6464)' ; 6465EXECUTE stmt ; 6466ERROR 21000: Subquery returns more than 1 row 6467EXECUTE stmt ; 6468ERROR 21000: Subquery returns more than 1 row 6469SET SESSION sql_mode=@old_sql_mode; 6470DEALLOCATE PREPARE stmt; 6471DROP TABLE t1; 6472# 6473# LP BUG#833777 Performance regression with deeply nested subqueries 6474# 6475create table t1 (a int not null, b char(10) not null); 6476insert into t1 values (1, 'a'); 6477set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off'; 6478select 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))))))))))))))))))))))))))))); 6479a 64801 6481set @@optimizer_switch=@subselect_tmp; 6482drop table t1; 6483# 6484# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF 6485# 6486CREATE TABLE t1 (a varchar(3)); 6487INSERT INTO t1 VALUES ('AAA'),('BBB'); 6488CREATE TABLE t2 (a varchar(3)); 6489INSERT INTO t2 VALUES ('CCC'); 6490set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; 6491SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ'); 6492a 6493set @@optimizer_switch=@subselect_tmp; 6494drop table t1, t2; 6495# 6496# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize 6497# with view , UNION and prepared statement (rewriting fake_select 6498# condition). 6499# 6500CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ; 6501INSERT INTO t1 VALUES (6,'d'),(7,'y'); 6502CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ; 6503INSERT INTO t2 VALUES (10,7); 6504CREATE VIEW v2 AS SELECT * FROM t2; 6505PREPARE st1 FROM " 6506 SELECT * 6507 FROM t1 6508 LEFT JOIN v2 ON ( v2.f2 = t1.f1 ) 6509 WHERE v2.f1 NOT IN ( 6510 SELECT 1 UNION 6511 SELECT 247 6512 ) 6513"; 6514EXECUTE st1; 6515f1 f4 f1 f2 65167 y 10 7 6517deallocate prepare st1; 6518DROP VIEW v2; 6519DROP TABLE t1,t2; 6520# 6521# LP bug #887458 Crash in subselect_union_engine::no_rows with 6522# double UNION and join_cache_level=3,8 6523# (IN/ALL/ANY optimizations should not be applied to fake_select) 6524CREATE TABLE t2 ( a int, b varchar(1)) ; 6525INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y'); 6526CREATE TABLE t1 ( b varchar(1)) ; 6527INSERT IGNORE INTO t1 VALUES (NULL),(NULL); 6528set @save_join_cache_level=@@join_cache_level; 6529SET SESSION join_cache_level=3; 6530SELECT * 6531FROM t1, t2 6532WHERE t2.b IN ( 6533SELECT 'm' UNION 6534SELECT 'm' 6535) OR t1.b <> SOME ( 6536SELECT 'v' UNION 6537SELECT 't' 6538); 6539b a b 6540set @@join_cache_level= @save_join_cache_level; 6541drop table t1,t2; 6542# 6543# LP bug #885162 Got error 124 from storage engine with UNION inside 6544# subquery and join_cache_level=3..8 6545# (IN/ALL/ANY optimizations should not be applied to fake_select) 6546# 6547CREATE TABLE t1 ( 6548f1 varchar(1) DEFAULT NULL 6549); 6550INSERT INTO t1 VALUES ('c'); 6551set @save_join_cache_level=@@join_cache_level; 6552SET SESSION join_cache_level=8; 6553SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' ); 6554f1 6555set @@join_cache_level= @save_join_cache_level; 6556drop table t1; 6557# 6558# LP BUG#747278 incorrect values of the NULL (no rows) single 6559# row subquery requested via element_index() interface 6560# 6561CREATE TABLE t1 (f1a int, f1b int) ; 6562INSERT IGNORE INTO t1 VALUES (1,1),(2,2); 6563CREATE TABLE t2 ( f2 int); 6564INSERT IGNORE INTO t2 VALUES (3),(4); 6565CREATE TABLE t3 (f3a int default 1, f3b int default 2); 6566INSERT INTO t3 VALUES (1,1),(2,2); 6567set @old_optimizer_switch = @@session.optimizer_switch; 6568set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off'; 6569SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; 6570(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) 6571NULL 6572NULL 6573SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; 6574(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) 6575NULL 6576NULL 6577SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; 6578(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6579NULL 6580NULL 6581SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); 6582(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6583NULL 6584SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; 6585(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) 6586NULL 6587NULL 6588SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; 6589(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) 6590NULL 6591NULL 6592SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; 6593(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6594NULL 6595NULL 6596SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); 6597(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6598NULL 6599set @@session.optimizer_switch=@old_optimizer_switch; 6600SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; 6601(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) 6602NULL 6603NULL 6604SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; 6605(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) 6606NULL 6607NULL 6608SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; 6609(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6610NULL 6611NULL 6612SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); 6613(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6614NULL 6615SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; 6616(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) 6617NULL 6618NULL 6619SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; 6620(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) 6621NULL 6622NULL 6623SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; 6624(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6625NULL 6626NULL 6627SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); 6628(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6629NULL 6630select (null, null) = (null, null); 6631(null, null) = (null, null) 6632NULL 6633SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0); 6634(SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0) 6635NULL 6636drop tables t1,t2,t3; 6637# 6638# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists 6639# 6640CREATE TABLE t1 (a date, KEY (a)) ; 6641INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02'); 6642set @old_optimizer_switch = @@optimizer_switch; 6643SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; 6644EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6645id select_type table type possible_keys key key_len ref rows Extra 66461 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index 66472 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index 6648SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6649a 66502009-01-01 66512009-02-02 6652SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off'; 6653EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6654id select_type table type possible_keys key key_len ref rows Extra 66551 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index 66562 MATERIALIZED t1 index NULL a 4 NULL 2 Using index 6657SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6658a 66592009-01-01 66602009-02-02 6661set @@optimizer_switch=@old_optimizer_switch; 6662drop table t1; 6663# 6664# LP BUG#908269 incorrect condition in case of subqueries depending 6665# on constant tables 6666# 6667CREATE TABLE t1 ( a INT ); 6668INSERT INTO t1 VALUES (1),(5); 6669CREATE TABLE t2 ( b INT ) ENGINE=MyISAM; 6670INSERT INTO t2 VALUES (1); 6671CREATE TABLE t3 ( c INT ); 6672INSERT INTO t3 VALUES (4),(5); 6673SET optimizer_switch='subquery_cache=off'; 6674SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; 6675( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) 66761 6677NULL 6678SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1; 6679( SELECT b FROM t2 WHERE b = a OR b * 0) 66801 6681NULL 6682SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1; 6683( SELECT b FROM t2 WHERE b = a OR rand() * 0) 66841 6685NULL 6686drop table t1,t2,t3; 6687set optimizer_switch=@subselect_tmp; 6688# 6689# LP BUG#905353 Wrong non-empty result with a constant table, 6690# aggregate function in subquery, MyISAM or Aria 6691# 6692CREATE TABLE t1 ( a INT ) ENGINE=MyISAM; 6693INSERT INTO t1 VALUES (1); 6694SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 ); 6695a 6696drop table t1; 6697# 6698# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in 6699# main query and implicit grouping 6700# 6701CREATE TABLE t1 (f1 int) engine=MyISAM; 6702INSERT INTO t1 VALUES (7),(8); 6703CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM; 6704INSERT INTO t2 VALUES (3,'f'); 6705EXPLAIN 6706SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6707id select_type table type possible_keys key key_len ref rows Extra 67081 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67092 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6710SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6711COUNT(f1) f4 67120 NULL 6713EXPLAIN 6714SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6715id select_type table type possible_keys key key_len ref rows Extra 67161 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67172 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6718SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6719COUNT(f1) f4 67200 0 6721EXPLAIN 6722SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6723id select_type table type possible_keys key key_len ref rows Extra 67241 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67252 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 6726SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6727COUNT(f1) f4 67280 1 6729EXPLAIN 6730SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6731id select_type table type possible_keys key key_len ref rows Extra 67321 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67332 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 6734SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6735COUNT(f1) f4 67360 0 6737drop table t1,t2; 6738# 6739# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table, 6740# HAVING, UNION in subquery 6741# 6742CREATE TABLE t1 (a INT); 6743INSERT INTO t1 VALUES (7),(0); 6744CREATE TABLE t2 (b INT); 6745EXPLAIN 6746SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; 6747id select_type table type possible_keys key key_len ref rows Extra 67481 PRIMARY t2 system NULL NULL NULL NULL 0 Const row not found 67491 PRIMARY t1 ALL NULL NULL NULL NULL 2 67502 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 67513 UNION NULL NULL NULL NULL NULL NULL NULL No tables used 6752NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6753SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; 6754f1 f2 6755drop table t1,t2; 6756# 6757# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible 6758# WHERE and UNION in HAVING 6759# 6760CREATE TABLE t1 (a INT); 6761INSERT INTO t1 VALUES (1),(7); 6762EXPLAIN 6763SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; 6764id select_type table type possible_keys key key_len ref rows Extra 67651 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 67662 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 67673 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used 6768NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6769SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; 6770min_a a 6771EXPLAIN 6772SELECT 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; 6773id select_type table type possible_keys key key_len ref rows Extra 67741 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 67752 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 67763 DEPENDENT UNION t1 ALL NULL NULL NULL NULL 2 Using where 6777NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6778SELECT 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; 6779min_a a 6780drop table t1; 6781# 6782# MDEV-367: Different results with and without subquery_cache on 6783# a query with a constant NOT IN condition 6784# 6785CREATE TABLE t1 (a INT) ENGINE=MyISAM; 6786INSERT INTO t1 VALUES (1),(2),(3); 6787set @mdev367_optimizer_switch = @@optimizer_switch; 6788set optimizer_switch = 'subquery_cache=on'; 6789SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; 6790a 6791SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; 6792a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) 67931 NULL 67942 NULL 67953 NULL 6796set optimizer_switch=@mdev367_optimizer_switch; 6797set optimizer_switch = 'subquery_cache=off'; 6798SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; 6799a 6800SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; 6801a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) 68021 NULL 68032 NULL 68043 NULL 6805set optimizer_switch=@mdev367_optimizer_switch; 6806DROP TABLE t1; 6807# 6808# MDEV-521 single value subselect transformation problem 6809# 6810CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM; 6811INSERT INTO t1 VALUES ('u1'),('u2'); 6812SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); 6813f1 6814u1 6815u2 6816FLUSH TABLES; 6817SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); 6818f1 6819u1 6820u2 6821DROP TABLE t1; 6822# return optimizer switch changed in the beginning of this test 6823set optimizer_switch=@subselect_tmp; 6824# 6825# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not 6826# precomputed and thus not part of optimization 6827# 6828CREATE TABLE t1 ( a VARCHAR(16), KEY (a) ); 6829INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'), 6830('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade'); 6831EXPLAIN 6832SELECT MAX( alias2.a ) AS field 6833FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6834WHERE alias1.a = alias2.a OR alias1.a = 'y' 6835HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); 6836id select_type table type possible_keys key key_len ref rows Extra 68371 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING 68382 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where 6839SELECT MAX( alias2.a ) AS field 6840FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6841WHERE alias1.a = alias2.a OR alias1.a = 'y' 6842HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); 6843field 6844EXPLAIN 6845SELECT MAX( alias2.a ) 6846FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6847WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); 6848id select_type table type possible_keys key key_len ref rows Extra 68491 PRIMARY alias1 index a a 19 NULL 11 Using where; Using index 68501 PRIMARY alias2 ref a a 19 test.alias1.a 2 Using index 68511 PRIMARY alias3 index NULL a 19 NULL 11 Using index; Using join buffer (flat, BNL join) 68522 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where 6853SELECT MAX( alias2.a ) 6854FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6855WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); 6856MAX( alias2.a ) 6857Arden-Arcade 6858drop table t1; 6859# 6860# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows 6861# with semijoin+materialization, IN and = subqueries 6862# 6863CREATE TABLE t1 (a1 INT); 6864INSERT INTO t1 VALUES (4),(6); 6865CREATE TABLE t2 (b1 INT); 6866INSERT INTO t2 VALUES (1),(7); 6867EXPLAIN 6868SELECT * FROM t1 6869WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); 6870id select_type table type possible_keys key key_len ref rows Extra 68711 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 68722 SUBQUERY t1 ALL NULL NULL NULL NULL 2 68732 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1 68743 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 68753 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 6876SELECT * FROM t1 6877WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); 6878a1 6879drop table t1, t2; 6880# 6881# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS, 6882# inner joins takes hundreds times longer 6883# 6884CREATE TABLE t1 (a INT); 6885INSERT INTO t1 VALUES (1),(7); 6886CREATE TABLE t2 (b INT); 6887INSERT INTO t2 VALUES (4),(5); 6888CREATE TABLE t3 (c INT); 6889INSERT INTO t3 VALUES (8),(3); 6890set @@expensive_subquery_limit= 0; 6891EXPLAIN 6892SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6893FROM t2 alias1, t1 alias2, t1 alias3; 6894id select_type table type possible_keys key key_len ref rows Extra 68951 PRIMARY alias1 ALL NULL NULL NULL NULL 2 68961 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 68971 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 68982 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 68992 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 69003 SUBQUERY t3 ALL NULL NULL NULL NULL 2 6901flush status; 6902SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6903FROM t2 alias1, t1 alias2, t1 alias3; 6904(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6905NULL 6906NULL 6907NULL 6908NULL 6909NULL 6910NULL 6911NULL 6912NULL 6913show status like "subquery_cache%"; 6914Variable_name Value 6915Subquery_cache_hit 0 6916Subquery_cache_miss 0 6917show status like '%Handler_read%'; 6918Variable_name Value 6919Handler_read_first 0 6920Handler_read_key 0 6921Handler_read_last 0 6922Handler_read_next 0 6923Handler_read_prev 0 6924Handler_read_retry 0 6925Handler_read_rnd 0 6926Handler_read_rnd_deleted 0 6927Handler_read_rnd_next 58 6928set @@expensive_subquery_limit= default; 6929EXPLAIN 6930SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6931FROM t2 alias1, t1 alias2, t1 alias3; 6932id select_type table type possible_keys key key_len ref rows Extra 69331 PRIMARY alias1 ALL NULL NULL NULL NULL 2 69341 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 69351 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 69362 SUBQUERY t1 ALL NULL NULL NULL NULL 2 69372 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 69383 SUBQUERY t3 ALL NULL NULL NULL NULL 2 6939flush status; 6940SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6941FROM t2 alias1, t1 alias2, t1 alias3; 6942(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6943NULL 6944NULL 6945NULL 6946NULL 6947NULL 6948NULL 6949NULL 6950NULL 6951show status like "subquery_cache%"; 6952Variable_name Value 6953Subquery_cache_hit 0 6954Subquery_cache_miss 0 6955show status like '%Handler_read%'; 6956Variable_name Value 6957Handler_read_first 0 6958Handler_read_key 0 6959Handler_read_last 0 6960Handler_read_next 0 6961Handler_read_prev 0 6962Handler_read_retry 0 6963Handler_read_rnd 0 6964Handler_read_rnd_deleted 0 6965Handler_read_rnd_next 16 6966drop table t1, t2, t3; 6967# 6968# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin 6969# 6970CREATE TABLE t1 (a INT); 6971INSERT INTO t1 VALUES (0),(8); 6972CREATE TABLE t2 (b INT PRIMARY KEY); 6973INSERT INTO t2 VALUES (1),(2); 6974EXPLAIN 6975SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); 6976id select_type table type possible_keys key key_len ref rows Extra 69771 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 69782 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 69793 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6980SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); 6981a 6982drop table t1,t2; 6983# 6984# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used 6985# 6986CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM; 6987INSERT INTO t1 VALUES ('USA'); 6988CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM; 6989INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia'); 6990CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM; 6991INSERT INTO t3 VALUES (12),(22),(9),(45); 6992create table t4 like t3; 6993insert into t4 select * from t3; 6994# This should not show range access for table t2 6995explain 6996SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 6997WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); 6998id select_type table type possible_keys key key_len ref rows Extra 69991 PRIMARY t1 system NULL NULL NULL NULL 1 70001 PRIMARY t2 ALL b NULL NULL NULL 2 Using where 70011 PRIMARY t3 ref d d 5 test.t2.b 2 Using index 70023 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 7003set @tmp_mdev410=@@global.userstat; 7004set global userstat=on; 7005flush table_statistics; 7006flush index_statistics; 7007SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 7008WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); 7009MIN(b) 7010NULL 7011# The following shows that t2 was indeed scanned with a full scan. 7012show table_statistics; 7013Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes 7014test t1 2 0 0 7015test t2 3 0 0 7016show index_statistics; 7017Table_schema Table_name Index_name Rows_read 7018test t2 b 1 7019set global userstat=@tmp_mdev410; 7020DROP TABLE t1,t2,t3,t4; 7021# 7022# MDEV-430: Server crashes in select_describe on EXPLAIN with 7023# materialization+semijoin, 2 nested subqueries, aggregate functions 7024# 7025CREATE TABLE t1 (a INT, KEY(a)); 7026INSERT INTO t1 VALUES (1),(8); 7027CREATE TABLE t2 (b INT, KEY(b)); 7028INSERT INTO t2 VALUES (45),(17),(20); 7029EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; 7030id select_type table type possible_keys key key_len ref rows Extra 70311 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 70322 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index 70332 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index 7034DROP TABLE t1,t2; 7035# 7036# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields 7037# 7038CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM; 7039INSERT INTO t1 VALUES (8),(0); 7040CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM; 7041INSERT INTO t2 VALUES (4,'j'),(6,'v'); 7042CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM; 7043INSERT INTO t3 VALUES ('b'),('c'); 7044EXPLAIN 7045SELECT * FROM t1 7046WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; 7047id select_type table type possible_keys key key_len ref rows Extra 70481 PRIMARY t1 index a a 5 NULL 2 Using where; Using index 70492 SUBQUERY <subquery3> ALL distinct_key NULL NULL NULL 1 70502 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 70513 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 7052SELECT * FROM t1 7053WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; 7054a 7055drop table t1, t2, t3; 7056# 7057# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery 7058# 7059CREATE TABLE t1 (a INT, KEY(a)); 7060INSERT INTO t1 VALUES (1),(8); 7061CREATE TABLE t2 (b INT, KEY(b)); 7062INSERT INTO t2 VALUES (45),(17),(20); 7063EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; 7064id select_type table type possible_keys key key_len ref rows Extra 70651 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 70662 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index 70672 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index 7068DROP TABLE t1,t2; 7069# 7070# MDEV-5991: crash in Item_field::used_tables 7071# 7072create table t1 (c int); 7073select exists(select 1 from t1 group by `c` in (select `c` from t1)); 7074exists(select 1 from t1 group by `c` in (select `c` from t1)) 70750 7076drop table t1; 7077# 7078# MDEV-7565: Server crash with Signal 6 (part 2) 7079# 7080Select 7081(Select Sum(`TestCase`.Revenue) From mysql.slow_log E 7082Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2) 7083) As `ControlRev` 7084From 7085(Select 3 as Revenue, 4 as TemplateID) As `TestCase` 7086Group By TestCase.Revenue, TestCase.TemplateID; 7087ControlRev 7088NULL 7089# 7090# MDEV-7445:Server crash with Signal 6 7091# 7092CREATE PROCEDURE procedure2() 7093BEGIN 7094Select 7095(Select Sum(`TestCase`.Revenue) From mysql.slow_log E 7096Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2) 7097) As `ControlRev` 7098 From 7099(Select 3 as Revenue, 4 as TemplateID) As `TestCase` 7100Group By TestCase.Revenue, TestCase.TemplateID; 7101END | 7102call procedure2(); 7103ControlRev 7104NULL 7105call procedure2(); 7106ControlRev 7107NULL 7108drop procedure procedure2; 7109# 7110# MDEV-7846:Server crashes in Item_subselect::fix 7111#_fields or fails with Thread stack overrun 7112# 7113CREATE TABLE t1 (column1 INT) ENGINE=MyISAM; 7114INSERT INTO t1 VALUES (3),(9); 7115CREATE TABLE t2 (column2 INT) ENGINE=MyISAM; 7116INSERT INTO t2 VALUES (1),(4); 7117CREATE TABLE t3 (column3 INT) ENGINE=MyISAM; 7118INSERT INTO t3 VALUES (6),(8); 7119CREATE TABLE t4 (column4 INT) ENGINE=MyISAM; 7120INSERT INTO t4 VALUES (2),(5); 7121PREPARE stmt FROM " 7122SELECT ( 7123 SELECT MAX( table1.column1 ) AS field1 7124 FROM t1 AS table1 7125 WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 ) 7126) AS sq 7127FROM t3 AS table3, t4 AS table4 GROUP BY sq 7128"; 7129EXECUTE stmt; 7130sq 7131NULL 7132EXECUTE stmt; 7133sq 7134NULL 7135deallocate prepare stmt; 7136drop table t1,t2,t3,t4; 7137# 7138# MDEV-7122 7139# Assertion `0' failed in subselect_hash_sj_engine::init 7140# 7141set tmp_memory_table_size=0; 7142CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; 7143INSERT INTO t1 VALUES(0),(0),(0); 7144SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1); 7145a 71460 71470 71480 7149DROP TABLE t1; 7150set tmp_memory_table_size=default; 7151# 7152# MDEV-10776: Server crash on query 7153# 7154create table t1 (field1 int); 7155insert into t1 values (1); 7156select round((select 1 from t1 limit 1)) 7157from t1 7158group by round((select 1 from t1 limit 1)); 7159round((select 1 from t1 limit 1)) 71601 7161drop table t1; 7162# 7163# MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE || 7164# m_lock_type != 2' failed in handler::ha_index_read_map 7165# 7166CREATE TABLE t1 (f1 INT); 7167INSERT INTO t1 VALUES (1),(2); 7168CREATE TABLE t2 (f2 INT, KEY(f2)); 7169INSERT INTO t2 VALUES (3); 7170CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2; 7171SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq; 7172ERROR 42000: Can't group on 'sq' 7173SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq; 7174ERROR 42000: Can't group on 'sq' 7175SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0; 7176f2 71773 7178SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 ); 7179count(*) 71801 7181delete from t1; 7182SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq; 7183ERROR 42000: Can't group on 'sq' 7184SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq; 7185ERROR 42000: Can't group on 'sq' 7186drop view v2; 7187drop table t1,t2; 7188# 7189# MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*) 7190# 7191CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL); 7192INSERT INTO t1 VALUES ('foo','bar'); 7193SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 ); 7194f1 f2 7195SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 ); 7196f1 f2 7197foo bar 7198DROP TABLE t1; 7199# 7200# MDEV-10146: Wrong result (or questionable result and behavior) 7201# with aggregate function in uncorrelated SELECT subquery 7202# 7203CREATE TABLE t1 (f1 INT); 7204CREATE VIEW v1 AS SELECT * FROM t1; 7205INSERT INTO t1 VALUES (1),(2); 7206CREATE TABLE t2 (f2 int); 7207INSERT INTO t2 VALUES (3); 7208SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1; 7209( SELECT MAX(f1) FROM t2 ) 72102 7211SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1; 7212( SELECT MAX(f1) FROM t2 ) 72132 7214INSERT INTO t2 VALUES (4); 7215SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1; 7216ERROR 21000: Subquery returns more than 1 row 7217SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1; 7218ERROR 21000: Subquery returns more than 1 row 7219drop view v1; 7220drop table t1,t2; 7221CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM; 7222INSERT t1 VALUES (4),(8); 7223CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM; 7224INSERT t2 VALUES (6); 7225SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2; 7226(SELECT MAX(sq.f2) FROM t1) 7227NULL 7228# 7229# Disable this query till MDEV-13399 is resolved 7230# 7231# INSERT t2 VALUES (9); 7232# --error ER_SUBQUERY_NO_1_ROW 7233# SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2; 7234# 7235drop table t1, t2; 7236# 7237# MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in 7238# (5.5 test) 7239# 7240SET @optimiser_switch_save= @@optimizer_switch; 7241CREATE TABLE t1 (a INT NOT NULL); 7242INSERT INTO t1 VALUES (1),(1),(1),(5),(5); 7243CREATE TABLE t2 (b INT); 7244INSERT INTO t2 VALUES (5),(1); 7245CREATE TABLE t3 (c INT, KEY(c)); 7246INSERT INTO t3 VALUES (5),(5); 7247SET optimizer_switch='semijoin=on'; 7248select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`) 7249and t1.a in (select `test`.`t3`.`c` from `test`.`t3`); 7250a 72515 72525 7253SET optimizer_switch='semijoin=off'; 7254select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`) 7255and t1.a in (select `test`.`t3`.`c` from `test`.`t3`); 7256a 72575 72585 7259SET @@optimizer_switch= @optimiser_switch_save; 7260DROP TABLE t1, t2, t3; 7261# 7262# MDEV-16820: impossible where with inexpensive subquery 7263# 7264create table t1 (a int) engine=myisam; 7265insert into t1 values (3), (1), (7); 7266create table t2 (b int, index idx(b)); 7267insert into t2 values (2), (5), (3), (2); 7268explain select * from t1 where (select max(b) from t2) = 10; 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 7272explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3; 7273id select_type table type possible_keys key key_len ref rows Extra 72741 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 72752 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 7276drop table t1,t2; 7277# 7278# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 7279# 7280create table t10 (a int); 7281insert into t10 values (1),(2),(3); 7282create table t12 (a int); 7283insert into t12 values (1),(2),(3); 7284select * from t10 where exists (select * from t12 order by a limit 0); 7285a 7286explain select * from t10 where exists (select * from t12 order by a limit 0); 7287id select_type table type possible_keys key key_len ref rows Extra 72881 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 72892 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit 7290prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; 7291set @l=1; 7292execute stmt1 using @l; 7293a 72941 72952 72963 7297set @l=2; 7298execute stmt1 using @l; 7299a 73001 73012 73023 7303set @l=0; 7304execute stmt1 using @l; 7305a 7306deallocate prepare stmt1; 7307drop table t10, t12; 7308End of 5.5 tests 7309# End of 10.0 tests 7310# 7311# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops 7312# with UNION in ALL subquery 7313# 7314SET NAMES utf8; 7315CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM; 7316INSERT INTO t1 VALUES ('foo'); 7317SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' ); 7318f 7319foo 7320SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar'); 7321f 7322foo 7323drop table t1; 7324SET NAMES default; 7325# 7326# MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops 7327# 7328SET NAMES utf8; 7329CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM; 7330CREATE TABLE t2 (f3 INT) ENGINE=MyISAM; 7331SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' ); 7332f1 f2 f3 7333SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo'); 7334f1 f2 f3 7335DROP TABLE t1, t2; 7336SET NAMES default; 7337# End of 10.1 tests 7338# 7339# MDEV-12564: IN TO EXISTS transformation for rows after 7340# conversion an outer join to inner join 7341# 7342CREATE TABLE t ( 7343pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c) 7344) ENGINE=MyISAM; 7345INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar'); 7346SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk) 7347WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo'; 7348pk i c pk i c 73491 10 foo 1 10 foo 7350DROP TABLE t; 7351# 7352# MDEV-25002: Outer reference in ON clause of subselect 7353# 7354create table t1 ( 7355pk int primary key, 7356a int 7357) engine=myisam; 7358insert into t1 values (1,1), (2,2); 7359create table t2 ( 7360pk int primary key, 7361b int 7362) engine=myisam; 7363insert into t2 values (1,1), (2,3); 7364create table t3 (a int); 7365insert into t3 values (1),(2); 7366select a, 7367(select count(*) from t1, t2 7368where t2.pk=t3.a and t1.pk=1) as sq 7369from t3; 7370a sq 73711 1 73722 1 7373select a, 7374(select count(*) from t1 join t2 on t2.pk=t3.a 7375where t1.pk=1) as sq 7376from t3; 7377a sq 73781 1 73792 1 7380select a from t3 7381where a in (select t2.b from t1,t2 where t2.pk=t3.a and t1.pk=1); 7382a 73831 7384select a from t3 7385where a in (select t2.b from t1 join t2 on t2.pk=t3.a where t1.pk=1); 7386a 73871 7388drop table t1,t2,t3; 7389# End of 10.2 tests 7390# 7391# Start of 10.4 tests 7392# 7393# 7394# MDEV-16861 Split Item::update_null_value() into a new virtual method in Type_handler 7395# 7396SELECT ROW(1,2) = EXISTS (SELECT 1); 7397ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7398SELECT ROW(1,2) = 1 IN (SELECT 1 UNION SELECT 2); 7399ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7400SELECT ROW(1,2) = (1 = ANY (SELECT 1 UNION SELECT 2)); 7401ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7402# 7403# End of 10.4 tests 7404# 7405set optimizer_switch=default; 7406select @@optimizer_switch like '%subquery_cache=on%'; 7407@@optimizer_switch like '%subquery_cache=on%' 74081 7409set @join_cache_level_for_subselect_test=NULL; 7410