📄 union.result
字号:
6SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;a34select found_rows();found_rows()6SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;a345select found_rows();found_rows()5SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;a5(SELECT * FROM t1 ORDER by a) UNION ALL (SELECT * FROM t2 ORDER BY a) ORDER BY A desc LIMIT 4;a5433(SELECT * FROM t1) UNION all (SELECT SQL_CALC_FOUND_ROWS * FROM t2) LIMIT 1;ERROR 42000: Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS'create temporary table t1 select a from t1 union select a from t2;drop temporary table t1;create table t1 select a from t1 union select a from t2;ERROR HY000: You can't specify target table 't1' for update in FROM clauseselect a from t1 union select a from t2 order by t2.a;ERROR 42S22: Unknown column 't2.a' in 'order clause'drop table t1,t2;select length(version()) > 1 as `*` UNION select 2;*12create table t1 (a int);insert into t1 values (0), (3), (1), (2);explain (select * from t1) union (select * from t1) order by a;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 ALL NULL NULL NULL NULL 4 2 UNION t1 ALL NULL NULL NULL NULL 4 NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL Using filesortdrop table t1;CREATE TABLE t1 ( id int(3) unsigned default '0') ENGINE=MyISAM;INSERT INTO t1 (id) VALUES("1");CREATE TABLE t2 ( id int(3) unsigned default '0', id_master int(5) default '0', text1 varchar(5) default NULL, text2 varchar(5) default NULL) ENGINE=MyISAM;INSERT INTO t2 (id, id_master, text1, text2) VALUES("1", "1","foo1", "bar1");INSERT INTO t2 (id, id_master, text1, text2) VALUES("2", "1","foo2", "bar2");INSERT INTO t2 (id, id_master, text1, text2) VALUES("3", "1", NULL,"bar3");INSERT INTO t2 (id, id_master, text1, text2) VALUES("4", "1","foo4", "bar4");SELECT 1 AS id_master, 1 AS id, NULL AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;id_master id text1 text21 1 NULL ABCDE1 1 foo1 bar11 2 foo2 bar21 3 NULL bar31 4 foo4 bar4SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;id_master id text1 text21 1 ABCDE ABCDE1 1 foo1 bar11 2 foo2 bar21 3 NULL bar31 4 foo4 bar4drop table if exists t1,t2;create table t1 (a int not null primary key auto_increment, b int, key(b));create table t2 (a int not null primary key auto_increment, b int);insert into t1 (b) values (1),(2),(2),(3);insert into t2 (b) values (10),(11),(12),(13);explain extended (select * from t1 where a=1) union (select * from t2 where a=1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 2 UNION t2 const PRIMARY PRIMARY 4 const 1 NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL Warnings:Note 1003 (select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a` = 1)) union (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` = 1))(select * from t1 where a=5) union (select * from t2 where a=1);a b1 10(select * from t1 where a=5 and a=6) union (select * from t2 where a=1);a b1 10(select t1.a,t1.b from t1,t2 where t1.a=5) union (select * from t2 where a=1);a b1 10(select * from t1 where a=1) union (select t1.a,t2.a from t1,t2 where t1.a=t2.a);a b1 12 23 34 4explain (select * from t1 where a=1 and b=10) union (select straight_join t1.a,t2.a from t1,t2 where t1.a=t2.a);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables2 UNION t1 index PRIMARY PRIMARY 4 NULL 4 Using index2 UNION t2 index PRIMARY PRIMARY 4 NULL 3 Using where; Using indexNULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL explain (select * from t1 where a=1) union (select * from t1 where b=1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 2 UNION t1 ref b b 5 const 1 Using whereNULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL drop table t1,t2;create table t1 ( id int not null auto_increment, primary key (id) ,user_name text );create table t2 ( id int not null auto_increment, primary key (id) ,group_name text );create table t3 ( id int not null auto_increment, primary key (id) ,user_id int ,index user_idx (user_id) ,foreign key (user_id) references users(id) ,group_id int ,index group_idx (group_id) ,foreign key (group_id) references groups(id) );insert into t1 (user_name) values ('Tester');insert into t2 (group_name) values ('Group A');insert into t2 (group_name) values ('Group B');insert into t3 (user_id, group_id) values (1,1);select 1 'is_in_group', a.user_name, c.group_name, b.id from t1 a, t3 b, t2 c where a.id = b.user_id and b.group_id = c.id UNION select 0 'is_in_group', a.user_name, c.group_name, null from t1 a, t2 c;is_in_group user_name group_name id1 Tester Group A 10 Tester Group A NULL0 Tester Group B NULLdrop table t1, t2, t3;create table t1 (mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, matintnum CHAR(6) NOT NULL, test MEDIUMINT UNSIGNED NULL);create table t2 (mat_id MEDIUMINT UNSIGNED NOT NULL, pla_id MEDIUMINT UNSIGNED NOT NULL);insert into t1 values (NULL, 'a', 1), (NULL, 'b', 2), (NULL, 'c', 3), (NULL, 'd', 4), (NULL, 'e', 5), (NULL, 'f', 6), (NULL, 'g', 7), (NULL, 'h', 8), (NULL, 'i', 9);insert into t2 values (1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104), (3, 101), (3, 102), (3, 105);SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id GROUP BY mp.pla_id union SELECT 0, 0;pla_id matintnum100 a101 a102 a103 b104 b105 c0 0drop table t1, t2;create table t1 SELECT "a" as a UNION select "aa" as a;select * from t1;aaaashow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a` varchar(2) NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT 12 as a UNION select "aa" as a;select * from t1;a12aashow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a` varbinary(20) NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT 12 as a UNION select 12.2 as a;select * from t1;a12.012.2show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a` decimal(3,1) NOT NULL default '0.0') ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t2 (it1 tinyint, it2 tinyint not null, i int not null, ib bigint, f float, d double, y year, da date, dt datetime, sc char(10), sv varchar(10), b blob, tx text);insert into t2 values (NULL, 1, 3, 4, 1.5, 2.5, 1972, '1972-10-22', '1972-10-22 11:50', 'testc', 'testv', 'tetetetetest', 'teeeeeeeeeeeest');create table t1 SELECT it2 from t2 UNION select it1 from t2;select * from t1;it21NULLshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `it2` tinyint(4) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT it2 from t2 UNION select i from t2;select * from t1;it213show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `it2` int(11) NOT NULL default '0') ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT i from t2 UNION select f from t2;select * from t1;i31.5show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `i` double default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT f from t2 UNION select d from t2;select * from t1;f1.52.5show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `f` double default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT ib from t2 UNION select f from t2;select * from t1;ib41.5show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `ib` double default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT ib from t2 UNION select d from t2;select * from t1;ib42.5show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `ib` double default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT f from t2 UNION select y from t2;select * from t1;f1.51972show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `f` float default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT f from t2 UNION select da from t2;select * from t1;f1.51972-10-22show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `f` varbinary(24) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT y from t2 UNION select da from t2;select * from t1;y19721972-10-22show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `y` varbinary(10) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT y from t2 UNION select dt from t2;select * from t1;y19721972-10-22 11:50:00show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `y` varbinary(19) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT da from t2 UNION select dt from t2;select * from t1;da1972-10-22 00:00:001972-10-22 11:50:00show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `da` datetime default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT dt from t2 UNION select sc from t2;select * from t1;dt1972-10-22 11:50:00testcshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `dt` varbinary(19) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT dt from t2 UNION select sv from t2;select * from t1;dt1972-10-22 11:50:00testvshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `dt` varbinary(19) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT sc from t2 UNION select sv from t2;select * from t1;sctestctestvshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `sc` varchar(10) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT dt from t2 UNION select b from t2;select * from t1;dt1972-10-22 11:50:00tetetetetestshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `dt` longblob) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT sv from t2 UNION select b from t2;select * from t1;svtestvtetetetetestshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `sv` longblob) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT i from t2 UNION select d from t2 UNION select b from t2;select * from t1;i32.5tetetetetestshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `i` longblob) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT sv from t2 UNION select tx from t2;select * from t1;svtestvteeeeeeeeeeeestshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `sv` longtext) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 SELECT b from t2 UNION select tx from t2;select * from t1;btetetetetestteeeeeeeeeeeestshow create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `b` longblob) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1,t2;create table t1 select 1 union select -1;select * from t1;11-1show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `1` bigint(20) NOT NULL default '0') ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 select _latin1"test" union select _latin2"testt" ;ERROR HY000: Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (latin2_general_ci,COERCIBLE) for operation 'UNION'create table t1 select _latin2"test" union select _latin2"testt" ;show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `test` varchar(5) character set latin2 NOT NULL default ''
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -