3 drop
table if exists t1, t2;
11 select NULL in (1,2,3);
12 select 1 in (1,NULL,3);
13 select 3 in (1,NULL,3);
14 select 10 in (1,NULL,3);
15 select 1.5 in (1.5,2.5,3.5);
16 select 10.5 in (1.5,2.5,3.5);
17 select NULL in (1.5,2.5,3.5);
18 select 1.5 in (1.5,NULL,3.5);
19 select 3.5 in (1.5,NULL,3.5);
20 select 10.5 in (1.5,NULL,3.5);
22 CREATE
TABLE t1 (a
int, b
int, c
int);
23 insert into t1 values (1,2,3), (1,NULL,3);
24 select 1 in (a,b,c) from t1;
25 select 3 in (a,b,c) from t1;
26 select 10 in (a,b,c) from t1;
27 select NULL in (a,b,c) from t1;
29 CREATE
TABLE t1 (a
float, b
float, c
float);
30 insert into t1 values (1.5,2.5,3.5), (1.5,NULL,3.5);
31 select 1.5 in (a,b,c) from t1;
32 select 3.5 in (a,b,c) from t1;
33 select 10.5 in (a,b,c) from t1;
35 CREATE
TABLE t1 (a varchar(10), b varchar(10), c varchar(10));
36 insert into t1 values ('
A','BC','EFD'), ('A',NULL,'EFD');
37 select 'A' in (a,b,c) from t1;
38 select 'EFD' in (a,b,c) from t1;
39 select 'XSFGGHF' in (a,b,c) from t1;
42 CREATE
TABLE t1 (field
char(1));
43 INSERT INTO t1 VALUES ('A'),(NULL);
44 SELECT * from t1 WHERE field IN (NULL);
45 SELECT * from t1 WHERE field NOT IN (NULL);
46 SELECT * from t1 where field = field;
47 SELECT * from t1 where field <=> field;
48 DELETE FROM t1 WHERE field NOT IN (NULL);
52 create
table t1 (
id int(10) primary key);
53 insert into t1 values (1),(2),(3),(4),(5),(6),(7),(8),(9);
54 select * from t1 where
id in (2,5,9);
58 a
char(1) character set latin1 collate latin1_general_ci,
59 b
char(1) character set latin1 collate latin1_swedish_ci,
60 c
char(1) character set latin1 collate latin1_danish_ci
62 insert into t1 values ('A','B','C');
63 insert into t1 values ('a','c','c');
65 select * from t1 where a in (b);
67 select * from t1 where a in (b,c);
69 select * from t1 where 'a' in (a,b,c);
70 select * from t1 where 'a' in (a);
71 select * from t1 where a in ('a');
72 select * from t1 where 'a' collate latin1_general_ci in (a,b,c);
73 select * from t1 where 'a' collate latin1_bin in (a,b,c);
74 select * from t1 where 'a' in (a,b,c collate latin1_bin);
75 explain extended select * from t1 where 'a' in (a,b,c collate latin1_bin);
79 create
table t1 (a
char(10) character set utf8 not null);
80 insert into t1 values ('bbbb'),(_koi8r'ÃÃÃÃ'),(_latin1'ÄÄÄÄ');
81 select a from t1 where a in ('bbbb',_koi8r'ÃÃÃÃ',_latin1'ÄÄÄÄ') order by a;
83 # Bug#7834 Illegal mix of collations in IN operator
84 create
table t1 (a
char(10) character
set latin1 not null);
85 insert into t1 values (
'a'),(
'b'),(
'c');
86 select a from t1 where a IN (
'a',
'b',
'c') order by a;
90 select '1.0' in (1,2);
91 select 1 in ('1.0',2);
92 select 1 in (1,'2.0');
93 select 1 in ('1.0',2.0);
94 select 1 in (1.0,'2.0');
95 select 1 in ('1.1',2);
96 select 1 in ('1.1',2.0);
98 # Test case for bug #6365
100 create
table t1 (a
char(2) character
set binary);
101 insert into t1 values (
'aa'), (
'bb');
102 select * from t1 where a in (NULL,
'aa');
106 create
table t1 (
id int, key(
id));
107 insert into t1 values (1),(2),(3);
108 select count(*) from t1 where
id not in (1);
109 select count(*) from t1 where
id not in (1,2);
114 # BUG#17047: CHAR() and IN() can return NULL without signaling NULL
117 # The problem was in the IN() function that ignored maybe_null flags
118 # of all arguments except the first (the one _before_ the IN
119 # keyword, '1' in the test case below).
122 DROP
TABLE IF EXISTS t1;
125 CREATE
TABLE t1 SELECT 1 IN (2, NULL);
126 --echo SELECT should
return NULL.
132 --echo End of 4.1 tests
136 # Bug #11885: WHERE condition with NOT IN (one element)
139 CREATE
TABLE t1 (a
int PRIMARY
KEY);
140 INSERT INTO t1 VALUES (44), (45), (46);
142 SELECT * FROM t1 WHERE a IN (45);
143 SELECT * FROM t1 WHERE a NOT IN (0, 45);
144 SELECT * FROM t1 WHERE a NOT IN (45);
146 CREATE VIEW v1 AS SELECT * FROM t1 WHERE a NOT IN (45);
153 # BUG#15872: Excessive memory consumption of range analysis of NOT IN
154 create
table t1 (a
int);
155 insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
156 create
table t2 (a
int, filler
char(200), key(a));
158 insert into t2 select C.a*2,
'no' from t1 A, t1 B, t1 C;
159 insert into t2 select C.a*2+1,
'yes' from t1 C;
162 select * from t2 where a NOT IN (0, 2,4,6,8,10,12,14,16,18);
163 select * from t2 where a NOT IN (0, 2,4,6,8,10,12,14,16,18);
165 explain select * from t2 force
index(a) where a NOT IN (2,2,2,2,2,2);
166 explain select * from t2 force
index(a) where a <> 2;
171 # Repeat the test for DATETIME
173 create
table t2 (a datetime, filler
char(200), key(a));
175 insert into t2 select
'2006-04-25 10:00:00' + interval C.a minute,
176 'no' from t1 A, t1 B, t1 C where C.a % 2 = 0;
178 insert into t2 select
'2006-04-25 10:00:00' + interval C.a*2+1 minute,
182 select * from t2 where a NOT IN (
183 '2006-04-25 10:00:00',
'2006-04-25 10:02:00',
'2006-04-25 10:04:00',
184 '2006-04-25 10:06:00',
'2006-04-25 10:08:00');
185 select * from t2 where a NOT IN (
186 '2006-04-25 10:00:00',
'2006-04-25 10:02:00',
'2006-04-25 10:04:00',
187 '2006-04-25 10:06:00',
'2006-04-25 10:08:00');
191 # Repeat the test for CHAR(N)
193 create
table t2 (a varchar(10), filler
char(200), key(a));
195 insert into t2 select
'foo',
'no' from t1 A, t1 B;
196 insert into t2 select
'barbar',
'no' from t1 A, t1 B;
197 insert into t2 select
'bazbazbaz',
'no' from t1 A, t1 B;
199 insert into t2 values (
'fon',
'1'), (
'fop',
'1'), (
'barbaq',
'1'),
200 (
'barbas',
'1'), (
'bazbazbay',
'1'),(
'zz',
'1');
202 explain select * from t2 where a not in(
'foo',
'barbar',
'bazbazbaz');
209 create
table t2 (a decimal(10,5), filler
char(200), key(a));
211 insert into t2 select 345.67890,
'no' from t1 A, t1 B;
212 insert into t2 select 43245.34,
'no' from t1 A, t1 B;
213 insert into t2 select 64224.56344,
'no' from t1 A, t1 B;
215 insert into t2 values (0,
'1'), (22334.123,
'1'), (33333,
'1'),
216 (55555,
'1'), (77777,
'1');
219 select * from t2 where a not in (345.67890, 43245.34, 64224.56344);
220 select * from t2 where a not in (345.67890, 43245.34, 64224.56344);
224 # Try a very big IN-list
225 create
table t2 (a
int, key(a), b
int);
226 insert into t2 values (1,1),(2,2);
229 set @str=
"update t2 set b=1 where a not in (";
230 select count(*) from (
231 select @str:=concat(@str, @cnt:=@cnt+1, ",")
232 from t1 A, t1 B, t1 C, t1 D) Z;
234 set @str:=concat(@str, "10000)");
235 select substr(@str, 1, 50);
238 deallocate prepare s;
244 # BUG#19618: Crash in range optimizer for
245 # "unsigned_keypart NOT IN(negative_number,...)"
246 # (introduced in fix BUG#15872)
248 some_id smallint(5)
unsigned,
251 insert into t1 values (1),(2);
252 select some_id from t1 where some_id not in(2,-1);
253 select some_id from t1 where some_id not in(-4,-1,-4);
254 select some_id from t1 where some_id not in(-4,-1,3423534,2342342);
257 # BUG#24261: crash when WHERE contains NOT IN ('<negative value>') for unsigned column type
260 select some_id from t1 where some_id not in(
'-1',
'0');
265 # BUG#20420: optimizer reports wrong keys on left join with IN
267 CREATE
TABLE t1 (a
int, b
int, PRIMARY
KEY (a));
268 INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1),(5,1),(6,1);
270 CREATE
TABLE t2 (a
int, b
int, PRIMARY
KEY (a));
271 INSERT INTO t2 VALUES (3,2),(4,2),(100,100),(101,201),(102,102);
273 CREATE
TABLE t3 (a
int PRIMARY
KEY);
274 INSERT INTO t3 VALUES (1),(2),(3),(4);
276 CREATE
TABLE t4 (a
int PRIMARY
KEY,b
int);
277 INSERT INTO t4 VALUES (1,1),(2,2),(1000,1000),(1001,1001),(1002,1002),
278 (1003,1003),(1004,1004);
280 EXPLAIN SELECT STRAIGHT_JOIN * FROM t3
283 JOIN t4 WHERE t4.a IN (t1.b, t2.b);
285 SELECT STRAIGHT_JOIN * FROM t3
288 JOIN t4 WHERE t4.a IN (t1.b, t2.b);
290 EXPLAIN SELECT STRAIGHT_JOIN
291 (SELECT SUM(t4.a) FROM t4 WHERE t4.a IN (t1.b, t2.b))
293 WHERE t3.a=t1.a AND t3.a=t2.a;
296 (SELECT SUM(t4.a) FROM t4 WHERE t4.a IN (t1.b, t2.b))
298 WHERE t3.a=t1.a AND t3.a=t2.a;
300 DROP
TABLE t1,t2,t3,t4;
303 # BUG#19342: IN works incorrectly for BIGINT UNSIGNED values
305 CREATE
TABLE t1(a BIGINT UNSIGNED);
306 INSERT INTO t1 VALUES (0xFFFFFFFFFFFFFFFF);
308 SELECT * FROM t1 WHERE a=-1 OR a=-2 ;
309 SELECT * FROM t1 WHERE a IN (-1, -2);
311 CREATE
TABLE t2 (a BIGINT UNSIGNED);
312 insert into t2 values(13491727406643098568),
313 (0x7fffffefffffffff),
314 (0x7ffffffeffffffff),
315 (0x7fffffffefffffff),
316 (0x7ffffffffeffffff),
317 (0x7fffffffffefffff),
318 (0x7ffffffffffeffff),
319 (0x7fffffffffffefff),
320 (0x7ffffffffffffeff),
321 (0x7fffffffffffffef),
322 (0x7ffffffffffffffe),
323 (0x7fffffffffffffff),
324 (0x8000000000000000),
325 (0x8000000000000001),
326 (0x8000000000000002),
327 (0x8000000000000300),
328 (0x8000000000000400),
329 (0x8000000000000401),
330 (0x8000000000004001),
331 (0x8000000000040001),
332 (0x8000000000400001),
333 (0x8000000004000001),
334 (0x8000000040000001),
335 (0x8000000400000001),
336 (0x8000004000000001),
337 (0x8000040000000001);
339 SELECT HEX(a) FROM t2 WHERE a IN
340 (CAST(0xBB3C3E98175D33C8 AS UNSIGNED),
343 SELECT HEX(a) FROM t2 WHERE a IN
344 (CAST(0xBB3C3E98175D33C8 AS UNSIGNED),
345 CAST(0x7fffffffffffffff AS UNSIGNED),
346 CAST(0x8000000000000000 AS UNSIGNED),
347 CAST(0x8000000000000400 AS UNSIGNED),
348 CAST(0x8000000000000401 AS UNSIGNED),
351 SELECT HEX(a) FROM t2 WHERE a IN
352 (CAST(0x7fffffffffffffff AS UNSIGNED),
353 CAST(0x8000000000000001 AS UNSIGNED));
354 SELECT HEX(a) FROM t2 WHERE a IN
355 (CAST(0x7ffffffffffffffe AS UNSIGNED),
356 CAST(0x7fffffffffffffff AS UNSIGNED));
357 SELECT HEX(a) FROM t2 WHERE a IN
362 CREATE
TABLE t3 (a BIGINT UNSIGNED);
363 INSERT INTO t3 VALUES (9223372036854775551);
365 SELECT HEX(a) FROM t3 WHERE a IN (9223372036854775807, 42);
367 CREATE
TABLE t4 (a DATE);
368 INSERT INTO t4 VALUES ('1972-02-06'), ('1972-07-29');
369 SELECT * FROM t4 WHERE a IN ('1972-02-06','19772-07-29');
371 DROP
TABLE t1,t2,t3,t4;
374 # BUG#27362: IN with a decimal expression that may return NULL
377 CREATE
TABLE t1 (
id int not null);
378 INSERT INTO t1 VALUES (1),(2);
380 SELECT
id FROM t1 WHERE
id IN(4564, (SELECT IF(1=0,1,1/0)) );
384 --echo End of 5.0 tests
388 # Bug#18360: Type aggregation for IN and CASE may lead to a wrong result
390 create
table t1(f1
char(1));
391 insert into t1 values (
'a'),(
'b'),(
'1');
392 select f1 from t1 where f1 in (
'a',1);
393 select f1,
case f1 when
'a' then
'+' when 1 then
'-' end from t1;
394 create
index t1f1_idx on t1(f1);
395 select f1 from t1 where f1 in (
'a',1);
396 explain select f1 from t1 where f1 in (
'a',1);
397 select f1 from t1 where f1 in (
'a',
'b');
398 explain select f1 from t1 where f1 in (
'a',
'b');
399 select f1 from t1 where f1 in (2,1);
400 explain select f1 from t1 where f1 in (2,1);
402 insert into t2 values(0),(1),(2);
403 select f2 from t2 where f2 in (
'a',2);
404 explain select f2 from t2 where f2 in (
'a',2);
405 select f2 from t2 where f2 in (
'a',
'b');
406 explain select f2 from t2 where f2 in (
'a',
'b');
407 select f2 from t2 where f2 in (1,
'b');
408 explain select f2 from t2 where f2 in (1,
'b');
412 # Bug #31075: crash in get_func_mm_tree
415 create
table t1 (a time, key(a));
416 insert into t1 values (),(),(),(),(),(),(),(),(),();
417 select a from t1 where a not in (a,a,a)
group by a;
421 # Bug #37761: IN handles NULL differently for table-subquery and value-list
424 create
table t1 (
id int);
425 select * from t1 where NOT
id in (select null
union all select 1);
426 select * from t1 where NOT
id in (null, 1);
430 # Bug #41363: crash of mysqld on windows with aggregate in case
433 CREATE
TABLE t1(c0 INTEGER, c1 INTEGER, c2 INTEGER);
434 INSERT INTO t1 VALUES(1, 1, 1), (1, 1, 1);
436 SELECT
CASE AVG (c0) WHEN c1 * c2 THEN 1 END FROM t1;
437 SELECT
CASE c1 * c2 WHEN SUM(c0) THEN 1 WHEN AVG(c0) THEN 2 END FROM t1;
438 SELECT
CASE c1 WHEN c1 + 1 THEN 1 END, ABS(AVG(c0)) FROM t1;
443 # Bug #44399: crash with statement using TEXT columns, aggregates, GROUP BY,
447 CREATE
TABLE t1(a TEXT, b INT, c INT UNSIGNED, d DECIMAL(12,2), e REAL);
448 INSERT INTO t1 VALUES(
'iynfj', 1, 1, 1, 1);
449 INSERT INTO t1 VALUES(
'innfj', 2, 2, 2, 2);
450 SELECT SUM( DISTINCT a ) FROM t1 GROUP BY a HAVING a IN ( AVG( 1 ), 1 + a);
451 SELECT SUM( DISTINCT b ) FROM t1 GROUP BY b HAVING b IN ( AVG( 1 ), 1 + b);
452 SELECT SUM( DISTINCT c ) FROM t1 GROUP BY c HAVING c IN ( AVG( 1 ), 1 + c);
453 SELECT SUM( DISTINCT d ) FROM t1 GROUP BY d HAVING d IN ( AVG( 1 ), 1 + d);
454 SELECT SUM( DISTINCT e ) FROM t1 GROUP BY e HAVING e IN ( AVG( 1 ), 1 + e);
455 SELECT SUM( DISTINCT e ) FROM t1 GROUP BY b,c,d HAVING (b,c,d) IN
456 ((AVG( 1 ), 1 + c, 1 + d), (AVG( 1 ), 2 + c, 2 + d));
460 --echo # Bug #44139: Table scan when NULL appears in IN clause
467 c_decimal DECIMAL(5,2) NOT NULL,
468 c_float FLOAT(5, 2) NOT NULL,
469 c_bit BIT(10) NOT NULL,
470 c_date DATE NOT NULL,
471 c_datetime DATETIME NOT NULL,
472 c_timestamp TIMESTAMP NOT NULL,
473 c_time TIME NOT NULL,
474 c_year YEAR NOT NULL,
475 c_char CHAR(10) NOT NULL,
476 INDEX(c_int), INDEX(c_decimal), INDEX(c_float), INDEX(c_bit), INDEX(c_date),
477 INDEX(c_datetime), INDEX(c_timestamp), INDEX(c_time), INDEX(c_year),
480 INSERT INTO t1 (c_int) VALUES (1), (2), (3), (4), (5);
481 INSERT INTO t1 (c_int) SELECT 0 FROM t1;
482 INSERT INTO t1 (c_int) SELECT 0 FROM t1;
486 EXPLAIN SELECT * FROM t1 WHERE c_int IN (1, 2, 3);
487 EXPLAIN SELECT * FROM t1 WHERE c_int IN (NULL, 1, 2, 3);
489 EXPLAIN SELECT * FROM t1 WHERE c_int IN (1, 2, 3);
490 EXPLAIN SELECT * FROM t1 WHERE c_int IN (1, NULL, 2, NULL, 3, NULL);
491 EXPLAIN SELECT * FROM t1 WHERE c_int IN (NULL);
492 EXPLAIN SELECT * FROM t1 WHERE c_int IN (NULL, NULL);
494 EXPLAIN SELECT * FROM t1 WHERE c_decimal IN (1, 2, 3);
495 EXPLAIN SELECT * FROM t1 WHERE c_decimal IN (NULL, 1, 2, 3);
496 EXPLAIN SELECT * FROM t1 WHERE c_decimal IN (NULL);
497 EXPLAIN SELECT * FROM t1 WHERE c_decimal IN (NULL, NULL);
499 EXPLAIN SELECT * FROM t1 WHERE c_float IN (1, 2, 3);
500 EXPLAIN SELECT * FROM t1 WHERE c_float IN (NULL, 1, 2, 3);
501 EXPLAIN SELECT * FROM t1 WHERE c_float IN (NULL);
502 EXPLAIN SELECT * FROM t1 WHERE c_float IN (NULL, NULL);
504 EXPLAIN SELECT * FROM t1 WHERE c_bit IN (1, 2, 3);
505 EXPLAIN SELECT * FROM t1 WHERE c_bit IN (NULL, 1, 2, 3);
506 EXPLAIN SELECT * FROM t1 WHERE c_bit IN (NULL);
507 EXPLAIN SELECT * FROM t1 WHERE c_bit IN (NULL, NULL);
509 EXPLAIN SELECT * FROM t1 WHERE c_date
510 IN ('2009-09-01', '2009-09-02', '2009-09-03');
511 EXPLAIN SELECT * FROM t1 WHERE c_date
512 IN (NULL, '2009-09-01', '2009-09-02', '2009-09-03');
513 EXPLAIN SELECT * FROM t1 WHERE c_date IN (NULL);
514 EXPLAIN SELECT * FROM t1 WHERE c_date IN (NULL, NULL);
516 EXPLAIN SELECT * FROM t1 WHERE c_datetime
517 IN ('2009-09-01 00:00:01', '2009-09-02 00:00:01', '2009-09-03 00:00:01');
518 EXPLAIN SELECT * FROM t1 WHERE c_datetime
519 IN (NULL, '2009-09-01 00:00:01', '2009-09-02 00:00:01', '2009-09-03 00:00:01');
520 EXPLAIN SELECT * FROM t1 WHERE c_datetime IN (NULL);
521 EXPLAIN SELECT * FROM t1 WHERE c_datetime IN (NULL, NULL);
523 EXPLAIN SELECT * FROM t1 WHERE c_timestamp
524 IN ('2009-09-01 00:00:01', '2009-09-01 00:00:02', '2009-09-01 00:00:03');
525 EXPLAIN SELECT * FROM t1 WHERE c_timestamp
526 IN (NULL, '2009-09-01 00:00:01', '2009-09-01 00:00:02', '2009-09-01 00:00:03');
527 EXPLAIN SELECT * FROM t1 WHERE c_timestamp IN (NULL);
528 EXPLAIN SELECT * FROM t1 WHERE c_timestamp IN (NULL, NULL);
530 EXPLAIN SELECT * FROM t1 WHERE c_year IN (1, 2, 3);
531 EXPLAIN SELECT * FROM t1 WHERE c_year IN (NULL, 1, 2, 3);
532 EXPLAIN SELECT * FROM t1 WHERE c_year IN (NULL);
533 EXPLAIN SELECT * FROM t1 WHERE c_year IN (NULL, NULL);
535 EXPLAIN SELECT * FROM t1 WHERE c_char IN ('1', '2', '3');
536 EXPLAIN SELECT * FROM t1 WHERE c_char IN (NULL, '1', '2', '3');
537 EXPLAIN SELECT * FROM t1 WHERE c_char IN (NULL);
538 EXPLAIN SELECT * FROM t1 WHERE c_char IN (NULL, NULL);
543 --echo # Bug#54477: Crash on IN /
CASE with NULL arguments
546 CREATE
TABLE t1 (a INT);
547 INSERT INTO t1 VALUES (1), (2);
549 SELECT 1 IN (NULL, a) FROM t1;
551 SELECT a IN (a, a) FROM t1 GROUP BY a WITH
ROLLUP;
553 SELECT
CASE a WHEN a THEN a END FROM t1 GROUP BY a WITH ROLLUP;
558 --echo # Bug#58628: Incorrect result
for 'WHERE NULL NOT IN (<subquery>)
561 CREATE TABLE t1 (pk INT NOT NULL, i INT);
562 INSERT INTO t1 VALUES (0,NULL), (1,NULL), (2,NULL), (3,NULL);
564 CREATE TABLE subq (pk INT NOT NULL, i INT NOT NULL, PRIMARY KEY(i,pk));
565 INSERT INTO subq VALUES (0,0), (1,1), (2,2), (3,3);
567 ## Baseline queries: t1.i contains only NULL and should effectively
568 ## be evaluated as 'WHERE NULL IN
'
569 ## .. These return correct resultset !
574 (SELECT i FROM subq WHERE subq.pk = t1.pk);
579 (SELECT i FROM subq WHERE subq.pk = t1.pk) IS UNKNOWN;
581 ## Replaced 't1.i
' with some constant expression which
582 ## also evaluates to NULL. Expected to return same result as above:
587 (SELECT i FROM subq WHERE subq.pk = t1.pk);
592 (SELECT i FROM subq WHERE subq.pk = t1.pk) IS UNKNOWN;
597 (SELECT i FROM subq WHERE subq.pk = t1.pk);
602 --echo # Bug #11766270 59343: YEAR(4): INCORRECT RESULT AND VALGRIND WARNINGS WITH MIN/MAX, UNION
605 CREATE TABLE t1(f1 YEAR(4));
606 INSERT INTO t1 VALUES (0000),(2001);
608 (SELECT MAX(f1) FROM t1) UNION (SELECT MAX(f1) FROM t1);
613 --echo # Bug #11764651-57510: IN(string,real,string) causes invalid read in sort function
616 SELECT LEFT(GEOMFROMTEXT("POINT(0 0)"),1) IN (@@global.query_cache_type,1,"");
618 --echo # End of test BUG#11764651-57510
620 --echo End of 5.1 tests