join_outer.test

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· TEST 代码 · 共 864 行 · 第 1/3 页

TEST
864
字号
insert into t1 values (1);insert into t2 values (1);insert into t3 values (1);insert into t4 values (1,1);insert into t5 values (1,1);--error 1054explain select * from t3 left join t4 on t4.seq_1_id = t2.t2_id left join t1 on t1.t1_id = t4.seq_0_id left join t5 on t5.seq_0_id = t1.t1_id left join t2 on t2.t2_id = t5.seq_1_id where t3.t3_id = 23;drop table t1,t2,t3,t4,t5;## Another LEFT JOIN problem# (The problem was that the result changed when we added ORDER BY)#create table t1 (n int, m int, o int, key(n));create table t2 (n int not null, m int, o int, primary key(n));insert into t1 values (1, 2, 11), (1, 2, 7), (2, 2, 8), (1,2,9),(1,3,9);insert into t2 values (1, 2, 3),(2, 2, 8), (4,3,9),(3,2,10);select t1.*, t2.* from t1 left join t2 on t1.n = t2.n andt1.m = t2.m where t1.n = 1;select t1.*, t2.* from t1 left join t2 on t1.n = t2.n andt1.m = t2.m where t1.n = 1 order by t1.o;drop table t1,t2;# Test bug with NATURAL join:CREATE TABLE t1 (id1 INT NOT NULL PRIMARY KEY, dat1 CHAR(1), id2 INT);   INSERT INTO t1 VALUES (1,'a',1);INSERT INTO t1 VALUES (2,'b',1);INSERT INTO t1 VALUES (3,'c',2);CREATE TABLE t2 (id2 INT NOT NULL PRIMARY KEY, dat2 CHAR(1));   INSERT INTO t2 VALUES (1,'x');INSERT INTO t2 VALUES (2,'y');INSERT INTO t2 VALUES (3,'z');SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL;SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;drop table t1,t2;create table t1 ( color varchar(20), name varchar(20) );insert into t1 values ( 'red', 'apple' );insert into t1 values ( 'yellow', 'banana' );insert into t1 values ( 'green', 'lime' );insert into t1 values ( 'black', 'grape' );insert into t1 values ( 'blue', 'blueberry' );create table t2 ( count int, color varchar(20) );insert into t2 values (10, 'green');insert into t2 values (5, 'black');insert into t2 values (15, 'white');insert into t2 values (7, 'green');select * from t1;select * from t2;select * from t2 natural join t1;select t2.count, t1.name from t2 natural join t1;select t2.count, t1.name from t2 inner join t1 using (color);drop table t1;drop table t2;## Test of LEFT JOIN + GROUP FUNCTIONS within functions:#CREATE TABLE t1 (  pcode varchar(8) DEFAULT '' NOT NULL);INSERT INTO t1 VALUES ('kvw2000'),('kvw2001'),('kvw3000'),('kvw3001'),('kvw3002'),('kvw3500'),('kvw3501'),('kvw3502'),('kvw3800'),('kvw3801'),('kvw3802'),('kvw3900'),('kvw3901'),('kvw3902'),('kvw4000'),('kvw4001'),('kvw4002'),('kvw4200'),('kvw4500'),('kvw5000'),('kvw5001'),('kvw5500'),('kvw5510'),('kvw5600'),('kvw5601'),('kvw6000'),('klw1000'),('klw1020'),('klw1500'),('klw2000'),('klw2001'),('klw2002'),('kld2000'),('klw2500'),('kmw1000'),('kmw1500'),('kmw2000'),('kmw2001'),('kmw2100'),('kmw3000'),('kmw3200');CREATE TABLE t2 (  pcode varchar(8) DEFAULT '' NOT NULL,  KEY pcode (pcode));INSERT INTO t2 VALUES ('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw2000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3000'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw3500'),('kvw6000'),('kvw6000'),('kld2000');SELECT t1.pcode, IF(ISNULL(t2.pcode), 0, COUNT(*)) AS count FROM t1LEFT JOIN t2 ON t1.pcode = t2.pcode GROUP BY t1.pcode;SELECT SQL_BIG_RESULT t1.pcode, IF(ISNULL(t2.pcode), 0, COUNT(*)) AS count FROM t1 LEFT JOIN t2 ON t1.pcode = t2.pcode GROUP BY t1.pcode;drop table t1,t2;## Another left join problem#CREATE TABLE t1 (  id int(11),  pid int(11),  rep_del tinyint(4),  KEY id (id),  KEY pid (pid));INSERT INTO t1 VALUES (1,NULL,NULL);INSERT INTO t1 VALUES (2,1,NULL);select * from t1 LEFT JOIN t1 t2 ON (t1.id=t2.pid) AND t2.rep_del IS NULL;create index rep_del ON t1(rep_del);select * from t1 LEFT JOIN t1 t2 ON (t1.id=t2.pid) AND t2.rep_del IS NULL;drop table t1;CREATE TABLE t1 (  id int(11) DEFAULT '0' NOT NULL,  name tinytext DEFAULT '' NOT NULL,  UNIQUE id (id));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)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?