github.com/matrixorigin/matrixone@v0.7.0/test/distributed/cases/subquery/subquery-with-in.sql (about) 1 -- @suit 2 3 -- @case 4 -- @desc:test for [in] subquery with constant operand 5 -- @label:bvt 6 SELECT 1 IN (SELECT 1); 7 -- @bvt:issue#3307 8 SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a)); 9 -- @bvt:issue 10 -- @bvt:issue#3556 11 SELECT 1 FROM (SELECT 1 as a) b WHERE 1 not IN (SELECT (SELECT a)); 12 -- @bvt:issue 13 SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id); 14 SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1); 15 SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *); 16 SELECT ((0,1) NOT IN (SELECT NULL,1)) IS NULL; 17 18 -- @case 19 -- @desc:test for [in] subquery with operand-is-column 20 -- @label:bvt 21 22 drop table if exists t1; 23 drop table if exists t2; 24 drop table if exists t3; 25 create table t1 (a int); 26 create table t2 (a int, b int); 27 create table t3 (a int); 28 create table t4 (a int not null, b int not null); 29 insert into t1 values (2); 30 insert into t2 values (1,7),(2,7); 31 insert into t4 values (4,8),(3,8),(5,9); 32 insert into t2 values (100, 5); 33 select * from t3 where a in (select b from t2); 34 select * from t3 where a in (select b from t2 where b > 7); 35 select * from t3 where a not in (select b from t2); 36 SELECT 0 IN (SELECT 1 FROM t1 a); 37 select * from t3 where a in (select a,b from t2); 38 select * from t3 where a in (select * from t2); 39 40 drop table if exists t1; 41 drop table if exists t2; 42 drop table if exists t3; 43 create table t1 (s1 char(5), index s1(s1)); 44 create table t2 (s1 char(5), index s1(s1)); 45 insert into t1 values ('a1'),('a2'),('a3'); 46 insert into t2 values ('a1'),('a2'); 47 select s1, s1 NOT IN (SELECT s1 FROM t2) from t1; 48 select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1; 49 50 drop table if exists t1; 51 drop table if exists t2; 52 create table t1(val varchar(10)); 53 insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp'); 54 select 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%'); 55 56 DROP TABLE IF EXISTS t1; 57 create table t1 (id int not null, text varchar(20) not null default '', primary key (id)); 58 insert 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'); 59 select * from t1 where id not in (select id from t1 where id < 8); 60 61 drop table if exists t1; 62 drop table if exists t2; 63 drop table if exists t3; 64 CREATE TABLE t1 (a int); 65 CREATE TABLE t2 (a int, b int); 66 CREATE TABLE t3 (b int NOT NULL); 67 INSERT INTO t1 VALUES (1), (2), (3), (4); 68 INSERT INTO t2 VALUES (1,10), (3,30); 69 select * from t1 where t1.a in (SELECT t1.a FROM t1 LEFT JOIN t2 ON t2.a=t1.a); 70 SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b WHERE t3.b IS NOT NULL OR t2.a > 10; 71 SELECT * FROM t1 WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b WHERE t3.b IS NOT NULL OR t2.a > 10); 72 drop table if exists t1; 73 drop table if exists t2; 74 drop table if exists t3; 75 CREATE TABLE t1(a int, INDEX (a)); 76 INSERT INTO t1 VALUES (1), (3), (5), (7); 77 INSERT INTO t1 VALUES (NULL); 78 CREATE TABLE t2(a int); 79 INSERT INTO t2 VALUES (1),(2),(3); 80 SELECT a, a IN (SELECT a FROM t1) FROM t2; 81 82 drop table if exists t1; 83 drop table if exists t2; 84 drop table if exists t3; 85 CREATE table t1 ( c1 int ); 86 INSERT INTO t1 VALUES ( 1 ); 87 INSERT INTO t1 VALUES ( 2 ); 88 INSERT INTO t1 VALUES ( 3 ); 89 CREATE TABLE t2 ( c2 int ); 90 INSERT INTO t2 VALUES ( 1 ); 91 INSERT INTO t2 VALUES ( 4 ); 92 INSERT INTO t2 VALUES ( 5 ); 93 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1); 94 SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) ); 95 96 drop table if exists t1; 97 drop table if exists t2; 98 DROP TABLE IF EXISTS c; 99 CREATE TABLE `c` ( 100 `int_nokey` int(11) NOT NULL, 101 `int_key` int(11) NOT NULL 102 ); 103 INSERT INTO `c` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4), 104 (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7), 105 (5,2), (1,8), (7,0), (0,9), (9,5); 106 -- @bvt:issue#3307 107 SELECT * FROM c WHERE `int_key` IN (SELECT `int_nokey`); 108 -- @bvt:issue 109 DROP TABLE IF EXISTS c; 110 111 drop table if exists t1; 112 drop table if exists t2; 113 CREATE TABLE t1(c INT); 114 CREATE TABLE t2(a INT, b INT); 115 INSERT INTO t2 VALUES (1, 10), (2, NULL); 116 INSERT INTO t1 VALUES (1), (3); 117 SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10); 118 drop table if exists t1; 119 drop table if exists t2; 120 121 CREATE TABLE t1 ( 122 a1 char(8) DEFAULT NULL, 123 a2 char(8) DEFAULT NULL 124 ); 125 CREATE TABLE t2 ( 126 b1 char(8) DEFAULT NULL, 127 b2 char(8) DEFAULT NULL 128 ); 129 INSERT INTO t1 VALUES 130 ('1 - 00', '2 - 00'),('1 - 01', '2 - 01'),('1 - 02', '2 - 02'); 131 INSERT INTO t2 VALUES 132 ('1 - 01', '2 - 01'),('1 - 01', '2 - 01'), 133 ('1 - 02', '2 - 02'),('1 - 02', '2 - 02'),('1 - 03', '2 - 03'); 134 SELECT * FROM t2 WHERE b1 NOT IN ('1 - 00', '1 - 01', '1 - 02'); 135 drop table if exists t1; 136 drop table if exists t2; 137 138 -- @case 139 -- @desc:test for [in] subquery with groupby,having, 140 -- @label:bvt 141 drop table if exists t1; 142 drop table if exists t2; 143 drop table if exists t3; 144 drop table if exists t4; 145 CREATE TABLE `t1` ( 146 `numeropost` int(8) unsigned NOT NULL, 147 `maxnumrep` int(10) unsigned NOT NULL default 0, 148 PRIMARY KEY (`numeropost`) 149 ) ; 150 151 INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2); 152 153 CREATE TABLE `t2` ( 154 `mot` varchar(30) NOT NULL default '', 155 `topic` int(8) unsigned NOT NULL default 0, 156 `dt` date, 157 `pseudo` varchar(35) NOT NULL default '', 158 PRIMARY KEY (`topic`) 159 ) ; 160 161 INSERT INTO t2 (mot,topic,dt,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce'); 162 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic); 163 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100); 164 -- @bvt:issue#3307 165 SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1); 166 -- @bvt:issue 167 SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 168 SELECT * from t2 where topic NOT IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000); 169 SELECT * FROM t2 WHERE mot IN (SELECT 'joce'); 170 171 drop table if exists t1; 172 drop table if exists t2; 173 create table t1 (a int); 174 create table t2 (a int); 175 insert into t1 values (1),(2); 176 insert into t2 values (0),(1),(2),(3); 177 select a from t2 where a in (select a from t1); 178 select a from t2 having a in (select a from t1); 179 180 drop table if exists t1; 181 drop table if exists t2; 182 create table t1 (oref int, grp int, ie int) ; 183 insert into t1 (oref, grp, ie) values(1, 1, 1),(1, 1, 1), (1, 2, NULL),(2, 1, 3),(3, 1, 4),(3, 2, NULL); 184 create table t2 (oref int, a int); 185 insert into t2 values(1, 1),(2, 2),(3, 3), (4, NULL),(2, NULL); 186 create table t3 (a int); 187 insert into t3 values (NULL), (NULL); 188 select a, oref, a in (select max(ie) from t1 where oref=t2.oref group by grp) Z from t2; 189 select a, oref from t2 where a in (select max(ie) from t1 where oref=t2.oref group by grp); 190 select a, oref, a in ( 191 select max(ie) from t1 where oref=t2.oref group by grp union 192 select max(ie) from t1 where oref=t2.oref group by grp 193 ) Z from t2; 194 select a in (select max(ie) from t1 where oref=4 group by grp) from t3; 195 196 drop table if exists t1; 197 drop table if exists t2; 198 drop table if exists t3; 199 create table t1 (a int, oref int); 200 insert into t1 values(1, 1),(1, NULL),(2, 3),(2, NULL),(3, NULL); 201 create table t2 (a int, oref int); 202 insert into t2 values (1, 1), (2,2), (NULL, 3), (NULL, 4); 203 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2; 204 select oref, a from t2 where a in (select a from t1 where oref=t2.oref); 205 -- @ignore{ 206 delete from t2; 207 insert into t2 values (NULL, 0),(NULL, 0), (NULL, 0), (NULL, 0); 208 select oref, a, a in (select a from t1 where oref=t2.oref) Z from t2; 209 drop table if exists t1; 210 drop table if exists t2; 211 212 -- @case 213 -- @desc:test for [in] subquery with UNION 214 -- @label:bvt 215 drop table if exists t1; 216 drop table if exists t2; 217 CREATE TABLE t2 (id int(11) default NULL); 218 INSERT INTO t2 VALUES (1),(2); 219 SELECT * FROM t2 WHERE id IN (SELECT 1); 220 -- @bvt:issue#4354 221 SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3); 222 -- @bvt:issue 223 SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1)); 224 -- @bvt:issue#4354 225 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3); 226 SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2); 227 SELECT * FROM t2 WHERE id NOT IN (SELECT 5 UNION SELECT 2); 228 -- @bvt:issue 229 230 -- @case 231 -- @desc:test for [in] subquery with null 232 -- @label:bvt 233 drop table if exists t1; 234 drop table if exists t2; 235 create table t1 (a int); 236 insert into t1 values (1),(2),(3); 237 select 1 IN (SELECT * from t1); 238 select 10 IN (SELECT * from t1); 239 select NULL IN (SELECT * from t1); 240 update t1 set a=NULL where a=2; 241 select 1 IN (SELECT * from t1); 242 select 3 IN (SELECT * from t1); 243 select 10 IN (SELECT * from t1); 244 245 DROP TABLE IF EXISTS t1; 246 create table t1 (a varchar(20)); 247 insert into t1 values ('A'),('BC'),('DEF'); 248 select 'A' IN (SELECT * from t1); 249 select 'XYZS' IN (SELECT * from t1); 250 select NULL IN (SELECT * from t1); 251 update t1 set a=NULL where a='BC'; 252 select 'A' IN (SELECT * from t1); 253 select 'DEF' IN (SELECT * from t1); 254 select 'XYZS' IN (SELECT * from t1); 255 256 DROP TABLE IF EXISTS t1; 257 create table t1 (a float); 258 insert into t1 values (1.5),(2.5),(3.5); 259 select 1.5 IN (SELECT * from t1); 260 select 10.5 IN (SELECT * from t1); 261 select NULL IN (SELECT * from t1); 262 update t1 set a=NULL where a=2.5; 263 select 1.5 IN (SELECT * from t1); 264 select 3.5 IN (SELECT * from t1); 265 select 10.5 IN (SELECT * from t1); 266 267 drop table if exists t1; 268 drop table if exists t2; 269 CREATE TABLE t1 (a int(11) NOT NULL default 0, PRIMARY KEY (a)); 270 CREATE TABLE t2 (a int(11) default 0, INDEX (a)); 271 CREATE TABLE t3 (a int(11) default 0); 272 INSERT INTO t3 VALUES (1),(2),(3); 273 INSERT INTO t1 VALUES (1),(2),(3),(4); 274 INSERT INTO t2 VALUES (1),(2),(3); 275 SELECT t1.a, t1.a in (select t2.a from t2) FROM t1; 276 SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1; 277 drop table if exists t1; 278 drop table if exists t2; 279 drop table if exists t3; 280 281 create table t1 (a int); 282 insert into t1 values (-1), (-4), (-2), (NULL); 283 select -10 IN (select a from t1); 284 DROP TABLE IF EXISTS t1; 285 286 -- @case 287 -- @desc:test for [in] subquery with limit 288 -- @label:bvt 289 create table t1 (a float); 290 select 10.5 IN (SELECT * from t1 LIMIT 1); 291 -- error 292 select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5); 293 -- error 294 select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1); 295 296 -- @case 297 -- @desc:test for [in] subquery with Multi tuple 298 -- @label:bvt 299 DROP TABLE IF EXISTS t1; 300 create table t1 (a int, b real, c varchar(10)); 301 insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b'); 302 select (1, 1, 'a') IN (select a,b,c from t1); 303 select (1, 2, 'a') IN (select a,b,c from t1); 304 select (1, 1, 'a') IN (select b,a,c from t1); 305 select (1, 1, 'a') IN (select a,b,c from t1 where a is not null); 306 select (1, 2, 'a') IN (select a,b,c from t1 where a is not null); 307 select (1, 1, 'a') IN (select b,a,c from t1 where a is not null); 308 select (1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 309 select (1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a'); 310 select (1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a'); 311 -- error 312 select (1, 1, 'a') IN (select b,a,c from t1 limit 2); 313 DROP TABLE IF EXISTS t1; 314 315 create table t1 (a integer, b integer); 316 -- @bvt:issue#7691 317 select (1,(2,2)) in (select * from t1 ); 318 -- error 319 select (1,(2,2)) = (select * from t1 ); 320 -- error 321 select (select * from t1) = (1,(2,2)); 322 -- @bvt:issue 323 324 DROP TABLE IF EXISTS t1; 325 drop table if exists t2; 326 CREATE TABLE t1 (a1 INT, a2 INT); 327 CREATE TABLE t2 (b1 INT, b2 INT); 328 INSERT INTO t1 VALUES (100, 200); 329 INSERT INTO t1 VALUES (101, 201); 330 INSERT INTO t2 VALUES (101, 201); 331 INSERT INTO t2 VALUES (103, 203); 332 SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1; 333 334 DROP TABLE IF EXISTS t1; 335 drop table if exists t2; 336 CREATE TABLE t1 ( 337 pk INTEGER, 338 col_int_nokey INTEGER, 339 col_int_key INTEGER, 340 col_datetime_key DATETIME, 341 PRIMARY KEY (pk) 342 ); 343 INSERT INTO t1 VALUES (1, 1, 7, '2001-11-04 19:07:55.051133'); 344 CREATE TABLE t2(field1 INT, field2 INT); 345 -- @bvt:issue#3307 346 SELECT * FROM t2 WHERE (field1, field2) IN ( 347 SELECT MAX(col_datetime_key), col_int_key 348 FROM t1 349 WHERE col_int_key > col_int_nokey 350 GROUP BY col_int_key); 351 -- @bvt:issue 352 353 DROP TABLE IF EXISTS t1; 354 drop table if exists t2; 355 356 create table t1 (a int, b int); 357 insert into t1 values (0,0), (2,2), (3,3); 358 create table t2 (a int, b int); 359 insert into t2 values (1,1), (3,3); 360 select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; 361 insert into t2 values (NULL,4); 362 select a, b, (a,b) in (select a, min(b) from t2 group by a) Z from t1; 363 DROP TABLE IF EXISTS t1; 364 drop table if exists t2; 365 366 DROP TABLE IF EXISTS t1; 367 CREATE TABLE t1 (a INT); 368 INSERT INTO t1 VALUES (1), (2), (11); 369 -- @bvt:issue#7691 370 SELECT a, (11, 12) = (SELECT a, 22), (11, 12) IN (SELECT a, 22) FROM t1 GROUP BY t1.a; 371 SELECT a, (11, 12) = (SELECT a, 12), (11, 12) IN (SELECT a, 12) FROM t1 GROUP BY t1.a; 372 SELECT a, (11, 12) = (SELECT a, 22), (11, 12) IN (SELECT a, 22) FROM t1; 373 SELECT a, (11, 12) = (SELECT a, 12), (11, 12) IN (SELECT a, 12) FROM t1; 374 SELECT a AS x, (11, 12) = (SELECT MAX(x), 22), (11, 12) IN (SELECT MAX(x), 22) FROM t1; 375 SELECT a AS x, (11, 12) = (SELECT MAX(x), 12), (11, 12) IN (SELECT MAX(x), 12) FROM t1; 376 DROP TABLE IF EXISTS t1; 377 SELECT (1,2) = (SELECT NULL, NULL), (1,2) IN (SELECT NULL, NULL); 378 SELECT (1,2) = (SELECT 1, NULL), (1,2) IN (SELECT 1, NULL); 379 SELECT (1,2) = (SELECT NULL, 2), (1,2) IN (SELECT NULL, 2); 380 SELECT (1,2) = (SELECT NULL, 1), (1,2) IN (SELECT NULL, 1); 381 SELECT (1,2) = (SELECT 1, 1), (1,2) IN (SELECT 1, 1); 382 SELECT (1,2) = (SELECT 1, 2), (1,2) IN (SELECT 1, 2); 383 -- @bvt:issue 384 385 create table t_out (subcase char(3),a1 char(2), b1 char(2), c1 char(2)); 386 create table t_in (a2 char(2), b2 char(2), c2 char(2)); 387 insert into t_out values ('A.1','2a', NULL, '2a'); 388 insert into t_out values ('A.3', '2a', NULL, '2a'); 389 insert into t_out values ('A.4', '2a', NULL, 'xx'); 390 insert into t_out values ('B.1', '2a', '2a', '2a'); 391 insert into t_out values ('B.2', '2a', '2a', '2a'); 392 insert into t_out values ('B.3', '3a', 'xx', '3a'); 393 insert into t_out values ('B.4', 'xx', '3a', '3a'); 394 insert into t_in values ('1a', '1a', '1a'); 395 insert into t_in values ('2a', '2a', '2a'); 396 insert into t_in values (NULL, '2a', '2a'); 397 insert into t_in values ('3a', NULL, '3a'); 398 select subcase, 399 (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 400 (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 401 from t_out where subcase = 'A.1'; 402 403 select subcase, 404 (a1, b1, c1) IN (select * from t_in) pred_in, 405 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in 406 from t_out where subcase = 'A.3'; 407 408 select subcase, 409 (a1, b1, c1) IN (select * from t_in) pred_in, 410 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in 411 from t_out where subcase = 'A.4'; 412 413 select subcase, 414 (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in, 415 (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in 416 from t_out where subcase = 'B.1'; 417 418 select subcase, 419 (a1, b1, c1) IN (select * from t_in) pred_in, 420 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in 421 from t_out where subcase = 'B.2'; 422 423 select subcase, 424 (a1, b1, c1) IN (select * from t_in) pred_in, 425 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in 426 from t_out where subcase = 'B.3'; 427 428 select subcase, 429 (a1, b1, c1) IN (select * from t_in) pred_in, 430 (a1, b1, c1) NOT IN (select * from t_in) pred_not_in 431 from t_out where subcase = 'B.4'; 432 433 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 434 where subcase = 'A.1' and 435 (a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 436 437 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 438 where subcase = 'A.1' and 439 (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 440 441 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 442 where subcase = 'A.1' and 443 NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 444 445 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 446 where subcase = 'A.3' and 447 (a1, b1, c1) IN (select * from t_in); 448 449 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 450 where subcase = 'A.3' and 451 (a1, b1, c1) NOT IN (select * from t_in); 452 453 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 454 where subcase = 'A.3' and 455 NOT((a1, b1, c1) IN (select * from t_in)); 456 457 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 458 where subcase = 'A.3' and 459 ((a1, b1, c1) IN (select * from t_in)) is NULL and 460 ((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 461 462 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 463 where subcase = 'A.4' and 464 (a1, b1, c1) IN (select * from t_in); 465 466 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 467 where subcase = 'A.4' and 468 (a1, b1, c1) NOT IN (select * from t_in); 469 470 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 471 where subcase = 'A.4' and 472 NOT((a1, b1, c1) IN (select * from t_in)); 473 474 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 475 where subcase = 'B.1' and 476 (a1, b1, c1) IN (select * from t_in where a1 = 'no_match'); 477 478 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 479 where subcase = 'B.1' and 480 (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match'); 481 482 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 483 where subcase = 'B.1' and 484 NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match')); 485 486 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 487 where subcase = 'B.2' and 488 (a1, b1, c1) IN (select * from t_in); 489 490 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 491 where subcase = 'B.2' and 492 (a1, b1, c1) NOT IN (select * from t_in); 493 494 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 495 where subcase = 'B.2' and 496 NOT((a1, b1, c1) IN (select * from t_in)); 497 498 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 499 where subcase = 'B.3' and 500 (a1, b1, c1) IN (select * from t_in); 501 502 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 503 where subcase = 'B.3' and 504 (a1, b1, c1) NOT IN (select * from t_in); 505 506 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 507 where subcase = 'B.3' and 508 NOT((a1, b1, c1) IN (select * from t_in)); 509 510 select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out 511 where subcase = 'B.3' and 512 ((a1, b1, c1) IN (select * from t_in)) is NULL and 513 ((a1, b1, c1) NOT IN (select * from t_in)) is NULL; 514 515 select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out 516 where subcase = 'B.4' and 517 (a1, b1, c1) IN (select * from t_in); 518 519 select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out 520 where subcase = 'B.4' and 521 (a1, b1, c1) NOT IN (select * from t_in); 522 523 select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out 524 where subcase = 'B.4' and 525 NOT((a1, b1, c1) IN (select * from t_in)); 526 527 DROP TABLE IF EXISTS t_out; 528 DROP TABLE IF EXISTS t_in; 529 530 -- @case 531 -- @desc:test for [in] subquery with netsed subquery 532 -- @label:bvt 533 DROP TABLE IF EXISTS t1; 534 drop table if exists t2; 535 create table t1 (a int); 536 create table t2 (b int); 537 insert into t1 values (1),(2); 538 insert into t2 values (1); 539 select a from t1 where a in (select a from t1 where a in (select b from t2)); 540 DROP TABLE IF EXISTS t1; 541 drop table if exists t2; 542 543 create table t1 (a int, b int); 544 create table t2 (a int, b int); 545 select * from t1 where (a,b) in (select a,b from t2); 546 DROP TABLE IF EXISTS t1; 547 drop table if exists t2; 548 549 create table t1 (a int); 550 insert into t1 values (1), (2), (3); 551 -- @bvt:issue#3307 552 SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1); 553 -- @bvt:issue 554 DROP TABLE IF EXISTS t1; 555 556 DROP TABLE IF EXISTS t1; 557 drop table if exists t2; 558 559 CREATE TABLE t1( a INT ); 560 INSERT INTO t1 VALUES (1),(2); 561 CREATE TABLE t2( a INT, b INT ); 562 SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2); 563 -- error 564 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) ); 565 SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ); 566 SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ); 567 568 DROP TABLE IF EXISTS t1; 569 drop table if exists t2; 570 drop table if exists t3; 571 drop table if exists t4; 572 drop table if exists t5; 573 create table t0 (a int); 574 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 575 create table t1 ( 576 a int(11) default null, 577 b int(11) default null 578 ); 579 insert into t1 select A.a+10*(B.a+10*C.a),A.a+10*(B.a+10*C.a) from t0 A, t0 B, t0 C; 580 create table t2 (a int(11) default null); 581 insert into t2 values (0),(1); 582 create table t3 (a int(11) default null); 583 insert into t3 values (0),(1); 584 create table t4 (a int(11) default null); 585 insert into t4 values (0),(1); 586 create table t5 (a int(11) default null); 587 insert into t5 values (0),(1),(0),(1); 588 select * from t2, t3 589 where 590 t2.a < 10 and 591 t3.a+1 = 2 and 592 t3.a in (select t1.b from t1 593 where t1.a+1=t1.a+1 and 594 t1.a < (select t4.a+10 595 from t4, t5 limit 2)); 596 DROP TABLE IF EXISTS t1; 597 drop table if exists t2; 598 drop table if exists t3; 599 drop table if exists t4; 600 drop table if exists t5; 601 602 CREATE TABLE t1 (i1 int DEFAULT NULL,i2 int DEFAULT NULL) ; 603 INSERT INTO t1 VALUES (1, NULL); 604 INSERT INTO t1 VALUES (2, 3); 605 INSERT INTO t1 VALUES (4, NULL); 606 INSERT INTO t1 VALUES (4, 0); 607 INSERT INTO t1 VALUES (NULL, NULL); 608 CREATE TABLE t2 (i1 int DEFAULT NULL,i2 int DEFAULT NULL) ; 609 INSERT INTO t2 VALUES (4, NULL); 610 INSERT INTO t2 VALUES (5, 0); 611 SELECT i1, i2 612 FROM t1 613 WHERE (i1, i2) 614 NOT IN (SELECT i1, i2 FROM t2); 615 INSERT INTO t1 VALUES (NULL, NULL); 616 SELECT i1, i2 617 FROM t1 618 WHERE (i1, i2) 619 NOT IN (SELECT i1, i2 FROM t2); 620 621 drop table if exists t1; 622 drop table if exists t2; 623 CREATE TABLE t1 (a INT); 624 INSERT INTO t1 VALUES(1); 625 CREATE TABLE t2(a INT); 626 INSERT INTO t2 VALUES(1),(1),(1),(1),(1); 627 SELECT 628 ( 629 (SELECT 1 IN (SELECT 1 FROM t1 AS x1)) 630 IN 631 ( 632 SELECT 1 FROM t2 633 WHERE a IN (SELECT 4 FROM t1 AS x2) 634 ) 635 ) AS result 636 FROM t1 AS x3; 637 SELECT 638 ( 639 (36, (SELECT 1 IN (SELECT 1 FROM t1 AS x1))) 640 IN 641 ( 642 SELECT 36, 1 FROM t2 643 WHERE a IN (SELECT 4 FROM t1 AS x2) 644 ) 645 ) AS result 646 FROM t1 AS x3; 647 DROP TABLE IF EXISTS t1; 648 drop table if exists t2; 649 650 -- @case 651 -- @desc:test for [in] subquery with delete,update 652 -- @label:bvt 653 CREATE TABLE t1 ( 654 id int(11) default NULL 655 ) ; 656 INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3); 657 CREATE TABLE t2 ( 658 id int(11) default NULL, 659 name varchar(15) default NULL 660 ) ; 661 INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita'); 662 -- @ignore{ 663 update t2 set t2.name='lenka' where t2.id in (select id from t1); 664 select * from t2; 665 delete from t1 where t1.id in (select id from t2); 666 select * from t1; 667 -- @ignore} 668 DROP TABLE IF EXISTS t1; 669 drop table if exists t2; 670 671 -- @case 672 -- @desc:test for [in] subquery with order by 673 -- @label:bvt 674 DROP TABLE IF EXISTS t1; 675 drop table if exists t2; 676 DROP TABLE IF EXISTS t3; 677 CREATE TABLE t1 (a int(11) NOT NULL default 0, PRIMARY KEY (a)); 678 CREATE TABLE t2 (a int(11) default 0); 679 CREATE TABLE t3 (a int(11) default 0); 680 INSERT INTO t3 VALUES (1),(2),(3); 681 INSERT INTO t1 VALUES (1),(2),(3),(4); 682 INSERT INTO t2 VALUES (1),(2),(3); 683 SELECT t1.a FROM t1 where t1.a in (select t2.a from t2 order by t2.a desc) ; 684 685 -- @case 686 -- @desc:test for [in] subquery with compound index 687 -- @label:bvt 688 DROP TABLE IF EXISTS t1; 689 drop table if exists t2; 690 CREATE TABLE `t1` ( `aid` int(11) NOT NULL default 0, `bid` int(11) NOT NULL default 0); 691 CREATE TABLE `t2` ( `aid` int(11) NOT NULL default 0, `bid` int(11) NOT NULL default 0); 692 insert into t1 values (1,1),(1,2),(2,1),(2,2); 693 insert into t2 values (1,2),(2,2); 694 select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid); 695 select * from t1 where t1.aid in (select aid from t2 where bid=t1.bid); 696 697 698 -- @case 699 -- @desc:test for [in] subquery with range access in a subquery 700 -- @label:bvt 701 DROP TABLE IF EXISTS t1; 702 drop table if exists t2; 703 CREATE TABLE t1(select_id BIGINT, values_id BIGINT); 704 INSERT INTO t1 VALUES (1, 1); 705 CREATE TABLE t2 (select_id BIGINT, values_id BIGINT); 706 INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5); 707 SELECT values_id FROM t1 708 WHERE values_id IN (SELECT values_id FROM t2 WHERE select_id IN (1, 0)); 709 SELECT values_id FROM t1 WHERE values_id IN (SELECT values_id FROM t2 WHERE select_id BETWEEN 0 AND 1); 710 SELECT values_id FROM t1 WHERE values_id IN (SELECT values_id FROM t2 WHERE select_id = 0 OR select_id = 1); 711 712 DROP TABLE IF EXISTS t1; 713 drop table if exists t2; 714 CREATE TABLE t1 (a INT NOT NULL); 715 INSERT INTO t1 VALUES (1),(-1), (65),(66); 716 CREATE TABLE t2 (a INT UNSIGNED NOT NULL PRIMARY KEY); 717 INSERT INTO t2 VALUES (65),(66); 718 SELECT a FROM t1 WHERE a NOT IN (65,66); 719 SELECT a FROM t1 WHERE a NOT IN (SELECT a FROM t2); 720 721 DROP TABLE IF EXISTS t1; 722 drop table if exists t2; 723 CREATE TABLE t1 (a INT); 724 INSERT INTO t1 VALUES(1); 725 CREATE TABLE t2 (placeholder CHAR(11)); 726 INSERT INTO t2 VALUES("placeholder"); 727 -- @bvt:issue#7691 728 SELECT (1, 2) IN (SELECT t1.a, 2) FROM t1 GROUP BY t1.a; 729 SELECT (1, 2) IN (SELECT t1.a, 2 FROM t2) FROM t1 GROUP BY t1.a; 730 -- @bvt:issue 731 732 DROP TABLE IF EXISTS t1; 733 drop table if exists t2; 734 CREATE TABLE t1 (a INT); 735 INSERT INTO t1 VALUES (1),(2),(3); 736 CREATE TABLE t2 (a INT); 737 INSERT INTO t1 VALUES (1),(2),(3); 738 SELECT 1 FROM t1 WHERE t1.a NOT IN (SELECT 1 FROM t1, t2 WHERE false); 739 DROP TABLE IF EXISTS t1; 740 drop table if exists t2; 741 742 743 -- @case 744 -- @desc:test for [in] subquery with an aggregate function in HAVING 745 -- @label:bvt 746 DROP TABLE IF EXISTS t1; 747 drop table if exists t2; 748 drop table if exists t3; 749 CREATE TABLE t1 (a int, b int); 750 CREATE TABLE t2 (c int, d int); 751 CREATE TABLE t3 (e int); 752 INSERT INTO t1 VALUES 753 (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40); 754 INSERT INTO t2 VALUES 755 (2,10), (2,20), (4,10), (5,10), (3,20), (2,40); 756 INSERT INTO t3 VALUES (10), (30), (10), (20) ; 757 758 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20); 759 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d); 760 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d); 761 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 762 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 763 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e)); 764 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d)); 765 SELECT a FROM t1 GROUP BY a HAVING a IN (SELECT c FROM t2 WHERE MIN(b) < d AND EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d)); 766 767 768 -- @case 769 -- @desc:test for [in] subquery that is join 770 -- @label:bvt 771 DROP TABLE IF EXISTS t1; 772 drop table if exists t2; 773 drop table if exists t3; 774 create table t1 (a int, b int); 775 insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9); 776 create table t2 (a int, b int); 777 insert into t2 values (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9); 778 -- @ignore{ 779 update t2 set b=1; 780 create table t3 (a int, oref int); 781 insert into t3 values (1, 1), (NULL,1), (NULL,0); 782 select a, oref,t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z from t3; 783 784 DROP TABLE IF EXISTS t1; 785 drop table if exists t2; 786 drop table if exists t3; 787 create table t1 (a int NOT NULL, b int NOT NULL); 788 insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9); 789 create table t2 (a int, b int); 790 insert into t2 values (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9); 791 update t2 set b=1; 792 create table t3 (a int, oref int); 793 insert into t3 values (1, 1), (NULL,1), (NULL,0); 794 select a, oref,t3.a in (select t1.a from t1, t2 where t1.b=t2.a and t2.b=t3.oref) Z from t3; 795 796 DROP TABLE IF EXISTS t1; 797 drop table if exists t2; 798 drop table if exists t3; 799 create table t1 (oref int, grp int); 800 insert into t1 (oref, grp) values(1, 1),(1, 1); 801 create table t2 (oref int, a int); 802 insert into t2 values(1, NULL),(2, NULL); 803 select a, oref, a in (select count(*) from t1 group by grp having grp=t2.oref) Z from t2; 804 805 DROP TABLE IF EXISTS t1; 806 drop table if exists t2; 807 create table t1 (a int, b int, primary key (a)); 808 insert into t1 values (1,1), (3,1),(100,1); 809 create table t2 (a int, b int); 810 insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0); 811 select a,b, a in (select a from t1 where t1.b = t2.b union select a from t1 where t1.b = t2.b) Z from t2 ; 812 select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ; 813 814 815 DROP TABLE IF EXISTS t1; 816 drop table if exists t2; 817 drop table if exists t3; 818 drop table if exists t4; 819 create table t3 (a int); 820 insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); 821 create table t2 (a int, b int, oref int); 822 insert into t2 values (NULL,1, 100), (NULL,2, 100); 823 create table t1 (a int, b int, c int); 824 insert into t1 select 2*A, 2*A, 100 from t3; 825 create table t4 (x int); 826 insert into t4 select A.a + 10*B.a from t1 A, t1 B; 827 select a,b, oref, (a,b) in (select a,b from t1 where c=t2.oref) z from t2; 828 829 DROP TABLE IF EXISTS t1; 830 drop table if exists t2; 831 drop table if exists t3; 832 drop table if exists t4; 833 create table t1 (oref char(4), grp int, ie1 int, ie2 int); 834 insert into t1 (oref, grp, ie1, ie2) values('aa', 10, 2, 1),('aa', 10, 1, 1),('aa', 20, 2, 1),('bb', 10, 3, 1),('cc', 10, 4, 2),('cc', 20, 3, 2),('ee', 10, 2, 1),('ee', 10, 1, 2),('ff', 20, 2, 2),('ff', 20, 1, 2); 835 create table t2 (oref char(4), a int, b int); 836 insert into t2 values('ee', NULL, 1),('bb', 2, 1),('ff', 2, 2),('cc', 3, NULL),('bb', NULL, NULL),('aa', 1, 1),('dd', 1, NULL); 837 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=3 and b is null ; 838 insert into t2 values ('new1', 10,10); 839 insert into t1 values ('new1', 1234, 10, NULL); 840 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2 where a=10 and b=10; 841 842 DROP TABLE IF EXISTS t1; 843 drop table if exists t2; 844 create table t1 (oref char(4), grp int, ie int); 845 insert into t1 (oref, grp, ie) values ('aa', 10, 2),('aa', 10, 1),('aa', 20, NULL),('bb', 10, 3),('cc', 10, 4),('cc', 20, NULL),('ee', 10, NULL),('ee', 10, NULL),('ff', 20, 2),('ff', 20, 1); 846 create table t2 (oref char(4), a int); 847 insert into t2 values('ee', NULL),('bb', 2),('ff', 2),('cc', 3),('aa', 1),('dd', NULL),('bb', NULL); 848 select oref, a, a in (select ie from t1 where oref=t2.oref) Z from t2; 849 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); 850 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref); 851 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2; 852 select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref group by grp); 853 select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref group by grp); 854 update t1 set ie=3 where oref='ff' and ie=1; 855 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) Z from t2; 856 select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref group by grp); 857 select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref group by grp); 858 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp having min(ie) > 1) Z from t2; 859 select oref, a from t2 where a in (select min(ie) from t1 where oref=t2.oref group by grp having min(ie) > 1); 860 select oref, a from t2 where a not in (select min(ie) from t1 where oref=t2.oref group by grp having min(ie) > 1); 861 862 DROP TABLE IF EXISTS t1; 863 drop table if exists t2; 864 create table t1 (oref char(4), grp int, ie1 int, ie2 int); 865 insert into t1 (oref, grp, ie1, ie2) values ('aa', 10, 2, 1),('aa', 10, 1, 1),('aa', 20, 2, 1),('bb', 10, 3, 1),('cc', 10, 4, 2),('cc', 20, 3, 2),('ee', 10, 2, 1),('ee', 10, 1, 2),('ff', 20, 2, 2),('ff', 20, 1, 2); 866 create table t2 (oref char(4), a int, b int); 867 insert into t2 values('ee', NULL, 1),('bb', 2, 1), ('ff', 2, 2),('cc', 3, NULL),('bb', NULL, NULL),('aa', 1, 1),('dd', 1, NULL); 868 select oref, a, b, (a,b) in (select ie1,ie2 from t1 where oref=t2.oref) Z from t2; 869 select oref, a, b from t2 where (a,b) in (select ie1,ie2 from t1 where oref=t2.oref); 870 select oref, a, b from t2 where (a,b) not in (select ie1,ie2 from t1 where oref=t2.oref); 871 select oref, a, b,(a,b) in (select min(ie1),max(ie2) from t1 where oref=t2.oref group by grp) Z from t2; 872 select oref, a, b from t2 where (a,b) in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp); 873 select oref, a, b from t2 where (a,b) not in (select min(ie1), max(ie2) from t1 where oref=t2.oref group by grp); 874 875 DROP TABLE IF EXISTS t1; 876 drop table if exists t2; 877 create table t1 (oref char(4), grp int, ie int primary key); 878 insert into t1 (oref, grp, ie) values('aa', 10, 2),('aa', 10, 1),('bb', 10, 3),('cc', 10, 4),('cc', 20, 5),('cc', 10, 6); 879 create table t2 (oref char(4), a int); 880 insert into t2 values ('ee', NULL),('bb', 2),('cc', 5),('cc', 2),('cc', NULL),('aa', 1),('bb', NULL); 881 select oref, a, a in (select ie from t1 where oref=t2.oref) z from t2; 882 select oref, a from t2 where a in (select ie from t1 where oref=t2.oref); 883 select oref, a from t2 where a not in (select ie from t1 where oref=t2.oref); 884 select oref, a, a in (select min(ie) from t1 where oref=t2.oref group by grp) z from t2; 885 886 DROP TABLE IF EXISTS t1; 887 drop table if exists t2; 888 CREATE TABLE t1 (a int); 889 CREATE TABLE t2 (b int, PRIMARY KEY(b)); 890 INSERT INTO t1 VALUES (1), (NULL), (4); 891 INSERT INTO t2 VALUES (3), (1),(2), (5), (4), (7), (6); 892 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1)); 893 SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4)); 894 895 DROP TABLE IF EXISTS t1; 896 drop table if exists t2; 897 CREATE TABLE t1 (id int); 898 CREATE TABLE t2 (id int PRIMARY KEY); 899 CREATE TABLE t3 (id int PRIMARY KEY, name varchar(10)); 900 INSERT INTO t1 VALUES (2), (NULL), (3), (1); 901 INSERT INTO t2 VALUES (234), (345), (457); 902 INSERT INTO t3 VALUES (222,'bbb'), (333,'ccc'), (111,'aaa'); 903 SELECT * FROM t1 WHERE t1.id NOT IN (SELECT t2.id FROM t2,t3 WHERE t3.name='xxx' AND t2.id=t3.id); 904 SELECT (t1.id IN (SELECT t2.id FROM t2,t3 WHERE t3.name='xxx' AND t2.id=t3.id)) AS x FROM t1; 905 DROP TABLE IF EXISTS t1; 906 drop table if exists t2; 907 drop table if exists t3; 908 909 -- @bvt:issue#5955 910 CREATE TABLE t1 ( 911 pk INT PRIMARY KEY, 912 int_key INT, 913 varchar_key VARCHAR(5) UNIQUE, 914 varchar_nokey VARCHAR(5) 915 ); 916 INSERT INTO t1 VALUES (9, 7,NULL,NULL), (10,8,'p' ,'p'); 917 SELECT varchar_nokey FROM t1 918 WHERE NULL NOT IN ( 919 SELECT INNR.pk FROM t1 AS INNR2 920 LEFT JOIN t1 AS INNR ON ( INNR2.int_key = INNR.int_key ) 921 WHERE INNR.varchar_key > 'n{' 922 ); 923 DROP TABLE IF EXISTS t1; 924 -- @bvt:issue 925 drop table if exists t1; 926 drop table if exists t2; 927 CREATE TABLE t1(i INT); 928 INSERT INTO t1 VALUES (1), (2), (3); 929 CREATE TABLE t1s(i INT); 930 INSERT INTO t1s VALUES (10), (20), (30); 931 CREATE TABLE t2s(i INT); 932 INSERT INTO t2s VALUES (100), (200), (300); 933 SELECT * FROM t1 934 WHERE t1.i NOT IN 935 ( 936 SELECT t2s.i 937 FROM 938 t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 939 HAVING t2s.i = 999 940 ); 941 942 SELECT * FROM t1 943 WHERE t1.I IN 944 ( 945 SELECT t2s.i 946 FROM 947 t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 948 HAVING t2s.i = 999 949 ); 950 951 SELECT * FROM t1 952 WHERE NOT t1.I = ANY 953 ( 954 SELECT t2s.i 955 FROM 956 t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 957 HAVING t2s.i = 999 958 ); 959 960 SELECT * FROM t1 961 WHERE t1.i = ANY ( 962 SELECT t2s.i 963 FROM 964 t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i 965 HAVING t2s.i = 999 966 ); 967 968 DROP TABLE IF EXISTS t1; 969 drop table if exists t2; 970 drop table if exists t3; 971 CREATE TABLE parent (id int); 972 INSERT INTO parent VALUES (1), (2); 973 CREATE TABLE child (parent_id int, other int); 974 INSERT INTO child VALUES (1,NULL); 975 SELECT p.id, c.parent_id 976 FROM parent p 977 LEFT JOIN child c 978 ON p.id = c.parent_id 979 WHERE c.parent_id NOT IN ( 980 SELECT parent_id 981 FROM child 982 WHERE parent_id = 3 983 ); 984 985 SELECT p.id, c.parent_id 986 FROM parent p 987 LEFT JOIN child c 988 ON p.id = c.parent_id 989 WHERE c.parent_id IN ( 990 SELECT parent_id 991 FROM child 992 WHERE parent_id = 3 993 ); 994 995 SELECT p.id, c.parent_id 996 FROM parent p 997 LEFT JOIN child c 998 ON p.id = c.parent_id 999 WHERE c.parent_id IN ( 1000 SELECT parent_id 1001 FROM child 1002 WHERE parent_id = 3 1003 ); 1004 1005 DROP TABLE IF EXISTS parent; 1006 DROP TABLE IF EXISTS child; 1007 1008 DROP TABLE IF EXISTS cc; 1009 DROP TABLE IF EXISTS bb; 1010 DROP TABLE IF EXISTS c; 1011 DROP TABLE IF EXISTS b; 1012 CREATE TABLE cc ( 1013 pk INT, 1014 col_int_key INT, 1015 col_varchar_key VARCHAR(1), 1016 PRIMARY KEY (pk) 1017 ); 1018 INSERT INTO cc VALUES (10,7,'v'); 1019 INSERT INTO cc VALUES (11,1,'r'); 1020 1021 CREATE TABLE bb ( 1022 pk INT, 1023 col_date_key DATE, 1024 PRIMARY KEY (pk) 1025 ); 1026 INSERT INTO bb VALUES (10,'2002-02-21'); 1027 1028 CREATE TABLE c ( 1029 pk INT, 1030 col_int_key INT, 1031 col_varchar_key VARCHAR(1), 1032 PRIMARY KEY (pk) 1033 ); 1034 INSERT INTO c VALUES (1,NULL,'w'); 1035 INSERT INTO c VALUES (19,NULL,'f'); 1036 1037 CREATE TABLE b ( 1038 pk INT, 1039 col_int_key INT, 1040 col_varchar_key VARCHAR(1), 1041 PRIMARY KEY (pk) 1042 ); 1043 INSERT INTO b VALUES (1,7,'f'); 1044 1045 -- @bvt:issue#4139 1046 SELECT col_int_key 1047 FROM b granparent1 1048 WHERE (col_int_key, col_int_key) IN ( 1049 SELECT parent1.pk, parent1.pk 1050 FROM bb parent1 JOIN cc parent2 1051 ON parent2.col_varchar_key = parent2.col_varchar_key 1052 WHERE granparent1.col_varchar_key IN ( 1053 SELECT col_varchar_key 1054 FROM c) 1055 AND parent1.pk = granparent1.col_int_key 1056 ORDER BY parent1.col_date_key 1057 ); 1058 -- @bvt:issue 1059 1060 -- @case 1061 -- @desc:test for [in] subquery IN with a double "(())" 1062 -- @label:bvt 1063 DROP TABLE IF EXISTS t1; 1064 DROP TABLE IF EXISTS t2; 1065 DROP TABLE IF EXISTS t1xt2; 1066 CREATE TABLE t1 ( 1067 id_1 int(5) NOT NULL, 1068 t varchar(4) DEFAULT NULL 1069 ); 1070 CREATE TABLE t2 ( 1071 id_2 int(5) NOT NULL, 1072 t varchar(4) DEFAULT NULL 1073 ); 1074 CREATE TABLE t1xt2 ( 1075 id_1 int(5) NOT NULL, 1076 id_2 int(5) NOT NULL 1077 ); 1078 INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'); 1079 INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa'); 1080 INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4); 1081 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1082 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 1083 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 1084 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1085 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))); 1086 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)))); 1087 insert INTO t1xt2 VALUES (1, 12); 1088 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1089 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 1090 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 1091 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1092 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 1093 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 1094 insert INTO t1xt2 VALUES (2, 12); 1095 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1096 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 1097 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 1098 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)); 1099 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))); 1100 SELECT DISTINCT t1.id_1 FROM t1 WHERE(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)))); 1101 DROP TABLE IF EXISTS t1; 1102 DROP TABLE IF EXISTS t2; 1103 DROP TABLE IF EXISTS t1xt2; 1104 1105 -- @case 1106 -- @desc:test for wrong result for select NULL in (<SUBQUERY>) 1107 -- @label:bvt 1108 DROP TABLE IF EXISTS t1; 1109 CREATE TABLE t1(a int ); 1110 INSERT INTO t1 VALUES(0); 1111 SELECT NULL IN (SELECT 1 FROM t1); 1112 SELECT (NULL AND 1) IN (SELECT 1 FROM t1); 1113 SELECT (NULL, 1) IN (SELECT 1,1 FROM t1); 1114 SELECT (NULL, NULL) IN (SELECT 1,1 FROM t1); 1115 SELECT (NULL OR 1) IN (SELECT 1 FROM t1); 1116 SELECT (NULL IS NULL) IN (SELECT 1 FROM t1); 1117 DELETE FROM t1; 1118 SELECT NULL IN (SELECT 1 FROM t1); 1119 SELECT (NULL AND 1) IN (SELECT 1 FROM t1); 1120 SELECT (NULL, 1) IN (SELECT 1,1 FROM t1); 1121 SELECT (NULL, NULL) IN (SELECT 1,1 FROM t1); 1122 SELECT (NULL OR 1) IN (SELECT 1 FROM t1); 1123 SELECT (NULL IS NULL) IN (SELECT 1 FROM t1); 1124 1125 DROP TABLE IF EXISTS t1; 1126 CREATE TABLE t1 (a INTEGER); 1127 INSERT INTO t1 VALUES (1), (2), (3); 1128 -- @bvt:issue#4354 1129 SELECT 2 IN ( SELECT 5 UNION SELECT NULL ) FROM t1; 1130 -- @bvt:issue 1131 DROP TABLE IF EXISTS t1; 1132 1133 1134 1135