📄 join_outer.test
字号:
);INSERT INTO t1 VALUES (1,'yes'),(2,'no');CREATE TABLE t2 ( id int(11) DEFAULT '0' NOT NULL, idx int(11) DEFAULT '0' NOT NULL, UNIQUE id (id,idx));INSERT INTO t2 VALUES (1,1);explain SELECT * from t1 left join t2 on t1.id=t2.id where t2.id IS NULL;SELECT * from t1 left join t2 on t1.id=t2.id where t2.id IS NULL;drop table t1,t2;## Test problem with using key_column= constant in ON and WHERE#create table t1 (bug_id mediumint, reporter mediumint);create table t2 (bug_id mediumint, who mediumint, index(who));insert into t2 values (1,1),(1,2);insert into t1 values (1,1),(2,1);SELECT * FROM t1 LEFT JOIN t2 ON (t1.bug_id = t2.bug_id AND t2.who = 2) WHERE (t1.reporter = 2 OR t2.who = 2);drop table t1,t2;## Test problem with LEFT JOINcreate table t1 (fooID smallint unsigned auto_increment, primary key (fooID));create table t2 (fooID smallint unsigned not null, barID smallint unsigned not null, primary key (fooID,barID));insert into t1 (fooID) values (10),(20),(30);insert into t2 values (10,1),(20,2),(30,3);explain select * from t2 left join t1 on t1.fooID = t2.fooID and t1.fooID = 30;select * from t2 left join t1 on t1.fooID = t2.fooID and t1.fooID = 30;select * from t2 left join t1 ignore index(primary) on t1.fooID = t2.fooID and t1.fooID = 30;drop table t1,t2;create table t1 (i int);create table t2 (i int);create table t3 (i int);insert into t1 values(1),(2);insert into t2 values(2),(3);insert into t3 values(2),(4);select * from t1 natural left join t2 natural left join t3;select * from t1 natural left join t2 where (t2.i is not null)=0;select * from t1 natural left join t2 where (t2.i is not null) is not null;select * from t1 natural left join t2 where (i is not null)=0;select * from t1 natural left join t2 where (i is not null) is not null;drop table t1,t2,t3;## Test of USING#create table t1 (f1 integer,f2 integer,f3 integer);create table t2 (f2 integer,f4 integer);create table t3 (f3 integer,f5 integer);select * from t1 left outer join t2 using (f2) left outer join t3 using (f3);drop table t1,t2,t3;create table t1 (a1 int, a2 int);create table t2 (b1 int not null, b2 int);create table t3 (c1 int, c2 int);insert into t1 values (1,2), (2,2), (3,2);insert into t2 values (1,3), (2,3);insert into t3 values (2,4), (3,4);select * from t1 left join t2 on b1 = a1 left join t3 on c1 = a1 and b1 is null;explain select * from t1 left join t2 on b1 = a1 left join t3 on c1 = a1 and b1 is null;drop table t1, t2, t3;# Test for BUG#8711 '<=>' was considered to be a NULL-rejecting predicate.create table t1 ( a int(11), b char(10), key (a));insert into t1 (a) values (1),(2),(3),(4);create table t2 (a int);select * from t1 left join t2 on t1.a=t2.a where not (t2.a <=> t1.a);select * from t1 left join t2 on t1.a=t2.a having not (t2.a <=> t1.a);drop table t1,t2;# Test for BUG#5088create table t1 ( match_id tinyint(3) unsigned not null auto_increment, home tinyint(3) unsigned default '0', unique key match_id (match_id), key match_id_2 (match_id));insert into t1 values("1", "2");create table t2 ( player_id tinyint(3) unsigned default '0', match_1_h tinyint(3) unsigned default '0', key player_id (player_id));insert into t2 values("1", "5");insert into t2 values("2", "9");insert into t2 values("3", "3");insert into t2 values("4", "7");insert into t2 values("5", "6");insert into t2 values("6", "8");insert into t2 values("7", "4");insert into t2 values("8", "12");insert into t2 values("9", "11");insert into t2 values("10", "10");explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from (t2 s left join t1 m on m.match_id = 1) order by m.match_id desc; explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from (t2 s left join t1 m on m.match_id = 1) order by UUX desc;select s.*, '*', m.*, (s.match_1_h - m.home) UUX from (t2 s left join t1 m on m.match_id = 1) order by UUX desc;explain select s.*, '*', m.*, (s.match_1_h - m.home) UUX from t2 s straight_join t1 m where m.match_id = 1 order by UUX desc;select s.*, '*', m.*, (s.match_1_h - m.home) UUX from t2 s straight_join t1 m where m.match_id = 1 order by UUX desc;drop table t1, t2;# Tests for bugs #6307 and 6460create table t1 (a int, b int, unique index idx (a, b));create table t2 (a int, b int, c int, unique index idx (a, b));insert into t1 values (1, 10), (1,11), (2,10), (2,11);insert into t2 values (1,10,3);select t1.a, t1.b, t2.c from t1 left join t2 on t1.a=t2.a and t1.b=t2.b and t2.c=3 where t1.a=1 and t2.c is null;drop table t1, t2;CREATE TABLE t1 ( ts_id bigint(20) default NULL, inst_id tinyint(4) default NULL, flag_name varchar(64) default NULL, flag_value text, UNIQUE KEY ts_id (ts_id,inst_id,flag_name)) ENGINE=MyISAM DEFAULT CHARSET=utf8;CREATE TABLE t2 ( ts_id bigint(20) default NULL, inst_id tinyint(4) default NULL, flag_name varchar(64) default NULL, flag_value text, UNIQUE KEY ts_id (ts_id,inst_id,flag_name)) ENGINE=MyISAM DEFAULT CHARSET=utf8;INSERT INTO t1 VALUES (111056548820001, 0, 'flag1', NULL), (111056548820001, 0, 'flag2', NULL), (2, 0, 'other_flag', NULL);INSERT INTO t2 VALUES (111056548820001, 3, 'flag1', 'sss');SELECT t1.flag_name,t2.flag_value FROM t1 LEFT JOIN t2 ON (t1.ts_id = t2.ts_id AND t1.flag_name = t2.flag_name AND t2.inst_id = 3) WHERE t1.inst_id = 0 AND t1.ts_id=111056548820001 AND t2.flag_value IS NULL;DROP TABLE t1,t2;CREATE TABLE t1 ( id int(11) unsigned NOT NULL auto_increment, text_id int(10) unsigned default NULL, PRIMARY KEY (id));INSERT INTO t1 VALUES("1", "0");INSERT INTO t1 VALUES("2", "10");CREATE TABLE t2 ( text_id char(3) NOT NULL default '', language_id char(3) NOT NULL default '', text_data text, PRIMARY KEY (text_id,language_id));INSERT INTO t2 VALUES("0", "EN", "0-EN");INSERT INTO t2 VALUES("0", "SV", "0-SV");INSERT INTO t2 VALUES("10", "EN", "10-EN");INSERT INTO t2 VALUES("10", "SV", "10-SV");SELECT t1.id, t1.text_id, t2.text_data FROM t1 LEFT JOIN t2 ON t1.text_id = t2.text_id AND t2.language_id = 'SV' WHERE (t1.id LIKE '%' OR t2.text_data LIKE '%');DROP TABLE t1, t2;# Test for bug #5896 CREATE TABLE t0 (a0 int PRIMARY KEY);CREATE TABLE t1 (a1 int PRIMARY KEY);CREATE TABLE t2 (a2 int);CREATE TABLE t3 (a3 int);INSERT INTO t0 VALUES (1);INSERT INTO t1 VALUES (1);INSERT INTO t2 VALUES (1), (2);INSERT INTO t3 VALUES (1), (2);SELECT * FROM t1 LEFT JOIN t2 ON a1=0;EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON a1=0;SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0;EXPLAIN SELECT * FROM t1 LEFT JOIN (t2,t3) ON a1=0;SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1;EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=0 WHERE a0=a1;INSERT INTO t0 VALUES (0);INSERT INTO t1 VALUES (0);SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1;EXPLAIN SELECT * FROM t0, t1 LEFT JOIN (t2,t3) ON a1=5 WHERE a0=a1 AND a0=1;# Test for BUG#4480drop table t1,t2;create table t1 (a int, b int);insert into t1 values (1,1),(2,2),(3,3);create table t2 (a int, b int);insert into t2 values (1,1), (2,2);select * from t2 right join t1 on t2.a=t1.a;select straight_join * from t2 right join t1 on t2.a=t1.a;DROP TABLE t0,t1,t2,t3;## Test for bug #9017: left join mistakingly converted to inner join#CREATE TABLE t1 (a int PRIMARY KEY, b int);CREATE TABLE t2 (a int PRIMARY KEY, b int);INSERT INTO t1 VALUES (1,1), (2,1), (3,1), (4,2);INSERT INTO t2 VALUES (1,2), (2,2);SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a;SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE t1.b=1;SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE t1.b=1 XOR (NOT ISNULL(t2.a) AND t2.b=1);SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a WHERE not(0+(t1.a=30 and t2.b=1));DROP TABLE t1,t2;# Bug #8681: Bad warning message when group_concat() exceeds max lengthset group_concat_max_len=5;create table t1 (a int, b varchar(20));create table t2 (a int, c varchar(20));insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb");insert into t2 values (1,"cccccccccc"),(2,"dddddddddd");select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a;select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a;select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a;select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by a;drop table t1, t2;set group_concat_max_len=default;# End of 4.1 tests## BUG#10162 - ON is merged with WHERE, left join is convered to a regular join#create table t1 (gid smallint(5) unsigned not null, x int(11) not null, y int(11) not null, art int(11) not null, primary key (gid,x,y));insert t1 values (1, -5, -8, 2), (1, 2, 2, 1), (1, 1, 1, 1);create table t2 (gid smallint(5) unsigned not null, x int(11) not null, y int(11) not null, id int(11) not null, primary key (gid,id,x,y), key id (id));insert t2 values (1, -5, -8, 1), (1, 1, 1, 1), (1, 2, 2, 1);create table t3 ( set_id smallint(5) unsigned not null, id tinyint(4) unsigned not null, name char(12) not null, primary key (id,set_id));insert t3 values (0, 1, 'a'), (1, 1, 'b'), (0, 2, 'c'), (1, 2, 'd'), (1, 3, 'e'), (1, 4, 'f'), (1, 5, 'g'), (1, 6, 'h');explain select name from t1 left join t2 on t1.x = t2.x and t1.y = t2.yleft join t3 on t1.art = t3.id where t2.id =1 and t2.x = -5 and t2.y =-8and t1.gid =1 and t2.gid =1 and t3.set_id =1;drop tables t1,t2,t3;## Test for bug #9938: invalid conversion from outer join to inner join # for queries containing indirect reference in WHERE clause#CREATE TABLE t1 (EMPNUM INT, GRP INT);INSERT INTO t1 VALUES (0, 10);INSERT INTO t1 VALUES (2, 30);CREATE TABLE t2 (EMPNUM INT, NAME CHAR(5));INSERT INTO t2 VALUES (0, 'KERI');INSERT INTO t2 VALUES (9, 'BARRY');CREATE VIEW v1 ASSELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS EMPNUM, NAME, GRP FROM t2 LEFT OUTER JOIN t1 ON t2.EMPNUM=t1.EMPNUM;SELECT * FROM v1;SELECT * FROM v1 WHERE EMPNUM < 10;DROP VIEW v1;DROP TABLE t1,t2;## Test for bug #11285: false Item_equal on expression in outer join# CREATE TABLE t1 (c11 int);CREATE TABLE t2 (c21 int);INSERT INTO t1 VALUES (30), (40), (50);INSERT INTO t2 VALUES (300), (400), (500);SELECT * FROM t1 LEFT JOIN t2 ON (c11=c21 AND c21=30) WHERE c11=40;DROP TABLE t1, t2;## Test for bugs# #12101: erroneously applied outer join elimination in case of WHERE NOT BETWEEN# #12102: erroneously missing outer join elimination in case of WHERE IN/IF#CREATE TABLE t1 (a int PRIMARY KEY, b int);CREATE TABLE t2 (a int PRIMARY KEY, b int);INSERT INTO t1 VALUES (1,2), (2,1), (3,2), (4,3), (5,6), (6,5), (7,8), (8,7), (9,10);INSERT INTO t2 VALUES (3,0), (4,1), (6,4), (7,5);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.b <= t1.a AND t1.a <= t1.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a BETWEEN t2.b AND t1.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a NOT BETWEEN t2.b AND t1.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.b > t1.a OR t1.a > t1.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a NOT BETWEEN t2.b AND t1.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a BETWEEN t2.b AND t1.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t2.b > t1.a OR t1.a > t1.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a != t2.a AND t1.a BETWEEN t2.b AND t1.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a AND (t2.b > t1.a OR t1.a > t1.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a != t2.a OR t1.a BETWEEN t2.b AND t1.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t1.a = t2.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a IN(t2.a, t2.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a NOT IN(t2.a, t2.b));SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a != t1.b AND t1.a != t2.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a NOT IN(t1.b, t2.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t1.a IN(t1.b, t2.b));SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.a != t2.b OR (t1.a != t2.a AND t1.a != t2.b);SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t2.a = t2.b AND t1.a IN(t2.a, t2.b));SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t2.a != t2.b AND t1.a != t1.b AND t1.a != t2.b;SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE NOT(t2.a = t2.b OR t1.a IN(t1.b, t2.b));EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a = t2.a OR t1.a = t2.b;EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a IN(t2.a, t2.b);EXPLAIN SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a WHERE t1.a > IF(t1.a = t2.b-2, t2.b, t2.b-1);DROP TABLE t1,t2;## Test for bug #17164: ORed FALSE blocked conversion of outer join into join# CREATE TABLE t1 (id int(11) NOT NULL PRIMARY KEY, name varchar(20), INDEX (name)) ENGINE=InnoDB;CREATE TABLE t2 (id int(11) NOT NULL PRIMARY KEY, fkey int(11), FOREIGN KEY (fkey) REFERENCES t2(id)) ENGINE=InnoDB;INSERT INTO t1 VALUES (1,'A1'),(2,'A2'),(3,'B');INSERT INTO t2 VALUES (1,1),(2,2),(3,2),(4,3),(5,3);EXPLAINSELECT COUNT(*) FROM t2 LEFT JOIN t1 ON t2.fkey = t1.id WHERE t1.name LIKE 'A%';EXPLAINSELECT COUNT(*) FROM t2 LEFT JOIN t1 ON t2.fkey = t1.id WHERE t1.name LIKE 'A%' OR FALSE;DROP TABLE t1,t2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -