📄 union.result
字号:
) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 (s char(200));insert into t1 values (repeat("1",200));create table t2 select * from t1;insert into t2 select * from t1;insert into t1 select * from t2;insert into t2 select * from t1;insert into t1 select * from t2;insert into t2 select * from t1;set local tmp_table_size=1024;select count(*) from (select * from t1 union all select * from t2 order by 1) b;count(*)21select count(*) from t1;count(*)8select count(*) from t2;count(*)13drop table t1,t2;set local tmp_table_size=default;create table t1 (a int, index (a), b int);insert t1 values (1,1),(2,2),(3,3),(4,4),(5,5);insert t1 select a+1, a+b from t1;insert t1 select a+1, a+b from t1;insert t1 select a+1, a+b from t1;insert t1 select a+1, a+b from t1;insert t1 select a+1, a+b from t1;FLUSH STATUS;show status like 'Slow_queries';Variable_name ValueSlow_queries 0select count(*) from t1 where a=7;count(*)26show status like 'Slow_queries';Variable_name ValueSlow_queries 1select count(*) from t1 where b=13;count(*)10show status like 'Slow_queries';Variable_name ValueSlow_queries 3select count(*) from t1 where b=13 union select count(*) from t1 where a=7;count(*)1026show status like 'Slow_queries';Variable_name ValueSlow_queries 5select count(*) from t1 where a=7 union select count(*) from t1 where b=13;count(*)2610show status like 'Slow_queries';Variable_name ValueSlow_queries 7flush status;select a from t1 where b not in (1,2,3) union select a from t1 where b not in (4,5,6);a45367891012show status like 'Slow_queries';Variable_name ValueSlow_queries 1drop table t1;create table t1 ( RID int(11) not null default '0', IID int(11) not null default '0', nada varchar(50) not null,NAME varchar(50) not null,PHONE varchar(50) not null) engine=MyISAM;insert into t1 ( RID,IID,nada,NAME,PHONE) values (1, 1, 'main', 'a', '111'), (2, 1, 'main', 'b', '222'), (3, 1, 'main', 'c', '333'), (4, 1, 'main', 'd', '444'), (5, 1, 'main', 'e', '555'), (6, 2, 'main', 'c', '333'), (7, 2, 'main', 'd', '454'), (8, 2, 'main', 'e', '555'), (9, 2, 'main', 'f', '666'), (10, 2, 'main', 'g', '777');select A.NAME, A.PHONE, B.NAME, B.PHONE from t1 A left join t1 B on A.NAME = B.NAME and B.IID = 2 where A.IID = 1 and (A.PHONE <> B.PHONE or B.NAME is null) union select A.NAME, A.PHONE, B.NAME, B.PHONE from t1 B left join t1 A on B.NAME = A.NAME and A.IID = 1 where B.IID = 2 and (A.PHONE <> B.PHONE or A.NAME is null);NAME PHONE NAME PHONEa 111 NULL NULLb 222 NULL NULLd 444 d 454NULL NULL f 666NULL NULL g 777drop table t1;create table t1 (col1 tinyint unsigned, col2 tinyint unsigned);insert into t1 values (1,2),(3,4),(5,6),(7,8),(9,10);select col1 n from t1 union select col2 n from t1 order by n;n12345678910alter table t1 add index myindex (col2);select col1 n from t1 union select col2 n from t1 order by n;n12345678910drop table t1;create table t1 (i int);insert into t1 values (1);select * from t1 UNION select * from t1;i1select * from t1 UNION ALL select * from t1;i11select * from t1 UNION select * from t1 UNION ALL select * from t1;i11drop table t1;select 1 as a union all select 1 union all select 2 union select 1 union all select 2;a122set sql_select_limit=1;select 1 union select 2;11(select 1) union (select 2);11(select 1) union (select 2) union (select 3) limit 2;112set sql_select_limit=default;create table t1 (a int);insert into t1 values (100), (1);create table t2 (a int);insert into t2 values (100);select a from t1 union select a from t2 order by a;a1100SET SQL_SELECT_LIMIT=1;select a from t1 union select a from t2 order by a;a1drop table t1, t2;set sql_select_limit=default;CREATE TABLE t1 (i int(11) default NULL,c char(1) default NULL,KEY i (i));CREATE TABLE t2 (i int(11) default NULL,c char(1) default NULL,KEY i (i));explain (select * from t1) union (select * from t2) order by not_existing_column;ERROR 42S22: Unknown column 'not_existing_column' in 'order clause'drop table t1, t2;CREATE TABLE t1 (uid int(1));INSERT INTO t1 SELECT 150;SELECT 'a' UNION SELECT uid FROM t1;aa150drop table t1;CREATE TABLE t1 ( ID1 int(10) unsigned NOT NULL DEFAULT '0' , ID2 datetime NOT NULL DEFAULT '0000-00-00 00:00:00' , DATA1 varchar(10) , DATA2 double(5,4) , DATA3 datetime , PRIMARY KEY (ID1,ID2));CREATE TABLE t2 ( ID int(3) unsigned NOT NULL DEFAULT '0' , DATA1 timestamp DEFAULT '0000-00-00 00:00:00' , PRIMARY KEY (ID));(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION(SELECT * FROM t1 AS PARTITIONED, t2 ASPARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1);ID1 ID2 DATA1 DATA2 DATA3 ID DATA1drop table t1,t2;create table t1 (a ENUM('Yes', 'No') NOT NULL);create table t2 (a ENUM('aaa', 'bbb') NOT NULL);insert into t1 values ('No');insert into t2 values ('bbb');create table t3 (a SET('Yes', 'No') NOT NULL);create table t4 (a SET('aaa', 'bbb') NOT NULL);insert into t3 values (1);insert into t4 values (3);select "1" as a union select a from t1;a1Noselect a as a from t1 union select "1";aNo1select a as a from t2 union select a from t1;abbbNoselect "1" as a union select a from t3;a1Yesselect a as a from t3 union select "1";aYes1select a as a from t4 union select a from t3;aaaa,bbbYesselect a as a from t1 union select a from t4;aNoaaa,bbbdrop table t1,t2,t3,t4;create table t1 as(select _latin1'test') union(select _latin1'TEST') union(select _latin1'TeST');show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `test` varchar(4) NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1select count(*) from t1;count(*)1drop table t1;create table t1 as(select _latin1'test' collate latin1_bin) union(select _latin1'TEST') union(select _latin1'TeST');show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `_latin1'test' collate latin1_bin` varchar(4) character set latin1 collate latin1_bin NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1select count(*) from t1;count(*)3drop table t1;create table t1 as(select _latin1'test') union(select _latin1'TEST' collate latin1_bin) union(select _latin1'TeST');show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `test` varchar(4) character set latin1 collate latin1_bin NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1select count(*) from t1;count(*)3drop table t1;create table t1 as(select _latin1'test') union(select _latin1'TEST') union(select _latin1'TeST' collate latin1_bin);show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `test` varchar(4) character set latin1 collate latin1_bin NOT NULL default '') ENGINE=MyISAM DEFAULT CHARSET=latin1select count(*) from t1;count(*)3drop table t1;create table t2 (a char character set latin1 collate latin1_swedish_ci,b char character set latin1 collate latin1_german1_ci);create table t1 as(select a from t2) union(select b from t2);ERROR HY000: Illegal mix of collations for operation 'UNION'create table t1 as(select a collate latin1_german1_ci from t2) union(select b from t2);show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a collate latin1_german1_ci` varchar(1) character set latin1 collate latin1_german1_ci default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 as(select a from t2) union(select b collate latin1_german1_ci from t2);show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a` varchar(1) character set latin1 collate latin1_german1_ci default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;create table t1 as(select a from t2) union(select b from t2) union(select 'c' collate latin1_german1_ci from t2);show create table t1;Table Create Tablet1 CREATE TABLE `t1` ( `a` varchar(1) character set latin1 collate latin1_german1_ci default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1drop table t1;drop table t2;create table t1(a1 int, f1 char(10));create table t2select f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) aunionselect f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) aorder by f2, a1;show columns from t2;Field Type Null Key Default Extraf2 date YES NULL a1 int(11) YES NULL drop table t1, t2;create table t1 (f1 int);create table t2 (f1 int, f2 int ,f3 date);create table t3 (f1 int, f2 char(10));create table t4(select t2.f3 as sdatefrom t1left outer join t2 on (t1.f1 = t2.f1)inner join t3 on (t2.f2 = t3.f1)order by t1.f1, t3.f1, t2.f3)union(select cast('2004-12-31' as date) as sdatefrom t1left outer join t2 on (t1.f1 = t2.f1)inner join t3 on (t2.f2 = t3.f1)group by t1.f1order by t1.f1, t3.f1, t2.f3)order by sdate;show columns from t4;Field Type Null Key Default Extrasdate date YES NULL drop table t1, t2, t3, t4;create table t1 (a int not null, b char (10) not null);insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');select * from ((select * from t1 limit 1)) a;a b1 aselect * from ((select * from t1 limit 1) union (select * from t1 limit 1)) a;a b1 aselect * from ((select * from t1 limit 1) union (select * from t1 limit 1) union (select * from t1 limit 1)) a;a b1 aselect * from ((((select * from t1))) union (select * from t1) union (select * from t1)) a;a b1 a2 b3 cselect * from ((select * from t1) union (((select * from t1))) union (select * from t1)) a;a b1 a2 b3 cdrop table t1;set @val:=6;select concat('value is: ', @val) union select 'some text';concat('value is: ', @val)value is: 6some textselect concat(_latin1'a', _ascii'b' collate ascii_bin);concat(_latin1'a', _ascii'b' collate ascii_bin)abcreate table t1 (foo varchar(100)) collate ascii_bin;insert into t1 (foo) values ("foo");select foo from t1 union select 'bar' as foo from dual;foofoobardrop table t1;CREATE TABLE t1 (a ENUM('
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -