1drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12; 2drop view if exists v2; 3call mtr.add_suppression("Sort aborted.*"); 4set @subselect_tmp=@@optimizer_switch; 5set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test, 6"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off"); 7set join_cache_level=1; 8SET optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on'; 9SET optimizer_use_condition_selectivity=4; 10select (select 2); 11(select 2) 122 13explain extended select (select 2); 14id select_type table type possible_keys key key_len ref rows filtered Extra 151 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used 16Warnings: 17Note 1249 Select 2 was reduced during optimization 18Note 1003 select 2 AS `(select 2)` 19SELECT (SELECT 1) UNION SELECT (SELECT 2); 20(SELECT 1) 211 222 23explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2); 24id select_type table type possible_keys key key_len ref rows filtered Extra 251 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 263 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 27NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 28Warnings: 29Note 1249 Select 2 was reduced during optimization 30Note 1249 Select 4 was reduced during optimization 31Note 1003 /* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)` 32SELECT (SELECT (SELECT 0 UNION SELECT 0)); 33(SELECT (SELECT 0 UNION SELECT 0)) 340 35explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0)); 36id select_type table type possible_keys key key_len ref rows filtered Extra 371 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 383 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 394 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 40NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL 41Warnings: 42Note 1249 Select 2 was reduced during optimization 43Note 1003 /* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))` 44SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a; 45ERROR 42S22: Reference 'a' not supported (forward reference in item list) 46SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b; 47ERROR 42S22: Reference 'b' not supported (forward reference in item list) 48SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a; 49(SELECT 1) MAX(1) 501 1 51SELECT (SELECT a) as a; 52ERROR 42S22: Reference 'a' not supported (forward reference in item list) 53EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 54id select_type table type possible_keys key key_len ref rows filtered Extra 551 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00 563 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 572 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 58Warnings: 59Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 60Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1 61Note 1003 /* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1 62SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1; 631 641 65SELECT (SELECT 1), a; 66ERROR 42S22: Unknown column 'a' in 'field list' 67SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1; 68a 691 70SELECT 1 FROM (SELECT (SELECT a) b) c; 71ERROR 42S22: Unknown column 'a' in 'field list' 72SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id); 73id 741 75SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1); 76ERROR 21000: Operand should contain 1 column(s) 77SELECT 1 IN (SELECT 1); 781 IN (SELECT 1) 791 80SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a)); 811 821 83select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1)); 84ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PROCEDURE ANALYSE(1))' at line 1 85SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1)); 86ERROR 42000: PROCEDURE does not support subqueries or stored functions 87SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL; 88ERROR 42S22: Unknown column 'a' in 'field list' 89SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL; 90ERROR 42S22: Unknown column 'a' in 'field list' 91SELECT (SELECT 1,2,3) = ROW(1,2,3); 92(SELECT 1,2,3) = ROW(1,2,3) 931 94SELECT (SELECT 1,2,3) = ROW(1,2,1); 95(SELECT 1,2,3) = ROW(1,2,1) 960 97SELECT (SELECT 1,2,3) < ROW(1,2,1); 98(SELECT 1,2,3) < ROW(1,2,1) 990 100SELECT (SELECT 1,2,3) > ROW(1,2,1); 101(SELECT 1,2,3) > ROW(1,2,1) 1021 103SELECT (SELECT 1,2,3) = ROW(1,2,NULL); 104(SELECT 1,2,3) = ROW(1,2,NULL) 105NULL 106SELECT ROW(1,2,3) = (SELECT 1,2,3); 107ROW(1,2,3) = (SELECT 1,2,3) 1081 109SELECT ROW(1,2,3) = (SELECT 1,2,1); 110ROW(1,2,3) = (SELECT 1,2,1) 1110 112SELECT ROW(1,2,3) < (SELECT 1,2,1); 113ROW(1,2,3) < (SELECT 1,2,1) 1140 115SELECT ROW(1,2,3) > (SELECT 1,2,1); 116ROW(1,2,3) > (SELECT 1,2,1) 1171 118SELECT ROW(1,2,3) = (SELECT 1,2,NULL); 119ROW(1,2,3) = (SELECT 1,2,NULL) 120NULL 121SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a'); 122(SELECT 1.5,2,'a') = ROW(1.5,2,'a') 1231 124SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b'); 125(SELECT 1.5,2,'a') = ROW(1.5,2,'b') 1260 127SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b'); 128(SELECT 1.5,2,'a') = ROW('1.5b',2,'b') 1290 130Warnings: 131Warning 1292 Truncated incorrect DOUBLE value: '1.5b' 132SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a'); 133(SELECT 'b',2,'a') = ROW(1.5,2,'a') 1340 135Warnings: 136Warning 1292 Truncated incorrect DOUBLE value: 'b' 137SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a'); 138(SELECT 1.5,2,'a') = ROW(1.5,'2','a') 1391 140SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a'); 141(SELECT 1.5,'c','a') = ROW(1.5,2,'a') 1420 143Warnings: 144Warning 1292 Truncated incorrect DOUBLE value: 'c' 145SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a); 146ERROR 21000: Operand should contain 1 column(s) 147SELECT 1 as a,(SELECT a+a) b,(SELECT b); 148a b (SELECT b) 1491 2 2 150create table t1 (a int); 151create table t2 (a int, b int); 152create table t3 (a int); 153create table t4 (a int not null, b int not null); 154insert into t1 values (2); 155insert into t2 values (1,7),(2,7); 156insert into t4 values (4,8),(3,8),(5,9); 157select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1; 158ERROR 42S22: Reference 'a1' not supported (forward reference in item list) 159select (select a from t1 where t1.a=t2.a), a from t2; 160(select a from t1 where t1.a=t2.a) a 161NULL 1 1622 2 163select (select a from t1 where t1.a=t2.b), a from t2; 164(select a from t1 where t1.a=t2.b) a 165NULL 1 166NULL 2 167select (select a from t1), a, (select 1 union select 2 limit 1) from t2; 168(select a from t1) a (select 1 union select 2 limit 1) 1692 1 1 1702 2 1 171select (select a from t3), a from t2; 172(select a from t3) a 173NULL 1 174NULL 2 175select * from t2 where t2.a=(select a from t1); 176a b 1772 7 178insert into t3 values (6),(7),(3); 179select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1); 180a b 1811 7 1822 7 183(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) 184union (select * from t4 order by a limit 2) order by a limit 3; 185a b 1861 7 1872 7 1883 8 189(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a); 190a b 1911 7 1922 7 1934 8 1943 8 195explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a); 196id select_type table type possible_keys key key_len ref rows filtered Extra 1971 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 1982 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort 1993 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where 2004 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 201NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL 202Warnings: 203Note 1003 (/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`b` = (/* select#2 */ select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1)) union (/* select#3 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where `test`.`t4`.`b` = (/* select#4 */ select max(`test`.`t2`.`a`) * 4 from `test`.`t2`)) 204select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2; 205(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a 2063 1 2077 2 208select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 209(select * from t2 where a>1) as tt; 210(select t3.a from t3 where a<8 order by 1 desc limit 1) a 2117 2 212set @tmp_optimizer_switch=@@optimizer_switch; 213set optimizer_switch='derived_merge=off,derived_with_keys=off'; 214explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 215(select * from t2 where a>1) as tt; 216id select_type table type possible_keys key key_len ref rows filtered Extra 2171 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00 2183 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where 2192 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort 220Warnings: 221Note 1003 /* select#1 */ select (/* select#2 */ select `test`.`t3`.`a` from `test`.`t3` where `test`.`t3`.`a` < 8 order by 1 desc limit 1) AS `(select t3.a from t3 where a<8 order by 1 desc limit 1)`,`tt`.`a` AS `a` from (/* select#3 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`a` > 1) `tt` 222set optimizer_switch=@tmp_optimizer_switch; 223select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1); 224a 2252 226select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1); 227a 2282 229select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1); 230a 231select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 232b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) 2338 7.5000 2348 4.5000 2359 7.5000 236explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4; 237id select_type table type possible_keys key key_len ref rows filtered Extra 2381 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00 2392 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 2403 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 241Warnings: 242Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1 243Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ select min(`test`.`t3`.`a`) from `test`.`t3` where `test`.`t3`.`a` >= `test`.`t4`.`a`)) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4` 244select * from t3 where exists (select * from t2 where t2.b=t3.a); 245a 2467 247select * from t3 where not exists (select * from t2 where t2.b=t3.a); 248a 2496 2503 251select * from t3 where a in (select b from t2); 252a 2537 254select * from t3 where a not in (select b from t2); 255a 2566 2573 258select * from t3 where a = some (select b from t2); 259a 2607 261select * from t3 where a <> any (select b from t2); 262a 2636 2643 265select * from t3 where a = all (select b from t2); 266a 2677 268select * from t3 where a <> all (select b from t2); 269a 2706 2713 272insert into t2 values (100, 5); 273select * from t3 where a < any (select b from t2); 274a 2756 2763 277select * from t3 where a < all (select b from t2); 278a 2793 280select * from t3 where a >= any (select b from t2); 281a 2826 2837 284explain extended select * from t3 where a >= any (select b from t2); 285id select_type table type possible_keys key key_len ref rows filtered Extra 2861 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 2872 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 288Warnings: 289Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))) 290select * from t3 where a >= all (select b from t2); 291a 2927 293delete from t2 where a=100; 294select * from t3 where a in (select a,b from t2); 295ERROR 21000: Operand should contain 1 column(s) 296select * from t3 where a in (select * from t2); 297ERROR 21000: Operand should contain 1 column(s) 298insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10); 299select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b); 300b ma 301insert into t2 values (2,10); 302select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b); 303b ma 30410 1 305delete from t2 where a=2 and b=10; 306select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b); 307b ma 3087 12 309create table t5 (a int); 310select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 311(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 312NULL 1 3132 2 314insert into t5 values (5); 315select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 316(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 317NULL 1 3182 2 319insert into t5 values (2); 320select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 321(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a 322NULL 1 3232 2 324explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2; 325id select_type table type possible_keys key key_len ref rows filtered Extra 3261 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 3272 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 3283 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where 329NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 330Warnings: 331Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1 332Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1 333Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ select `test`.`t5`.`a` from `test`.`t5` where `test`.`t5`.`a` = `test`.`t2`.`a`)) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2` 334select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2; 335ERROR 21000: Subquery returns more than 1 row 336create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq)); 337create table t7( uq int primary key, name char(25)); 338insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta"); 339insert into t6 values (1,1),(1,2),(2,2),(1,3); 340select * from t6 where exists (select * from t7 where uq = clinic_uq); 341patient_uq clinic_uq 3421 1 3431 2 3442 2 345explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq); 346id select_type table type possible_keys key key_len ref rows filtered Extra 3471 PRIMARY t7 index PRIMARY PRIMARY 4 NULL 2 100.00 Using index 3481 PRIMARY t6 ALL i1 NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) 349Warnings: 350Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 351Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t7` join `test`.`t6` where `test`.`t6`.`clinic_uq` = `test`.`t7`.`uq` 352select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); 353ERROR 23000: Column 'a' in field list is ambiguous 354drop table t1,t2,t3; 355CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0'); 356INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b'); 357CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0'); 358INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2'); 359CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00'); 360INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13'); 361SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1); 362a b 363W 1732-02-22 364SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2); 365a b 366W 1 367SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3); 368a b 369W a 370CREATE TABLE `t8` ( 371`pseudo` varchar(35) character set latin1 NOT NULL default '', 372`email` varchar(60) character set latin1 NOT NULL default '', 373PRIMARY KEY (`pseudo`), 374UNIQUE KEY `email` (`email`) 375) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 376INSERT INTO t8 (pseudo,email) VALUES ('joce','test'); 377INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1'); 378INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1'); 379EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); 380id select_type table type possible_keys key key_len ref rows filtered Extra 3811 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3824 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 3832 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 3843 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index 385Warnings: 386Note 1003 /* select#1 */ select 'joce' AS `pseudo`,(/* select#2 */ select 'test' from `test`.`t8` where 1) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where 1 387SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM 388t8 WHERE pseudo='joce'); 389ERROR HY000: Illegal parameter data types varchar and row for operation '=' 390SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE 391pseudo='joce'); 392ERROR HY000: Illegal parameter data types varchar and row for operation '=' 393SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'); 394pseudo 395joce 396SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%'); 397ERROR 21000: Subquery returns more than 1 row 398drop table if exists t1,t2,t3,t4,t5,t6,t7,t8; 399CREATE TABLE `t1` ( 400`topic` mediumint(8) unsigned NOT NULL default '0', 401`date` date NOT NULL default '0000-00-00', 402`pseudo` varchar(35) character set latin1 NOT NULL default '', 403PRIMARY KEY (`pseudo`,`date`,`topic`), 404KEY `topic` (`topic`) 405) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 406INSERT INTO t1 (topic,date,pseudo) VALUES 407('43506','2002-10-02','joce'),('40143','2002-08-03','joce'); 408EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 409id select_type table type possible_keys key key_len ref rows filtered Extra 4101 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 411Warnings: 412Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03' 413EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 414id select_type table type possible_keys key key_len ref rows filtered Extra 4151 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4162 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index 417Warnings: 418Note 1003 /* select#1 */ select (/* select#2 */ select distinct `test`.`t1`.`date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03') AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')` 419SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'; 420date 4212002-08-03 422SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03'); 423(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03') 4242002-08-03 425SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1; 4261 4271 4281 4291 430SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1; 431ERROR 21000: Subquery returns more than 1 row 432EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1); 433id select_type table type possible_keys key key_len ref rows filtered Extra 4341 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index 4352 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4363 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 437NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 438Warnings: 439Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 440drop table t1; 441CREATE TABLE `t1` ( 442`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 443`maxnumrep` int(10) unsigned NOT NULL default '0', 444PRIMARY KEY (`numeropost`), 445UNIQUE KEY `maxnumrep` (`maxnumrep`) 446) ENGINE=MyISAM ROW_FORMAT=FIXED; 447INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2); 448CREATE TABLE `t2` ( 449`mot` varchar(30) NOT NULL default '', 450`topic` mediumint(8) unsigned NOT NULL default '0', 451`date` date NOT NULL default '0000-00-00', 452`pseudo` varchar(35) NOT NULL default '', 453PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`) 454) ENGINE=MyISAM ROW_FORMAT=DYNAMIC; 455INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce'); 456select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 457a 45840143 459SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20; 460numeropost maxnumrep 46143506 2 46240143 1 463SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b; 464ERROR 42S22: Unknown column 'a' in 'having clause' 465SELECT 1 IN (SELECT 1 FROM t2 HAVING a); 466ERROR 42S22: Unknown column 'a' in 'having clause' 467SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic); 468mot topic date pseudo 469joce 40143 2002-10-22 joce 470joce 43506 2002-10-22 joce 471SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 472mot topic date pseudo 473SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1); 474mot topic date pseudo 475SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic); 476mot topic date pseudo 477joce 40143 2002-10-22 joce 478joce 43506 2002-10-22 joce 479SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 480mot topic date pseudo 481SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1); 482mot topic date pseudo 483SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic); 484mot topic date pseudo 485SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 486mot topic date pseudo 487joce 40143 2002-10-22 joce 488joce 43506 2002-10-22 joce 489SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2; 490mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) 491joce 40143 2002-10-22 joce 1 492joce 43506 2002-10-22 joce 1 493SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2); 494mot topic date pseudo 495SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2); 496mot topic date pseudo 497joce 40143 2002-10-22 joce 498joce 43506 2002-10-22 joce 499SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 500mot topic date pseudo 501joce 40143 2002-10-22 joce 502SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 503mot topic date pseudo 504joce 40143 2002-10-22 joce 505SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 506mot topic date pseudo 507joce 40143 2002-10-22 joce 508SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2; 509mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) 510joce 40143 2002-10-22 joce 1 511joce 43506 2002-10-22 joce 0 512drop table t1,t2; 513CREATE TABLE `t1` ( 514`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 515`maxnumrep` int(10) unsigned NOT NULL default '0', 516PRIMARY KEY (`numeropost`), 517UNIQUE KEY `maxnumrep` (`maxnumrep`) 518) ENGINE=MyISAM ROW_FORMAT=FIXED; 519INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1); 520select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1); 521ERROR 21000: Subquery returns more than 1 row 522select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1); 523ERROR 21000: Subquery returns more than 1 row 524show warnings; 525Level Code Message 526Error 1242 Subquery returns more than 1 row 527Error 1028 Sort aborted: Subquery returns more than 1 row 528drop table t1; 529create table t1 (a int); 530insert into t1 values (1),(2),(3); 531(select * from t1) union (select * from t1) order by (select a from t1 limit 1); 532a 5331 5342 5353 536drop table t1; 537CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b'); 538INSERT INTO t1 VALUES (); 539SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b'); 540ERROR 21000: Subquery returns more than 1 row 541drop table t1; 542CREATE TABLE `t1` ( 543`numeropost` mediumint(8) unsigned NOT NULL default '0', 544`numreponse` int(10) unsigned NOT NULL auto_increment, 545`pseudo` varchar(35) NOT NULL default '', 546PRIMARY KEY (`numeropost`,`numreponse`), 547UNIQUE KEY `numreponse` (`numreponse`), 548KEY `pseudo` (`pseudo`,`numeropost`) 549) ENGINE=MyISAM; 550SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a; 551ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list) 552SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a; 553ERROR 42S22: Unknown column 'a' in 'having clause' 554SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a; 555numreponse (SELECT numeropost FROM t1 HAVING numreponse=1) 556INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test'); 557EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 558ERROR 21000: Subquery returns more than 1 row 559SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1'); 560ERROR 21000: Subquery returns more than 1 row 561EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'; 562id select_type table type possible_keys key key_len ref rows filtered Extra 5631 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 564Warnings: 565Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where `test`.`t1`.`numeropost` = '1' 566EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1'); 567id select_type table type possible_keys key key_len ref rows filtered Extra 5681 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using index 5692 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 570Warnings: 571Note 1003 /* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 1 572drop table t1; 573CREATE TABLE t1 (a int(1)); 574INSERT INTO t1 VALUES (1); 575SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1; 5761 5771 578drop table t1; 579create table t1 (a int NOT NULL, b int, primary key (a)); 580create table t2 (a int NOT NULL, b int, primary key (a)); 581insert into t1 values (0, 10),(1, 11),(2, 12); 582insert into t2 values (1, 21),(2, 22),(3, 23); 583select * from t1; 584a b 5850 10 5861 11 5872 12 588update t1 set b= (select b from t2); 589ERROR 21000: Subquery returns more than 1 row 590update t1 set b= (select b from t2 where t1.a = t2.a); 591select * from t1; 592a b 5930 NULL 5941 21 5952 22 596drop table t1, t2; 597create table t1 (a int NOT NULL, b int, primary key (a)); 598create table t2 (a int NOT NULL, b int, primary key (a)); 599insert into t1 values (0, 10),(1, 11),(2, 12); 600insert into t2 values (1, 21),(2, 12),(3, 23); 601select * from t1; 602a b 6030 10 6041 11 6052 12 606select * from t1 where b = (select b from t2 where t1.a = t2.a); 607a b 6082 12 609delete from t1 where b in (select b from t1); 610affected rows: 3 611insert into t1 values (0, 10),(1, 11),(2, 12); 612delete from t1 where b = (select b from t2); 613ERROR 21000: Subquery returns more than 1 row 614delete from t1 where b = (select b from t2 where t1.a = t2.a); 615select * from t1 order by b; 616a b 6170 10 6181 11 619drop table t1, t2; 620create table t11 (a int NOT NULL, b int, primary key (a)); 621create table t12 (a int NOT NULL, b int, primary key (a)); 622create table t2 (a int NOT NULL, b int, primary key (a)); 623insert into t11 values (0, 10),(1, 11),(2, 12); 624insert into t12 values (33, 10),(22, 11),(2, 12); 625insert into t2 values (1, 21),(2, 12),(3, 23); 626select * from t11; 627a b 6280 10 6291 11 6302 12 631select * from t12; 632a b 63333 10 63422 11 6352 12 636delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a); 637ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data 638delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2); 639ERROR 21000: Subquery returns more than 1 row 640delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a); 641select * from t11; 642a b 6430 10 6441 11 645select * from t12; 646a b 64733 10 64822 11 649drop table t11, t12, t2; 650CREATE TABLE t1 (x int) ENGINE=MyISAM; 651create table t2 (a int) ENGINE=MyISAM; 652create table t3 (b int); 653insert into t2 values (1); 654insert into t3 values (1),(2); 655INSERT INTO t1 (x) VALUES ((SELECT x FROM t1)); 656ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data 657INSERT INTO t1 (x) VALUES ((SELECT b FROM t3)); 658ERROR 21000: Subquery returns more than 1 row 659INSERT INTO t1 (x) VALUES ((SELECT a FROM t2)); 660select * from t1; 661x 6621 663insert into t2 values (1); 664INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); 665select * from t1; 666x 6671 6682 669INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2; 670select * from t1; 671x 6721 6732 6743 6753 676INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2; 677select * from t1; 678x 6791 6802 6813 6823 68311 68411 685INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2)); 686select * from t1; 687x 6881 6892 6903 6913 69211 69311 6942 695drop table t1, t2, t3; 696CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM; 697create table t2 (a int); 698create table t3 (a int); 699insert into t2 values (1); 700insert into t3 values (1),(2); 701select * from t1; 702x y 703replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2)); 704ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data 705replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2)); 706ERROR 21000: Subquery returns more than 1 row 707replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2)); 708select * from t1; 709x y 7101 2 711replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2)); 712select * from t1; 713x y 7141 3 715replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2)); 716select * from t1; 717x y 7181 3 7194 1 720replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2)); 721select * from t1; 722x y 7231 3 7244 2 725replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2)); 726select * from t1; 727x y 7281 3 7294 2 7302 1 731drop table t1, t2, t3; 732SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *); 733ERROR HY000: No tables used 734CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; 735INSERT INTO t2 VALUES (1),(2); 736SELECT * FROM t2 WHERE id IN (SELECT 1); 737id 7381 739EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1); 740id select_type table type possible_keys key key_len ref rows filtered Extra 7411 SIMPLE t2 ref id id 5 const 1 100.00 Using index 742Warnings: 743Note 1249 Select 2 was reduced during optimization 744Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1 745SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); 746id 7471 748SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); 749id 7502 751EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); 752id select_type table type possible_keys key key_len ref rows filtered Extra 7531 SIMPLE t2 ref id id 5 const 1 100.00 Using where; Using index 754Warnings: 755Note 1249 Select 3 was reduced during optimization 756Note 1249 Select 2 was reduced during optimization 757Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1) 758EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); 759id select_type table type possible_keys key key_len ref rows filtered Extra 7601 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index 7612 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 7623 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 763NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 764Warnings: 765Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(/* select#2 */ select 1 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(1) union /* select#3 */ select 3 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))) 766SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3); 767id 768SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2); 769id 7702 771INSERT INTO t2 VALUES ((SELECT * FROM t2)); 772ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data 773INSERT INTO t2 VALUES ((SELECT id FROM t2)); 774ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data 775SELECT * FROM t2; 776id 7771 7782 779CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1; 780INSERT INTO t1 values (1),(1); 781UPDATE t2 SET id=(SELECT * FROM t1); 782ERROR 21000: Subquery returns more than 1 row 783drop table t2, t1; 784create table t1 (a int); 785insert into t1 values (1),(2),(3); 786select 1 IN (SELECT * from t1); 7871 IN (SELECT * from t1) 7881 789select 10 IN (SELECT * from t1); 79010 IN (SELECT * from t1) 7910 792select NULL IN (SELECT * from t1); 793NULL IN (SELECT * from t1) 794NULL 795update t1 set a=NULL where a=2; 796select 1 IN (SELECT * from t1); 7971 IN (SELECT * from t1) 7981 799select 3 IN (SELECT * from t1); 8003 IN (SELECT * from t1) 8011 802select 10 IN (SELECT * from t1); 80310 IN (SELECT * from t1) 804NULL 805select 1 > ALL (SELECT * from t1); 8061 > ALL (SELECT * from t1) 8070 808select 10 > ALL (SELECT * from t1); 80910 > ALL (SELECT * from t1) 810NULL 811select 1 > ANY (SELECT * from t1); 8121 > ANY (SELECT * from t1) 813NULL 814select 10 > ANY (SELECT * from t1); 81510 > ANY (SELECT * from t1) 8161 817drop table t1; 818create table t1 (a varchar(20)); 819insert into t1 values ('A'),('BC'),('DEF'); 820select 'A' IN (SELECT * from t1); 821'A' IN (SELECT * from t1) 8221 823select 'XYZS' IN (SELECT * from t1); 824'XYZS' IN (SELECT * from t1) 8250 826select NULL IN (SELECT * from t1); 827NULL IN (SELECT * from t1) 828NULL 829update t1 set a=NULL where a='BC'; 830select 'A' IN (SELECT * from t1); 831'A' IN (SELECT * from t1) 8321 833select 'DEF' IN (SELECT * from t1); 834'DEF' IN (SELECT * from t1) 8351 836select 'XYZS' IN (SELECT * from t1); 837'XYZS' IN (SELECT * from t1) 838NULL 839select 'A' > ALL (SELECT * from t1); 840'A' > ALL (SELECT * from t1) 8410 842select 'XYZS' > ALL (SELECT * from t1); 843'XYZS' > ALL (SELECT * from t1) 844NULL 845select 'A' > ANY (SELECT * from t1); 846'A' > ANY (SELECT * from t1) 847NULL 848select 'XYZS' > ANY (SELECT * from t1); 849'XYZS' > ANY (SELECT * from t1) 8501 851drop table t1; 852create table t1 (a float); 853insert into t1 values (1.5),(2.5),(3.5); 854select 1.5 IN (SELECT * from t1); 8551.5 IN (SELECT * from t1) 8561 857select 10.5 IN (SELECT * from t1); 85810.5 IN (SELECT * from t1) 8590 860select NULL IN (SELECT * from t1); 861NULL IN (SELECT * from t1) 862NULL 863update t1 set a=NULL where a=2.5; 864select 1.5 IN (SELECT * from t1); 8651.5 IN (SELECT * from t1) 8661 867select 3.5 IN (SELECT * from t1); 8683.5 IN (SELECT * from t1) 8691 870select 10.5 IN (SELECT * from t1); 87110.5 IN (SELECT * from t1) 872NULL 873select 1.5 > ALL (SELECT * from t1); 8741.5 > ALL (SELECT * from t1) 8750 876select 10.5 > ALL (SELECT * from t1); 87710.5 > ALL (SELECT * from t1) 878NULL 879select 1.5 > ANY (SELECT * from t1); 8801.5 > ANY (SELECT * from t1) 881NULL 882select 10.5 > ANY (SELECT * from t1); 88310.5 > ANY (SELECT * from t1) 8841 885explain extended select (select a+1) from t1; 886id select_type table type possible_keys key key_len ref rows filtered Extra 8871 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 888Warnings: 889Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 890Note 1249 Select 2 was reduced during optimization 891Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1` 892select (select a+1) from t1; 893(select a+1) 8942.5 895NULL 8964.5 897drop table t1; 898CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a)); 899CREATE TABLE t2 (a int(11) default '0', INDEX (a)); 900INSERT INTO t1 VALUES (1),(2),(3),(4); 901INSERT INTO t2 VALUES (1),(2),(3); 902SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; 903a t1.a in (select t2.a from t2) 9041 1 9052 1 9063 1 9074 0 908explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; 909id select_type table type possible_keys key key_len ref rows filtered Extra 9101 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index 9112 SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index 912Warnings: 913Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having `test`.`t2`.`a` is null)))) AS `t1.a in (select t2.a from t2)` from `test`.`t1` 914CREATE TABLE t3 (a int(11) default '0'); 915INSERT INTO t3 VALUES (1),(2),(3); 916SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; 917a t1.a in (select t2.a from t2,t3 where t3.a=t2.a) 9181 1 9192 1 9203 1 9214 0 922explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; 923id select_type table type possible_keys key key_len ref rows filtered Extra 9241 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index 9252 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index 9262 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) 927Warnings: 928Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where `test`.`t3`.`a` = `test`.`t2`.`a` and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a` or `test`.`t2`.`a` is null) having `test`.`t2`.`a` is null))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1` 929drop table t1,t2,t3; 930# check correct NULL Processing for normal IN/ALL/ANY 931# and 2 ways of max/min optimization 932create table t1 (a int); 933insert into t1 values (1), (100), (NULL), (1000); 934create table t2 (a int not null); 935# subselect returns empty set (for NULL and non-NULL left part) 936select a, a in (select * from t2) from t1; 937a a in (select * from t2) 9381 0 939100 0 940NULL 0 9411000 0 942select a, a > any (select * from t2) from t1; 943a a > any (select * from t2) 9441 0 945100 0 946NULL 0 9471000 0 948select a, a > all (select * from t2) from t1; 949a a > all (select * from t2) 9501 1 951100 1 952NULL 1 9531000 1 954select a from t1 where a in (select * from t2); 955a 956select a from t1 where a > any (select * from t2); 957a 958select a from t1 where a > all (select * from t2); 959a 9601 961100 962NULL 9631000 964select a from t1 where a in (select * from t2 group by a); 965a 966select a from t1 where a > any (select * from t2 group by a); 967a 968select a from t1 where a > all (select * from t2 group by a); 969a 9701 971100 972NULL 9731000 974insert into t2 values (1),(200); 975# sebselect returns non-empty set without NULLs 976select a, a in (select * from t2) from t1; 977a a in (select * from t2) 9781 1 979100 0 980NULL NULL 9811000 0 982select a, a > any (select * from t2) from t1; 983a a > any (select * from t2) 9841 0 985100 1 986NULL NULL 9871000 1 988select a, a > all (select * from t2) from t1; 989a a > all (select * from t2) 9901 0 991100 0 992NULL NULL 9931000 1 994select a from t1 where a in (select * from t2); 995a 9961 997select a from t1 where a > any (select * from t2); 998a 999100 10001000 1001select a from t1 where a > all (select * from t2); 1002a 10031000 1004select a from t1 where a in (select * from t2 group by a); 1005a 10061 1007select a from t1 where a > any (select * from t2 group by a); 1008a 1009100 10101000 1011select a from t1 where a > all (select * from t2 group by a); 1012a 10131000 1014drop table t2; 1015create table t2 (a int); 1016insert into t2 values (1),(NULL),(200); 1017# sebselect returns non-empty set with NULLs 1018select a, a in (select * from t2) from t1; 1019a a in (select * from t2) 10201 1 1021100 NULL 1022NULL NULL 10231000 NULL 1024select a, a > any (select * from t2) from t1; 1025a a > any (select * from t2) 10261 NULL 1027100 1 1028NULL NULL 10291000 1 1030select a, a > all (select * from t2) from t1; 1031a a > all (select * from t2) 10321 0 1033100 0 1034NULL NULL 10351000 NULL 1036select a from t1 where a in (select * from t2); 1037a 10381 1039select a from t1 where a > any (select * from t2); 1040a 1041100 10421000 1043select a from t1 where a > all (select * from t2); 1044a 1045select a from t1 where a in (select * from t2 group by a); 1046a 10471 1048select a from t1 where a > any (select * from t2 group by a); 1049a 1050100 10511000 1052select a from t1 where a > all (select * from t2 group by a); 1053a 1054drop table t1, t2; 1055create table t1 (a float); 1056select 10.5 IN (SELECT * from t1 LIMIT 1); 1057ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1058select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5); 1059ERROR 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 1060select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1); 1061ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1062drop table t1; 1063create table t1 (a int, b int, c varchar(10)); 1064create table t2 (a int); 1065insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c'); 1066insert into t2 values (1),(2),(NULL); 1067select 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; 1068a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a) 10691 1 a 10702 0 b 1071NULL NULL NULL 1072select 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; 1073a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a) 10741 0 a 10752 1 b 1076NULL NULL NULL 1077select 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; 1078a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a) 10791 0 a 10802 0 b 1081NULL NULL NULL 1082drop table t1,t2; 1083create table t1 (a int, b real, c varchar(10)); 1084insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b'); 1085select ROW(1, 1, 'a') IN (select a,b,c from t1); 1086ROW(1, 1, 'a') IN (select a,b,c from t1) 10871 1088select ROW(1, 2, 'a') IN (select a,b,c from t1); 1089ROW(1, 2, 'a') IN (select a,b,c from t1) 10900 1091select ROW(1, 1, 'a') IN (select b,a,c from t1); 1092ROW(1, 1, 'a') IN (select b,a,c from t1) 10931 1094select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null); 1095ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null) 10961 1097select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null); 1098ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null) 10990 1100select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null); 1101ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null) 11021 1103select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 1104ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a') 11051 1106select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 1107ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a') 11080 1109select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a'); 1110ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a') 11111 1112select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2); 1113ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' 1114drop table t1; 1115create table t1 (a int); 1116insert into t1 values (1); 1117do @a:=(SELECT a from t1); 1118select @a; 1119@a 11201 1121set @a:=2; 1122set @a:=(SELECT a from t1); 1123select @a; 1124@a 11251 1126drop table t1; 1127do (SELECT a from t1); 1128ERROR 42S02: Table 'test.t1' doesn't exist 1129set @a:=(SELECT a from t1); 1130ERROR 42S02: Table 'test.t1' doesn't exist 1131CREATE TABLE t1 (a int, KEY(a)); 1132HANDLER t1 OPEN; 1133HANDLER t1 READ a=((SELECT 1)); 1134ERROR 42000: HANDLER..READ does not support subqueries or stored functions 1135HANDLER t1 CLOSE; 1136drop table t1; 1137create table t1 (a int); 1138create table t2 (b int); 1139insert into t1 values (1),(2); 1140insert into t2 values (1); 1141select a from t1 where a in (select a from t1 where a in (select b from t2)); 1142a 11431 1144drop table t1, t2; 1145create table t1 (a int, b int); 1146create table t2 like t1; 1147insert into t1 values (1,2),(1,3),(1,4),(1,5); 1148insert into t2 values (1,2),(1,3); 1149select * from t1 where row(a,b) in (select a,b from t2); 1150a b 11511 2 11521 3 1153drop table t1, t2; 1154CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1; 1155INSERT INTO t1 VALUES (1); 1156UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); 1157select * from t1; 1158i 11592 1160drop table t1; 1161CREATE TABLE t1 (a int(1)); 1162EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1; 1163id select_type table type possible_keys key key_len ref rows filtered Extra 11641 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 11652 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found 1166Warnings: 1167Note 1003 /* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1` 1168EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') 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 encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1` 1174EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) 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 benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1` 1180drop table t1; 1181CREATE TABLE `t1` ( 1182`mot` varchar(30) character set latin1 NOT NULL default '', 1183`topic` mediumint(8) unsigned NOT NULL default '0', 1184`date` date NOT NULL default '0000-00-00', 1185`pseudo` varchar(35) character set latin1 NOT NULL default '', 1186PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), 1187KEY `pseudo` (`pseudo`,`date`,`topic`), 1188KEY `topic` (`topic`) 1189) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 1190CREATE TABLE `t2` ( 1191`mot` varchar(30) character set latin1 NOT NULL default '', 1192`topic` mediumint(8) unsigned NOT NULL default '0', 1193`date` date NOT NULL default '0000-00-00', 1194`pseudo` varchar(35) character set latin1 NOT NULL default '', 1195PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`), 1196KEY `pseudo` (`pseudo`,`date`,`topic`), 1197KEY `topic` (`topic`) 1198) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC; 1199CREATE TABLE `t3` ( 1200`numeropost` mediumint(8) unsigned NOT NULL auto_increment, 1201`maxnumrep` int(10) unsigned NOT NULL default '0', 1202PRIMARY KEY (`numeropost`), 1203UNIQUE KEY `maxnumrep` (`maxnumrep`) 1204) ENGINE=MyISAM CHARSET=latin1; 1205INSERT IGNORE INTO t1 VALUES ('joce','1','','joce'),('test','2','','test'); 1206Warnings: 1207Warning 1265 Data truncated for column 'date' at row 1 1208Warning 1265 Data truncated for column 'date' at row 2 1209INSERT IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test'); 1210Warnings: 1211Warning 1265 Data truncated for column 'date' at row 1 1212Warning 1265 Data truncated for column 'date' at row 2 1213INSERT INTO t3 VALUES (1,1); 1214SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE 1215numeropost=topic); 1216topic 12172 1218select * from t1; 1219mot topic date pseudo 1220joce 1 0000-00-00 joce 1221test 2 0000-00-00 test 1222DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT 1223EXISTS(SELECT * FROM t3 WHERE numeropost=topic)); 1224select * from t1; 1225mot topic date pseudo 1226joce 1 0000-00-00 joce 1227drop table t1, t2, t3; 1228SELECT * FROM (SELECT 1 as a,(SELECT a)) a; 1229a (SELECT a) 12301 1 1231CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a; 1232SHOW CREATE TABLE t1; 1233Table Create Table 1234t1 CREATE TABLE `t1` ( 1235 `a` int(1) NOT NULL DEFAULT 0, 1236 `(SELECT 1)` int(1) NOT NULL DEFAULT 0 1237) ENGINE=MyISAM DEFAULT CHARSET=latin1 1238drop table t1; 1239CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a; 1240SHOW CREATE TABLE t1; 1241Table Create Table 1242t1 CREATE TABLE `t1` ( 1243 `a` int(1) NOT NULL DEFAULT 0, 1244 `(SELECT a)` int(1) NOT NULL DEFAULT 0 1245) ENGINE=MyISAM DEFAULT CHARSET=latin1 1246drop table t1; 1247CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; 1248SHOW CREATE TABLE t1; 1249Table Create Table 1250t1 CREATE TABLE `t1` ( 1251 `a` int(1) NOT NULL DEFAULT 0, 1252 `(SELECT a+0)` int(3) NOT NULL DEFAULT 0 1253) ENGINE=MyISAM DEFAULT CHARSET=latin1 1254drop table t1; 1255CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a; 1256select * from t1; 1257a 12582 1259SHOW CREATE TABLE t1; 1260Table Create Table 1261t1 CREATE TABLE `t1` ( 1262 `a` int(3) NOT NULL 1263) ENGINE=MyISAM DEFAULT CHARSET=latin1 1264drop table t1; 1265create table t1 (a int); 1266insert into t1 values (1), (2), (3); 1267explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1) 1268from t1; 1269id select_type table type possible_keys key key_len ref rows filtered Extra 12701 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 12712 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 12723 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00 1273Warnings: 1274Note 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` 1275drop table t1; 1276select 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); 1277ERROR 42S02: Table 'test.t1' doesn't exist 1278CREATE TABLE t1 ( 1279ID int(11) NOT NULL auto_increment, 1280name char(35) NOT NULL default '', 1281t2 char(3) NOT NULL default '', 1282District char(20) NOT NULL default '', 1283Population int(11) NOT NULL default '0', 1284PRIMARY KEY (ID) 1285) ENGINE=MyISAM; 1286INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207); 1287INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329); 1288INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117); 1289CREATE TABLE t2 ( 1290Code char(3) NOT NULL default '', 1291Name char(52) NOT NULL default '', 1292Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', 1293Region char(26) NOT NULL default '', 1294SurfaceArea float(10,2) NOT NULL default '0.00', 1295IndepYear smallint(6) default NULL, 1296Population int(11) NOT NULL default '0', 1297LifeExpectancy float(3,1) default NULL, 1298GNP float(10,2) default NULL, 1299GNPOld float(10,2) default NULL, 1300LocalName char(45) NOT NULL default '', 1301GovernmentForm char(45) NOT NULL default '', 1302HeadOfState char(60) default NULL, 1303Capital int(11) default NULL, 1304Code2 char(2) NOT NULL default '', 1305PRIMARY KEY (Code) 1306) ENGINE=MyISAM; 1307INSERT 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'); 1308INSERT 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'); 1309select 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); 1310Continent Name Population 1311Oceania Sydney 3276207 1312drop table t1, t2; 1313CREATE TABLE `t1` ( 1314`id` mediumint(8) unsigned NOT NULL auto_increment, 1315`pseudo` varchar(35) character set latin1 NOT NULL default '', 1316PRIMARY KEY (`id`), 1317UNIQUE KEY `pseudo` (`pseudo`) 1318) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC; 1319INSERT INTO t1 (pseudo) VALUES ('test'); 1320SELECT 0 IN (SELECT 1 FROM t1 a); 13210 IN (SELECT 1 FROM t1 a) 13220 1323EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); 1324id select_type table type possible_keys key key_len ref rows filtered Extra 13251 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 13262 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1327Warnings: 1328Note 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)` 1329INSERT INTO t1 (pseudo) VALUES ('test1'); 1330SELECT 0 IN (SELECT 1 FROM t1 a); 13310 IN (SELECT 1 FROM t1 a) 13320 1333EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a); 1334id select_type table type possible_keys key key_len ref rows filtered Extra 13351 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 13362 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 1337Warnings: 1338Note 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)` 1339drop table t1; 1340CREATE TABLE `t1` ( 1341`i` int(11) NOT NULL default '0', 1342PRIMARY KEY (`i`) 1343) ENGINE=MyISAM CHARSET=latin1; 1344INSERT INTO t1 VALUES (1); 1345UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); 1346UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); 1347UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); 1348ERROR 42S22: Unknown column 't.i' in 'field list' 1349select * from t1; 1350i 13513 1352drop table t1; 1353CREATE TABLE t1 ( 1354id int(11) default NULL 1355) ENGINE=MyISAM CHARSET=latin1; 1356INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3); 1357CREATE TABLE t2 ( 1358id int(11) default NULL, 1359name varchar(15) default NULL 1360) ENGINE=MyISAM CHARSET=latin1; 1361INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita'); 1362update t1, t2 set t2.name='lenka' where t2.id in (select id from t1); 1363select * from t2; 1364id name 13654 vita 13661 lenka 13672 lenka 13681 lenka 1369drop table t1,t2; 1370create table t1 (a int, unique index indexa (a)); 1371insert into t1 values (-1), (-4), (-2), (NULL); 1372select -10 IN (select a from t1 FORCE INDEX (indexa)); 1373-10 IN (select a from t1 FORCE INDEX (indexa)) 1374NULL 1375drop table t1; 1376create table t1 (id int not null auto_increment primary key, salary int, key(salary)); 1377insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000); 1378explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1); 1379id select_type table type possible_keys key key_len ref rows filtered Extra 13801 PRIMARY t1 ref salary salary 5 const 1 100.00 Using where 13812 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 1382Warnings: 1383Note 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`) 1384drop table t1; 1385CREATE TABLE t1 ( 1386ID int(10) unsigned NOT NULL auto_increment, 1387SUB_ID int(3) unsigned NOT NULL default '0', 1388REF_ID int(10) unsigned default NULL, 1389REF_SUB int(3) unsigned default '0', 1390PRIMARY KEY (ID,SUB_ID), 1391UNIQUE KEY t1_PK (ID,SUB_ID), 1392KEY t1_FK (REF_ID,REF_SUB), 1393KEY t1_REFID (REF_ID) 1394) ENGINE=MyISAM CHARSET=cp1251; 1395INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL); 1396SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2); 1397REF_ID 1398DROP TABLE t1; 1399create table t1 (a int, b int); 1400create table t2 (a int, b int); 1401insert into t1 values (1,0), (2,0), (3,0); 1402insert into t2 values (1,1), (2,1), (3,1), (2,2); 1403update ignore t1 set b=(select b from t2 where t1.a=t2.a); 1404Warnings: 1405Warning 1242 Subquery returns more than 1 row 1406select * from t1; 1407a b 14081 1 14092 NULL 14103 1 1411drop table t1, t2; 1412CREATE TABLE `t1` ( 1413`id` mediumint(8) unsigned NOT NULL auto_increment, 1414`pseudo` varchar(35) NOT NULL default '', 1415`email` varchar(60) NOT NULL default '', 1416PRIMARY KEY (`id`), 1417UNIQUE KEY `email` (`email`), 1418UNIQUE KEY `pseudo` (`pseudo`) 1419) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC; 1420INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1'); 1421SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1); 1422a b 1423test test 1424test1 test1 1425drop table if exists t1; 1426(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0); 1427a 14281 1429create table t1 (a int not null, b int, primary key (a)); 1430create table t2 (a int not null, primary key (a)); 1431create table t3 (a int not null, b int, primary key (a)); 1432insert into t1 values (1,10), (2,20), (3,30), (4,40); 1433insert into t2 values (2), (3), (4), (5); 1434insert into t3 values (10,3), (20,4), (30,5); 1435select * from t2 where t2.a in (select a from t1); 1436a 14372 14383 14394 1440explain extended select * from t2 where t2.a in (select a from t1); 1441id select_type table type possible_keys key key_len ref rows filtered Extra 14421 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14431 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 Using index 1444Warnings: 1445Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a` 1446select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1447a 14482 14494 1450explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1451id select_type table type possible_keys key key_len ref rows filtered Extra 14521 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14531 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 Using where 1454Warnings: 1455Note 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 1456select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1457a 14582 14593 1460explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1461id select_type table type possible_keys key key_len ref rows filtered Extra 14621 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index 14631 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t2.a 1 100.00 14641 PRIMARY t3 index PRIMARY PRIMARY 4 NULL 3 100.00 Using where; Using index; Using join buffer (flat, BNL join) 1465Warnings: 1466Note 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` 1467drop table t1, t2, t3; 1468create table t1 (a int, b int, index a (a,b)); 1469create table t2 (a int, index a (a)); 1470create table t3 (a int, b int, index a (a)); 1471insert into t1 values (1,10), (2,20), (3,30), (4,40); 1472create table t0(a int); 1473insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 1474insert into t1 1475select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D; 1476insert into t2 values (2), (3), (4), (5); 1477insert into t3 values (10,3), (20,4), (30,5); 1478select * from t2 where t2.a in (select a from t1); 1479a 14802 14813 14824 1483explain extended select * from t2 where t2.a in (select a from t1); 1484id select_type table type possible_keys key key_len ref rows filtered Extra 14851 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 14861 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using index; FirstMatch(t2) 1487Warnings: 1488Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where `test`.`t1`.`a` = `test`.`t2`.`a` 1489select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1490a 14912 14924 1493explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1494id select_type table type possible_keys key key_len ref rows filtered Extra 14951 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 14961 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) 1497Warnings: 1498Note 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 1499select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1500a 15012 15023 1503explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a); 1504id select_type table type possible_keys key key_len ref rows filtered Extra 15051 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 15061 PRIMARY t3 range a a 5 NULL 3 100.00 Using where; Using index 15071 PRIMARY t1 ref a a 10 test.t2.a,test.t3.a 116 100.00 Using index; FirstMatch(t2) 1508Warnings: 1509Note 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` 1510insert into t1 values (3,31); 1511select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1512a 15132 15143 15154 1516select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31); 1517a 15182 15194 1520explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30); 1521id select_type table type possible_keys key key_len ref rows filtered Extra 15221 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index 15231 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2) 1524Warnings: 1525Note 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 1526drop table t0, t1, t2, t3; 1527create table t1 (a int, b int); 1528create table t2 (a int, b int); 1529create table t3 (a int, b int); 1530insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10); 1531insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1); 1532insert into t3 values (3,3), (2,2), (1,1); 1533select 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; 1534a (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) 15353 1 15362 2 15371 2 1538drop table t1,t2,t3; 1539create table t1 (s1 int); 1540create table t2 (s1 int); 1541insert into t1 values (1); 1542insert into t2 values (1); 1543select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1); 1544s1 15451 1546drop table t1,t2; 1547create table t1 (s1 int); 1548create table t2 (s1 int); 1549insert into t1 values (1); 1550insert into t2 values (1); 1551update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); 1552ERROR 42S22: Unknown column 'x.s1' in 'field list' 1553DROP TABLE t1, t2; 1554CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci, 1555s2 CHAR(5) COLLATE latin1_swedish_ci); 1556INSERT INTO t1 VALUES ('z','?'); 1557select * from t1 where s1 > (select max(s2) from t1); 1558ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>' 1559select * from t1 where s1 > any (select max(s2) from t1); 1560ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<' 1561drop table t1; 1562create table t1(toid int,rd int); 1563create table t2(userid int,pmnew int,pmtotal int); 1564insert into t2 values(1,0,0),(2,0,0); 1565insert 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); 1566select 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); 1567userid pmtotal pmnew calc_total calc_new 15681 0 0 9 3 15692 0 0 4 2 1570drop table t1, t2; 1571create table t1 (s1 char(5)); 1572select (select 'a','b' from t1 union select 'a','b' from t1) from t1; 1573ERROR 21000: Operand should contain 1 column(s) 1574insert into t1 values ('tttt'); 1575select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1); 1576s1 1577tttt 1578explain extended (select * from t1); 1579id select_type table type possible_keys key key_len ref rows filtered Extra 15801 SIMPLE t1 system NULL NULL NULL NULL 1 100.00 1581Warnings: 1582Note 1003 (select 'tttt' AS `s1` from dual) 1583(select * from t1); 1584s1 1585tttt 1586drop table t1; 1587create table t1 (s1 char(5), index s1(s1)); 1588create table t2 (s1 char(5), index s1(s1)); 1589insert into t1 values ('a1'),('a2'),('a3'); 1590insert into t2 values ('a1'),('a2'); 1591select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; 1592s1 s1 NOT IN (SELECT s1 FROM t2) 1593a1 0 1594a2 0 1595a3 1 1596select s1, s1 = ANY (SELECT s1 FROM t2) from t1; 1597s1 s1 = ANY (SELECT s1 FROM t2) 1598a1 1 1599a2 1 1600a3 0 1601select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; 1602s1 s1 <> ALL (SELECT s1 FROM t2) 1603a1 0 1604a2 0 1605a3 1 1606select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; 1607s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') 1608a1 0 1609a2 1 1610a3 1 1611explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; 1612id select_type table type possible_keys key key_len ref rows filtered Extra 16131 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16142 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1615Warnings: 1616Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1` 1617explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1; 1618id select_type table type possible_keys key key_len ref rows filtered Extra 16191 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16202 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1621Warnings: 1622Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1` 1623explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1; 1624id select_type table type possible_keys key key_len ref rows filtered Extra 16251 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index 16262 SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key 1627Warnings: 1628Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(`test`.`t2`.`s1` is null))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1` 1629explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') 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 50.00 Using index; Using where; Full scan on NULL key 1633Warnings: 1634Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL 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` 1635drop table t1,t2; 1636create table t2 (a int, b int not null); 1637create table t3 (a int); 1638insert into t3 values (6),(7),(3); 1639select * from t3 where a >= all (select b from t2); 1640a 16416 16427 16433 1644explain extended select * from t3 where a >= all (select b from t2); 1645id select_type table type possible_keys key key_len ref rows filtered Extra 16461 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16472 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1648Warnings: 1649Note 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`))) 1650select * from t3 where a >= some (select b from t2); 1651a 1652explain extended select * from t3 where a >= some (select b from t2); 1653id select_type table type possible_keys key key_len ref rows filtered Extra 16541 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16552 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1656Warnings: 1657Note 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`))) 1658select * from t3 where a >= all (select b from t2 group by 1); 1659a 16606 16617 16623 1663explain extended select * from t3 where a >= all (select b from t2 group by 1); 1664id select_type table type possible_keys key key_len ref rows filtered Extra 16651 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16662 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1667Warnings: 1668Note 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`))) 1669select * from t3 where a >= some (select b from t2 group by 1); 1670a 1671explain extended select * from t3 where a >= some (select b from t2 group by 1); 1672id select_type table type possible_keys key key_len ref rows filtered Extra 16731 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 16742 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found 1675Warnings: 1676Note 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`))) 1677select * from t3 where NULL >= any (select b from t2); 1678a 1679explain extended select * from t3 where NULL >= any (select b from t2); 1680id select_type table type possible_keys key key_len ref rows filtered Extra 16811 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 16822 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1683Warnings: 1684Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1685select * from t3 where NULL >= any (select b from t2 group by 1); 1686a 1687explain extended select * from t3 where NULL >= any (select b from t2 group by 1); 1688id select_type table type possible_keys key key_len ref rows filtered Extra 16891 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 16902 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1691Warnings: 1692Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1693select * from t3 where NULL >= some (select b from t2); 1694a 1695explain extended select * from t3 where NULL >= some (select b from t2); 1696id select_type table type possible_keys key key_len ref rows filtered Extra 16971 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 16982 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1699Warnings: 1700Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1701select * from t3 where NULL >= some (select b from t2 group by 1); 1702a 1703explain extended select * from t3 where NULL >= some (select b from t2 group by 1); 1704id select_type table type possible_keys key key_len ref rows filtered Extra 17051 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 17062 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table 1707Warnings: 1708Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0 1709insert into t2 values (2,2), (2,1), (3,3), (3,1); 1710select * from t3 where a > all (select max(b) from t2 group by a); 1711a 17126 17137 1714explain extended select * from t3 where a > all (select max(b) from t2 group by a); 1715id select_type table type possible_keys key key_len ref rows filtered Extra 17161 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where 17172 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary 1718Warnings: 1719Note 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`))) 1720drop table t2, t3; 1721CREATE 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 ; 1722INSERT 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()); 1723CREATE 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; 1724INSERT 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); 1725CREATE 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 ; 1726INSERT 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); 1727CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; 1728INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status'); 1729select 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; 1730dbid 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') 1731-1 Valid 1 1732-1 Valid 2 1 1733-1 Should Not Return 0 1734SELECT 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; 1735dbid name 1736-1 Valid 1737-1 Valid 2 1738drop table t1,t2,t3,t4; 1739CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; 1740INSERT INTO t1 VALUES (1),(5); 1741CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1; 1742INSERT INTO t2 VALUES (2),(6); 1743select * from t1 where (1,2,6) in (select * from t2); 1744ERROR 21000: Operand should contain 3 column(s) 1745DROP TABLE t1,t2; 1746create table t1 (s1 int); 1747insert into t1 values (1); 1748insert into t1 values (2); 1749set sort_buffer_size = (select s1 from t1); 1750ERROR 21000: Subquery returns more than 1 row 1751do (select * from t1); 1752Warnings: 1753Error 1242 Subquery returns more than 1 row 1754drop table t1; 1755create table t1 (s1 char); 1756insert into t1 values ('e'); 1757select * from t1 where 'f' > any (select s1 from t1); 1758s1 1759e 1760select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); 1761s1 1762e 1763explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1); 1764id select_type table type possible_keys key key_len ref rows filtered Extra 17651 PRIMARY t1 system NULL NULL NULL NULL 1 100.00 17662 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00 17673 UNION t1 system NULL NULL NULL NULL 1 100.00 1768NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 1769Warnings: 1770Note 1003 /* select#1 */ select 'e' AS `s1` from dual where 1 1771drop table t1; 1772CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1; 1773INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874'); 1774CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1; 1775INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6'); 1776select 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; 1777phone code 177869294728265 6 177918621828126 1862 178089356874041 NULL 178195895001874 NULL 1782drop table t1, t2; 1783create table t1 (s1 int); 1784create table t2 (s1 int); 1785select * from t1 where (select count(*) from t2 where t1.s2) = 1; 1786ERROR 42S22: Unknown column 't1.s2' in 'where clause' 1787select * from t1 where (select count(*) from t2 group by t1.s2) = 1; 1788ERROR 42S22: Unknown column 't1.s2' in 'group statement' 1789select count(*) from t2 group by t1.s2; 1790ERROR 42S22: Unknown column 't1.s2' in 'group statement' 1791drop table t1, t2; 1792CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB)); 1793CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA)); 1794INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365'); 1795INSERT INTO t2 VALUES (100, 200, 'C'); 1796SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1); 1797COLC 1798DROP TABLE t1, t2; 1799CREATE TABLE t1 (a int(1)); 1800INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5); 1801SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100; 1802(SELECT a) 18031 18042 18053 18064 18075 1808DROP TABLE t1; 1809create table t1 (a int, b decimal(13, 3)); 1810insert into t1 values (1, 0.123); 1811select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1; 1812delete from t1; 1813load data infile "../../tmp/subselect.out.file.1" into table t1; 1814select * from t1; 1815a b 18161 0.123 1817drop table t1; 1818CREATE TABLE `t1` ( 1819`id` int(11) NOT NULL auto_increment, 1820`id_cns` tinyint(3) unsigned NOT NULL default '0', 1821`tipo` enum('','UNO','DUE') NOT NULL default '', 1822`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000', 1823`particolare` mediumint(8) unsigned NOT NULL default '0', 1824`generale` mediumint(8) unsigned NOT NULL default '0', 1825`bis` tinyint(3) unsigned NOT NULL default '0', 1826PRIMARY KEY (`id`), 1827UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`), 1828UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`) 1829); 1830INSERT 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); 1831CREATE TABLE `t2` ( 1832`id` tinyint(3) unsigned NOT NULL auto_increment, 1833`max_anno_dep` smallint(6) unsigned NOT NULL default '0', 1834PRIMARY KEY (`id`) 1835); 1836INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990); 1837SELECT 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; 1838id max_anno_dep PIPPO 183916 1987 1 184050 1990 0 184151 1990 NULL 1842DROP TABLE t1, t2; 1843create table t1 (a int); 1844insert into t1 values (1), (2), (3); 1845SET SQL_SELECT_LIMIT=1; 1846select sum(a) from (select * from t1) as a; 1847sum(a) 18486 1849select 2 in (select * from t1); 18502 in (select * from t1) 18511 1852SET SQL_SELECT_LIMIT=default; 1853drop table t1; 1854CREATE TABLE t1 (a int, b int, INDEX (a)); 1855INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3); 1856SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b; 1857a b 18581 1 18591 2 18601 3 1861DROP TABLE t1; 1862create table t1(val varchar(10)); 1863insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp'); 1864select 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%'); 1865count(*) 18660 1867drop table t1; 1868create table t1 (id int not null, text varchar(20) not null default '', primary key (id)); 1869insert 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'); 1870select * from t1 where id not in (select id from t1 where id < 8); 1871id text 18728 text8 18739 text9 187410 text10 187511 text11 187612 text12 1877select * 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); 1878id text 18798 text8 18809 text9 188110 text10 188211 text11 188312 text12 1884explain extended select * from t1 where id not in (select id from t1 where id < 8); 1885id select_type table type possible_keys key key_len ref rows filtered Extra 18861 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where 18872 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where 1888Warnings: 1889Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where !<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where `test`.`t1`.`id` < 8 and <cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`)))) 1890explain 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); 1891id select_type table type possible_keys key key_len ref rows filtered Extra 18921 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where 18932 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index 1894Warnings: 1895Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 1896Note 1003 /* select#1 */ select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where !<in_optimizer>(1,<expr_cache><`test`.`tt`.`id`>(exists(/* select#2 */ select `test`.`t1`.`id` from `test`.`t1` where `test`.`t1`.`id` < 8 and `test`.`t1`.`id` = `test`.`tt`.`id` having `test`.`t1`.`id` is not null limit 1))) 1897insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); 1898create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); 1899insert 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'); 1900select * 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); 1901id text id text id text 19021 text1 1 text1 1 text1 19032 text2 2 text2 2 text2 19043 text3 3 text3 3 text3 19054 text4 4 text4 4 text4 19065 text5 5 text5 5 text5 19076 text6 6 text6 6 text6 19087 text7 7 text7 7 text7 19098 text8 8 text8 8 text8 19109 text9 9 text9 9 text9 191110 text10 10 text10 10 text10 191211 text11 11 text1 11 text11 191312 text12 12 text2 12 text12 19141000 text1000 NULL NULL 1000 text1000 19151001 text1001 NULL NULL 1000 text1000 1916explain 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); 1917id select_type table type possible_keys key key_len ref rows filtered Extra 19181 SIMPLE a ALL NULL NULL NULL NULL 14 100.00 19191 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00 19201 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition 1921Warnings: 1922Note 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` 1923drop table t1,t2; 1924create table t1 (a int); 1925insert into t1 values (1); 1926explain select benchmark(1000, (select a from t1 where a=sha(rand()))); 1927id select_type table type possible_keys key key_len ref rows Extra 19281 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used 19292 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1 1930drop table t1; 1931create table t1(id int); 1932create table t2(id int); 1933create table t3(flag int); 1934select (select * from t3 where id not null) from t1, t2; 1935ERROR 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 1936drop table t1,t2,t3; 1937CREATE TABLE t1 (id INT); 1938CREATE TABLE t2 (id INT); 1939INSERT INTO t1 VALUES (1), (2); 1940INSERT INTO t2 VALUES (1); 1941SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); 1942id c 19431 1 19442 0 1945SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id); 1946id c 19471 1 19482 0 1949SELECT 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; 1950id c 19511 1 19522 0 1953SELECT 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; 1954id c 19551 1 19562 0 1957DROP TABLE t1,t2; 1958CREATE TABLE t1 ( a int, b int ); 1959INSERT INTO t1 VALUES (1,1),(2,2),(3,3); 1960SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); 1961a 19623 1963SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); 1964a 19651 1966SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); 1967a 19682 1969SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); 1970a 19712 19723 1973SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); 1974a 19751 19762 1977SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); 1978a 19791 19803 1981SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); 1982a 19833 1984SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); 1985a 19861 1987SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); 1988a 19892 1990SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); 1991a 19922 19933 1994SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); 1995a 19961 19972 1998SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); 1999a 20001 20013 2002ALTER TABLE t1 ADD INDEX (a); 2003SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 ); 2004a 20053 2006SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 ); 2007a 20081 2009SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 ); 2010a 20112 2012SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 ); 2013a 20142 20153 2016SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 ); 2017a 20181 20192 2020SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 ); 2021a 20221 20233 2024SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 ); 2025a 20263 2027SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 ); 2028a 20291 2030SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 ); 2031a 20322 2033SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); 2034a 20352 20363 2037SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); 2038a 20391 20402 2041SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); 2042a 20431 20443 2045SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2); 2046a 20473 2048SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2); 2049a 20501 2051SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2); 2052a 20532 2054SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2); 2055a 20562 20573 2058SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2); 2059a 20601 20612 2062SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2); 2063a 20641 20653 2066SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2); 2067a 20683 2069SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2); 2070a 20711 2072SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2); 2073a 20742 2075SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2); 2076a 20772 20783 2079SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2); 2080a 20811 20822 2083SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2); 2084a 20851 20863 2087SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2088a 20893 2090SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2091a 20921 2093SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2094a 20952 2096SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2097a 20982 20993 2100SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2101a 21021 21032 2104SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2105a 21061 21073 2108SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2109a 21103 2111SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2112a 21131 2114SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2115a 21162 2117SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2118a 21192 21203 2121SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2122a 21231 21242 2125SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2); 2126a 21271 21283 2129SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2130a 21313 2132SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2133a 21341 2135SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2136a 21372 2138SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2139a 21402 21413 2142SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2143a 21441 21452 2146SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2147a 21481 21493 2150SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2151a 21523 2153SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2154a 21551 2156SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2157a 21582 2159SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2160a 21612 21623 2163SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2164a 21651 21662 2167SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2); 2168a 21691 21703 2171SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2); 2172ERROR 21000: Operand should contain 1 column(s) 2173SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2); 2174ERROR 21000: Operand should contain 1 column(s) 2175SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2); 2176ERROR 21000: Operand should contain 1 column(s) 2177SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2); 2178ERROR 21000: Operand should contain 1 column(s) 2179SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2); 2180ERROR 21000: Operand should contain 1 column(s) 2181SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2); 2182ERROR 21000: Operand should contain 1 column(s) 2183SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2); 2184ERROR 21000: Operand should contain 1 column(s) 2185SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2); 2186ERROR 21000: Operand should contain 1 column(s) 2187SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2); 2188ERROR 21000: Operand should contain 2 column(s) 2189SELECT a FROM t1 WHERE a = ANY (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); 2192a 2193SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2); 2194ERROR 21000: Operand should contain 2 column(s) 2195SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2); 2196ERROR 21000: Operand should contain 1 column(s) 2197SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2); 2198a 21991 22002 22013 2202SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2); 2203a 22042 2205SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2); 2206a 22071 22083 2209SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2); 2210a 22112 2212SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2); 2213a 22141 22153 2216SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); 2217a 22182 2219SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2); 2220a 22211 22223 2223SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); 2224a 22252 2226SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2); 2227a 22281 22293 2230SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2231a 22323 2233SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2234a 22351 2236SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2237a 22382 2239SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2240a 22412 22423 2243SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2244a 22451 22462 2247SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a); 2248a 22491 22503 2251SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2252a 22533 2254SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2255a 22561 2257SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2258a 22592 2260SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2261a 22622 22633 2264SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2265a 22661 22672 2268SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a); 2269a 22701 22713 2272SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2); 2273a 22743 2275SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2); 2276a 22771 2278SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2); 2279a 22802 2281SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2); 2282a 22832 22843 2285SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2); 2286a 22871 22882 2289SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2); 2290a 22911 22923 2293SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2); 2294a 22953 2296SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2); 2297a 22981 2299SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2); 2300a 23012 2302SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2); 2303a 23042 23053 2306SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2); 2307a 23081 23092 2310SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2); 2311a 23121 23133 2314SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a; 2315concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') 23160- 23170- 23181- 2319SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a; 2320concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') 23211- 23220- 23230- 2324SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a; 2325concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') 23260- 23271- 23280- 2329DROP TABLE t1; 2330CREATE TABLE t1 ( a double, b double ); 2331INSERT INTO t1 VALUES (1,1),(2,2),(3,3); 2332SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0); 2333a 23343 2335SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0); 2336a 23371 2338SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0); 2339a 23402 2341SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0); 2342a 23432 23443 2345SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0); 2346a 23471 23482 2349SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0); 2350a 23511 23523 2353SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0); 2354a 23553 2356SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0); 2357a 23581 2359SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0); 2360a 23612 2362SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0); 2363a 23642 23653 2366SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0); 2367a 23681 23692 2370SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0); 2371a 23721 23733 2374DROP TABLE t1; 2375CREATE TABLE t1 ( a char(1), b char(1)); 2376INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3'); 2377SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2'); 2378a 23793 2380SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2'); 2381a 23821 2383SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2'); 2384a 23852 2386SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2'); 2387a 23882 23893 2390SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2'); 2391a 23921 23932 2394SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2'); 2395a 23961 23973 2398SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2'); 2399a 24003 2401SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2'); 2402a 24031 2404SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2'); 2405a 24062 2407SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2'); 2408a 24092 24103 2411SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2'); 2412a 24131 24142 2415SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2'); 2416a 24171 24183 2419DROP TABLE t1; 2420create table t1 (a int, b int); 2421insert into t1 values (1,2),(3,4); 2422select * from t1 up where exists (select * from t1 where t1.a=up.a); 2423a b 24241 2 24253 4 2426explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a); 2427id select_type table type possible_keys key key_len ref rows filtered Extra 24281 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 24291 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 24302 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 2431Warnings: 2432Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 2433Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` semi join (`test`.`t1`) where 1 2434drop table t1; 2435CREATE TABLE t1 (t1_a int); 2436INSERT INTO t1 VALUES (1); 2437CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b)); 2438INSERT INTO t2 VALUES (1, 1), (1, 2); 2439SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1 2440HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a); 2441t1_a t2_a t2_b 24421 1 2 2443DROP TABLE t1, t2; 2444CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL); 2445INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL); 2446CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL); 2447INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix'); 2448SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id; 2449id name id pet 24501 Tim 1 Fido 24512 Rebecca 2 Spot 24523 NULL 3 Felix 2453drop table t1,t2; 2454CREATE TABLE t1 ( a int, b int ); 2455CREATE TABLE t2 ( c int, d int ); 2456INSERT INTO t1 VALUES (1,2), (2,3), (3,4); 2457SELECT a AS abc, b FROM t1 outr WHERE b = 2458(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2459abc b 24601 2 24612 3 24623 4 2463INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = 2464(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2465select * from t2; 2466c d 24671 2 24682 3 24693 4 2470CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = 2471(SELECT MIN(b) FROM t1 WHERE a=outr.a); 2472select * from t3; 2473abc b 24741 2 24752 3 24763 4 2477prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2478execute stmt1; 2479deallocate prepare stmt1; 2480select * from t2; 2481c d 24821 2 24832 3 24843 4 24851 2 24862 3 24873 4 2488drop table t3; 2489prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);"; 2490execute stmt1; 2491select * from t3; 2492abc b 24931 2 24942 3 24953 4 2496deallocate prepare stmt1; 2497DROP TABLE t1, t2, t3; 2498CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2499insert into t1 values (1); 2500CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1; 2501insert into t2 values (1,2); 2502select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1); 2503a C 25041 1 2505drop table t1,t2; 2506create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b)); 2507insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan'); 2508create table t2 (a int); 2509insert into t2 values (1),(3),(2),(7); 2510select a,b from t1 where match(b) against ('Ball') > 0; 2511a b 25121 ball 25132 ball games 2514select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0); 2515a 25161 25172 2518drop table t1,t2; 2519CREATE 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); 2520CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel); 2521INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001'); 2522INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001'); 2523INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001'); 2524INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001'); 2525SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001'); 2526IZAVORGANG_ID 2527D0000000001 2528drop table t1; 2529CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2530CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`)); 2531insert into t1 values (1,1),(1,2),(2,1),(2,2); 2532insert into t2 values (1,2),(2,2); 2533select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2534aid bid 25351 1 25362 1 2537alter table t2 drop primary key; 2538alter table t2 add key KEY1 (aid, bid); 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 key KEY1; 2544alter table t2 add primary key (bid, aid); 2545select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 2546aid bid 25471 1 25482 1 2549drop table t1,t2; 2550CREATE TABLE t1 (howmanyvalues bigint, avalue int); 2551INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4); 2552SELECT howmanyvalues, count(*) from t1 group by howmanyvalues; 2553howmanyvalues count(*) 25541 1 25552 2 25563 3 25574 4 2558SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues; 2559howmanyvalues mycount 25601 1 25612 2 25623 3 25634 4 2564CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues); 2565SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues; 2566howmanyvalues mycount 25671 1 25682 2 25693 3 25704 4 2571SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) 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.avalue) as mycount from t1 a group by a.howmanyvalues; 2578howmanyvalues mycount 25791 1 25802 1 25813 1 25824 1 2583drop table t1; 2584create table t1 (x int); 2585select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x; 2586(select b.x from t1 as b where b.x=a.x) 2587drop table t1; 2588CREATE 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`)); 2589INSERT 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); 2590CREATE 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`)) ; 2591INSERT 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'); 2592SELECT 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; 2593ERROR 42S22: Unknown column 'b.sc' in 'field list' 2594SELECT 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; 2595ac 2596700 2597NULL 2598drop tables t1,t2; 2599create table t1 (a int not null, b int not null, c int, primary key (a,b)); 2600insert into t1 values (1,1,1), (2,2,2), (3,3,3); 2601set @b:= 0; 2602explain select sum(a) from t1 where b > @b; 2603id select_type table type possible_keys key key_len ref rows Extra 26041 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index 2605set @a:= (select sum(a) from t1 where b > @b); 2606explain select a from t1 where c=2; 2607id select_type table type possible_keys key key_len ref rows Extra 26081 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2609do @a:= (select sum(a) from t1 where b > @b); 2610explain select a from t1 where c=2; 2611id select_type table type possible_keys key key_len ref rows Extra 26121 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where 2613drop table t1; 2614connect root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK; 2615connection root; 2616set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ; 2617disconnect root; 2618connection default; 2619create table t1 (a int, b int); 2620create table t2 (a int, b int); 2621insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5); 2622insert into t2 values (1,3),(2,1); 2623select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b; 2624a b (select max(b) from t2 where t1.b=t2.a) 26251 1 3 26261 2 1 26271 3 NULL 26282 4 NULL 26292 5 NULL 2630drop table t1, t2; 2631create table t1 (id int); 2632create table t2 (id int, body text, fulltext (body)); 2633insert into t1 values(1),(2),(3); 2634insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test'); 2635select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode)); 2636count(distinct id) 26371 2638drop table t2,t1; 2639create table t1 (s1 int,s2 int); 2640insert into t1 values (20,15); 2641select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0)); 2642s1 s2 2643drop table t1; 2644create table t1 (s1 int); 2645insert into t1 values (1),(null); 2646select * from t1 where s1 < all (select s1 from t1); 2647s1 2648select s1, s1 < all (select s1 from t1) from t1; 2649s1 s1 < all (select s1 from t1) 26501 0 2651NULL NULL 2652drop table t1; 2653CREATE TABLE t1 ( 2654Code char(3) NOT NULL default '', 2655Name char(52) NOT NULL default '', 2656Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia', 2657Region char(26) NOT NULL default '', 2658SurfaceArea float(10,2) NOT NULL default '0.00', 2659IndepYear smallint(6) default NULL, 2660Population int(11) NOT NULL default '0', 2661LifeExpectancy float(3,1) default NULL, 2662GNP float(10,2) default NULL, 2663GNPOld float(10,2) default NULL, 2664LocalName char(45) NOT NULL default '', 2665GovernmentForm char(45) NOT NULL default '', 2666HeadOfState char(60) default NULL, 2667Capital int(11) default NULL, 2668Code2 char(2) NOT NULL default '' 2669) ENGINE=MyISAM; 2670INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX'); 2671INSERT 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'); 2672INSERT 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'); 2673INSERT 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'); 2674/*!40000 ALTER TABLE t1 ENABLE KEYS */; 2675SELECT DISTINCT Continent AS c FROM t1 outr WHERE 2676Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND 2677Population < 200); 2678c 2679Oceania 2680drop table t1; 2681create table t1 (a1 int); 2682create table t2 (b1 int); 2683select * from t1 where a2 > any(select b1 from t2); 2684ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery' 2685select * from t1 where a1 > any(select b1 from t2); 2686a1 2687drop table t1,t2; 2688create table t1 (a integer, b integer); 2689select (select * from t1) = (select 1,2); 2690(select * from t1) = (select 1,2) 2691NULL 2692select (select 1,2) = (select * from t1); 2693(select 1,2) = (select * from t1) 2694NULL 2695select row(1,2) = ANY (select * from t1); 2696row(1,2) = ANY (select * from t1) 26970 2698select row(1,2) != ALL (select * from t1); 2699row(1,2) != ALL (select * from t1) 27001 2701drop table t1; 2702create table t1 (a integer, b integer); 2703select row(1,(2,2)) in (select * from t1 ); 2704ERROR 21000: Operand should contain 2 column(s) 2705select row(1,(2,2)) = (select * from t1 ); 2706ERROR 21000: Operand should contain 2 column(s) 2707select (select * from t1) = row(1,(2,2)); 2708ERROR 21000: Operand should contain 1 column(s) 2709drop table t1; 2710create table t1 (a integer); 2711insert into t1 values (1); 2712select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ; 2713ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2714select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2715ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2716select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx ); 2717xx 1 = ALL ( select 1 from t1 where 1 = xx ) 27181 1 2719select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx; 2720ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2721select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL; 2722ERROR 42S22: Reference 'xx' not supported (forward reference in item list) 2723drop table t1; 2724CREATE TABLE t1 ( 2725categoryId int(11) NOT NULL, 2726courseId int(11) NOT NULL, 2727startDate datetime NOT NULL, 2728endDate datetime NOT NULL, 2729createDate datetime NOT NULL, 2730modifyDate timestamp NOT NULL, 2731attributes text NOT NULL 2732); 2733INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2734(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2735(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2736(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''), 2737(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''), 2738(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2739(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''), 2740(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''), 2741(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18',''); 2742CREATE TABLE t2 ( 2743userId int(11) NOT NULL, 2744courseId int(11) NOT NULL, 2745date datetime NOT NULL 2746); 2747INSERT INTO t2 VALUES (5141,71,'2003-11-18'), 2748(5141,72,'2003-11-25'),(5141,41,'2004-08-06'), 2749(5141,52,'2004-08-06'),(5141,53,'2004-08-06'), 2750(5141,12,'2004-08-06'),(5141,86,'2004-10-21'), 2751(5141,87,'2004-10-21'),(5141,88,'2004-10-21'), 2752(5141,89,'2004-10-22'),(5141,51,'2004-10-26'); 2753CREATE TABLE t3 ( 2754groupId int(11) NOT NULL, 2755parentId int(11) NOT NULL, 2756startDate datetime NOT NULL, 2757endDate datetime NOT NULL, 2758createDate datetime NOT NULL, 2759modifyDate timestamp NOT NULL, 2760ordering int(11) 2761); 2762INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL); 2763CREATE TABLE t4 ( 2764id int(11) NOT NULL, 2765groupTypeId int(11) NOT NULL, 2766groupKey varchar(50) NOT NULL, 2767name text, 2768ordering int(11), 2769description text, 2770createDate datetime NOT NULL, 2771modifyDate timestamp NOT NULL 2772); 2773INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'), 2774(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29'); 2775CREATE TABLE t5 ( 2776userId int(11) NOT NULL, 2777groupId int(11) NOT NULL, 2778createDate datetime NOT NULL, 2779modifyDate timestamp NOT NULL 2780); 2781INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06'); 2782select 2783count(distinct t2.userid) pass, 2784groupstuff.*, 2785count(t2.courseid) crse, 2786t1.categoryid, 2787t2.courseid, 2788date_format(date, '%b%y') as colhead 2789from t2 2790join t1 on t2.courseid=t1.courseid 2791join 2792( 2793select 2794t5.userid, 2795parentid, 2796parentgroup, 2797childid, 2798groupname, 2799grouptypeid 2800from t5 2801join 2802( 2803select t4.id as parentid, 2804t4.name as parentgroup, 2805t4.id as childid, 2806t4.name as groupname, 2807t4.grouptypeid 2808from t4 2809) as gin on t5.groupid=gin.childid 2810) as groupstuff on t2.userid = groupstuff.userid 2811group by 2812groupstuff.groupname, colhead , t2.courseid; 2813pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead 28141 5141 12 group2 12 group2 5 1 5 12 Aug04 28151 5141 12 group2 12 group2 5 1 1 41 Aug04 28161 5141 12 group2 12 group2 5 1 2 52 Aug04 28171 5141 12 group2 12 group2 5 1 2 53 Aug04 28181 5141 12 group2 12 group2 5 1 3 51 Oct04 28191 5141 12 group2 12 group2 5 1 1 86 Oct04 28201 5141 12 group2 12 group2 5 1 1 87 Oct04 28211 5141 12 group2 12 group2 5 1 2 88 Oct04 28221 5141 12 group2 12 group2 5 1 2 89 Oct04 2823drop table t1, t2, t3, t4, t5; 2824create table t1 (a int); 2825insert into t1 values (1), (2), (3); 2826SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1); 28271 28281 28291 28301 2831drop table t1; 2832create table t1 (a int); 2833create table t2 (a int); 2834insert into t1 values (1),(2); 2835insert into t2 values (0),(1),(2),(3); 2836select a from t2 where a in (select a from t1); 2837a 28381 28392 2840select a from t2 having a in (select a from t1); 2841a 28421 28432 2844prepare stmt1 from "select a from t2 where a in (select a from t1)"; 2845execute stmt1; 2846a 28471 28482 2849execute stmt1; 2850a 28511 28522 2853deallocate prepare stmt1; 2854prepare stmt1 from "select a from t2 having a in (select a from t1)"; 2855execute stmt1; 2856a 28571 28582 2859execute stmt1; 2860a 28611 28622 2863deallocate prepare stmt1; 2864drop table t1, t2; 2865create table t1 (a int, b int); 2866insert into t1 values (1,2); 2867select 1 = (select * from t1); 2868ERROR HY000: Illegal parameter data types int and row for operation '=' 2869select (select * from t1) = 1; 2870ERROR HY000: Illegal parameter data types row and int for operation '=' 2871select (1,2) = (select a from t1); 2872ERROR HY000: Illegal parameter data types row and int for operation '=' 2873select (select a from t1) = (1,2); 2874ERROR HY000: Illegal parameter data types int and row for operation '=' 2875select (1,2,3) = (select * from t1); 2876ERROR 21000: Operand should contain 3 column(s) 2877select (select * from t1) = (1,2,3); 2878ERROR 21000: Operand should contain 2 column(s) 2879drop table t1; 2880CREATE TABLE `t1` ( 2881`itemid` bigint(20) unsigned NOT NULL auto_increment, 2882`sessionid` bigint(20) unsigned default NULL, 2883`time` int(10) unsigned NOT NULL default '0', 2884`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 2885NULL default '', 2886`data` text collate latin1_general_ci NOT NULL, 2887PRIMARY KEY (`itemid`) 2888) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2889INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 2890CREATE TABLE `t2` ( 2891`sessionid` bigint(20) unsigned NOT NULL auto_increment, 2892`pid` int(10) unsigned NOT NULL default '0', 2893`date` int(10) unsigned NOT NULL default '0', 2894`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 2895PRIMARY KEY (`sessionid`) 2896) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 2897INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 2898SELECT 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; 2899ip count( e.itemid ) 290010.10.10.1 1 2901drop tables t1,t2; 2902create table t1 (fld enum('0','1')); 2903insert into t1 values ('1'); 2904select * from (select max(fld) from t1) as foo; 2905max(fld) 29061 2907drop table t1; 2908set @tmp11867_optimizer_switch=@@optimizer_switch; 2909set optimizer_switch='semijoin_with_cache=off'; 2910CREATE TABLE t1 (one int, two int, flag char(1)); 2911CREATE TABLE t2 (one int, two int, flag char(1)); 2912INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2913INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N'); 2914SELECT * FROM t1 2915WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N'); 2916one two flag 29175 6 N 29187 8 N 2919SELECT * FROM t1 2920WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N'); 2921one two flag 29225 6 N 29237 8 N 2924insert into t2 values (null,null,'N'); 2925insert into t2 values (null,3,'0'); 2926insert into t2 values (null,5,'0'); 2927insert into t2 values (10,null,'0'); 2928insert into t1 values (10,3,'0'); 2929insert into t1 values (10,5,'0'); 2930insert into t1 values (10,10,'0'); 2931SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1; 2932one two test 29331 2 NULL 29342 3 NULL 29353 4 NULL 29365 6 1 29377 8 1 293810 3 NULL 293910 5 NULL 294010 10 NULL 2941SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 2942one two 29435 6 29447 8 2945SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1; 2946one two test 29471 2 NULL 29482 3 NULL 29493 4 NULL 29505 6 1 29517 8 1 295210 3 NULL 295310 5 NULL 295410 10 NULL 2955SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2956one two test 29571 2 0 29582 3 NULL 29593 4 0 29605 6 0 29617 8 0 296210 3 NULL 296310 5 NULL 296410 10 NULL 2965SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2966one two test 29671 2 0 29682 3 NULL 29693 4 0 29705 6 0 29717 8 0 297210 3 NULL 297310 5 NULL 297410 10 NULL 2975explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1; 2976id select_type table type possible_keys key key_len ref rows filtered Extra 29771 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29782 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2979Warnings: 2980Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null)))) AS `test` from `test`.`t1` 2981explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N'); 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 29841 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00 29852 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2986Warnings: 2987Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where `test`.`t2`.`flag` = 'N' 2988explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1; 2989id select_type table type possible_keys key key_len ref rows filtered Extra 29901 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 29912 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where 2992Warnings: 2993Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' and trigcond(trigcond(<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one` or `test`.`t2`.`one` is null)) and trigcond(trigcond(<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two` or `test`.`t2`.`two` is null)) having trigcond(`test`.`t2`.`one` is null) and trigcond(`test`.`t2`.`two` is null)))) AS `test` from `test`.`t1` 2994DROP TABLE t1,t2; 2995set optimizer_switch=@tmp11867_optimizer_switch; 2996CREATE TABLE t1 (a char(5), b char(5)); 2997INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa'); 2998SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb')); 2999a b 3000aaa aaa 3001DROP TABLE t1; 3002CREATE TABLE t1 (a int); 3003CREATE TABLE t2 (a int, b int); 3004CREATE TABLE t3 (b int NOT NULL); 3005INSERT INTO t1 VALUES (1), (2), (3), (4); 3006INSERT INTO t2 VALUES (1,10), (3,30); 3007SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3008WHERE t3.b IS NOT NULL OR t2.a > 10; 3009a b b 3010SELECT * FROM t1 3011WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b 3012WHERE t3.b IS NOT NULL OR t2.a > 10); 3013a 30141 30152 30163 30174 3018DROP TABLE t1,t2,t3; 3019CREATE TABLE t1 (f1 INT); 3020CREATE TABLE t2 (f2 INT); 3021INSERT INTO t1 VALUES (1); 3022SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2); 3023f1 30241 3025SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0); 3026f1 30271 3028INSERT INTO t2 VALUES (1); 3029INSERT INTO t2 VALUES (2); 3030SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0); 3031f1 30321 3033DROP TABLE t1, t2; 3034select 1 from dual where 1 < any (select 2); 30351 30361 3037select 1 from dual where 1 < all (select 2); 30381 30391 3040select 1 from dual where 2 > any (select 1); 30411 30421 3043select 1 from dual where 2 > all (select 1); 30441 30451 3046select 1 from dual where 1 < any (select 2 from dual); 30471 30481 3049select 1 from dual where 1 < all (select 2 from dual where 1!=1); 30501 30511 3052create table t1 (s1 char); 3053insert into t1 values (1),(2); 3054select * from t1 where (s1 < any (select s1 from t1)); 3055s1 30561 3057select * from t1 where not (s1 < any (select s1 from t1)); 3058s1 30592 3060select * from t1 where (s1 < ALL (select s1+1 from t1)); 3061s1 30621 3063select * from t1 where not(s1 < ALL (select s1+1 from t1)); 3064s1 30652 3066select * from t1 where (s1+1 = ANY (select s1 from t1)); 3067s1 30681 3069select * from t1 where NOT(s1+1 = ANY (select s1 from t1)); 3070s1 30712 3072select * from t1 where (s1 = ALL (select s1/s1 from t1)); 3073s1 30741 3075select * from t1 where NOT(s1 = ALL (select s1/s1 from t1)); 3076s1 30772 3078drop table t1; 3079create table t1 ( 3080retailerID varchar(8) NOT NULL, 3081statusID int(10) unsigned NOT NULL, 3082changed datetime NOT NULL, 3083UNIQUE KEY retailerID (retailerID, statusID, changed) 3084); 3085INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56"); 3086INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53"); 3087INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56"); 3088INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53"); 3089INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50"); 3090INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50"); 3091select * from t1 r1 3092where (r1.retailerID,(r1.changed)) in 3093(SELECT r2.retailerId,(max(changed)) from t1 r2 3094group by r2.retailerId); 3095retailerID statusID changed 30960026 2 2006-01-06 12:25:53 30970037 2 2006-01-06 12:25:53 30980048 1 2006-01-06 12:37:50 30990059 1 2006-01-06 12:37:50 3100drop table t1; 3101create table t1(a int, primary key (a)); 3102insert into t1 values (10); 3103create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b)); 3104insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989'); 3105insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999'); 3106analyze table t1; 3107Table Op Msg_type Msg_text 3108test.t1 analyze status Engine-independent statistics collected 3109test.t1 analyze status OK 3110explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3111ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3112 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3113id select_type table type possible_keys key key_len ref rows Extra 31141 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31151 PRIMARY r const PRIMARY PRIMARY 4 const 1 31162 SUBQUERY t2 range b b 40 NULL 3 Using where 3117SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3118ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3119 ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10; 3120a a b 312110 3 35989 3122explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3123ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3124 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3125id select_type table type possible_keys key key_len ref rows Extra 31261 PRIMARY t1 system PRIMARY NULL NULL NULL 1 31271 PRIMARY r const PRIMARY PRIMARY 4 const 1 31282 SUBQUERY t2 range b b 40 NULL 3 Using index condition 3129SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r 3130ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899' 3131 ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10; 3132a a b 313310 1 359 3134drop table t1,t2; 3135CREATE TABLE t1 ( 3136field1 int NOT NULL, 3137field2 int NOT NULL, 3138field3 int NOT NULL, 3139PRIMARY KEY (field1,field2,field3) 3140); 3141CREATE TABLE t2 ( 3142fieldA int NOT NULL, 3143fieldB int NOT NULL, 3144PRIMARY KEY (fieldA,fieldB) 3145); 3146INSERT INTO t1 VALUES 3147(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1); 3148INSERT INTO t2 VALUES (1,1), (1,2), (1,3); 3149SELECT field1, field2, COUNT(*) 3150FROM t1 GROUP BY field1, field2; 3151field1 field2 COUNT(*) 31521 1 2 31531 2 3 31541 3 1 3155SELECT field1, field2 3156FROM t1 3157GROUP BY field1, field2 3158HAVING COUNT(*) >= ALL (SELECT fieldB 3159FROM t2 WHERE fieldA = field1); 3160field1 field2 31611 2 3162SELECT field1, field2 3163FROM t1 3164GROUP BY field1, field2 3165HAVING COUNT(*) < ANY (SELECT fieldB 3166FROM t2 WHERE fieldA = field1); 3167field1 field2 31681 1 31691 3 3170DROP TABLE t1, t2; 3171CREATE TABLE t1(a int, INDEX (a)); 3172INSERT INTO t1 VALUES (1), (3), (5), (7); 3173INSERT INTO t1 VALUES (NULL); 3174CREATE TABLE t2(a int); 3175INSERT INTO t2 VALUES (1),(2),(3); 3176EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2; 3177id select_type table type possible_keys key key_len ref rows Extra 31781 PRIMARY t2 ALL NULL NULL NULL NULL 3 31792 SUBQUERY t1 index_subquery a a 5 func 3 Using index; Full scan on NULL key 3180SELECT a, a IN (SELECT a FROM t1) FROM t2; 3181a a IN (SELECT a FROM t1) 31821 1 31832 NULL 31843 1 3185DROP TABLE t1,t2; 3186CREATE TABLE t1 (a DATETIME); 3187INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25'); 3188CREATE TABLE t2 AS SELECT 3189(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a 3190FROM t1 WHERE a > '2000-01-01'; 3191SHOW CREATE TABLE t2; 3192Table Create Table 3193t2 CREATE TABLE `t2` ( 3194 `sub_a` datetime DEFAULT NULL 3195) ENGINE=MyISAM DEFAULT CHARSET=latin1 3196CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01'); 3197SHOW CREATE TABLE t3; 3198Table Create Table 3199t3 CREATE TABLE `t3` ( 3200 `a` datetime DEFAULT NULL 3201) ENGINE=MyISAM DEFAULT CHARSET=latin1 3202DROP TABLE t1,t2,t3; 3203CREATE TABLE t1 (a int); 3204INSERT INTO t1 VALUES (1), (2); 3205SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0; 3206a 3207SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3208a 32091 32102 3211EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL; 3212id select_type table type possible_keys key key_len ref rows Extra 32131 PRIMARY t1 ALL NULL NULL NULL NULL 2 32142 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 3215DROP TABLE t1; 3216CREATE TABLE t1 (a int); 3217INSERT INTO t1 VALUES (2), (4), (1), (3); 3218CREATE TABLE t2 (b int, c int); 3219INSERT INTO t2 VALUES 3220(2,1), (1,3), (2,1), (4,4), (2,2), (1,4); 3221SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 ); 3222a 32232 32244 32251 32263 3227SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1); 3228ERROR 21000: Subquery returns more than 1 row 3229SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a; 3230a 32311 32322 32333 32344 3235SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a; 3236ERROR 21000: Subquery returns more than 1 row 3237SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2); 3238b MAX(c) 32391 4 32402 2 32414 4 3242SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1); 3243ERROR 21000: Subquery returns more than 1 row 3244SELECT a FROM t1 GROUP BY a 3245HAVING IFNULL((SELECT b FROM t2 WHERE b > 2), 3246(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3247a 32481 32492 32503 32514 3252SELECT a FROM t1 GROUP BY a 3253HAVING IFNULL((SELECT b FROM t2 WHERE b > 1), 3254(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3255ERROR 21000: Subquery returns more than 1 row 3256SELECT a FROM t1 GROUP BY a 3257HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3258(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3; 3259a 32604 3261SELECT a FROM t1 GROUP BY a 3262HAVING IFNULL((SELECT b FROM t2 WHERE b > 4), 3263(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3; 3264ERROR 21000: Subquery returns more than 1 row 3265SELECT a FROM t1 3266ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2), 3267(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3268a 32691 32702 32713 32724 3273SELECT a FROM t1 3274ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1), 3275(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3276ERROR 21000: Subquery returns more than 1 row 3277SELECT a FROM t1 3278ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3279(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)); 3280a 32811 32822 32833 32844 3285SELECT a FROM t1 3286ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4), 3287(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)); 3288ERROR 21000: Subquery returns more than 1 row 3289DROP TABLE t1,t2; 3290create table t1 (df decimal(5,1)); 3291insert into t1 values(1.1); 3292insert into t1 values(2.2); 3293select * from t1 where df <= all (select avg(df) from t1 group by df); 3294df 32951.1 3296select * from t1 where df >= all (select avg(df) from t1 group by df); 3297df 32982.2 3299drop table t1; 3300create table t1 (df decimal(5,1)); 3301insert into t1 values(1.1); 3302select 1.1 * exists(select * from t1); 33031.1 * exists(select * from t1) 33041.1 3305drop table t1; 3306CREATE TABLE t1 ( 3307grp int(11) default NULL, 3308a decimal(10,2) default NULL); 3309insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL); 3310select * from t1; 3311grp a 33121 1.00 33132 2.00 33142 3.00 33153 4.00 33163 5.00 33173 6.00 3318NULL NULL 3319select min(a) from t1 group by grp; 3320min(a) 3321NULL 33221.00 33232.00 33244.00 3325drop table t1; 3326CREATE table t1 ( c1 integer ); 3327INSERT INTO t1 VALUES ( 1 ); 3328INSERT INTO t1 VALUES ( 2 ); 3329INSERT INTO t1 VALUES ( 3 ); 3330CREATE TABLE t2 ( c2 integer ); 3331INSERT INTO t2 VALUES ( 1 ); 3332INSERT INTO t2 VALUES ( 4 ); 3333INSERT INTO t2 VALUES ( 5 ); 3334SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1); 3335c1 c2 33361 1 3337SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 3338WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) ); 3339c1 c2 33401 1 3341DROP TABLE t1,t2; 3342CREATE TABLE t1 ( c1 integer ); 3343INSERT INTO t1 VALUES ( 1 ); 3344INSERT INTO t1 VALUES ( 2 ); 3345INSERT INTO t1 VALUES ( 3 ); 3346INSERT INTO t1 VALUES ( 6 ); 3347CREATE TABLE t2 ( c2 integer ); 3348INSERT INTO t2 VALUES ( 1 ); 3349INSERT INTO t2 VALUES ( 4 ); 3350INSERT INTO t2 VALUES ( 5 ); 3351INSERT INTO t2 VALUES ( 6 ); 3352CREATE TABLE t3 ( c3 integer ); 3353INSERT INTO t3 VALUES ( 7 ); 3354INSERT INTO t3 VALUES ( 8 ); 3355SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2 3356WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL ); 3357c1 c2 33582 NULL 33593 NULL 3360DROP TABLE t1,t2,t3; 3361CREATE TABLE `t1` ( 3362`itemid` bigint(20) unsigned NOT NULL auto_increment, 3363`sessionid` bigint(20) unsigned default NULL, 3364`time` int(10) unsigned NOT NULL default '0', 3365`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT 3366NULL default '', 3367`data` text collate latin1_general_ci NOT NULL, 3368PRIMARY KEY (`itemid`) 3369) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3370INSERT INTO `t1` VALUES (1, 1, 1, 'D', ''); 3371CREATE TABLE `t2` ( 3372`sessionid` bigint(20) unsigned NOT NULL auto_increment, 3373`pid` int(10) unsigned NOT NULL default '0', 3374`date` int(10) unsigned NOT NULL default '0', 3375`ip` varchar(15) collate latin1_general_ci NOT NULL default '', 3376PRIMARY KEY (`sessionid`) 3377) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; 3378INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1'); 3379SELECT 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; 3380ip count( e.itemid ) 338110.10.10.1 1 3382drop tables t1,t2; 3383CREATE TABLE t1 (EMPNUM CHAR(3)); 3384CREATE TABLE t2 (EMPNUM CHAR(3) ); 3385INSERT INTO t1 VALUES ('E1'),('E2'); 3386INSERT INTO t2 VALUES ('E1'); 3387DELETE FROM t1 3388WHERE t1.EMPNUM NOT IN 3389(SELECT t2.EMPNUM 3390FROM t2 3391WHERE t1.EMPNUM = t2.EMPNUM); 3392select * from t1; 3393EMPNUM 3394E1 3395DROP TABLE t1,t2; 3396CREATE TABLE t1(select_id BIGINT, values_id BIGINT); 3397INSERT INTO t1 VALUES (1, 1); 3398CREATE TABLE t2 (select_id BIGINT, values_id BIGINT, 3399PRIMARY KEY(select_id,values_id)); 3400INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5); 3401SELECT values_id FROM t1 3402WHERE values_id IN (SELECT values_id FROM t2 3403WHERE select_id IN (1, 0)); 3404values_id 34051 3406SELECT values_id FROM t1 3407WHERE values_id IN (SELECT values_id FROM t2 3408WHERE select_id BETWEEN 0 AND 1); 3409values_id 34101 3411SELECT values_id FROM t1 3412WHERE values_id IN (SELECT values_id FROM t2 3413WHERE select_id = 0 OR select_id = 1); 3414values_id 34151 3416DROP TABLE t1, t2; 3417create table t1 (fld enum('0','1')); 3418insert into t1 values ('1'); 3419select * from (select max(fld) from t1) as foo; 3420max(fld) 34211 3422drop table t1; 3423CREATE TABLE t1 (a int, b int); 3424CREATE TABLE t2 (c int, d int); 3425CREATE TABLE t3 (e int); 3426INSERT INTO t1 VALUES 3427(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40); 3428INSERT INTO t2 VALUES 3429(2,10), (2,20), (4,10), (5,10), (3,20), (2,40); 3430INSERT INTO t3 VALUES (10), (30), (10), (20) ; 3431SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a; 3432a MAX(b) MIN(b) 34331 20 10 34342 30 10 34353 20 20 34364 40 40 3437SELECT * FROM t2; 3438c d 34392 10 34402 20 34414 10 34425 10 34433 20 34442 40 3445SELECT * FROM t3; 3446e 344710 344830 344910 345020 3451SELECT a FROM t1 GROUP BY a 3452HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20); 3453a 34542 34554 3456SELECT a FROM t1 GROUP BY a 3457HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d); 3458a 34592 3460SELECT a FROM t1 GROUP BY a 3461HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d); 3462a 34632 34644 3465SELECT a FROM t1 GROUP BY a 3466HAVING a IN (SELECT c FROM t2 3467WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3468a 34692 34703 3471SELECT a FROM t1 GROUP BY a 3472HAVING a IN (SELECT c FROM t2 3473WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3474a 34752 34763 3477SELECT a FROM t1 GROUP BY a 3478HAVING a IN (SELECT c FROM t2 3479WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 3480a 34812 3482SELECT a FROM t1 GROUP BY a 3483HAVING a IN (SELECT c FROM t2 3484WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d)); 3485a 34862 3487SELECT a FROM t1 GROUP BY a 3488HAVING a IN (SELECT c FROM t2 3489WHERE MIN(b) < d AND 3490EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 3491a 34922 3493SELECT a, SUM(a) FROM t1 GROUP BY a; 3494a SUM(a) 34951 2 34962 6 34973 3 34984 4 3499SELECT a FROM t1 3500WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a; 3501a 35023 35034 3504SELECT a FROM t1 GROUP BY a 3505HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c); 3506a 35071 35083 35094 3510SELECT a FROM t1 3511WHERE a < 3 AND 3512EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a; 3513a 35141 35152 3516SELECT a FROM t1 3517WHERE a < 3 AND 3518EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c); 3519a 35201 35212 35221 35232 35242 3525SELECT t1.a FROM t1 GROUP BY t1.a 3526HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c 3527HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3528HAVING SUM(t1.a+t2.c) < t3.e/4)); 3529a 35301 35312 3532SELECT t1.a FROM t1 GROUP BY t1.a 3533HAVING t1.a > ALL(SELECT t2.c FROM t2 3534WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e 3535HAVING SUM(t1.a+t2.c) < t3.e/4)); 3536a 35374 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 3541WHERE SUM(t1.a+t2.c) < t3.e/4)); 3542ERROR HY000: Invalid use of group function 3543SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20; 3544ERROR HY000: Invalid use of group function 3545SELECT t1.a FROM t1 GROUP BY t1.a 3546HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3547HAVING AVG(t2.c+SUM(t1.b)) > 20); 3548a 35492 35503 35514 3552SELECT t1.a FROM t1 GROUP BY t1.a 3553HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3554HAVING AVG(SUM(t1.b)) > 20); 3555a 35562 35574 3558SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a 3559HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 3560HAVING t2.c+sum > 20); 3561a sum 35622 60 35633 20 35644 40 3565DROP TABLE t1,t2,t3; 3566CREATE TABLE t1 (a varchar(5), b varchar(10)); 3567INSERT INTO t1 VALUES 3568('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2), 3569('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8); 3570SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3571a b 3572BBB 4 3573CCC 7 3574AAA 8 3575EXPLAIN 3576SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3577id select_type table type possible_keys key key_len ref rows Extra 35781 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where 35791 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35802 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3581ALTER TABLE t1 ADD INDEX(a); 3582SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3583a b 3584BBB 4 3585CCC 7 3586AAA 8 3587EXPLAIN 3588SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a); 3589id select_type table type possible_keys key key_len ref rows Extra 35901 PRIMARY t1 ALL a NULL NULL NULL 9 Using where 35911 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1 35922 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary 3593DROP TABLE t1; 3594create table t1( f1 int,f2 int); 3595insert into t1 values (1,1),(2,2); 3596select 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'; 3597t 3598crash1 3599crash1 3600drop table t1; 3601create table t1 (c int, key(c)); 3602insert into t1 values (1142477582), (1142455969); 3603create table t2 (a int, b int); 3604insert into t2 values (2, 1), (1, 0); 3605delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1; 3606drop table t1, t2; 3607CREATE TABLE t1 (a INT); 3608CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3609ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3610CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1); 3611ERROR 42S22: Unknown column 'no_such_column' in 'where clause' 3612SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1); 3613ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery' 3614DROP TABLE t1; 3615create table t1 (i int, j bigint); 3616insert into t1 values (1, 2), (2, 2), (3, 2); 3617select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3; 3618min(i) 36191 3620drop table t1; 3621CREATE TABLE t1 (i BIGINT UNSIGNED); 3622INSERT INTO t1 VALUES (10000000000000000000); 3623INSERT INTO t1 VALUES (1); 3624CREATE TABLE t2 (i BIGINT UNSIGNED); 3625INSERT INTO t2 VALUES (10000000000000000000); 3626INSERT INTO t2 VALUES (1); 3627/* simple test */ 3628SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i; 3629i 363010000000000000000000 36311 3632/* subquery test */ 3633SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2); 3634i 363510000000000000000000 3636/* subquery test with cast*/ 3637SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED); 3638i 363910000000000000000000 3640DROP TABLE t1; 3641DROP TABLE t2; 3642CREATE TABLE t1 ( 3643id bigint(20) unsigned NOT NULL auto_increment, 3644name varchar(255) NOT NULL, 3645PRIMARY KEY (id) 3646); 3647INSERT INTO t1 VALUES 3648(1, 'Balazs'), (2, 'Joe'), (3, 'Frank'); 3649CREATE TABLE t2 ( 3650id bigint(20) unsigned NOT NULL auto_increment, 3651mid bigint(20) unsigned NOT NULL, 3652date date NOT NULL, 3653PRIMARY KEY (id) 3654); 3655INSERT INTO t2 VALUES 3656(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'), 3657(4, 2, '2006-04-20'), (5, 1, '2006-05-01'); 3658SELECT *, 3659(SELECT date FROM t2 WHERE mid = t1.id 3660ORDER BY date DESC LIMIT 0, 1) AS date_last, 3661(SELECT date FROM t2 WHERE mid = t1.id 3662ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last 3663FROM t1; 3664id name date_last date_next_to_last 36651 Balazs 2006-05-01 NULL 36662 Joe 2006-04-20 NULL 36673 Frank 2006-04-13 NULL 3668SELECT *, 3669(SELECT COUNT(*) FROM t2 WHERE mid = t1.id 3670ORDER BY date DESC LIMIT 1, 1) AS date_count 3671FROM t1; 3672id name date_count 36731 Balazs NULL 36742 Joe NULL 36753 Frank NULL 3676SELECT *, 3677(SELECT date FROM t2 WHERE mid = t1.id 3678ORDER BY date DESC LIMIT 0, 1) AS date_last, 3679(SELECT date FROM t2 WHERE mid = t1.id 3680ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last 3681FROM t1; 3682id name date_last date_next_to_last 36831 Balazs 2006-05-01 2006-03-30 36842 Joe 2006-04-20 2006-04-06 36853 Frank 2006-04-13 NULL 3686DROP TABLE t1,t2; 3687CREATE TABLE t1 ( 3688i1 int(11) NOT NULL default '0', 3689i2 int(11) NOT NULL default '0', 3690t datetime NOT NULL default '0000-00-00 00:00:00', 3691PRIMARY KEY (i1,i2,t) 3692); 3693INSERT INTO t1 VALUES 3694(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'), 3695(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'), 3696(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'), 3697(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'), 3698(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'), 3699(24,2,'2005-05-27 12:40:06'); 3700CREATE TABLE t2 ( 3701i1 int(11) NOT NULL default '0', 3702i2 int(11) NOT NULL default '0', 3703t datetime default NULL, 3704PRIMARY KEY (i1) 3705); 3706INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40'); 3707EXPLAIN 3708SELECT * FROM t1,t2 3709WHERE t1.t = (SELECT t1.t FROM t1 3710WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3711ORDER BY t1.t DESC LIMIT 1); 3712id select_type table type possible_keys key key_len ref rows Extra 37131 PRIMARY t2 system NULL NULL NULL NULL 1 37141 PRIMARY t1 index NULL PRIMARY 13 NULL 11 Using where; Using index 37152 SUBQUERY t1 range PRIMARY PRIMARY 13 NULL 6 Using where; Using index 3716SELECT * FROM t1,t2 3717WHERE t1.t = (SELECT t1.t FROM t1 3718WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1 3719ORDER BY t1.t DESC LIMIT 1); 3720i1 i2 t i1 i2 t 372124 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40 3722DROP TABLE t1, t2; 3723CREATE TABLE t1 (i INT); 3724(SELECT i FROM t1) UNION (SELECT i FROM t1); 3725i 3726SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS 3727( 3728(SELECT i FROM t1) UNION 3729(SELECT i FROM t1) 3730); 3731i 3732SELECT * FROM t1 3733WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1))); 3734i 3735explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12)) 3736from t1; 3737id select_type table type possible_keys key key_len ref rows Extra 37381 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37392 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37403 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3741NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3742explain select * from t1 where not exists 3743((select t11.i from t1 t11) union (select t12.i from t1 t12)); 3744id select_type table type possible_keys key key_len ref rows Extra 37451 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found 37462 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table 37473 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table 3748NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 3749DROP TABLE t1; 3750CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b)); 3751insert into t1 (a) values (FLOOR(rand() * 100)); 3752insert into t1 (a) select FLOOR(rand() * 100) from t1; 3753insert into t1 (a) select FLOOR(rand() * 100) from t1; 3754insert into t1 (a) select FLOOR(rand() * 100) from t1; 3755insert into t1 (a) select FLOOR(rand() * 100) from t1; 3756insert into t1 (a) select FLOOR(rand() * 100) from t1; 3757insert into t1 (a) select FLOOR(rand() * 100) from t1; 3758insert into t1 (a) select FLOOR(rand() * 100) from t1; 3759insert into t1 (a) select FLOOR(rand() * 100) from t1; 3760insert into t1 (a) select FLOOR(rand() * 100) from t1; 3761insert into t1 (a) select FLOOR(rand() * 100) from t1; 3762insert into t1 (a) select FLOOR(rand() * 100) from t1; 3763insert into t1 (a) select FLOOR(rand() * 100) from t1; 3764insert into t1 (a) select FLOOR(rand() * 100) from t1; 3765SELECT a, 3766(SELECT REPEAT(' ',250) FROM t1 i1 3767WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a 3768FROM t1 ORDER BY a LIMIT 5; 3769a a 37700 NULL 37710 NULL 37720 NULL 37730 NULL 37740 NULL 3775DROP TABLE t1; 3776CREATE TABLE t1 (a INT, b INT); 3777CREATE TABLE t2 (a INT); 3778INSERT INTO t2 values (1); 3779INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4); 3780SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a; 3781(SELECT COUNT(DISTINCT t1.b) from t2) 37822 37831 37841 3785SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 3786FROM t1 GROUP BY t1.a; 3787(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3) 37882 37891 37901 3791SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a; 3792COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b)) 37932 2 37941 1 37951 1 3796SELECT COUNT(DISTINCT t1.b), 3797(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 3798FROM t1 GROUP BY t1.a; 3799COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3) 38002 2 38011 1 38021 1 3803SELECT ( 3804SELECT ( 3805SELECT COUNT(DISTINCT t1.b) 3806) 3807) 3808FROM t1 GROUP BY t1.a; 3809( 3810SELECT ( 3811SELECT COUNT(DISTINCT t1.b) 3812) 3813) 38142 38151 38161 3817SELECT ( 3818SELECT ( 3819SELECT ( 3820SELECT COUNT(DISTINCT t1.b) 3821) 3822) 3823FROM t1 GROUP BY t1.a LIMIT 1) 3824FROM t1 t2 3825GROUP BY t2.a; 3826( 3827SELECT ( 3828SELECT ( 3829SELECT COUNT(DISTINCT t1.b) 3830) 3831) 3832FROM t1 GROUP BY t1.a LIMIT 1) 38332 38342 38352 3836DROP TABLE t1,t2; 3837CREATE TABLE t1 (a int, b int, PRIMARY KEY (b)); 3838CREATE TABLE t2 (x int auto_increment, y int, z int, 3839PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b)); 3840create table t3 (a int); 3841insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 3842insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a)) 3843from t3 A, t3 B, t3 C, t3 D where D.a<3; 3844insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3; 3845SET SESSION sort_buffer_size = 32 * 1024; 3846SELECT SQL_NO_CACHE COUNT(*) 3847FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c 3848FROM t1) t; 3849COUNT(*) 38503000 3851SET SESSION sort_buffer_size = 8 * 1024 * 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 3857DROP TABLE t1,t2,t3; 3858CREATE TABLE t1 (id char(4) PRIMARY KEY, c int); 3859CREATE TABLE t2 (c int); 3860INSERT INTO t1 VALUES ('aa', 1); 3861INSERT INTO t2 VALUES (1); 3862SELECT * FROM t1 3863WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3864UNION 3865SELECT c from t2 WHERE c=t1.c); 3866id c 3867aa 1 3868INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1); 3869SELECT * FROM t1 3870WHERE EXISTS (SELECT c FROM t2 WHERE c=1 3871UNION 3872SELECT c from t2 WHERE c=t1.c); 3873id c 3874aa 1 3875bb 2 3876cc 3 3877dd 1 3878INSERT INTO t2 VALUES (2); 3879CREATE TABLE t3 (c int); 3880INSERT INTO t3 VALUES (1); 3881SELECT * FROM t1 3882WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1 3883UNION 3884SELECT c from t2 WHERE c=t1.c); 3885id c 3886aa 1 3887bb 2 3888cc 3 3889dd 1 3890DROP TABLE t1,t2,t3; 3891CREATE TABLE t1(f1 int); 3892CREATE TABLE t2(f2 int, f21 int, f3 timestamp); 3893INSERT INTO t1 VALUES (1),(1),(2),(2); 3894INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11"); 3895SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1; 3896sq 38972 38984 3899SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2; 3900tt 39012 39022 3903PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1'; 3904EXECUTE stmt1; 3905sq 39062 39074 3908EXECUTE stmt1; 3909sq 39102 39114 3912DEALLOCATE PREPARE stmt1; 3913SELECT f2, AVG(f21), 3914(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test 3915FROM t2 GROUP BY f2; 3916f2 AVG(f21) test 39171 1.0000 2004-02-29 11:11:11 39182 2.0000 2004-02-29 11:11:11 3919DROP TABLE t1,t2; 3920CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL); 3921INSERT INTO t1 VALUES 3922(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'), 3923(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'), 3924(3,2,'k'), (3,1,'l'), (1,9,'m'); 3925SELECT a, MAX(b), 3926(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test 3927FROM t1 GROUP BY a; 3928a MAX(b) test 39291 9 m 39302 3 h 39313 4 i 3932DROP TABLE t1; 3933DROP TABLE IF EXISTS t1; 3934DROP TABLE IF EXISTS t2; 3935DROP TABLE IF EXISTS t1xt2; 3936CREATE TABLE t1 ( 3937id_1 int(5) NOT NULL, 3938t varchar(4) DEFAULT NULL 3939); 3940CREATE TABLE t2 ( 3941id_2 int(5) NOT NULL, 3942t varchar(4) DEFAULT NULL 3943); 3944CREATE TABLE t1xt2 ( 3945id_1 int(5) NOT NULL, 3946id_2 int(5) NOT NULL 3947); 3948INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'); 3949INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa'); 3950INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4); 3951SELECT DISTINCT t1.id_1 FROM t1 WHERE 3952(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3953id_1 3954SELECT DISTINCT t1.id_1 FROM t1 WHERE 3955(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 3956id_1 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 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3962id_1 39631 39642 39653 39664 3967SELECT DISTINCT t1.id_1 FROM t1 WHERE 3968(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))); 3969id_1 39701 39712 39723 39734 3974SELECT DISTINCT t1.id_1 FROM t1 WHERE 3975(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)))); 3976id_1 39771 39782 39793 39804 3981insert INTO t1xt2 VALUES (1, 12); 3982SELECT DISTINCT t1.id_1 FROM t1 WHERE 3983(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3984id_1 39851 3986SELECT DISTINCT t1.id_1 FROM t1 WHERE 3987(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 3988id_1 39891 3990SELECT DISTINCT t1.id_1 FROM t1 WHERE 3991(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 3992id_1 39931 3994SELECT DISTINCT t1.id_1 FROM t1 WHERE 3995(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 3996id_1 39972 39983 39994 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 4012insert INTO t1xt2 VALUES (2, 12); 4013SELECT DISTINCT t1.id_1 FROM t1 WHERE 4014(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4015id_1 40161 40172 4018SELECT DISTINCT t1.id_1 FROM t1 WHERE 4019(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4020id_1 40211 40222 4023SELECT DISTINCT t1.id_1 FROM t1 WHERE 4024(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4025id_1 40261 40272 4028SELECT DISTINCT t1.id_1 FROM t1 WHERE 4029(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 4030id_1 40313 40324 4033SELECT DISTINCT t1.id_1 FROM t1 WHERE 4034(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 4035id_1 40363 40374 4038SELECT DISTINCT t1.id_1 FROM t1 WHERE 4039(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 4040id_1 40413 40424 4043DROP TABLE t1; 4044DROP TABLE t2; 4045DROP TABLE t1xt2; 4046CREATE TABLE t1 (a int); 4047INSERT INTO t1 VALUES (3), (1), (2); 4048SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1; 4049col1 col2 4050this is a test. 3 4051this is a test. 1 4052this is a test. 2 4053SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t; 4054col1 t2 4055this is a test. 3 4056this is a test. 1 4057this is a test. 2 4058DROP table t1; 4059CREATE TABLE t1 (a int, b int); 4060CREATE TABLE t2 (m int, n int); 4061INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4062INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4063SELECT COUNT(*), a, 4064(SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 4065FROM t1 GROUP BY a; 4066COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1) 40672 2 2 40683 3 3 40691 4 1 4070SELECT COUNT(*), a, 4071(SELECT MIN(m) FROM t2 WHERE m = count(*)) 4072FROM t1 GROUP BY a; 4073COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*)) 40742 2 2 40753 3 3 40761 4 1 4077SELECT COUNT(*), a 4078FROM t1 GROUP BY a 4079HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1; 4080COUNT(*) a 40812 2 40823 3 4083DROP TABLE t1,t2; 4084CREATE TABLE t1 (a int, b int); 4085CREATE TABLE t2 (m int, n int); 4086INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4); 4087INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44); 4088SELECT COUNT(*) c, a, 4089(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 4090FROM t1 GROUP BY a; 4091c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a) 40922 2 2 40933 3 3 40941 4 1,1 4095SELECT COUNT(*) c, a, 4096(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 4097FROM t1 GROUP BY a; 4098c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a) 40992 2 3 41003 3 4 41011 4 2,2 4102DROP table t1,t2; 4103CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b)); 4104INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'), 4105(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'), 4106(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p'); 4107SELECT a, MAX(b), 4108(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test 4109FROM t1 GROUP BY a; 4110a MAX(b) test 41111 9 m 41122 3 h 41133 4 i 4114SELECT a x, MAX(b), 4115(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test 4116FROM t1 GROUP BY a; 4117x MAX(b) test 41181 9 m 41192 3 h 41203 4 i 4121SELECT a, AVG(b), 4122(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test 4123FROM t1 WHERE t1.d=0 GROUP BY a; 4124a AVG(b) test 41251 4.0000 d 41262 2.0000 g 41273 2.5000 NULL 4128SELECT tt.a, 4129(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4130LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4131FROM t1 as tt; 4132a test 41331 n 41341 n 41351 n 41361 n 41371 n 41381 n 41391 n 41402 o 41412 o 41422 o 41432 o 41443 p 41453 p 41463 p 41473 p 41483 p 4149SELECT tt.a, 4150(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4151LIMIT 1) 4152FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test 4153FROM t1 as tt GROUP BY tt.a; 4154a test 41551 n 41562 o 41573 p 4158SELECT tt.a, MAX( 4159(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a) 4160LIMIT 1) 4161FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test 4162FROM t1 as tt GROUP BY tt.a; 4163a test 41641 n 41652 o 41663 p 4167DROP TABLE t1; 4168CREATE TABLE t1 (a int, b int); 4169INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4170SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4171a 41721 41732 4174SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4175a 4176SELECT a FROM t1 t0 4177WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4178a 41791 41802 4181SET @@sql_mode='ansi'; 4182SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4183ERROR HY000: Invalid use of group function 4184SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4185ERROR HY000: Invalid use of group function 4186SELECT a FROM t1 t0 4187WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4188ERROR HY000: Invalid use of group function 4189SET @@sql_mode=default; 4190DROP TABLE t1; 4191CREATE TABLE t1 (a INT); 4192INSERT INTO t1 values (1),(1),(1),(1); 4193CREATE TABLE t2 (x INT); 4194INSERT INTO t1 values (1000),(1001),(1002); 4195SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1; 4196ERROR HY000: Invalid use of group function 4197SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1; 4198ERROR HY000: Invalid use of group function 4199SELECT COUNT(1) FROM DUAL; 4200COUNT(1) 42011 4202SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1; 4203ERROR HY000: Invalid use of group function 4204SELECT 4205SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) ) 4206FROM t1; 4207ERROR HY000: Invalid use of group function 4208SELECT t1.a as XXA, 4209SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) ) 4210FROM t1; 4211ERROR HY000: Invalid use of group function 4212DROP TABLE t1,t2; 4213CREATE TABLE t1 (a int, b int, KEY (a)); 4214INSERT INTO t1 VALUES (1,1),(2,1); 4215EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b); 4216id select_type table type possible_keys key key_len ref rows Extra 42171 PRIMARY t1 ref a a 5 const 1 Using where; Using index 42182 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 4219DROP TABLE t1; 4220CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id)); 4221INSERT INTO t1 VALUES 4222(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY'); 4223CREATE TABLE t2 (id int NOT NULL, INDEX idx(id)); 4224INSERT INTO t2 VALUES (7), (5), (1), (3); 4225SELECT id, st FROM t1 4226WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4227id st 42283 FL 42291 GA 42307 FL 4231SELECT id, st FROM t1 4232WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4233GROUP BY id; 4234id st 42351 GA 42363 FL 42377 FL 4238SELECT id, st FROM t1 4239WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id); 4240id st 42412 GA 42424 FL 4243SELECT id, st FROM t1 4244WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id) 4245GROUP BY id; 4246id st 42472 GA 42484 FL 4249DROP TABLE t1,t2; 4250CREATE TABLE t1 (a int); 4251INSERT INTO t1 VALUES (1), (2); 4252EXPLAIN EXTENDED 4253SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res; 4254id select_type table type possible_keys key key_len ref rows filtered Extra 42551 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00 42562 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort 4257Warnings: 4258Note 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` 4259DROP TABLE t1; 4260CREATE TABLE t1 ( 4261a varchar(255) default NULL, 4262b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 4263INDEX idx(a,b) 4264); 4265CREATE TABLE t2 ( 4266a varchar(255) default NULL 4267); 4268INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24'); 4269INSERT INTO t1 SELECT * FROM t1; 4270INSERT INTO t1 SELECT * FROM t1; 4271INSERT INTO t1 SELECT * FROM t1; 4272INSERT INTO t1 SELECT * FROM t1; 4273INSERT INTO t1 SELECT * FROM t1; 4274INSERT INTO t1 SELECT * FROM t1; 4275INSERT INTO t1 SELECT * FROM t1; 4276INSERT INTO t1 SELECT * FROM t1; 4277INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26'); 4278INSERT INTO `t2` VALUES ('abcdefghijk'); 4279INSERT INTO `t2` VALUES ('asdf'); 4280SET session sort_buffer_size=8192; 4281SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2; 4282d1 42831 42841 4285DROP TABLE t1,t2; 4286CREATE TABLE t1 (a INTEGER, b INTEGER); 4287CREATE TABLE t2 (x INTEGER); 4288INSERT INTO t1 VALUES (1,11), (2,22), (2,22); 4289INSERT INTO t2 VALUES (1), (2); 4290SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a; 4291ERROR 21000: Subquery returns more than 1 row 4292SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a; 4293ERROR 21000: Subquery returns more than 1 row 4294SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1; 4295(SELECT SUM(t1.a)/AVG(t2.x) FROM t2) 42963.3333 4297DROP TABLE t1,t2; 4298CREATE TABLE t1 (a INT, b INT); 4299INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2); 4300SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1 4301AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a) 4302GROUP BY a1.a; 4303a COUNT(*) 43041 3 4305DROP TABLE t1; 4306CREATE TABLE t1 (a INT); 4307CREATE TABLE t2 (a INT); 4308INSERT INTO t1 VALUES (1),(2); 4309INSERT INTO t2 VALUES (1),(2); 4310SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1; 4311(SELECT SUM(t1.a) FROM t2 WHERE a=0) 4312NULL 4313SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1; 4314ERROR 21000: Subquery returns more than 1 row 4315SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1; 4316(SELECT SUM(t1.a) FROM t2 WHERE a=1) 43173 4318DROP TABLE t1,t2; 4319CREATE TABLE t1 (a1 INT, a2 INT); 4320CREATE TABLE t2 (b1 INT, b2 INT); 4321INSERT INTO t1 VALUES (100, 200); 4322INSERT INTO t1 VALUES (101, 201); 4323INSERT INTO t2 VALUES (101, 201); 4324INSERT INTO t2 VALUES (103, 203); 4325SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1; 4326((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL 43270 43280 4329DROP TABLE t1, t2; 4330CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5)); 4331INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43); 4332SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1); 4333s1 s2 4334SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1); 4335s1 s2 4336CREATE INDEX I1 ON t1 (s1); 4337CREATE INDEX I2 ON t1 (s2); 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 4342TRUNCATE t1; 4343INSERT INTO t1 VALUES (0x41,0x41); 4344SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1); 4345s1 s2 4346DROP TABLE t1; 4347CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1)); 4348CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2)); 4349CREATE TABLE t3 (a3 BINARY(2) default '0'); 4350INSERT INTO t1 VALUES (1),(2),(3),(4); 4351INSERT INTO t2 VALUES (1),(2),(3); 4352INSERT INTO t3 VALUES (1),(2),(3); 4353SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2; 4354LEFT(t2.a2, 1) 43551 43562 43573 4358SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1; 4359a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) 43601 0 43612 0 43623 0 43634 0 4364DROP TABLE t1,t2,t3; 4365CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3)); 4366CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY); 4367CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY); 4368INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40); 4369INSERT INTO t2 VALUES (2), (3), (4), (5); 4370INSERT INTO t3 VALUES (10), (20), (30); 4371SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3; 4372LEFT(t1.a1,1) 43731 43742 43753 4376SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3); 4377a2 4378DROP TABLE t1, t2, t3; 4379SET @save_optimizer_switch=@@optimizer_switch; 4380SET optimizer_switch='semijoin_with_cache=off'; 4381SET optimizer_switch='materialization=off'; 4382CREATE TABLE t1 (a CHAR(1), b VARCHAR(10)); 4383INSERT INTO t1 VALUES ('a', 'aa'); 4384INSERT INTO t1 VALUES ('a', 'aaa'); 4385SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4386a b 4387CREATE INDEX I1 ON t1 (a); 4388CREATE INDEX I2 ON t1 (b); 4389EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4390id select_type table type possible_keys key key_len ref rows Extra 43911 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 43921 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4393SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1); 4394a b 4395CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10)); 4396INSERT INTO t2 SELECT * FROM t1; 4397CREATE INDEX I1 ON t2 (a); 4398CREATE INDEX I2 ON t2 (b); 4399EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4400id select_type table type possible_keys key key_len ref rows Extra 44011 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where 44021 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2) 4403SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2); 4404a b 4405EXPLAIN 4406SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4407id select_type table type possible_keys key key_len ref rows Extra 44081 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where 44091 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1) 4410SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500); 4411a b 4412DROP TABLE t1,t2; 4413SET optimizer_switch= @save_optimizer_switch; 4414CREATE TABLE t1(a INT, b INT); 4415INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4); 4416EXPLAIN 4417SELECT a AS out_a, MIN(b) FROM t1 4418WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4419GROUP BY a; 4420ERROR 42S22: Unknown column 'out_a' in 'where clause' 4421SELECT a AS out_a, MIN(b) FROM t1 4422WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a) 4423GROUP BY a; 4424ERROR 42S22: Unknown column 'out_a' in 'where clause' 4425EXPLAIN 4426SELECT a AS out_a, MIN(b) FROM t1 t1_outer 4427WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a) 4428GROUP BY a; 4429id select_type table type possible_keys key key_len ref rows Extra 44301 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort 44312 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where 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; 4435out_a MIN(b) 44361 2 44372 4 4438DROP TABLE t1; 4439CREATE TABLE t1 (a INT); 4440CREATE TABLE t2 (a INT); 4441INSERT INTO t1 VALUES (1),(2); 4442INSERT INTO t2 VALUES (1),(2); 4443SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 44442 44452 44462 4447EXPLAIN EXTENDED 4448SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a)); 4449id select_type table type possible_keys key key_len ref rows filtered Extra 44501 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 44511 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00 44522 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00 4453Warnings: 4454Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4455Note 1003 select 2 AS `2` from `test`.`t1` semi join (`test`.`t2`) where 1 4456EXPLAIN EXTENDED 4457SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION 4458(SELECT 1 FROM t2 WHERE t1.a = t2.a)); 4459id select_type table type possible_keys key key_len ref rows filtered Extra 44601 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where 44612 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where 44623 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where 4463NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL 4464Warnings: 4465Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 4466Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 4467Note 1003 /* select#1 */ select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((/* select#2 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) union (/* select#3 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)) 4468DROP TABLE t1,t2; 4469create table t0(a int); 4470insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 4471create table t1(f11 int, f12 int); 4472create table t2(f21 int unsigned not null, f22 int, f23 varchar(10)); 4473insert into t1 values(1,1),(2,2), (3, 3); 4474insert ignore into t2 4475select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a 4476from t0 A, t0 B, t0 C, t0 D; 4477set session sort_buffer_size= 33*1024; 4478select count(*) from t1 where f12 = 4479(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1); 4480count(*) 44813 4482drop table t0,t1,t2; 4483CREATE TABLE t4 ( 4484f7 varchar(32) collate utf8_bin NOT NULL default '', 4485f10 varchar(32) collate utf8_bin default NULL, 4486PRIMARY KEY (f7) 4487); 4488INSERT INTO t4 VALUES(1,1), (2,null); 4489CREATE TABLE t2 ( 4490f4 varchar(32) collate utf8_bin NOT NULL default '', 4491f2 varchar(50) collate utf8_bin default NULL, 4492f3 varchar(10) collate utf8_bin default NULL, 4493PRIMARY KEY (f4), 4494UNIQUE KEY uk1 (f2) 4495); 4496INSERT INTO t2 VALUES(1,1,null), (2,2,null); 4497CREATE TABLE t1 ( 4498f8 varchar(32) collate utf8_bin NOT NULL default '', 4499f1 varchar(10) collate utf8_bin default NULL, 4500f9 varchar(32) collate utf8_bin default NULL, 4501PRIMARY KEY (f8) 4502); 4503INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2); 4504CREATE TABLE t3 ( 4505f6 varchar(32) collate utf8_bin NOT NULL default '', 4506f5 varchar(50) collate utf8_bin default NULL, 4507PRIMARY KEY (f6) 4508); 4509INSERT INTO t3 VALUES (1,null), (2,null); 4510SELECT 4511IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4, 4512IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3, 4513SUM( 4514IF( 4515(SELECT VPC.f2 4516FROM t2 VPC, t4 a2, t2 a3 4517WHERE 4518VPC.f4 = a2.f10 AND a3.f2 = a4 4519LIMIT 1) IS NULL, 45200, 4521t3.f5 4522) 4523) AS a6 4524FROM 4525t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4 4526GROUP BY a4; 4527a4 f3 a6 45281 NULL NULL 45292 NULL NULL 4530DROP TABLE t1, t2, t3, t4; 4531create table t1 (a float(5,4) zerofill); 4532create table t2 (a float(5,4),b float(2,0)); 4533select t1.a from t1 where 4534t1.a= (select b from t2 limit 1) and not 4535t1.a= (select a from t2 limit 1) ; 4536a 4537drop table t1, t2; 4538CREATE TABLE t1 (a INT); 4539INSERT INTO t1 VALUES (1),(2); 4540SET @save_join_cache_level=@@join_cache_level; 4541SET join_cache_level=0; 4542EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a); 4543id select_type table type possible_keys key key_len ref rows filtered Extra 45441 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45451 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45462 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary 4547Warnings: 4548Note 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 4549EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a); 4550id select_type table type possible_keys key key_len ref rows filtered Extra 45511 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00 45521 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 45532 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary 4554Warnings: 4555Note 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 4556SET join_cache_level=@save_join_cache_level; 4557DROP TABLE t1; 4558# 4559# Bug#45061: Incorrectly market field caused wrong result. 4560# 4561CREATE TABLE `C` ( 4562`int_nokey` int(11) NOT NULL, 4563`int_key` int(11) NOT NULL, 4564KEY `int_key` (`int_key`) 4565); 4566INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), 4567(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), 4568(5,2), (1,8), (7,0), (0,9), (9,5); 4569SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4570int_nokey int_key 45719 9 45720 0 45735 5 45740 0 4575EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`); 4576id select_type table type possible_keys key key_len ref rows filtered Extra 45771 SIMPLE C ALL NULL NULL NULL NULL 20 100.00 Using where 4578DROP TABLE C; 4579# End of test for bug#45061. 4580# 4581# Bug #46749: Segfault in add_key_fields() with outer subquery level 4582# field references 4583# 4584CREATE TABLE t1 ( 4585a int, 4586b int, 4587UNIQUE (a), KEY (b) 4588); 4589INSERT INTO t1 VALUES (1,1), (2,1); 4590CREATE TABLE st1 like t1; 4591INSERT INTO st1 VALUES (1,1), (2,1); 4592CREATE TABLE st2 like t1; 4593INSERT INTO st2 VALUES (1,1), (2,1); 4594EXPLAIN 4595SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4596FROM t1 4597WHERE a = 230; 4598id select_type table type possible_keys key key_len ref rows Extra 45991 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 46002 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 4601SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4602FROM t1 4603WHERE a = 230; 4604MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b) 4605NULL NULL 4606DROP TABLE t1, st1, st2; 4607# 4608# Bug #48709: Assertion failed in sql_select.cc:11782: 4609# int join_read_key(JOIN_TAB*) 4610# 4611CREATE TABLE t1 (pk int PRIMARY KEY, int_key int); 4612INSERT INTO t1 VALUES (10,1), (14,1); 4613CREATE TABLE t2 (pk int PRIMARY KEY, int_key int); 4614INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3); 4615# should have eq_ref for t1 4616EXPLAIN 4617SELECT * FROM t2 outr 4618WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4619ORDER BY outr.pk; 4620id select_type table type possible_keys key key_len ref rows Extra 4621x x outr ALL x x x x x x 4622x x t1 eq_ref x x x x x x 4623x x t2 index x x x x x x 4624# should not crash on debug binaries 4625SELECT * FROM t2 outr 4626WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2) 4627ORDER BY outr.pk; 4628pk int_key 46293 3 46307 3 4631DROP TABLE t1,t2; 4632# 4633# Bug#12329653 4634# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY 4635# 4636CREATE TABLE t1(a1 int); 4637INSERT INTO t1 VALUES (1),(2); 4638SELECT @@session.sql_mode INTO @old_sql_mode; 4639SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4640EXPLAIN EXTENDED 4641SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 4642id select_type table type possible_keys key key_len ref rows filtered Extra 46431 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 46442 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 4645Warnings: 4646Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1 4647SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 46481 46491 46501 4651PREPARE stmt FROM 4652'SELECT 1 UNION ALL 4653SELECT 1 FROM t1 4654ORDER BY 4655(SELECT 1 FROM t1 AS t1_0 4656 WHERE 1 < SOME (SELECT a1 FROM t1) 4657)' ; 4658EXECUTE stmt ; 4659ERROR 21000: Subquery returns more than 1 row 4660EXECUTE stmt ; 4661ERROR 21000: Subquery returns more than 1 row 4662SET SESSION sql_mode=@old_sql_mode; 4663DEALLOCATE PREPARE stmt; 4664DROP TABLE t1; 4665# 4666# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4667# 4668CREATE TABLE t1(a1 int); 4669INSERT INTO t1 VALUES (1),(2); 4670CREATE TABLE t2(a1 int); 4671INSERT INTO t2 VALUES (3); 4672SELECT @@session.sql_mode INTO @old_sql_mode; 4673SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4674SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); 46751 46761 46771 4678SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); 46791 46801 46811 4682SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); 46831 4684Warnings: 4685Warning 1292 Truncated incorrect DOUBLE value: 'a' 4686SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); 46871 46881 46891 4690SET SESSION sql_mode=@old_sql_mode; 4691DROP TABLE t1, t2; 4692# 4693# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 4694# 4695create table t2(i int); 4696insert into t2 values(0); 4697SELECT @@session.sql_mode INTO @old_sql_mode; 4698SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 4699CREATE VIEW v1 AS 4700SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 4701; 4702CREATE TABLE t1 ( 4703pk int NOT NULL, 4704col_varchar_key varchar(1) DEFAULT NULL, 4705PRIMARY KEY (pk), 4706KEY col_varchar_key (col_varchar_key) 4707); 4708SELECT t1.pk 4709FROM t1 4710WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) 4711; 4712pk 4713SET SESSION sql_mode=@old_sql_mode; 4714drop table t2, t1; 4715drop view v1; 4716# End of 5.0 tests. 4717create table t_out (subcase char(3), 4718a1 char(2), b1 char(2), c1 char(2)); 4719create table t_in (a2 char(2), b2 char(2), c2 char(2)); 4720insert into t_out values ('A.1','2a', NULL, '2a'); 4721insert into t_out values ('A.3', '2a', NULL, '2a'); 4722insert into t_out values ('A.4', '2a', NULL, 'xx'); 4723insert into t_out values ('B.1', '2a', '2a', '2a'); 4724insert into t_out values ('B.2', '2a', '2a', '2a'); 4725insert into t_out values ('B.3', '3a', 'xx', '3a'); 4726insert into t_out values ('B.4', 'xx', '3a', '3a'); 4727insert into t_in values ('1a', '1a', '1a'); 4728insert into t_in values ('2a', '2a', '2a'); 4729insert into t_in values (NULL, '2a', '2a'); 4730insert into t_in values ('3a', NULL, '3a'); 4731 4732Test general IN semantics (not top-level) 4733 4734case A.1 4735select subcase, 4736(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4737(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4738from t_out where subcase = 'A.1'; 4739subcase pred_in pred_not_in 4740A.1 0 1 4741case A.2 - impossible 4742case A.3 4743select subcase, 4744(a1, b1, c1) IN (select * from t_in) pred_in, 4745(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4746from t_out where subcase = 'A.3'; 4747subcase pred_in pred_not_in 4748A.3 NULL NULL 4749case A.4 4750select subcase, 4751(a1, b1, c1) IN (select * from t_in) pred_in, 4752(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4753from t_out where subcase = 'A.4'; 4754subcase pred_in pred_not_in 4755A.4 0 1 4756case B.1 4757select subcase, 4758(a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 4759(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 4760from t_out where subcase = 'B.1'; 4761subcase pred_in pred_not_in 4762B.1 0 1 4763case B.2 4764select subcase, 4765(a1, b1, c1) IN (select * from t_in) pred_in, 4766(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4767from t_out where subcase = 'B.2'; 4768subcase pred_in pred_not_in 4769B.2 1 0 4770case B.3 4771select subcase, 4772(a1, b1, c1) IN (select * from t_in) pred_in, 4773(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4774from t_out where subcase = 'B.3'; 4775subcase pred_in pred_not_in 4776B.3 NULL NULL 4777case B.4 4778select subcase, 4779(a1, b1, c1) IN (select * from t_in) pred_in, 4780(a1, b1, c1) NOT IN (select * from t_in) pred_not_in 4781from t_out where subcase = 'B.4'; 4782subcase pred_in pred_not_in 4783B.4 0 1 4784 4785Test IN as top-level predicate, and 4786as non-top level for cases A.3, B.3 (the only cases with NULL result). 4787 4788case A.1 4789select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4790where subcase = 'A.1' and 4791(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4792pred_in 4793F 4794select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4795where subcase = 'A.1' and 4796(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4797pred_not_in 4798T 4799select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4800where subcase = 'A.1' and 4801NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4802not_pred_in 4803T 4804case A.3 4805select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4806where subcase = 'A.3' and 4807(a1, b1, c1) IN (select * from t_in); 4808pred_in 4809F 4810select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4811where subcase = 'A.3' and 4812(a1, b1, c1) NOT IN (select * from t_in); 4813pred_not_in 4814F 4815select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4816where subcase = 'A.3' and 4817NOT((a1, b1, c1) IN (select * from t_in)); 4818not_pred_in 4819F 4820select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4821where subcase = 'A.3' and 4822((a1, b1, c1) IN (select * from t_in)) is NULL and 4823((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4824pred_in 4825N 4826case A.4 4827select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4828where subcase = 'A.4' and 4829(a1, b1, c1) IN (select * from t_in); 4830pred_in 4831F 4832select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4833where subcase = 'A.4' and 4834(a1, b1, c1) NOT IN (select * from t_in); 4835pred_not_in 4836T 4837select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4838where subcase = 'A.4' and 4839NOT((a1, b1, c1) IN (select * from t_in)); 4840not_pred_in 4841T 4842case B.1 4843select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4844where subcase = 'B.1' and 4845(a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 4846pred_in 4847F 4848select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4849where subcase = 'B.1' and 4850(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 4851pred_not_in 4852T 4853select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4854where subcase = 'B.1' and 4855NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 4856not_pred_in 4857T 4858case B.2 4859select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4860where subcase = 'B.2' and 4861(a1, b1, c1) IN (select * from t_in); 4862pred_in 4863T 4864select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4865where subcase = 'B.2' and 4866(a1, b1, c1) NOT IN (select * from t_in); 4867pred_not_in 4868F 4869select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4870where subcase = 'B.2' and 4871NOT((a1, b1, c1) IN (select * from t_in)); 4872not_pred_in 4873F 4874case B.3 4875select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4876where subcase = 'B.3' and 4877(a1, b1, c1) IN (select * from t_in); 4878pred_in 4879F 4880select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4881where subcase = 'B.3' and 4882(a1, b1, c1) NOT IN (select * from t_in); 4883pred_not_in 4884F 4885select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4886where subcase = 'B.3' and 4887NOT((a1, b1, c1) IN (select * from t_in)); 4888not_pred_in 4889F 4890select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 4891where subcase = 'B.3' and 4892((a1, b1, c1) IN (select * from t_in)) is NULL and 4893((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 4894pred_in 4895N 4896case B.4 4897select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 4898where subcase = 'B.4' and 4899(a1, b1, c1) IN (select * from t_in); 4900pred_in 4901F 4902select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 4903where subcase = 'B.4' and 4904(a1, b1, c1) NOT IN (select * from t_in); 4905pred_not_in 4906T 4907select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 4908where subcase = 'B.4' and 4909NOT((a1, b1, c1) IN (select * from t_in)); 4910not_pred_in 4911T 4912drop table t_out; 4913drop table t_in; 4914CREATE TABLE t1 (a INT, b INT); 4915INSERT INTO t1 VALUES (2,22),(1,11),(2,22); 4916SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4917a 49181 49192 4920SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4921a 4922SELECT a FROM t1 t0 4923WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4924a 49251 49262 4927SET @@sql_mode='ansi'; 4928SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a; 4929ERROR HY000: Invalid use of group function 4930SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a; 4931ERROR HY000: Invalid use of group function 4932SELECT a FROM t1 t0 4933WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a; 4934ERROR HY000: Invalid use of group function 4935SET @@sql_mode=default; 4936DROP TABLE t1; 4937CREATE TABLE t1 (s1 CHAR(1)); 4938INSERT INTO t1 VALUES ('a'); 4939SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1); 4940s1 4941a 4942DROP TABLE t1; 4943CREATE TABLE t1(c INT, KEY(c)); 4944CREATE TABLE t2(a INT, b INT); 4945INSERT INTO t2 VALUES (1, 10), (2, NULL); 4946INSERT INTO t1 VALUES (1), (3); 4947SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10); 4948a b 4949DROP TABLE t1,t2; 4950CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a)); 4951INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20); 4952CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a)); 4953INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100); 4954SELECT * FROM t1 4955WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b); 4956pk a 49571 10 4958DROP TABLE t1,t2; 4959CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b)); 4960INSERT INTO t1 VALUES (1,NULL), (9,NULL); 4961CREATE TABLE t2 ( 4962a INT, 4963b INT, 4964c INT, 4965d INT, 4966PRIMARY KEY (a), 4967UNIQUE KEY b (b,c,d), 4968KEY b_2 (b), 4969KEY c (c), 4970KEY d (d) 4971); 4972INSERT INTO t2 VALUES 4973(43, 2, 11 ,30), 4974(44, 2, 12 ,30), 4975(45, 1, 1 ,10000), 4976(46, 1, 2 ,10000), 4977(556,1, 32 ,10000); 4978CREATE TABLE t3 ( 4979a INT, 4980b INT, 4981c INT, 4982PRIMARY KEY (a), 4983UNIQUE KEY b (b,c), 4984KEY c (c), 4985KEY b_2 (b) 4986); 4987INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2); 4988explain 4989SELECT 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; 4990id select_type table type possible_keys key key_len ref rows Extra 49911 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index 49921 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index 49932 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index 4994SELECT 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; 4995a incorrect 49961 1 4997DROP TABLE t1,t2,t3; 4998CREATE TABLE t1 (id int); 4999CREATE TABLE t2 (id int, c int); 5000INSERT INTO t1 (id) VALUES (1); 5001INSERT INTO t2 (id) VALUES (1); 5002INSERT INTO t1 (id) VALUES (1); 5003INSERT INTO t2 (id) VALUES (1); 5004CREATE VIEW v1 AS 5005SELECT t2.c AS c FROM t1, t2 5006WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5007UPDATE v1 SET c=1; 5008CREATE VIEW v2 (a,b) AS 5009SELECT t2.id, t2.c AS c FROM t1, t2 5010WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5011INSERT INTO v2(a,b) VALUES (2,2); 5012ERROR 44000: CHECK OPTION failed `test`.`v2` 5013SELECT * FROM v1; 5014c 50151 50161 50171 50181 5019CREATE VIEW v3 AS 5020SELECT t2.c AS c FROM t2 5021WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION; 5022DELETE FROM v3; 5023DROP VIEW v1,v2,v3; 5024DROP TABLE t1,t2; 5025# 5026# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5027# 5028create table t1(id integer primary key, g integer, v integer, s char(1)); 5029create table t2(id integer primary key, g integer, v integer, s char(1)); 5030insert into t1 values 5031(10, 10, 10, 'l'), 5032(20, 20, 20, 'l'), 5033(40, 40, 40, 'l'), 5034(41, 40, null, 'l'), 5035(50, 50, 50, 'l'), 5036(51, 50, null, 'l'), 5037(60, 60, 60, 'l'), 5038(61, 60, null, 'l'), 5039(70, 70, 70, 'l'), 5040(90, 90, null, 'l'); 5041insert into t2 values 5042(10, 10, 10, 'r'), 5043(30, 30, 30, 'r'), 5044(50, 50, 50, 'r'), 5045(60, 60, 60, 'r'), 5046(61, 60, null, 'r'), 5047(70, 70, 70, 'r'), 5048(71, 70, null, 'r'), 5049(80, 80, 80, 'r'), 5050(81, 80, null, 'r'), 5051(100,100,null, 'r'); 5052select * 5053from t1 5054where v in(select v 5055from t2 5056where t1.g=t2.g) is unknown; 5057id g v s 505851 50 NULL l 505961 60 NULL l 5060drop table t1, t2; 5061# 5062# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result 5063# 5064create table t1(id integer primary key, g integer, v integer, s char(1)); 5065create table t2(id integer primary key, g integer, v integer, s char(1)); 5066insert into t1 values 5067(10, 10, 10, 'l'), 5068(20, 20, 20, 'l'), 5069(40, 40, 40, 'l'), 5070(41, 40, null, 'l'), 5071(50, 50, 50, 'l'), 5072(51, 50, null, 'l'), 5073(60, 60, 60, 'l'), 5074(61, 60, null, 'l'), 5075(70, 70, 70, 'l'), 5076(90, 90, null, 'l'); 5077insert into t2 values 5078(10, 10, 10, 'r'), 5079(30, 30, 30, 'r'), 5080(50, 50, 50, 'r'), 5081(60, 60, 60, 'r'), 5082(61, 60, null, 'r'), 5083(70, 70, 70, 'r'), 5084(71, 70, null, 'r'), 5085(80, 80, 80, 'r'), 5086(81, 80, null, 'r'), 5087(100,100,null, 'r'); 5088select * 5089from t1 5090where v in(select v 5091from t2 5092where t1.g=t2.g) is unknown; 5093id g v s 509451 50 NULL l 509561 60 NULL l 5096drop table t1, t2; 5097# 5098# Bug#33204: INTO is allowed in subselect, causing inconsistent results 5099# 5100CREATE TABLE t1( a INT ); 5101INSERT INTO t1 VALUES (1),(2); 5102CREATE TABLE t2( a INT, b INT ); 5103SELECT * 5104FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a; 5105ERROR 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 5106SELECT * 5107FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a; 5108ERROR 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 5109SELECT * 5110FROM (SELECT a INTO DUMPFILE 'file' 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 DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2 5112SELECT * FROM ( 5113SELECT 1 a 5114UNION 5115SELECT a INTO @var FROM t1 WHERE a = 2 5116) 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 @var FROM t1 WHERE a = 2 5118) t1a' at line 4 5119SELECT * FROM ( 5120SELECT 1 a 5121UNION 5122SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 5123) t1a; 5124ERROR 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 5125) t1a' at line 4 5126SELECT * FROM ( 5127SELECT 1 a 5128UNION 5129SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 5130) t1a; 5131ERROR 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 5132) t1a' at line 4 5133SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a; 5134a 51352 5136SELECT * FROM ( 5137SELECT a FROM t1 WHERE a = 2 5138UNION 5139SELECT a FROM t1 WHERE a = 2 5140) t1a; 5141a 51422 5143SELECT * FROM ( 5144SELECT 1 a 5145UNION 5146SELECT a FROM t1 WHERE a = 2 5147UNION 5148SELECT a FROM t1 WHERE a = 2 5149) t1a; 5150a 51511 51522 5153SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q; 5154a 51551 5156SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias; 5157a 51581 5159SELECT * FROM (SELECT 1 UNION SELECT 1) t1a; 51601 51611 5162SELECT * FROM ((SELECT 1 a INTO @a)) t1a; 5163ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1 5164SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a; 5165ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1 5166SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a; 5167ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1 5168SELECT * FROM (SELECT 1 a UNION (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 UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a; 5171ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1 5172SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE '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 OUTFILE '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 ORDER BY a) t1a; 5181a 51821 5183SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a; 5184a 51851 5186SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a; 5187a 51881 5189SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a; 5190a 51911 5192SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1; 5193a 1 51941 1 51952 1 5196SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1; 5197ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON 1' at line 1 5198SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1; 5199ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1) ON 1' at line 1 5200SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1; 5201ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1)) ON 1' at line 1 5202SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1; 5203ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1 5204SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1; 5205ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1 5206SELECT * FROM t1 JOIN (t1 t1a) ON 1; 5207a a 52081 1 52092 1 52101 2 52112 2 5212SELECT * FROM t1 JOIN ((t1 t1a)) ON 1; 5213a a 52141 1 52152 1 52161 2 52172 2 5218SELECT * FROM (t1 t1a); 5219a 52201 52212 5222SELECT * FROM ((t1 t1a)); 5223a 52241 52252 5226SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1; 5227a t1a 52281 1 52292 1 5230SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1; 5231a t1a 52321 1 52332 1 5234SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1; 5235a a 52361 1 52372 1 5238SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1; 5239a a 52401 1 52412 1 5242SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2; 5243ERROR 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 5244SELECT * FROM t1 WHERE a = ALL ( SELECT 1 ); 5245a 52461 5247SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 ); 5248a 52491 5250SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 ); 5251a 52521 5253SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a); 5254ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1 5255SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' ); 5256ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1 5257SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' ); 5258ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5259SELECT * FROM t1 WHERE a = ( SELECT 1 ); 5260a 52611 5262SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 ); 5263a 52641 5265SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a); 5266ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1 5267SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' ); 5268ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1 5269SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' ); 5270ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1 5271SELECT * FROM t1 WHERE a = ( SELECT 1 UNION 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 UNION 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 UNION 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 ( SELECT 1 INTO @v ); 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 @v )' at line 1 5279SELECT ( 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 ( 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 UNION 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 UNION 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 UNION 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 a FROM t1 WHERE a = 1 ), a FROM t1; 5290( SELECT a FROM t1 WHERE a = 1 ) a 52911 1 52921 2 5293SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1; 5294( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a 52951 1 52961 2 5297SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2); 5298a b 5299SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ); 53001 53011 5302( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 53031 53041 5305SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5306( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ) 53071 5308SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1; 5309ERROR 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 5310SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5311( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) 53121 5313SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1)); 5314((SELECT 1 UNION SELECT 1 UNION SELECT 1)) 53151 5316SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5317ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5318SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5319ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 5320SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a; 53211 53221 5323SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a; 53241 53251 5326SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5327a 53281 5329SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5330a 53311 5332SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5333a 53341 5335SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 5336a 53371 5338SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5339a 53401 5341SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5342a 53431 5344SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5345a 53461 5347SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 5348a 53491 5350SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5351a 53521 5353SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5354a 53551 5356SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5357a 53581 5359SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 5360a 53611 5362SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v ); 5363ERROR 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 5364SELECT EXISTS(SELECT 1+1); 5365EXISTS(SELECT 1+1) 53661 5367SELECT EXISTS(SELECT 1+1 INTO @test); 5368ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @test)' at line 1 5369SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v ); 5370ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1 5371SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v ); 5372ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1 5373SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v ); 5374ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1 5375DROP TABLE t1, t2; 5376CREATE TABLE t1 (a ENUM('rainbow')); 5377INSERT INTO t1 VALUES (),(),(),(),(); 5378SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID())); 53791 53801 5381DROP TABLE t1; 5382CREATE TABLE t1 (a LONGBLOB); 5383INSERT INTO t1 SET a = 'aaaa'; 5384INSERT INTO t1 SET a = 'aaaa'; 5385SELECT 1 FROM t1 GROUP BY 5386(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1); 53871 53881 5389DROP TABLE t1; 5390# 5391# Bug #49512 : subquery with aggregate function crash 5392# subselect_single_select_engine::exec() 5393CREATE TABLE t1(a INT); 5394INSERT INTO t1 VALUES(); 5395# should not crash 5396SELECT 1 FROM t1 WHERE a <> SOME 5397( 5398SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d 5399FROM t1,t1 a 5400); 54011 5402DROP TABLE t1; 5403# 5404# Bug #45989 take 2 : memory leak after explain encounters an 5405# error in the query 5406# 5407CREATE TABLE t1(a LONGTEXT); 5408INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet)); 5409INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet)); 5410EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1, 5411(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1 5412WHERE t1.a = d1.a; 5413ERROR 42S22: Unknown column 'd1.a' in 'where clause' 5414DROP TABLE t1; 5415Set up test tables. 5416CREATE TABLE t1 ( 5417t1_id INT UNSIGNED, 5418PRIMARY KEY(t1_id) 5419) Engine=MyISAM; 5420INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5); 5421CREATE TABLE t2 SELECT * FROM t1; 5422CREATE TABLE t3 ( 5423t3_id INT UNSIGNED AUTO_INCREMENT, 5424t1_id INT UNSIGNED, 5425amount DECIMAL(16,2), 5426PRIMARY KEY(t3_id), 5427KEY(t1_id) 5428) Engine=MyISAM; 5429INSERT INTO t3 (t1_id, t3_id, amount) 5430VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00); 5431This is the 'inner query' running by itself. 5432Produces correct results. 5433SELECT 5434t1.t1_id, 5435IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5436FROM 5437t1 5438LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5439GROUP BY 5440t1.t1_id 5441; 5442t1_id total_amount 54431 100.00 54442 200.00 54453 0.00 54464 400.00 54475 0.00 5448SELECT * FROM (the same inner query) 5449Produces correct results. 5450SELECT * FROM ( 5451SELECT 5452t1.t1_id, 5453IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5454FROM 5455t1 5456LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5457GROUP BY 5458t1.t1_id 5459) AS t; 5460t1_id total_amount 54611 100.00 54622 200.00 54633 0.00 54644 400.00 54655 0.00 5466Now make t2.t1_id part of a key. 5467ALTER TABLE t2 ADD PRIMARY KEY(t1_id); 5468Same inner query by itself. 5469Still correct results. 5470SELECT 5471t1.t1_id, 5472IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5473FROM 5474t1 5475LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5476GROUP BY 5477t1.t1_id; 5478t1_id total_amount 54791 100.00 54802 200.00 54813 0.00 54824 400.00 54835 0.00 5484SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN 5485SELECT * FROM ( 5486SELECT 5487t1.t1_id, 5488IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount 5489FROM 5490t1 5491LEFT JOIN t2 ON t2.t1_id=t1.t1_id 5492GROUP BY 5493t1.t1_id 5494) AS t; 5495t1_id total_amount 54961 100.00 54972 200.00 54983 0.00 54994 400.00 55005 0.00 5501DROP TABLE t3; 5502DROP TABLE t2; 5503DROP TABLE t1; 5504# 5505# Bug #52711: Segfault when doing EXPLAIN SELECT with 5506# union...order by (select... where...) 5507# 5508CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a)); 5509INSERT INTO t1 VALUES (1),(2); 5510CREATE TABLE t2 (b INT); 5511INSERT INTO t2 VALUES (1),(2); 5512# Should not crash 5513EXPLAIN 5514SELECT * FROM t2 UNION SELECT * FROM t2 5515ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5516# Should not crash 5517SELECT * FROM t2 UNION SELECT * FROM t2 5518ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE)); 5519DROP TABLE t1,t2; 5520# 5521# Bug #58818: Incorrect result for IN/ANY subquery 5522# with HAVING condition 5523# 5524CREATE TABLE t1(i INT); 5525INSERT INTO t1 VALUES (1), (2), (3); 5526CREATE TABLE t1s(i INT); 5527INSERT INTO t1s VALUES (10), (20), (30); 5528CREATE TABLE t2s(i INT); 5529INSERT INTO t2s VALUES (100), (200), (300); 5530SELECT * FROM t1 5531WHERE t1.i NOT IN 5532( 5533SELECT STRAIGHT_JOIN t2s.i 5534FROM 5535t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5536HAVING t2s.i = 999 5537); 5538i 55391 55402 55413 5542SELECT * FROM t1 5543WHERE t1.I IN 5544( 5545SELECT STRAIGHT_JOIN t2s.i 5546FROM 5547t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5548HAVING t2s.i = 999 5549) IS UNKNOWN; 5550i 5551SELECT * FROM t1 5552WHERE NOT t1.I = ANY 5553( 5554SELECT STRAIGHT_JOIN t2s.i 5555FROM 5556t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5557HAVING t2s.i = 999 5558); 5559i 55601 55612 55623 5563SELECT * FROM t1 5564WHERE t1.i = ANY ( 5565SELECT STRAIGHT_JOIN t2s.i 5566FROM 5567t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 5568HAVING t2s.i = 999 5569) IS UNKNOWN; 5570i 5571DROP TABLE t1,t1s,t2s; 5572# LP BUG#675248 - select->prep_where references on freed memory 5573CREATE TABLE t1 (a int, b int); 5574insert into t1 values (1,1),(0,0); 5575CREATE TABLE t2 (c int); 5576insert into t2 values (1),(2); 5577prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub 5578from t1 as table1 group by sub"; 5579execute stmt1; 5580sum(a) sub 55810 NULL 55821 3 5583deallocate prepare stmt1; 5584prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub 5585from t1 as table1"; 5586execute stmt1; 5587sum(a) sub 55881 3 5589deallocate prepare stmt1; 5590drop table t1,t2; 5591# 5592# Bug LP#693935/#58727: Assertion failure with 5593# a single row subquery returning more than one row 5594# 5595create table t1 (a char(1) charset utf8); 5596insert into t1 values ('a'), ('b'); 5597create table t2 (a binary(1)); 5598insert into t2 values ('x'), ('y'); 5599select * from t2 where a=(select a from t1) and a='x'; 5600ERROR 21000: Subquery returns more than 1 row 5601drop table t1,t2; 5602# End of 5.1 tests 5603# 5604# Bug #11765713 58705: 5605# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES 5606# CREATED BY OPT_SUM_QUERY 5607# 5608CREATE TABLE t1(a INT NOT NULL, KEY (a)); 5609INSERT INTO t1 VALUES (0), (1); 5610SELECT 1 as foo FROM t1 WHERE a < SOME 5611(SELECT a FROM t1 WHERE a <=> 5612(SELECT a FROM t1) 5613); 5614ERROR 21000: Subquery returns more than 1 row 5615SELECT 1 as foo FROM t1 WHERE a < SOME 5616(SELECT a FROM t1 WHERE a <=> 5617(SELECT a FROM t1 where a is null) 5618); 5619foo 5620DROP TABLE t1; 5621# 5622# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool): 5623# Assertion `file' failed. 5624# 5625CREATE TABLE t1 (a INT); 5626SELECT 1 FROM 5627(SELECT ROW( 5628(SELECT 1 FROM t1 RIGHT JOIN 5629(SELECT 1 FROM t1, t1 t2) AS d ON 1), 56301) FROM t1) AS e; 5631ERROR 21000: Operand should contain 1 column(s) 5632DROP TABLE t1; 5633# 5634# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY 5635# 5636CREATE TABLE t1(a TIME NOT NULL); 5637INSERT INTO t1 VALUES ('00:00:32'); 5638SELECT 1 FROM t1 WHERE a > 5639(SELECT timestamp(a) AS a FROM t1); 56401 5641DROP TABLE t1; 5642# 5643# No BUG#, a case brought from 5.2's innodb_mysql_lock.test 5644# 5645create table t1 (i int not null primary key); 5646insert into t1 values (1),(2),(3),(4),(5); 5647create table t2 (j int not null primary key); 5648insert into t2 values (1),(2),(3),(4),(5); 5649create table t3 (k int not null primary key); 5650insert into t3 values (1),(2),(3); 5651create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1); 5652select * from t3 where k in (select j from v2); 5653k 56541 56552 56563 5657drop table t1,t2,t3; 5658drop view v2; 5659# 5660# Bug#52068: Optimizer generates invalid semijoin materialization plan 5661# 5662drop table if exists ot1, ot2, it1, it2; 5663CREATE TABLE ot1(a INTEGER); 5664INSERT INTO ot1 VALUES(5), (8); 5665CREATE TABLE it2(a INTEGER); 5666INSERT INTO it2 VALUES(9), (5), (1), (8); 5667CREATE TABLE it3(a INTEGER); 5668INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4); 5669CREATE TABLE ot4(a INTEGER); 5670INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1); 5671SELECT * FROM ot1,ot4 5672WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5673FROM it2,it3); 5674a a 56755 1 56768 1 56775 5 56788 5 56795 7 56808 7 56815 7 56828 7 56835 1 56848 1 5685explain SELECT * FROM ot1,ot4 5686WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a 5687FROM it2,it3); 5688id select_type table type possible_keys key key_len ref rows Extra 56891 PRIMARY ot1 ALL NULL NULL NULL NULL 2 56901 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using join buffer (flat, BNL join) 56911 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 56922 MATERIALIZED it2 ALL NULL NULL NULL NULL 4 56932 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join) 5694DROP TABLE IF EXISTS ot1, ot4, it2, it3; 5695# 5696# Bug#729039: NULL keys used to evaluate subquery 5697# 5698CREATE TABLE t1 (a int) ; 5699INSERT INTO t1 VALUES (NULL), (1), (NULL), (2); 5700CREATE TABLE t2 (a int, INDEX idx(a)) ; 5701INSERT INTO t2 VALUES (NULL), (1), (NULL); 5702SELECT * FROM t1 5703WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a); 5704a 57051 5706EXPLAIN 5707SELECT * FROM t1 5708WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a); 5709id select_type table type possible_keys key key_len ref rows Extra 57101 PRIMARY t1 ALL NULL NULL NULL NULL 4 57111 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 57122 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 5713SELECT * FROM t1 5714WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5715a 57161 5717EXPLAIN 5718SELECT * FROM t1 5719WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a); 5720id select_type table type possible_keys key key_len ref rows Extra 57211 PRIMARY t1 ALL NULL NULL NULL NULL 4 57221 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 57232 MATERIALIZED t2 index idx idx 5 NULL 3 Using index 5724DROP TABLE t1,t2; 5725# 5726# BUG#752992: Wrong results for a subquery with 'semijoin=on' 5727# 5728CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); 5729INSERT INTO t1 VALUES (11,0); 5730INSERT INTO t1 VALUES (12,5); 5731INSERT INTO t1 VALUES (15,0); 5732CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL); 5733INSERT INTO t2 VALUES (11,1); 5734INSERT INTO t2 VALUES (12,2); 5735INSERT INTO t2 VALUES (15,4); 5736SET @save_join_cache_level=@@join_cache_level; 5737SET join_cache_level=0; 5738EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); 5739id select_type table type possible_keys key key_len ref rows Extra 57401 PRIMARY t1 ALL PRIMARY NULL NULL NULL 3 57411 PRIMARY it eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 Using index 57421 PRIMARY t2 index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(it) 5743SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1); 5744pk i 574511 0 574612 5 574715 0 5748SET join_cache_level=@save_join_cache_level; 5749DROP table t1,t2; 5750# 5751# Bug#751350: crash with pushed condition for outer references when 5752# there should be none of such conditions 5753# 5754CREATE TABLE t1 (a int, b int) ; 5755INSERT INTO t1 VALUES (0,0),(0,0); 5756set @optimizer_switch_save=@@optimizer_switch; 5757set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on'; 5758EXPLAIN 5759SELECT b FROM t1 5760WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) 5761GROUP BY b; 5762id select_type table type possible_keys key key_len ref rows Extra 57631 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 57642 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 5765SELECT b FROM t1 5766WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a ) 5767GROUP BY b; 5768b 57690 5770set @@optimizer_switch=@optimizer_switch_save; 5771DROP TABLE t1; 5772# 5773# Bug #11765713 58705: 5774# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES 5775# CREATED BY OPT_SUM_QUERY 5776# 5777CREATE TABLE t1(a INT NOT NULL, KEY (a)); 5778INSERT INTO t1 VALUES (0), (1); 5779SELECT 1 as foo FROM t1 WHERE a < SOME 5780(SELECT a FROM t1 WHERE a <=> 5781(SELECT a FROM t1) 5782); 5783ERROR 21000: Subquery returns more than 1 row 5784SELECT 1 as foo FROM t1 WHERE a < SOME 5785(SELECT a FROM t1 WHERE a <=> 5786(SELECT a FROM t1 where a is null) 5787); 5788foo 5789DROP TABLE t1; 5790CREATE TABLE t1 (a int(11), b varchar(1)); 5791INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); 5792SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); 5793a 57945 5795SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); 5796a 57975 5798SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); 5799a 58007 5801SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); 5802a 58037 5804SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); 5805a 58065 58077 5808SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); 5809a 58105 58117 5812SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); 5813a 58145 58157 5816SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 ); 5817a 58185 58197 5820SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 ); 5821a 58225 58237 5824SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); 5825a 58265 58277 5828SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); 5829a 58305 58317 5832SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); 5833a 58345 58357 5836SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b ); 5837a 5838SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 ); 5839a 5840SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b ); 5841a 5842SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 ); 5843a 5844SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b ); 5845a 5846SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 ); 5847a 5848SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b ); 5849a 5850SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); 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 GROUP BY b ); 5859a 5860delete from t1; 5861INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g'); 5862SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b ); 5863a 58645 5865SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 ); 5866a 58675 5868SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b ); 5869a 58707 5871SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 ); 5872a 58737 5874SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b ); 5875a 58765 58777 5878SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 ); 5879a 58805 58817 5882SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b ); 5883a 58845 58857 5886SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 ); 5887a 58885 58897 5890SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 ); 5891a 58925 58937 5894SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b ); 5895a 58965 58977 5898SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 ); 5899a 59005 59017 5902SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b ); 5903a 59045 59057 5906SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b ); 5907a 5908SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 ); 5909a 5910SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b ); 5911a 5912SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 ); 5913a 5914SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b ); 5915a 5916SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 ); 5917a 5918SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b ); 5919a 5920SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 ); 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 GROUP BY b ); 5929a 5930drop table t1; 5931# 5932# Fix of lp:780386 (NULL left part with empty ALL subquery). 5933# 5934CREATE TABLE t1 ( f11 int) ; 5935INSERT IGNORE INTO t1 VALUES (0),(0); 5936CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ; 5937INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0); 5938DROP TABLE IF EXISTS t3; 5939Warnings: 5940Note 1051 Unknown table 'test.t3' 5941CREATE TABLE t3 ( f3 int) ; 5942INSERT INTO t3 VALUES (0),(0); 5943SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ; 5944r 5945NULL 59465 5947NULL 59485 5949DROP TABLE t1, t2, t3; 5950# 5951# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER 5952# 5953CREATE TABLE t1(a1 int); 5954INSERT INTO t1 VALUES (1),(2); 5955CREATE TABLE t2(a1 int); 5956INSERT INTO t2 VALUES (3); 5957SELECT @@session.sql_mode INTO @old_sql_mode; 5958SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 5959SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2); 59601 59611 59621 5963SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2); 59641 59651 59661 5967SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2); 59681 5969Warnings: 5970Warning 1292 Truncated incorrect DOUBLE value: 'a' 5971SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2); 59721 59731 59741 5975SET SESSION sql_mode=@old_sql_mode; 5976DROP TABLE t1, t2; 5977create table t2(i int); 5978insert into t2 values(0); 5979SELECT @@session.sql_mode INTO @old_sql_mode; 5980SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 5981CREATE VIEW v1 AS 5982SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2 5983; 5984CREATE TABLE t1 ( 5985pk int NOT NULL, 5986col_varchar_key varchar(1) DEFAULT NULL, 5987PRIMARY KEY (pk), 5988KEY col_varchar_key (col_varchar_key) 5989); 5990SELECT t1.pk 5991FROM t1 5992WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 ) 5993; 5994pk 5995SET SESSION sql_mode=@old_sql_mode; 5996drop table t2, t1; 5997drop view v1; 5998# 5999# BUG#50257: Missing info in REF column of the EXPLAIN 6000# lines for subselects 6001# 6002CREATE TABLE t1 (a INT, b INT, INDEX (a)); 6003INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); 6004EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; 6005id select_type table type possible_keys key key_len ref rows Extra 60061 SIMPLE t1 ref a a 5 const 1 6007EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); 6008id select_type table type possible_keys key key_len ref rows Extra 60091 PRIMARY t1 ALL NULL NULL NULL NULL 4 60102 SUBQUERY t1 ref a a 5 const 1 Using index 6011DROP TABLE t1; 6012# 6013# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) 6014# (duplicate of LP bug #888456) 6015# 6016CREATE TABLE t1 (f1 varchar(1)); 6017INSERT INTO t1 VALUES ('v'),('s'); 6018CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); 6019INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), 6020('d'),('y'),('t'),('d'),('s'); 6021EXPLAIN 6022SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 6023WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 6024WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); 6025id select_type table type possible_keys key key_len ref rows Extra 60261 PRIMARY table1 ALL NULL NULL NULL NULL 2 60271 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) 60282 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index 6029SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2 6030WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 6031WHERE f1_key != table2.f1_key AND f1_key >= table1.f1); 6032f1 f1_key 6033s c 6034s d 6035s d 6036s d 6037s j 6038s m 6039s s 6040s t 6041s v 6042s y 6043v c 6044v d 6045v d 6046v d 6047v j 6048v m 6049v s 6050v t 6051v v 6052v y 6053DROP TABLE t1,t2; 6054# 6055# LP bug 919427: EXPLAIN for a query over a single-row table 6056# with IN subquery in WHERE condition 6057# 6058CREATE TABLE ot ( 6059col_int_nokey int(11), 6060col_varchar_nokey varchar(1) 6061) ; 6062INSERT INTO ot VALUES (1,'x'); 6063CREATE TABLE it1( 6064col_int_key int(11), 6065col_varchar_key varchar(1), 6066KEY idx_cvk_cik (col_varchar_key,col_int_key) 6067); 6068INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f'); 6069CREATE TABLE it2 ( 6070col_int_key int(11), 6071col_varchar_key varchar(1), 6072col_varchar_key2 varchar(1), 6073KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key), 6074KEY idx_cvk_cik (col_varchar_key, col_int_key) 6075); 6076INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f'); 6077EXPLAIN 6078SELECT col_int_nokey FROM ot 6079WHERE col_varchar_nokey IN 6080(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); 6081id select_type table type possible_keys key key_len ref rows Extra 60821 PRIMARY ot system NULL NULL NULL NULL 1 60831 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 60842 MATERIALIZED it1 ref idx_cvk_cik idx_cvk_cik 9 const,const 1 Using where; Using index 6085SELECT col_int_nokey FROM ot 6086WHERE col_varchar_nokey IN 6087(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL); 6088col_int_nokey 60891 6090EXPLAIN 6091SELECT col_int_nokey FROM ot 6092WHERE (col_varchar_nokey, 'x') IN 6093(SELECT col_varchar_key, col_varchar_key2 FROM it2); 6094id select_type table type possible_keys key key_len ref rows Extra 60951 PRIMARY ot system NULL NULL NULL NULL 1 60961 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 60972 MATERIALIZED it2 ref idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 const,const 1 Using where; Using index 6098SELECT col_int_nokey FROM ot 6099WHERE (col_varchar_nokey, 'x') IN 6100(SELECT col_varchar_key, col_varchar_key2 FROM it2); 6101col_int_nokey 61021 6103DROP TABLE ot,it1,it2; 6104# 6105# MDEV-746 6106# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY 6107# HAS AN EMPTY RESULT 6108# 6109CREATE TABLE t1 ( 6110pk int NOT NULL, 6111col_int_nokey int NOT NULL, 6112col_int_key int NOT NULL, 6113col_time_key time NOT NULL, 6114col_varchar_key varchar(1) NOT NULL, 6115col_varchar_nokey varchar(1) NOT NULL, 6116PRIMARY KEY (pk), 6117KEY col_int_key (col_int_key), 6118KEY col_time_key (col_time_key), 6119KEY col_varchar_key (col_varchar_key,col_int_key) 6120) ENGINE=MyISAM; 6121CREATE TABLE t2 ( 6122pk int NOT NULL AUTO_INCREMENT, 6123col_int_nokey int NOT NULL, 6124col_int_key int NOT NULL, 6125col_time_key time NOT NULL, 6126col_varchar_key varchar(1) NOT NULL, 6127col_varchar_nokey varchar(1) NOT NULL, 6128PRIMARY KEY (pk), 6129KEY col_int_key (col_int_key), 6130KEY col_time_key (col_time_key), 6131KEY col_varchar_key (col_varchar_key,col_int_key) 6132) ENGINE=MyISAM; 6133INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b'); 6134SET @var2:=4, @var3:=8; 6135 6136Testcase without inner subquery 6137EXPLAIN SELECT @var3:=12, sq4_alias1.* 6138FROM t1 AS sq4_alias1 6139WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6140sq4_alias1.col_varchar_key = @var3; 6141id select_type table type possible_keys key key_len ref rows Extra 61421 SIMPLE sq4_alias1 system NULL NULL NULL NULL 0 Const row not found 6143SELECT @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; 6147@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6148SELECT @var3; 6149@var3 61508 6151EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* 6152FROM t1 AS sq4_alias1 6153WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6154sq4_alias1.col_varchar_key = @var3 ) AS alias3; 6155id select_type table type possible_keys key key_len ref rows Extra 61561 PRIMARY <derived2> system NULL NULL NULL NULL 0 Const row not found 61572 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table 6158SELECT * FROM ( SELECT @var3:=12, sq4_alias1.* 6159FROM t1 AS sq4_alias1 6160WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR 6161sq4_alias1.col_varchar_key = @var3 ) AS alias3; 6162@var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6163SELECT @var3; 6164@var3 61658 6166 6167Testcase with inner subquery; crashed WL#6095 6168SET @var3=8; 6169EXPLAIN SELECT sq4_alias1.* 6170FROM t1 AS sq4_alias1 6171WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6172NOT IN 6173(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6174c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6175FROM t2 AS c_sq1_alias1 6176WHERE (c_sq1_alias1.col_int_nokey != @var2 6177OR c_sq1_alias1.pk != @var3)); 6178id select_type table type possible_keys key key_len ref rows Extra 61791 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 61802 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 6181SELECT sq4_alias1.* 6182FROM t1 AS sq4_alias1 6183WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6184NOT IN 6185(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6186c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6187FROM t2 AS c_sq1_alias1 6188WHERE (c_sq1_alias1.col_int_nokey != @var2 6189OR c_sq1_alias1.pk != @var3)); 6190pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6191EXPLAIN SELECT * FROM ( SELECT sq4_alias1.* 6192FROM t1 AS sq4_alias1 6193WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6194NOT IN 6195(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6196c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6197FROM t2 AS c_sq1_alias1 6198WHERE (c_sq1_alias1.col_int_nokey != @var2 6199OR c_sq1_alias1.pk != @var3)) ) AS alias3; 6200id select_type table type possible_keys key key_len ref rows Extra 62011 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 62023 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1 6203SELECT * FROM ( SELECT sq4_alias1.* 6204FROM t1 AS sq4_alias1 6205WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey) 6206NOT IN 6207(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1, 6208c_sq1_alias1.col_varchar_nokey AS c_sq1_field2 6209FROM t2 AS c_sq1_alias1 6210WHERE (c_sq1_alias1.col_int_nokey != @var2 6211OR c_sq1_alias1.pk != @var3)) ) AS alias3; 6212pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey 6213DROP TABLE t1,t2; 6214# End of 5.2 tests 6215# 6216# BUG#779885: Crash in eliminate_item_equal with materialization=on in 6217# 6218CREATE TABLE t1 ( f1 int ); 6219INSERT INTO t1 VALUES (19), (20); 6220CREATE TABLE t2 ( f10 varchar(32) ); 6221INSERT INTO t2 VALUES ('c'),('d'); 6222CREATE TABLE t3 ( f10 varchar(32) ); 6223INSERT INTO t3 VALUES ('a'),('b'); 6224SELECT * 6225FROM t1 6226WHERE 6227( 't' ) IN ( 6228SELECT t3.f10 6229FROM t3 6230JOIN t2 6231ON t2.f10 = t3.f10 6232); 6233f1 6234DROP TABLE t1,t2,t3; 6235# 6236# BUG lp:813473: Wrong result with outer join + NOT IN subquery 6237# This bug is a duplicate of Bug#11764086 whose test case is added below 6238# 6239CREATE TABLE t1 (c int) ; 6240INSERT INTO t1 VALUES (5),(6); 6241CREATE TABLE t2 (a int, b int) ; 6242INSERT INTO t2 VALUES (20,9),(20,9); 6243create table t3 (d int, e int); 6244insert into t3 values (2, 9), (3,10); 6245SET @save_optimizer_switch=@@optimizer_switch; 6246SET optimizer_switch='outer_join_with_cache=off'; 6247EXPLAIN 6248SELECT t2.b , t1.c 6249FROM t2 LEFT JOIN t1 ON t1.c < 3 6250WHERE (t2.b , t1.c) NOT IN (SELECT * from t3); 6251id select_type table type possible_keys key key_len ref rows Extra 62521 PRIMARY t2 ALL NULL NULL NULL NULL 2 62531 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 62542 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where 6255SELECT t2.b , t1.c 6256FROM t2 LEFT JOIN t1 ON t1.c < 3 6257WHERE (t2.b, t1.c) NOT IN (SELECT * from t3); 6258b c 62599 NULL 62609 NULL 6261SET optimizer_switch=@save_optimizer_switch; 6262drop table t1, t2, t3; 6263# 6264# BUG#50257: Missing info in REF column of the EXPLAIN 6265# lines for subselects 6266# 6267CREATE TABLE t1 (a INT, b INT, INDEX (a)); 6268INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20); 6269 6270set @tmp_optimizer_switch=@@optimizer_switch; 6271set optimizer_switch='derived_merge=off,derived_with_keys=off'; 6272EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t; 6273id select_type table type possible_keys key key_len ref rows Extra 62741 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 62752 DERIVED t1 ref a a 5 const 1 6276set optimizer_switch=@tmp_optimizer_switch; 6277 6278EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7); 6279id select_type table type possible_keys key key_len ref rows Extra 62801 PRIMARY t1 ALL NULL NULL NULL NULL 4 62812 SUBQUERY t1 ref a a 5 const 1 Using index 6282 6283DROP TABLE t1; 6284# 6285# Bug#11764086: Null left operand to NOT IN in WHERE clause 6286# behaves differently than real NULL 6287# 6288CREATE TABLE parent (id int); 6289INSERT INTO parent VALUES (1), (2); 6290CREATE TABLE child (parent_id int, other int); 6291INSERT INTO child VALUES (1,NULL); 6292# Offending query (c.parent_id is NULL for null-complemented rows only) 6293SELECT p.id, c.parent_id 6294FROM parent p 6295LEFT JOIN child c 6296ON p.id = c.parent_id 6297WHERE c.parent_id NOT IN ( 6298SELECT parent_id 6299FROM child 6300WHERE parent_id = 3 6301); 6302id parent_id 63031 1 63042 NULL 6305# Some syntactic variations with IS FALSE and IS NOT TRUE 6306SELECT p.id, c.parent_id 6307FROM parent p 6308LEFT JOIN child c 6309ON p.id = c.parent_id 6310WHERE c.parent_id IN ( 6311SELECT parent_id 6312FROM child 6313WHERE parent_id = 3 6314) IS NOT TRUE; 6315id parent_id 63161 1 63172 NULL 6318SELECT p.id, c.parent_id 6319FROM parent p 6320LEFT JOIN child c 6321ON p.id = c.parent_id 6322WHERE c.parent_id IN ( 6323SELECT parent_id 6324FROM child 6325WHERE parent_id = 3 6326) IS FALSE; 6327id parent_id 63281 1 63292 NULL 6330DROP TABLE parent, child; 6331# End of test for bug#11764086. 6332# 6333# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET || 6334# BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX 6335# 6336CREATE TABLE t1(a INT); 6337INSERT INTO t1 VALUES (0), (1); 6338CREATE TABLE t2( 6339b TEXT, 6340c INT, 6341PRIMARY KEY (b(1)) 6342); 6343INSERT INTO t2 VALUES ('a', 2), ('b', 3); 6344SELECT 1 FROM t1 WHERE a = 6345(SELECT 1 FROM t2 WHERE b = 6346(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) 6347ORDER BY b 6348); 63491 6350Warnings: 6351Warning 1292 Truncated incorrect DOUBLE value: 'a' 6352Warning 1292 Truncated incorrect DOUBLE value: 'b' 6353Warning 1292 Truncated incorrect DOUBLE value: 'a' 6354Warning 1292 Truncated incorrect DOUBLE value: 'b' 6355SELECT 1 FROM t1 WHERE a = 6356(SELECT 1 FROM t2 WHERE b = 6357(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2) 6358GROUP BY b 6359); 63601 6361Warnings: 6362Warning 1292 Truncated incorrect DOUBLE value: 'a' 6363Warning 1292 Truncated incorrect DOUBLE value: 'b' 6364Warning 1292 Truncated incorrect DOUBLE value: 'a' 6365Warning 1292 Truncated incorrect DOUBLE value: 'b' 6366DROP TABLE t1, t2; 6367# 6368# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS) 6369# 6370CREATE TABLE t1 (f1 varchar(1)); 6371INSERT INTO t1 VALUES ('v'),('s'); 6372CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key)); 6373INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'), 6374('d'),('y'),('t'),('d'),('s'); 6375SELECT table1.f1, table2.f1_key 6376FROM t1 AS table1, t2 AS table2 6377WHERE EXISTS 6378( 6379SELECT DISTINCT f1_key 6380FROM t2 6381WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); 6382f1 f1_key 6383s c 6384s d 6385s d 6386s d 6387s j 6388s m 6389s s 6390s t 6391s v 6392s y 6393v c 6394v d 6395v d 6396v d 6397v j 6398v m 6399v s 6400v t 6401v v 6402v y 6403explain SELECT table1.f1, table2.f1_key 6404FROM t1 AS table1, t2 AS table2 6405WHERE EXISTS 6406( 6407SELECT DISTINCT f1_key 6408FROM t2 6409WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 ); 6410id select_type table type possible_keys key key_len ref rows Extra 64111 PRIMARY table1 ALL NULL NULL NULL NULL 2 64121 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join) 64132 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index 6414DROP TABLE t1,t2; 6415# 6416# lp:826279: assertion failure with GROUP BY a result of subquery 6417# 6418CREATE TABLE t1 (a int); 6419INSERT INTO t1 VALUES (0), (0); 6420CREATE TABLE t2 (a int, b int, c int); 6421INSERT INTO t2 VALUES (10,7,0), (0,7,0); 6422CREATE TABLE t3 (a int, b int); 6423INSERT INTO t3 VALUES (10,7), (0,7); 6424SELECT SUM(DISTINCT b), 6425(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 6426WHERE t.a != 0 AND t2.a != 0) 6427FROM (SELECT * FROM t3) AS t 6428GROUP BY 2; 6429SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0 6430WHERE t.a != 0 AND t2.a != 0) 64317 NULL 6432SELECT SUM(DISTINCT b), 6433(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) 6434FROM (SELECT * FROM t3) AS t 6435GROUP BY 2; 6436SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1) 64377 NULL 64387 10 6439DROP TABLE t1,t2,t3; 6440# 6441# Bug#12329653 6442# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY 6443# 6444CREATE TABLE t1(a1 int); 6445INSERT INTO t1 VALUES (1),(2); 6446SELECT @@session.sql_mode INTO @old_sql_mode; 6447SET SESSION sql_mode='ONLY_FULL_GROUP_BY'; 6448SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1); 64491 64501 64511 6452PREPARE stmt FROM 6453'SELECT 1 UNION ALL 6454SELECT 1 FROM t1 6455ORDER BY 6456(SELECT 1 FROM t1 AS t1_0 6457 WHERE 1 < SOME (SELECT a1 FROM t1) 6458)' ; 6459EXECUTE stmt ; 6460ERROR 21000: Subquery returns more than 1 row 6461EXECUTE stmt ; 6462ERROR 21000: Subquery returns more than 1 row 6463SET SESSION sql_mode=@old_sql_mode; 6464DEALLOCATE PREPARE stmt; 6465DROP TABLE t1; 6466# 6467# LP BUG#833777 Performance regression with deeply nested subqueries 6468# 6469create table t1 (a int not null, b char(10) not null); 6470insert into t1 values (1, 'a'); 6471set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off'; 6472select 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))))))))))))))))))))))))))))); 6473a 64741 6475set @@optimizer_switch=@subselect_tmp; 6476drop table t1; 6477# 6478# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF 6479# 6480CREATE TABLE t1 (a varchar(3)); 6481INSERT INTO t1 VALUES ('AAA'),('BBB'); 6482CREATE TABLE t2 (a varchar(3)); 6483INSERT INTO t2 VALUES ('CCC'); 6484set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; 6485SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ'); 6486a 6487set @@optimizer_switch=@subselect_tmp; 6488drop table t1, t2; 6489# 6490# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize 6491# with view , UNION and prepared statement (rewriting fake_select 6492# condition). 6493# 6494CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ; 6495INSERT INTO t1 VALUES (6,'d'),(7,'y'); 6496CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ; 6497INSERT INTO t2 VALUES (10,7); 6498CREATE VIEW v2 AS SELECT * FROM t2; 6499PREPARE st1 FROM " 6500 SELECT * 6501 FROM t1 6502 LEFT JOIN v2 ON ( v2.f2 = t1.f1 ) 6503 WHERE v2.f1 NOT IN ( 6504 SELECT 1 UNION 6505 SELECT 247 6506 ) 6507"; 6508EXECUTE st1; 6509f1 f4 f1 f2 65107 y 10 7 6511deallocate prepare st1; 6512DROP VIEW v2; 6513DROP TABLE t1,t2; 6514# 6515# LP bug #887458 Crash in subselect_union_engine::no_rows with 6516# double UNION and join_cache_level=3,8 6517# (IN/ALL/ANY optimizations should not be applied to fake_select) 6518CREATE TABLE t2 ( a int, b varchar(1)) ; 6519INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y'); 6520CREATE TABLE t1 ( b varchar(1)) ; 6521INSERT IGNORE INTO t1 VALUES (NULL),(NULL); 6522set @save_join_cache_level=@@join_cache_level; 6523SET SESSION join_cache_level=3; 6524SELECT * 6525FROM t1, t2 6526WHERE t2.b IN ( 6527SELECT 'm' UNION 6528SELECT 'm' 6529) OR t1.b <> SOME ( 6530SELECT 'v' UNION 6531SELECT 't' 6532); 6533b a b 6534set @@join_cache_level= @save_join_cache_level; 6535drop table t1,t2; 6536# 6537# LP bug #885162 Got error 124 from storage engine with UNION inside 6538# subquery and join_cache_level=3..8 6539# (IN/ALL/ANY optimizations should not be applied to fake_select) 6540# 6541CREATE TABLE t1 ( 6542f1 varchar(1) DEFAULT NULL 6543); 6544INSERT INTO t1 VALUES ('c'); 6545set @save_join_cache_level=@@join_cache_level; 6546SET SESSION join_cache_level=8; 6547SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' ); 6548f1 6549set @@join_cache_level= @save_join_cache_level; 6550drop table t1; 6551# 6552# LP BUG#747278 incorrect values of the NULL (no rows) single 6553# row subquery requested via element_index() interface 6554# 6555CREATE TABLE t1 (f1a int, f1b int) ; 6556INSERT IGNORE INTO t1 VALUES (1,1),(2,2); 6557CREATE TABLE t2 ( f2 int); 6558INSERT IGNORE INTO t2 VALUES (3),(4); 6559CREATE TABLE t3 (f3a int default 1, f3b int default 2); 6560INSERT INTO t3 VALUES (1,1),(2,2); 6561set @old_optimizer_switch = @@session.optimizer_switch; 6562set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off'; 6563SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; 6564(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) 6565NULL 6566NULL 6567SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; 6568(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) 6569NULL 6570NULL 6571SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; 6572(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6573NULL 6574NULL 6575SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); 6576(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6577NULL 6578SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; 6579(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) 6580NULL 6581NULL 6582SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; 6583(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) 6584NULL 6585NULL 6586SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; 6587(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6588NULL 6589NULL 6590SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); 6591(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6592NULL 6593set @@session.optimizer_switch=@old_optimizer_switch; 6594SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2; 6595(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) 6596NULL 6597NULL 6598SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2; 6599(SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) 6600NULL 6601NULL 6602SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2; 6603(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6604NULL 6605NULL 6606SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1); 6607(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) 6608NULL 6609SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2; 6610(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) 6611NULL 6612NULL 6613SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2; 6614(SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) 6615NULL 6616NULL 6617SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2; 6618(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6619NULL 6620NULL 6621SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1); 6622(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) 6623NULL 6624select (null, null) = (null, null); 6625(null, null) = (null, null) 6626NULL 6627SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0); 6628(SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0) 6629NULL 6630drop tables t1,t2,t3; 6631# 6632# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists 6633# 6634CREATE TABLE t1 (a date, KEY (a)) ; 6635INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02'); 6636set @old_optimizer_switch = @@optimizer_switch; 6637SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off'; 6638EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6639id select_type table type possible_keys key key_len ref rows Extra 66401 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index 66412 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index 6642SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6643a 66442009-01-01 66452009-02-02 6646SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off'; 6647EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6648id select_type table type possible_keys key key_len ref rows Extra 66491 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index 66502 MATERIALIZED t1 index NULL a 4 NULL 2 Using index 6651SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1); 6652a 66532009-01-01 66542009-02-02 6655set @@optimizer_switch=@old_optimizer_switch; 6656drop table t1; 6657# 6658# LP BUG#908269 incorrect condition in case of subqueries depending 6659# on constant tables 6660# 6661CREATE TABLE t1 ( a INT ); 6662INSERT INTO t1 VALUES (1),(5); 6663CREATE TABLE t2 ( b INT ) ENGINE=MyISAM; 6664INSERT INTO t2 VALUES (1); 6665CREATE TABLE t3 ( c INT ); 6666INSERT INTO t3 VALUES (4),(5); 6667SET optimizer_switch='subquery_cache=off'; 6668SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1; 6669( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) 66701 6671NULL 6672SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1; 6673( SELECT b FROM t2 WHERE b = a OR b * 0) 66741 6675NULL 6676SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1; 6677( SELECT b FROM t2 WHERE b = a OR rand() * 0) 66781 6679NULL 6680drop table t1,t2,t3; 6681set optimizer_switch=@subselect_tmp; 6682# 6683# LP BUG#905353 Wrong non-empty result with a constant table, 6684# aggregate function in subquery, MyISAM or Aria 6685# 6686CREATE TABLE t1 ( a INT ) ENGINE=MyISAM; 6687INSERT INTO t1 VALUES (1); 6688SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 ); 6689a 6690drop table t1; 6691# 6692# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in 6693# main query and implicit grouping 6694# 6695CREATE TABLE t1 (f1 int) engine=MyISAM; 6696INSERT INTO t1 VALUES (7),(8); 6697CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM; 6698INSERT INTO t2 VALUES (3,'f'); 6699EXPLAIN 6700SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6701id select_type table type possible_keys key key_len ref rows Extra 67021 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67032 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6704SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6705COUNT(f1) f4 67060 NULL 6707EXPLAIN 6708SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6709id select_type table type possible_keys key key_len ref rows Extra 67101 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67112 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6712SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3; 6713COUNT(f1) f4 67140 0 6715EXPLAIN 6716SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6717id select_type table type possible_keys key key_len ref rows Extra 67181 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67192 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 6720SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6721COUNT(f1) f4 67220 1 6723EXPLAIN 6724SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6725id select_type table type possible_keys key key_len ref rows Extra 67261 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables 67272 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 6728SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3; 6729COUNT(f1) f4 67300 0 6731drop table t1,t2; 6732# 6733# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table, 6734# HAVING, UNION in subquery 6735# 6736CREATE TABLE t1 (a INT); 6737INSERT INTO t1 VALUES (7),(0); 6738CREATE TABLE t2 (b INT); 6739EXPLAIN 6740SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; 6741id select_type table type possible_keys key key_len ref rows Extra 67421 PRIMARY t2 system NULL NULL NULL NULL 0 Const row not found 67431 PRIMARY t1 ALL NULL NULL NULL NULL 2 67442 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 67453 UNION NULL NULL NULL NULL NULL NULL NULL No tables used 6746NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6747SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7; 6748f1 f2 6749drop table t1,t2; 6750# 6751# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible 6752# WHERE and UNION in HAVING 6753# 6754CREATE TABLE t1 (a INT); 6755INSERT INTO t1 VALUES (1),(7); 6756EXPLAIN 6757SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; 6758id select_type table type possible_keys key key_len ref rows Extra 67591 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 67602 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used 67613 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used 6762NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6763SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1; 6764min_a a 6765EXPLAIN 6766SELECT 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; 6767id select_type table type possible_keys key key_len ref rows Extra 67681 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 67692 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 67703 DEPENDENT UNION t1 ALL NULL NULL NULL NULL 2 Using where 6771NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 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; 6773min_a a 6774drop table t1; 6775# 6776# MDEV-367: Different results with and without subquery_cache on 6777# a query with a constant NOT IN condition 6778# 6779CREATE TABLE t1 (a INT) ENGINE=MyISAM; 6780INSERT INTO t1 VALUES (1),(2),(3); 6781set @mdev367_optimizer_switch = @@optimizer_switch; 6782set optimizer_switch = 'subquery_cache=on'; 6783SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; 6784a 6785SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; 6786a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) 67871 NULL 67882 NULL 67893 NULL 6790set optimizer_switch=@mdev367_optimizer_switch; 6791set optimizer_switch = 'subquery_cache=off'; 6792SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100; 6793a 6794SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1; 6795a ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) 67961 NULL 67972 NULL 67983 NULL 6799set optimizer_switch=@mdev367_optimizer_switch; 6800DROP TABLE t1; 6801# 6802# MDEV-521 single value subselect transformation problem 6803# 6804CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM; 6805INSERT INTO t1 VALUES ('u1'),('u2'); 6806SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); 6807f1 6808u1 6809u2 6810FLUSH TABLES; 6811SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) ); 6812f1 6813u1 6814u2 6815DROP TABLE t1; 6816# return optimizer switch changed in the beginning of this test 6817set optimizer_switch=@subselect_tmp; 6818# 6819# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not 6820# precomputed and thus not part of optimization 6821# 6822CREATE TABLE t1 ( a VARCHAR(16), KEY (a) ); 6823INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'), 6824('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade'); 6825EXPLAIN 6826SELECT MAX( alias2.a ) AS field 6827FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6828WHERE alias1.a = alias2.a OR alias1.a = 'y' 6829HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); 6830id select_type table type possible_keys key key_len ref rows Extra 68311 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING 68322 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where 6833SELECT MAX( alias2.a ) AS field 6834FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6835WHERE alias1.a = alias2.a OR alias1.a = 'y' 6836HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 ); 6837field 6838EXPLAIN 6839SELECT MAX( alias2.a ) 6840FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6841WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); 6842id select_type table type possible_keys key key_len ref rows Extra 68431 PRIMARY alias1 index a a 19 NULL 11 Using where; Using index 68441 PRIMARY alias2 ref a a 19 test.alias1.a 2 Using index 68451 PRIMARY alias3 index NULL a 19 NULL 11 Using index; Using join buffer (flat, BNL join) 68462 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where 6847SELECT MAX( alias2.a ) 6848FROM t1 AS alias1, t1 AS alias2, t1 AS alias3 6849WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 ); 6850MAX( alias2.a ) 6851Arden-Arcade 6852drop table t1; 6853# 6854# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows 6855# with semijoin+materialization, IN and = subqueries 6856# 6857CREATE TABLE t1 (a1 INT); 6858INSERT INTO t1 VALUES (4),(6); 6859CREATE TABLE t2 (b1 INT); 6860INSERT INTO t2 VALUES (1),(7); 6861EXPLAIN 6862SELECT * FROM t1 6863WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); 6864id select_type table type possible_keys key key_len ref rows Extra 68651 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where 68662 SUBQUERY t1 ALL NULL NULL NULL NULL 2 68672 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1 68683 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 68693 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 6870SELECT * FROM t1 6871WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2)); 6872a1 6873drop table t1, t2; 6874# 6875# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS, 6876# inner joins takes hundreds times longer 6877# 6878CREATE TABLE t1 (a INT); 6879INSERT INTO t1 VALUES (1),(7); 6880CREATE TABLE t2 (b INT); 6881INSERT INTO t2 VALUES (4),(5); 6882CREATE TABLE t3 (c INT); 6883INSERT INTO t3 VALUES (8),(3); 6884set @@expensive_subquery_limit= 0; 6885EXPLAIN 6886SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6887FROM t2 alias1, t1 alias2, t1 alias3; 6888id select_type table type possible_keys key key_len ref rows Extra 68891 PRIMARY alias1 ALL NULL NULL NULL NULL 2 68901 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 68911 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 68922 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where 68932 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 68943 SUBQUERY t3 ALL NULL NULL NULL NULL 2 6895flush status; 6896SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6897FROM t2 alias1, t1 alias2, t1 alias3; 6898(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6899NULL 6900NULL 6901NULL 6902NULL 6903NULL 6904NULL 6905NULL 6906NULL 6907show status like "subquery_cache%"; 6908Variable_name Value 6909Subquery_cache_hit 6 6910Subquery_cache_miss 2 6911show status like '%Handler_read%'; 6912Variable_name Value 6913Handler_read_first 0 6914Handler_read_key 8 6915Handler_read_last 0 6916Handler_read_next 0 6917Handler_read_prev 0 6918Handler_read_retry 0 6919Handler_read_rnd 0 6920Handler_read_rnd_deleted 0 6921Handler_read_rnd_next 22 6922set @@expensive_subquery_limit= default; 6923EXPLAIN 6924SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6925FROM t2 alias1, t1 alias2, t1 alias3; 6926id select_type table type possible_keys key key_len ref rows Extra 69271 PRIMARY alias1 ALL NULL NULL NULL NULL 2 69281 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 69291 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join) 69302 SUBQUERY t1 ALL NULL NULL NULL NULL 2 69312 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 69323 SUBQUERY t3 ALL NULL NULL NULL NULL 2 6933flush status; 6934SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6935FROM t2 alias1, t1 alias2, t1 alias3; 6936(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3))) 6937NULL 6938NULL 6939NULL 6940NULL 6941NULL 6942NULL 6943NULL 6944NULL 6945show status like "subquery_cache%"; 6946Variable_name Value 6947Subquery_cache_hit 0 6948Subquery_cache_miss 0 6949show status like '%Handler_read%'; 6950Variable_name Value 6951Handler_read_first 0 6952Handler_read_key 0 6953Handler_read_last 0 6954Handler_read_next 0 6955Handler_read_prev 0 6956Handler_read_retry 0 6957Handler_read_rnd 0 6958Handler_read_rnd_deleted 0 6959Handler_read_rnd_next 16 6960drop table t1, t2, t3; 6961# 6962# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin 6963# 6964CREATE TABLE t1 (a INT); 6965INSERT INTO t1 VALUES (0),(8); 6966CREATE TABLE t2 (b INT PRIMARY KEY); 6967INSERT INTO t2 VALUES (1),(2); 6968EXPLAIN 6969SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); 6970id select_type table type possible_keys key key_len ref rows Extra 69711 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 69722 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 69733 SUBQUERY t1 ALL NULL NULL NULL NULL 2 6974SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1)); 6975a 6976drop table t1,t2; 6977# 6978# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used 6979# 6980CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM; 6981INSERT INTO t1 VALUES ('USA'); 6982CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM; 6983INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia'); 6984CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM; 6985INSERT INTO t3 VALUES (12),(22),(9),(45); 6986create table t4 like t3; 6987insert into t4 select * from t3; 6988# This should not show range access for table t2 6989explain 6990SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 6991WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); 6992id select_type table type possible_keys key key_len ref rows Extra 69931 PRIMARY t1 system NULL NULL NULL NULL 1 69941 PRIMARY t2 ALL b NULL NULL NULL 2 Using where 69951 PRIMARY t3 ref d d 5 test.t2.b 2 Using index 69963 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 6997set @tmp_mdev410=@@global.userstat; 6998set global userstat=on; 6999flush table_statistics; 7000flush index_statistics; 7001SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 7002WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 ); 7003MIN(b) 7004NULL 7005# The following shows that t2 was indeed scanned with a full scan. 7006show table_statistics; 7007Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes 7008test t1 2 0 0 7009test t2 3 0 0 7010show index_statistics; 7011Table_schema Table_name Index_name Rows_read 7012test t2 b 1 7013set global userstat=@tmp_mdev410; 7014DROP TABLE t1,t2,t3,t4; 7015# 7016# MDEV-430: Server crashes in select_describe on EXPLAIN with 7017# materialization+semijoin, 2 nested subqueries, aggregate functions 7018# 7019CREATE TABLE t1 (a INT, KEY(a)); 7020INSERT INTO t1 VALUES (1),(8); 7021CREATE TABLE t2 (b INT, KEY(b)); 7022INSERT INTO t2 VALUES (45),(17),(20); 7023EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; 7024id select_type table type possible_keys key key_len ref rows Extra 70251 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 70262 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index 70272 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index 7028DROP TABLE t1,t2; 7029# 7030# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields 7031# 7032CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM; 7033INSERT INTO t1 VALUES (8),(0); 7034CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM; 7035INSERT INTO t2 VALUES (4,'j'),(6,'v'); 7036CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM; 7037INSERT INTO t3 VALUES ('b'),('c'); 7038EXPLAIN 7039SELECT * FROM t1 7040WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; 7041id select_type table type possible_keys key key_len ref rows Extra 70421 PRIMARY t1 index a a 5 NULL 2 Using where; Using index 70432 SUBQUERY <subquery3> ALL distinct_key NULL NULL NULL 1 70442 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join) 70453 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 7046SELECT * FROM t1 7047WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10; 7048a 7049drop table t1, t2, t3; 7050# 7051# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery 7052# 7053CREATE TABLE t1 (a INT, KEY(a)); 7054INSERT INTO t1 VALUES (1),(8); 7055CREATE TABLE t2 (b INT, KEY(b)); 7056INSERT INTO t2 VALUES (45),(17),(20); 7057EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ; 7058id select_type table type possible_keys key key_len ref rows Extra 70591 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 70602 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index 70612 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index 7062DROP TABLE t1,t2; 7063# 7064# MDEV-5991: crash in Item_field::used_tables 7065# 7066create table t1 (c int); 7067select exists(select 1 from t1 group by `c` in (select `c` from t1)); 7068exists(select 1 from t1 group by `c` in (select `c` from t1)) 70690 7070drop table t1; 7071# 7072# MDEV-7565: Server crash with Signal 6 (part 2) 7073# 7074Select 7075(Select Sum(`TestCase`.Revenue) From mysql.slow_log E 7076Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2) 7077) As `ControlRev` 7078From 7079(Select 3 as Revenue, 4 as TemplateID) As `TestCase` 7080Group By TestCase.Revenue, TestCase.TemplateID; 7081ControlRev 7082NULL 7083# 7084# MDEV-7445:Server crash with Signal 6 7085# 7086CREATE PROCEDURE procedure2() 7087BEGIN 7088Select 7089(Select Sum(`TestCase`.Revenue) From mysql.slow_log E 7090Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2) 7091) As `ControlRev` 7092 From 7093(Select 3 as Revenue, 4 as TemplateID) As `TestCase` 7094Group By TestCase.Revenue, TestCase.TemplateID; 7095END | 7096call procedure2(); 7097ControlRev 7098NULL 7099call procedure2(); 7100ControlRev 7101NULL 7102drop procedure procedure2; 7103# 7104# MDEV-7846:Server crashes in Item_subselect::fix 7105#_fields or fails with Thread stack overrun 7106# 7107CREATE TABLE t1 (column1 INT) ENGINE=MyISAM; 7108INSERT INTO t1 VALUES (3),(9); 7109CREATE TABLE t2 (column2 INT) ENGINE=MyISAM; 7110INSERT INTO t2 VALUES (1),(4); 7111CREATE TABLE t3 (column3 INT) ENGINE=MyISAM; 7112INSERT INTO t3 VALUES (6),(8); 7113CREATE TABLE t4 (column4 INT) ENGINE=MyISAM; 7114INSERT INTO t4 VALUES (2),(5); 7115PREPARE stmt FROM " 7116SELECT ( 7117 SELECT MAX( table1.column1 ) AS field1 7118 FROM t1 AS table1 7119 WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 ) 7120) AS sq 7121FROM t3 AS table3, t4 AS table4 GROUP BY sq 7122"; 7123EXECUTE stmt; 7124sq 7125NULL 7126EXECUTE stmt; 7127sq 7128NULL 7129deallocate prepare stmt; 7130drop table t1,t2,t3,t4; 7131# 7132# MDEV-7122 7133# Assertion `0' failed in subselect_hash_sj_engine::init 7134# 7135set tmp_memory_table_size=0; 7136CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; 7137INSERT INTO t1 VALUES(0),(0),(0); 7138SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1); 7139a 71400 71410 71420 7143DROP TABLE t1; 7144set tmp_memory_table_size=default; 7145# 7146# MDEV-10776: Server crash on query 7147# 7148create table t1 (field1 int); 7149insert into t1 values (1); 7150select round((select 1 from t1 limit 1)) 7151from t1 7152group by round((select 1 from t1 limit 1)); 7153round((select 1 from t1 limit 1)) 71541 7155drop table t1; 7156# 7157# MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE || 7158# m_lock_type != 2' failed in handler::ha_index_read_map 7159# 7160CREATE TABLE t1 (f1 INT); 7161INSERT INTO t1 VALUES (1),(2); 7162CREATE TABLE t2 (f2 INT, KEY(f2)); 7163INSERT INTO t2 VALUES (3); 7164CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2; 7165SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq; 7166ERROR 42000: Can't group on 'sq' 7167SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq; 7168ERROR 42000: Can't group on 'sq' 7169SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0; 7170f2 71713 7172SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 ); 7173count(*) 71741 7175delete from t1; 7176SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq; 7177ERROR 42000: Can't group on 'sq' 7178SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq; 7179ERROR 42000: Can't group on 'sq' 7180drop view v2; 7181drop table t1,t2; 7182# 7183# MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*) 7184# 7185CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL); 7186INSERT INTO t1 VALUES ('foo','bar'); 7187SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 ); 7188f1 f2 7189SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 ); 7190f1 f2 7191foo bar 7192DROP TABLE t1; 7193# 7194# MDEV-10146: Wrong result (or questionable result and behavior) 7195# with aggregate function in uncorrelated SELECT subquery 7196# 7197CREATE TABLE t1 (f1 INT); 7198CREATE VIEW v1 AS SELECT * FROM t1; 7199INSERT INTO t1 VALUES (1),(2); 7200CREATE TABLE t2 (f2 int); 7201INSERT INTO t2 VALUES (3); 7202SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1; 7203( SELECT MAX(f1) FROM t2 ) 72042 7205SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1; 7206( SELECT MAX(f1) FROM t2 ) 72072 7208INSERT INTO t2 VALUES (4); 7209SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1; 7210ERROR 21000: Subquery returns more than 1 row 7211SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1; 7212ERROR 21000: Subquery returns more than 1 row 7213drop view v1; 7214drop table t1,t2; 7215CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM; 7216INSERT t1 VALUES (4),(8); 7217CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM; 7218INSERT t2 VALUES (6); 7219SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2; 7220(SELECT MAX(sq.f2) FROM t1) 7221NULL 7222# 7223# Disable this query till MDEV-13399 is resolved 7224# 7225# INSERT t2 VALUES (9); 7226# --error ER_SUBQUERY_NO_1_ROW 7227# SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2; 7228# 7229drop table t1, t2; 7230# 7231# MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in 7232# (5.5 test) 7233# 7234SET @optimiser_switch_save= @@optimizer_switch; 7235CREATE TABLE t1 (a INT NOT NULL); 7236INSERT INTO t1 VALUES (1),(1),(1),(5),(5); 7237CREATE TABLE t2 (b INT); 7238INSERT INTO t2 VALUES (5),(1); 7239CREATE TABLE t3 (c INT, KEY(c)); 7240INSERT INTO t3 VALUES (5),(5); 7241SET optimizer_switch='semijoin=on'; 7242select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`) 7243and t1.a in (select `test`.`t3`.`c` from `test`.`t3`); 7244a 72455 72465 7247SET optimizer_switch='semijoin=off'; 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= @optimiser_switch_save; 7254DROP TABLE t1, t2, t3; 7255# 7256# MDEV-16820: impossible where with inexpensive subquery 7257# 7258create table t1 (a int) engine=myisam; 7259insert into t1 values (3), (1), (7); 7260create table t2 (b int, index idx(b)); 7261insert into t2 values (2), (5), (3), (2); 7262explain select * from t1 where (select max(b) from t2) = 10; 7263id select_type table type possible_keys key key_len ref rows Extra 72641 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 72652 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 7266explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3; 7267id select_type table type possible_keys key key_len ref rows Extra 72681 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 72692 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away 7270drop table t1,t2; 7271# 7272# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 7273# 7274create table t10 (a int); 7275insert into t10 values (1),(2),(3); 7276create table t12 (a int); 7277insert into t12 values (1),(2),(3); 7278select * from t10 where exists (select * from t12 order by a limit 0); 7279a 7280explain select * from t10 where exists (select * from t12 order by a limit 0); 7281id select_type table type possible_keys key key_len ref rows Extra 72821 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 72832 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit 7284prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; 7285set @l=1; 7286execute stmt1 using @l; 7287a 72881 72892 72903 7291set @l=2; 7292execute stmt1 using @l; 7293a 72941 72952 72963 7297set @l=0; 7298execute stmt1 using @l; 7299a 7300deallocate prepare stmt1; 7301drop table t10, t12; 7302End of 5.5 tests 7303# End of 10.0 tests 7304# 7305# MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops 7306# with UNION in ALL subquery 7307# 7308SET NAMES utf8; 7309CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM; 7310INSERT INTO t1 VALUES ('foo'); 7311SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' ); 7312f 7313foo 7314SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar'); 7315f 7316foo 7317drop table t1; 7318SET NAMES default; 7319# 7320# MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops 7321# 7322SET NAMES utf8; 7323CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM; 7324CREATE TABLE t2 (f3 INT) ENGINE=MyISAM; 7325SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' ); 7326f1 f2 f3 7327SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo'); 7328f1 f2 f3 7329DROP TABLE t1, t2; 7330SET NAMES default; 7331# End of 10.1 tests 7332# 7333# MDEV-12564: IN TO EXISTS transformation for rows after 7334# conversion an outer join to inner join 7335# 7336CREATE TABLE t ( 7337pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c) 7338) ENGINE=MyISAM; 7339INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar'); 7340SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk) 7341WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo'; 7342pk i c pk i c 73431 10 foo 1 10 foo 7344DROP TABLE t; 7345# 7346# MDEV-25002: Outer reference in ON clause of subselect 7347# 7348create table t1 ( 7349pk int primary key, 7350a int 7351) engine=myisam; 7352insert into t1 values (1,1), (2,2); 7353create table t2 ( 7354pk int primary key, 7355b int 7356) engine=myisam; 7357insert into t2 values (1,1), (2,3); 7358create table t3 (a int); 7359insert into t3 values (1),(2); 7360select a, 7361(select count(*) from t1, t2 7362where t2.pk=t3.a and t1.pk=1) as sq 7363from t3; 7364a sq 73651 1 73662 1 7367select a, 7368(select count(*) from t1 join t2 on t2.pk=t3.a 7369where t1.pk=1) as sq 7370from t3; 7371a sq 73721 1 73732 1 7374select a from t3 7375where a in (select t2.b from t1,t2 where t2.pk=t3.a and t1.pk=1); 7376a 73771 7378select a from t3 7379where a in (select t2.b from t1 join t2 on t2.pk=t3.a where t1.pk=1); 7380a 73811 7382drop table t1,t2,t3; 7383# End of 10.2 tests 7384# 7385# Start of 10.4 tests 7386# 7387# 7388# MDEV-16861 Split Item::update_null_value() into a new virtual method in Type_handler 7389# 7390SELECT ROW(1,2) = EXISTS (SELECT 1); 7391ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7392SELECT ROW(1,2) = 1 IN (SELECT 1 UNION SELECT 2); 7393ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7394SELECT ROW(1,2) = (1 = ANY (SELECT 1 UNION SELECT 2)); 7395ERROR HY000: Illegal parameter data types row and boolean for operation '=' 7396# 7397# End of 10.4 tests 7398# 7399