github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/dml/select/union_and_union_all.sql (about) 1 2 -- test different type union, union all 3 4 drop table if exists t1; 5 create table t1( 6 a int, 7 b varchar(100) 8 ); 9 10 insert into t1 values(30, 'cccc'); 11 insert into t1 values(20, 'bbbb'); 12 insert into t1 values(10, 'aaaa'); 13 insert into t1 values (); 14 select * from t1; 15 16 17 drop table if exists t2; 18 create table t2( 19 col1 date, 20 col2 datetime, 21 col3 timestamp 22 ); 23 24 insert into t2 values (); 25 insert into t2 values('2022-01-01', '2022-01-01', '2022-01-01'); 26 insert into t2 values('2022-01-01', '2022-01-01 00:00:00', '2022-01-01 00:00:00.000000'); 27 insert into t2 values('2022-01-01', '2022-01-01 00:00:00.000000', '2022-01-01 23:59:59.999999'); 28 select * from t2; 29 30 -- test int type union all date type 31 32 (select a from t1 union all select col1 from t2) order by a; 33 34 (select a from t1 union all select col1 from t2) order by col1; 35 36 select a from t1 union all select col1 from t2; 37 38 (select a from t1 order by a) union all select col1 from t2; 39 (select a from t1) union all (select col1 from t2 order by col1); 40 (select a from t1 order by a) union all (select col1 from t2 order by col1); 41 42 (select a from t1 union all select col1 from t2) order by col1; 43 44 -- test int type union datetime type 45 46 (select a from t1 union select col2 from t2) order by a; 47 48 (select a from t1 union select col2 from t2) order by col2; 49 50 select a from t1 union select col2 from t2; 51 52 (select a from t1 order by a) union select col2 from t2; 53 (select a from t1) union (select col2 from t2 order by col2); 54 (select a from t1 order by a) union (select col2 from t2 order by col2); 55 56 (select a from t1 union select col2 from t2) order by col2; 57 58 drop table t1; 59 drop table t2; 60 61 62 -- test different length type union 63 drop table if exists t3; 64 create table t3( 65 a tinyint 66 ); 67 68 insert into t3 values (20),(10),(30),(-10); 69 70 drop table if exists t4; 71 create table t4( 72 col1 smallint, 73 col2 smallint unsigned, 74 col3 float, 75 col4 bool 76 ); 77 78 insert into t4 values(100, 65535, 127.0, 1); 79 insert into t4 values(300, 0, 1.0, 0); 80 insert into t4 values(500, 100, 0.0, 0); 81 insert into t4 values(200, 35, 127.0, 1); 82 insert into t4 values(200, 35, 127.44, 1); 83 84 select a from t3 union select col1 from t4; 85 86 (select a from t3) union (select col2 from t4 order by col2); 87 88 select a from t3 union select col2 from t4; 89 90 select a from t3 union select col3 from t4; 91 92 select a from t3 union select col4 from t4; 93 94 drop table t3; 95 drop table t4; 96 97 -- test int type and text type union varchar type and text type 98 drop table if exists t5; 99 create table t5( 100 a int, 101 b text 102 ); 103 104 insert into t5 values (12, 'aa'); 105 insert into t5 values (20, 'bb'); 106 insert into t5 values (18, 'aa'); 107 insert into t5 values (15, 'bb'); 108 109 drop table if exists t6; 110 create table t6 ( 111 col1 varchar(100), 112 col2 text 113 ); 114 115 insert into t6 values ('aa', '11'); 116 insert into t6 values ('bb', '22'); 117 insert into t6 values ('cc', '33'); 118 insert into t6 values ('dd', '44'); 119 120 select a from t5 union select col1 from t6; 121 select a from t5 union select col2 from t6; 122 select b from t5 union select col1 from t6; 123 select b from t5 union select col2 from t6; 124 125 126 drop table t5; 127 drop table t6; 128 129 -- test subquery union, union all 130 drop table if exists t7; 131 CREATE TABLE t7 ( 132 a int not null, 133 b char (10) not null 134 ); 135 136 insert into t7 values(1,'a'),(2,'b'),(3,'c'),(3,'c'); 137 138 select * from t7 union select * from t7 limit 2; 139 140 select * from (select * from t7 union select * from t7) a; 141 142 select * from (select * from t7 union all select * from t7) a; 143 144 select * from (select * from t7 union all select * from t7 limit 2) a; 145 select * from (select * from t7 union select * from t7 limit 2) a; 146 147 select * from (select * from t7 union select * from t7 where a > 1) a; 148 select * from (select * from t7 union all select * from t7 where a > 1) a; 149 150 select * from (select * from t7 union select * from t7 where a < 1) a; 151 select * from (select * from t7 union all select * from t7 where a < 1) a; 152 153 select * from (select * from t7 where a > 1 union select * from t7 where a < 1) a; 154 select * from (select * from t7 where a > 1 union all select * from t7 where a < 1) a; 155 156 select * from (select * from t7 where a >=1 union select * from t7 where a <= 1) a; 157 select * from (select * from t7 where a >=1 union all select * from t7 where a <= 1) a; 158 159 select * from (select * from t7 where a between 1 and 3 union select * from t7 where a <= 1) a; 160 select * from (select * from t7 where a between 1 and 3 union all select * from t7 where a <= 1) a; 161 162 select * from (select * from t7 where a between 1 and 3 union all select * from t7 where a between 3 and 1) a; 163 select * from (select * from t7 where a between 1 and 3 union all select * from t7 where a between 3 and 1) a; 164 165 drop table t7; 166 167 168 -- test union distinct, union all, union 169 create table t8(a int); 170 create table t9(a int); 171 create table t10(a int); 172 insert into t8 values(1),(1); 173 insert into t9 values(2),(2); 174 insert into t10 values(3),(3); 175 176 select * from t8 union distinct select * from t9 union all select * from t10; 177 178 select * from t8 union distinct select * from t9 union distinct select * from t10; 179 180 select * from (select * from t8 union distinct select * from t9 union all select * from t10) X; 181 182 select * from t8 union select * from t9 intersect select * from t10; 183 select * from t8 union select * from t9 minus select * from t10; 184 (select * from t8 union select * from t9) intersect select * from t10; 185 (select * from t8 union select * from t9) minus select * from t10; 186 drop table t8; 187 drop table t9; 188 drop table t10; 189 190 191 -- test select ... union select case ... when ...; 192 SELECT 'case+union+test' UNION 193 SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END; 194 195 select 'case+union+tet' union 196 SELECT CASE '1' WHEN '1' THEN 'BUG' ELSE 'nobug' END; 197 198 SELECT 1, 2 UNION SELECT 'a', 'b'; 199 200 201 -- test union and concat function 202 select 'a' union select concat('a', -4); 203 select 'a' union select concat('a', -4.5); 204 select 'a' union select concat('a', -(4 + 1)); 205 select 'a' union select concat('a', 4 - 5); 206 select 'a' union select concat('a', -'3'); 207 select 'a' union select concat('a', -concat('3',4)); 208 select 'a' union select concat('a', -0); 209 select 'a' union select concat('a', -0.0); 210 select 'a' union select concat('a', -0.0000); 211 212 select concat((select x from (select 'a' as x) as t1 ), 213 (select y from (select 'b' as y) as t2 )) from (select 1 union select 2 ) 214 as t3; 215 216 drop table if exists t11; 217 create table t11(f1 varchar(6)); 218 insert into t11 values ("123456"); 219 select concat(f1, 2) a from t11 union select 'x' a from t11; 220 drop table t11; 221 222 223 -- test union all, where 224 drop table if exists t12; 225 create table t12 (EVENT_ID int auto_increment primary key, LOCATION char(20)); 226 insert into t12 values (NULL,"Mic-4"),(NULL,"Mic-5"),(NULL,"Mic-6"); 227 SELECT LOCATION FROM t12 WHERE EVENT_ID=2 UNION ALL SELECT LOCATION FROM t12 WHERE EVENT_ID=3; 228 SELECT LOCATION FROM t12 WHERE EVENT_ID=2 UNION ALL SELECT LOCATION FROM t12 WHERE EVENT_ID=3; 229 SELECT LOCATION FROM t12 WHERE EVENT_ID=2 UNION ALL SELECT LOCATION FROM t12 WHERE EVENT_ID=3; 230 drop table t12; 231 232 233 -- test union prepare 234 drop table if exists t13; 235 create table t13 (a int primary key); 236 insert into t13 values (1); 237 select * from t13 where 3 in (select (1+1) union select 1); 238 select * from t13 where 3 in (select (1+2) union select 1); 239 prepare st_18492 from 'select * from t13 where 3 in (select (1+1) union select 1)'; 240 execute st_18492; 241 242 prepare st_18493 from 'select * from t13 where 3 in (select (2+1) union select 1)'; 243 execute st_18493; 244 245 deallocate prepare st_18492; 246 deallocate prepare st_18493; 247 drop table t13; 248 249 select cast(a as DECIMAL(3,2)) 250 from (select 11.1234 as a 251 UNION select 11.1234 252 UNION select 11.1234 253 ) t; 254 255 drop table if exists t14; 256 CREATE TABLE t14 ( 257 `pseudo` char(35) NOT NULL default '', 258 `pseudo1` char(35) NOT NULL default '', 259 `same` tinyint(1) unsigned NOT NULL default '1', 260 PRIMARY KEY (`pseudo1`), 261 KEY `pseudo` (`pseudo`) 262 ); 263 264 INSERT INTO t14 (pseudo,pseudo1,same) VALUES 265 ('joce', 'testtt', 1),('joce', 'tsestset', 1),('dekad', 'joce', 1); 266 267 SELECT pseudo FROM t14 WHERE pseudo1='joce' UNION SELECT pseudo FROM t14 WHERE pseudo='joce'; 268 SELECT pseudo1 FROM t14 WHERE pseudo1='joce' UNION SELECT pseudo1 FROM t14 WHERE pseudo='joce'; 269 SELECT * FROM t14 WHERE pseudo1='joce' UNION SELECT * FROM t14 WHERE pseudo='joce' order by pseudo desc,pseudo1 desc; 270 SELECT pseudo1 FROM t14 WHERE pseudo='joce' UNION SELECT pseudo FROM t14 WHERE pseudo1='joce'; 271 SELECT pseudo1 FROM t14 WHERE pseudo='joce' UNION ALL SELECT pseudo FROM t14 WHERE pseudo1='joce'; 272 273 drop table t14; 274 275 276 -- test union,union all, join, left join, right join 277 drop table if exists t15; 278 279 CREATE TABLE t15 ( 280 id int(3) unsigned default '0' 281 ); 282 283 INSERT INTO t15 (id) VALUES("1"); 284 285 drop table if exists t16; 286 CREATE TABLE t16 ( 287 id int(3) unsigned default '0', 288 id_master int(5) default '0', 289 text15 varchar(5) default NULL, 290 text16 varchar(5) default NULL 291 ); 292 293 INSERT INTO t16 (id, id_master, text15, text16) VALUES("1", "1", "foo1", "bar1"); 294 INSERT INTO t16 (id, id_master, text15, text16) VALUES("2", "1", "foo2", "bar2"); 295 INSERT INTO t16 (id, id_master, text15, text16) VALUES("3", "1", NULL, "bar3"); 296 INSERT INTO t16 (id, id_master, text15, text16) VALUES("4", "1", "foo4", "bar4"); 297 298 SELECT 1 AS id_master, 1 AS id, NULL AS text15, 'ABCDE' AS text16 299 UNION 300 SELECT id_master, t16.id, text15, text16 FROM t15 LEFT JOIN t16 ON t15.id = t16.id_master; 301 302 SELECT 1 AS id_master, 1 AS id, NULL AS text15, 'ABCDE' AS text16 303 UNION ALL 304 SELECT id_master, t16.id, text15, text16 FROM t15 LEFT JOIN t16 ON t15.id = t16.id_master; 305 306 307 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 308 UNION 309 SELECT id_master, t16.id, text15, text16 FROM t15 LEFT JOIN t16 ON t15.id = t16.id_master; 310 311 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 312 UNION 313 SELECT id_master, t16.id, text15, text16 FROM t15 right join t16 ON t15.id = t16.id_master; 314 315 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 316 UNION 317 SELECT id_master, t16.id, text15, text16 FROM t15 JOIN t16 ON t15.id = t16.id_master; 318 319 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 320 UNION ALL 321 SELECT id_master, t16.id, text15, text16 FROM t15 LEFT JOIN t16 ON t15.id = t16.id_master; 322 323 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 324 UNION ALL 325 SELECT id_master, t16.id, text15, text16 FROM t15 right JOIN t16 ON t15.id = t16.id_master; 326 327 SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text15, 'ABCDE' AS text16 328 UNION ALL 329 SELECT id_master, t16.id, text15, text16 FROM t15 JOIN t16 ON t15.id = t16.id_master; 330 331 drop table t15; 332 drop table t16; 333 334 335 drop table if exists t17; 336 create table t17 ( 337 RID int(11) not null default '0', 338 IID int(11) not null default '0', 339 nada varchar(50) not null, 340 NAME varchar(50) not null, 341 PHONE varchar(50) not null); 342 343 insert into t17 ( RID,IID,nada,NAME,PHONE) values 344 (1, 1, 'main', 'a', '111'), 345 (2, 1, 'main', 'b', '222'), 346 (3, 1, 'main', 'c', '333'), 347 (4, 1, 'main', 'd', '444'), 348 (5, 1, 'main', 'e', '555'), 349 (6, 2, 'main', 'c', '333'), 350 (7, 2, 'main', 'd', '454'), 351 (8, 2, 'main', 'e', '555'), 352 (9, 2, 'main', 'f', '666'), 353 (10, 2, 'main', 'g', '777'); 354 355 select A.NAME, A.PHONE, B.NAME, B.PHONE from t17 A 356 left join t17 B on A.NAME = B.NAME and B.IID = 2 where A.IID = 1 and (A.PHONE <> B.PHONE or B.NAME is null) 357 union 358 select A.NAME, A.PHONE, B.NAME, B.PHONE from t17 B left join t17 A on B.NAME = A.NAME and A.IID = 1 359 where B.IID = 2 and (A.PHONE <> B.PHONE or A.NAME is null); 360 361 select A.NAME, A.PHONE, B.NAME, B.PHONE from t17 A 362 left join t17 B on A.NAME = B.NAME and B.IID = 2 where A.IID = 1 and (A.PHONE <> B.PHONE or B.NAME is null) 363 union all 364 select A.NAME, A.PHONE, B.NAME, B.PHONE from t17 B left join t17 A on B.NAME = A.NAME and A.IID = 1 365 where B.IID = 2 and (A.PHONE <> B.PHONE or A.NAME is null); 366 367 drop table t17; 368 select * from (select 'tb1' as name, 1 as count union all select 'tb3' as name, 3 as count union all select 'tb2' as name, 2 as count) order by count;