6
drop table if exists t1,t2,t3,t4,t5,t6;
9
CREATE TABLE t1 (a int not null, b char (10) not null);
10
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
11
CREATE TABLE t2 (a int not null, b char (10) not null);
12
insert into t2 values (3,'c'),(4,'d'),(5,'f'),(6,'e');
14
select a,b from t1 union distinct select a,b from t2;
15
select a,b from t1 union all select a,b from t2;
16
select a,b from t1 union all select a,b from t2 order by b;
17
select a,b from t1 union all select a,b from t2 union select 7,'g';
18
select 0,'#' union select a,b from t1 union all select a,b from t2 union select 7,'gg';
19
select a,b from t1 union select a,b from t1;
20
select 't1',b,count(*) from t1 group by b UNION select 't2',b,count(*) from t2 group by b;
22
# Test alternate syntax for unions
23
(select a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 4;
24
(select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1);
25
(select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1) order by b desc;
27
(select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1) order by t1.b;
29
explain extended (select a,b from t1 limit 2) union all (select a,b from t2 order by a limit 1) order by b desc;
30
(select sql_calc_found_rows a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 2;
32
select sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
36
# Test some error conditions with UNION
40
explain select a,b from t1 union all select a,b from t2;
43
explain select xx from t1 union select 1;
45
explain select a,b from t1 union select 1;
47
explain select 1 union select a,b from t1 union select 1;
49
explain select a,b from t1 union select 1 limit 0;
52
select a,b from t1 into outfile 'skr' union select a,b from t2;
55
select a,b from t1 order by a union select a,b from t2;
58
insert into t3 select a from t1 order by a union select a from t2;
61
create table t3 select a,b from t1 union select a from t2;
64
select a,b from t1 union select a from t2;
67
select * from t1 union select a from t2;
70
select a from t1 union select * from t2;
73
select * from t1 union select SQL_BUFFER_RESULT * from t2;
75
# Test CREATE, INSERT and REPLACE
76
create table t3 select a,b from t1 union all select a,b from t2;
77
insert into t3 select a,b from t1 union all select a,b from t2;
78
# PS can't handle REPLACE ... SELECT
79
replace into t3 select a,b as c from t1 union all select a,b from t2;
84
# Test some unions without tables
87
select * union select 1;
88
select 1 as a,(select a union select a);
90
(select 1) union (select 2) order by 0;
91
SELECT @a:=1 UNION SELECT @a:=@a+1;
93
(SELECT 1) UNION (SELECT 2) ORDER BY (SELECT a);
94
(SELECT 1,3) UNION (SELECT 2,1) ORDER BY (SELECT 2);
97
# Test bug reported by joc@presence-pc.com
101
`pseudo` char(35) NOT NULL default '',
102
`pseudo1` char(35) NOT NULL default '',
103
`same` int NOT NULL default '1',
104
PRIMARY KEY (`pseudo1`),
105
KEY `pseudo` (`pseudo`)
107
INSERT INTO t1 (pseudo,pseudo1,same) VALUES ('joce', 'testtt', 1),('joce', 'tsestset', 1),('dekad', 'joce', 1);
108
SELECT pseudo FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo FROM t1 WHERE pseudo='joce';
109
SELECT pseudo1 FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo1 FROM t1 WHERE pseudo='joce';
110
SELECT * FROM t1 WHERE pseudo1='joce' UNION SELECT * FROM t1 WHERE pseudo='joce' order by pseudo desc,pseudo1 desc;
111
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT pseudo FROM t1 WHERE pseudo1='joce';
112
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION ALL SELECT pseudo FROM t1 WHERE pseudo1='joce';
113
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT 1;
116
create table t1 (a int);
117
create table t2 (a int);
118
insert into t1 values (1),(2),(3),(4),(5);
119
insert into t2 values (11),(12),(13),(14),(15);
120
(select * from t1 limit 2) union (select * from t2 limit 3) limit 4;
121
(select * from t1 limit 2) union (select * from t2 limit 3);
122
(select * from t1 limit 2) union (select * from t2 limit 20,3);
123
set SQL_SELECT_LIMIT=2;
124
(select * from t1 limit 1) union (select * from t2 limit 3);
125
set SQL_SELECT_LIMIT=DEFAULT;
129
# Test error with left join
133
cid int NOT NULL default '0',
134
cv varchar(190) NOT NULL default '',
138
INSERT INTO t1 VALUES (8,'dummy');
140
cid int NOT NULL auto_increment,
141
cap varchar(255) NOT NULL default '',
146
gid int NOT NULL auto_increment,
147
gn varchar(255) NOT NULL default '',
148
must int default NULL,
152
INSERT INTO t3 VALUES (1,'V1',NULL);
154
uid bigint NOT NULL default '0',
155
gid bigint default NULL,
156
rid bigint default NULL,
157
cid bigint default NULL,
158
UNIQUE KEY m (uid,gid,rid,cid),
162
KEY container (gid,rid,cid)
164
INSERT INTO t4 VALUES (1,1,NULL,NULL);
166
rid bigint NOT NULL auto_increment,
167
rl varchar(255) NOT NULL default '',
172
uid bigint NOT NULL auto_increment,
173
un varchar(190) NOT NULL default '',
174
uc int NOT NULL default '0',
176
UNIQUE KEY nc (un,uc),
179
INSERT INTO t6 VALUES (1,'test',8);
181
SELECT t4.uid, t5.rl, t3.gn as g1, t4.cid, t4.gid as gg FROM t3, t6, t1, t4 left join t5 on t5.rid = t4.rid left join t2 on t2.cid = t4.cid WHERE t3.gid=t4.gid AND t6.uid = t4.uid AND t6.uc = t1.cid AND t1.cv = "dummy" AND t6.un = "test";
182
SELECT t4.uid, t5.rl, t3.gn as g1, t4.cid, t4.gid as gg FROM t3, t6, t1, t4 left join t5 on t5.rid = t4.rid left join t2 on t2.cid = t4.cid WHERE t3.gid=t4.gid AND t6.uid = t4.uid AND t3.must IS NOT NULL AND t6.uc = t1.cid AND t1.cv = "dummy" AND t6.un = "test";
183
(SELECT t4.uid, t5.rl, t3.gn as g1, t4.cid, t4.gid as gg FROM t3, t6, t1, t4 left join t5 on t5.rid = t4.rid left join t2 on t2.cid = t4.cid WHERE t3.gid=t4.gid AND t6.uid = t4.uid AND t3.must IS NOT NULL AND t6.uc = t1.cid AND t1.cv = "dummy" AND t6.un = "test") UNION (SELECT t4.uid, t5.rl, t3.gn as g1, t4.cid, t4.gid as gg FROM t3, t6, t1, t4 left join t5 on t5.rid = t4.rid left join t2 on t2.cid = t4.cid WHERE t3.gid=t4.gid AND t6.uid = t4.uid AND t6.uc = t1.cid AND t1.cv = "dummy" AND t6.un = "test");
184
drop table t1,t2,t3,t4,t5,t6;
187
# Test insert ... SELECT with UNION
190
CREATE TABLE t1 (a int not null, b char (10) not null);
191
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
192
CREATE TABLE t2 (a int not null, b char (10) not null);
193
insert into t2 values (3,'c'),(4,'d'),(5,'f'),(6,'e');
194
create table t3 select a,b from t1 union select a,b from t2;
195
create table t4 (select a,b from t1) union (select a,b from t2) limit 2;
196
insert into t4 select a,b from t1 union select a,b from t2;
197
insert into t3 (select a,b from t1) union (select a,b from t2) limit 2;
200
drop table t1,t2,t3,t4;
203
# Test of SQL_CALC_FOUND_ROW handling
205
create table t1 (a int);
206
insert into t1 values (1),(2),(3);
207
create table t2 (a int);
208
insert into t2 values (3),(4),(5);
211
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
213
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
216
# Test cases where found_rows() should return number of returned rows
217
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
219
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
221
# This used to work in 4.0 but not anymore in 4.1
223
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
224
#select found_rows();
226
# In these case found_rows() should work
227
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION all SELECT * FROM t2 LIMIT 2;
229
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
232
# The following examples will not be exact
233
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2;
235
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
237
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2;
239
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2;
241
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2;
243
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
245
SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;
248
# Test some limits with ORDER BY
249
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
250
(SELECT * FROM t1 ORDER by a) UNION ALL (SELECT * FROM t2 ORDER BY a) ORDER BY A desc LIMIT 4;
254
(SELECT * FROM t1) UNION all (SELECT SQL_CALC_FOUND_ROWS * FROM t2) LIMIT 1;
256
create temporary table t1 select a from t1 union select a from t2;
257
drop temporary table t1;
259
create table t1 select a from t1 union select a from t2;
261
select a from t1 union select a from t2 order by t2.a;
265
# Problem with alias '*' (BUG #1249)
268
select length(version()) > 1 as `*` UNION select 2;
271
# Bug #4980: problem with explain
274
create table t1 (a int);
275
insert into t1 values (0), (3), (1), (2);
276
explain (select * from t1) union (select * from t1) order by a;
279
# Test for another bug with UNION and LEFT JOIN
281
CREATE TEMPORARY TABLE t1 ( id int default '0') ENGINE=MyISAM;
282
INSERT INTO t1 (id) VALUES("1");
283
CREATE TEMPORARY TABLE t2 ( id int default '0', id_master int default '0', text1 varchar(5) default NULL, text2 varchar(5) default NULL) ENGINE=MyISAM;
284
INSERT INTO t2 (id, id_master, text1, text2) VALUES("1", "1",
286
INSERT INTO t2 (id, id_master, text1, text2) VALUES("2", "1",
288
INSERT INTO t2 (id, id_master, text1, text2) VALUES("3", "1", NULL,
290
INSERT INTO t2 (id, id_master, text1, text2) VALUES("4", "1",
292
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;
293
SELECT 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;
294
drop table if exists t1,t2;
297
# Test of bug when using the same table multiple times
299
create table t1 (a int not null primary key auto_increment, b int, key(b));
300
create table t2 (a int not null primary key auto_increment, b int);
301
insert into t1 (b) values (1),(2),(2),(3);
302
insert into t2 (b) values (10),(11),(12),(13);
304
explain extended (select * from t1 where a=1) union (select * from t2 where a=1);
305
(select * from t1 where a=5) union (select * from t2 where a=1);
306
(select * from t1 where a=5 and a=6) union (select * from t2 where a=1);
307
(select t1.a,t1.b from t1,t2 where t1.a=5) union (select * from t2 where a=1);
308
(select * from t1 where a=1) union (select t1.a,t2.a from t1,t2 where t1.a=t2.a);
309
explain (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);
310
explain (select * from t1 where a=1) union (select * from t1 where b=1);
312
create table t1 ( id int not null auto_increment, primary key (id) ,user_name text );
313
create table t2 ( id int not null auto_increment, primary key (id) ,group_name text );
314
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 t1(id) ,group_id int ,index group_idx (group_id) ,foreign key (group_id) references t2(id) );
315
insert into t1 (user_name) values ('Tester');
316
insert into t2 (group_name) values ('Group A');
317
insert into t2 (group_name) values ('Group B');
318
insert into t3 (user_id, group_id) values (1,1);
319
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;
320
drop table t3, t1, t2;
325
create table t1 (mat_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, matintnum CHAR(6) NOT NULL, test INT NULL);
326
create table t2 (mat_id INT NOT NULL, pla_id INT NOT NULL);
327
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);
328
insert into t2 values (1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104), (3, 101), (3, 102), (3, 105);
329
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;
335
create table t1 SELECT "a" as a UNION select "aa" as a;
337
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
338
show create table t1;
340
create table t1 SELECT 12 as a UNION select "aa" as a;
342
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
343
show create table t1;
345
create table t1 SELECT 12 as a UNION select 12.2 as a;
347
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
348
show create table t1;
351
create table t2 (it1 int, it2 int not null, i int not null, ib int, f float, d double, da date, dt datetime, sc char(10), sv varchar(10), b blob, tx text);
352
insert into t2 values (NULL, 1, 3, 4, 1.5, 2.5, '1972-10-22', '1972-10-22 11:50:00', 'testc', 'testv', 'tetetetetest', 'teeeeeeeeeeeest');
354
create table t1 SELECT it2 from t2 UNION select it1 from t2;
356
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
357
show create table t1;
359
create table t1 SELECT it2 from t2 UNION select i from t2;
361
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
362
show create table t1;
364
create table t1 SELECT i from t2 UNION select f from t2;
366
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
367
show create table t1;
369
create table t1 SELECT f from t2 UNION select d from t2;
371
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
372
show create table t1;
374
create table t1 SELECT ib from t2 UNION select f from t2;
376
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
377
show create table t1;
379
create table t1 SELECT ib from t2 UNION select d from t2;
381
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
382
show create table t1;
384
create table t1 SELECT f from t2 UNION select da from t2;
386
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
387
show create table t1;
389
create table t1 SELECT da from t2 UNION select dt from t2;
391
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
392
show create table t1;
394
create table t1 SELECT dt from t2 UNION select trim(sc) from t2;
395
select trim(dt) from t1;
396
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
397
show create table t1;
399
create table t1 SELECT dt from t2 UNION select sv from t2;
401
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
402
show create table t1;
404
create table t1 SELECT sc from t2 UNION select sv from t2;
406
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
407
show create table t1;
409
create table t1 SELECT dt from t2 UNION select b from t2;
411
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
412
show create table t1;
414
create table t1 SELECT sv from t2 UNION select b from t2;
416
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
417
show create table t1;
419
create table t1 SELECT i from t2 UNION select d from t2 UNION select b from t2;
421
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
422
show create table t1;
424
create table t1 SELECT sv from t2 UNION select tx from t2;
426
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
427
show create table t1;
429
create table t1 SELECT b from t2 UNION select tx from t2;
431
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
432
show create table t1;
434
create table t1 select 1 union select -1;
436
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
437
show create table t1;
439
create table t1 select _latin1"test" union select _latin1"testt" ;
440
create table t1 select _utf8"test" union select _utf8"testt" ;
441
create table t1 select "test" union select "testt" ;
442
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
443
show create table t1;
447
# conversion memory->disk table
449
create table t1 (s char(200));
450
insert into t1 values (repeat("1",200));
451
create table t2 select * from t1;
452
insert into t2 select * from t1;
453
insert into t1 select * from t2;
454
insert into t2 select * from t1;
455
insert into t1 select * from t2;
456
insert into t2 select * from t1;
457
set local tmp_table_size=1024;
458
select count(*) from (select * from t1 union all select * from t2 order by 1) b;
459
select count(*) from t1;
460
select count(*) from t2;
462
set local tmp_table_size=default;
467
create table t1 (a int, index (a), b int);
468
insert t1 values (1,1),(2,2),(3,3),(4,4),(5,5);
469
insert t1 select a+1, a+b from t1;
470
insert t1 select a+1, a+b from t1;
471
insert t1 select a+1, a+b from t1;
472
insert t1 select a+1, a+b from t1;
473
insert t1 select a+1, a+b from t1;
476
show status like 'Slow_queries';
477
select count(*) from t1 where a=7;
479
show status like 'Slow_queries';
480
select count(*) from t1 where b=13;
482
show status like 'Slow_queries';
483
select count(*) from t1 where b=13 union select count(*) from t1 where a=7;
485
show status like 'Slow_queries';
486
select count(*) from t1 where a=7 union select count(*) from t1 where b=13;
488
show status like 'Slow_queries';
489
# additional test for examined rows
491
select a from t1 where b not in (1,2,3) union select a from t1 where b not in (4,5,6);
493
show status like 'Slow_queries';
497
# Column 'name' cannot be null (error with union and left join) (bug #2508)
499
create table t1 ( RID int not null default '0', IID int not null default '0', nada varchar(50) not null,NAME varchar(50) not null,PHONE varchar(50) not null);
500
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');
501
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);
505
# Bug #2809 (UNION fails on MyIsam tables when index on second column from
508
create table t1 (col1 int, col2 int);
509
insert into t1 values (1,2),(3,4),(5,6),(7,8),(9,10);
510
select col1 n from t1 union select col2 n from t1 order by n;
511
alter table t1 add index myindex (col2);
512
select col1 n from t1 union select col2 n from t1 order by n;
516
# Incorrect handling of UNION ALL (Bug #1428)
518
create table t1 (i int);
519
insert into t1 values (1);
520
select * from t1 UNION select * from t1;
521
select * from t1 UNION ALL select * from t1;
522
select * from t1 UNION select * from t1 UNION ALL select * from t1;
524
select 1 as a union all select 1 union all select 2 union select 1 union all select 2;
525
set sql_select_limit=1;
526
select 1 union select 2;
527
(select 1) union (select 2);
528
(select 1) union (select 2) union (select 3) limit 2;
529
set sql_select_limit=default;
534
create table t1 (a int);
535
insert into t1 values (100), (1);
536
create table t2 (a int);
537
insert into t2 values (100);
538
select a from t1 union select a from t2 order by a;
539
SET SQL_SELECT_LIMIT=1;
540
select a from t1 union select a from t2 order by a;
542
set sql_select_limit=default;
545
# nonexisting column in global ORDER BY
547
CREATE TABLE t1 (i int default NULL,c char(1) default NULL,KEY i (i));
548
CREATE TABLE t2 (i int default NULL,c char(1) default NULL,KEY i (i));
550
explain (select * from t1) union (select * from t2) order by not_existing_column;
556
CREATE TABLE t1 (uid int);
557
INSERT INTO t1 SELECT 150;
558
SELECT 'a' UNION SELECT uid FROM t1;
562
# parser stack overflow
564
CREATE TABLE t1 ( ID1 int NOT NULL DEFAULT '0' , ID2 datetime, DATA1 varchar(10) , DATA2 double(5,4) , DATA3 datetime , PRIMARY KEY (ID1,ID2));
566
CREATE TABLE t2 ( ID int NOT NULL DEFAULT '0' , DATA1 timestamp NULL, PRIMARY KEY (ID));
567
(SELECT * FROM t1 AS PARTITIONED, t2 AS
568
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
569
(SELECT * FROM t1 AS PARTITIONED, t2 AS
570
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
571
(SELECT * FROM t1 AS PARTITIONED, t2 AS
572
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
573
(SELECT * FROM t1 AS PARTITIONED, t2 AS
574
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
575
(SELECT * FROM t1 AS PARTITIONED, t2 AS
576
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
577
(SELECT * FROM t1 AS PARTITIONED, t2 AS
578
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
579
(SELECT * FROM t1 AS PARTITIONED, t2 AS
580
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
581
(SELECT * FROM t1 AS PARTITIONED, t2 AS
582
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
583
(SELECT * FROM t1 AS PARTITIONED, t2 AS
584
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
585
(SELECT * FROM t1 AS PARTITIONED, t2 AS
586
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
587
(SELECT * FROM t1 AS PARTITIONED, t2 AS
588
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
589
(SELECT * FROM t1 AS PARTITIONED, t2 AS
590
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1);
594
# merging ENUM and SET fields in one UNION
596
create table t1 (a ENUM('Yes', 'No') NOT NULL);
597
create table t2 (a ENUM('aaa', 'bbb') NOT NULL);
598
insert into t1 values ('No');
599
insert into t2 values ('bbb');
600
create table t3 (a ENUM('Yes', 'No') NOT NULL);
601
create table t4 (a ENUM('aaa', 'bbb') NOT NULL);
602
insert into t3 values (1);
603
--error 1691 # Bad enum
604
insert into t4 values (3);
605
select "1" as a union select a from t1;
606
select a as a from t1 union select "1";
607
select a as a from t2 union select a from t1;
608
select "1" as a union select a from t3;
609
select a as a from t3 union select "1";
610
select a as a from t4 union select a from t3;
611
select a as a from t1 union select a from t4;
612
drop table t1,t2,t3,t4;
615
# Bug #6139 UNION doesn't understand collate in the column of second select
618
(select 'test') union
619
(select 'TEST') union
621
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
622
show create table t1;
623
select count(*) from t1;
627
(select 'test' collate utf8_bin) union
628
(select 'TEST') union
630
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
631
show create table t1;
632
select count(*) from t1;
636
(select 'test') union
637
(select 'TEST' collate utf8_bin) union
639
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
640
show create table t1;
641
select count(*) from t1;
645
(select 'test') union
646
(select 'TEST') union
647
(select 'TeST' collate utf8_bin);
648
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
649
show create table t1;
650
select count(*) from t1;
653
# Drizzle doesn't support specifying character set, it is all UTF8
656
a char character set utf8 collate utf8_swedish_ci,
657
b char character set utf8 collate utf8_spanish_ci);
660
a char collate utf8_swedish_ci,
661
b char collate utf8_spanish_ci);
664
(select a from t2) union
669
(select a collate utf8_swedish_ci from t2) union
671
#show create table t1;
675
(select a from t2) union
676
(select b collate utf8_swedish_ci from t2);
677
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
678
show create table t1;
681
(select a from t2) union
682
(select b from t2) union
683
(select 'c' collate utf8_spanish_ci from t2);
684
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
685
show create table t1;
690
# Bug 6931: Date Type column problem when using UNION-Table.
692
create table t1(a1 int, f1 char(10));
694
select f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) a
696
select f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) a
698
show columns from t2;
701
create table t1 (f1 int);
702
create table t2 (f1 int, f2 int ,f3 date);
703
create table t3 (f1 int, f2 char(10));
706
select t2.f3 as sdate
708
left outer join t2 on (t1.f1 = t2.f1)
709
inner join t3 on (t2.f2 = t3.f1)
710
order by t1.f1, t3.f1, t2.f3
714
select cast('2004-12-31' as date) as sdate
716
left outer join t2 on (t1.f1 = t2.f1)
717
inner join t3 on (t2.f2 = t3.f1)
719
order by t1.f1, t3.f1, t2.f3
722
show columns from t4;
723
drop table t1, t2, t3, t4;
726
# Bug #2435 UNION with parentheses not supported
728
create table t1 (a int not null, b char (10) not null);
729
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
730
select * from ((select * from t1 limit 1)) a;
731
select * from ((select * from t1 limit 1) union (select * from t1 limit 1)) a;
732
select * from ((select * from t1 limit 1) union (select * from t1 limit 1) union (select * from t1 limit 1)) a;
733
select * from ((((select * from t1))) union (select * from t1) union (select * from t1)) a;
734
select * from ((select * from t1) union (((select * from t1))) union (select * from t1)) a;
738
# Bugs#6519 UNION with collation binary and latin1_swedish_ci fails
741
select concat('value is: ', @val) union select 'some text';
744
# Bug#15949 union + illegal mix of collations (IMPLICIT + COERCIBLE)
746
select concat('a', 'b' collate utf8_bin);
747
create table t1 (foo varchar(100)) collate utf8_bin;
748
insert into t1 (foo) values ("foo");
750
select foo from t1 union select 'bar' as foo from dual;
751
select foo from t1 union select 'bar' as foo;
755
# Commenting out this test until Bug 308841 is fixed
760
# a ENUM('ďż˝','ďż˝','ďż˝') character set utf8 not null default 'ďż˝',
761
# b ENUM("one", "two") character set utf8,
762
# c ENUM("one", "two")
764
#--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
765
#show create table t1;
766
#insert into t1 values ('ďż˝', 'one', 'one'), ('ďż˝', 'two', 'one'), ('ďż˝', NULL, NULL);
767
#create table t2 select NULL union select a from t1;
768
#show columns from t2;
770
#create table t2 select a from t1 union select NULL;
771
#show columns from t2;
773
#create table t2 select a from t1 union select a from t1;
774
#show columns from t2;
776
#create table t2 select a from t1 union select c from t1;
778
#create table t2 select a from t1 union select b from t1;
779
#show columns from t2;
783
# Bug #14216: UNION + DECIMAL wrong values in result
785
create table t1 (f1 decimal(60,25), f2 decimal(60,25));
786
insert into t1 values (0.0,0.0);
787
select f1 from t1 union all select f2 from t1;
788
select 'XXXXXXXXXXXXXXXXXXXX' as description, f1 from t1
790
select 'YYYYYYYYYYYYYYYYYYYY' as description, f2 from t1;
792
create table t1 (f1 decimal(60,24), f2 decimal(60,24));
793
insert into t1 values (0.0,0.0);
794
select f1 from t1 union all select f2 from t1;
795
select 'XXXXXXXXXXXXXXXXXXXX' as description, f1 from t1
797
select 'YYYYYYYYYYYYYYYYYYYY' as description, f2 from t1;
801
# Test that union with VARCHAR produces dynamic row tables
804
create table t1 (a varchar(5));
805
create table t2 select * from t1 union select 'abcdefghijkl';
806
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
807
show create table t2;
808
select row_format from data_dictionary.TABLES where table_schema="test" and table_name="t2";
809
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
810
show create table t2;
814
# correct conversion long string to TEXT (BUG#10025)
817
CREATE TABLE t1 (a mediumtext);
818
CREATE TABLE t2 (b varchar(20));
819
INSERT INTO t1 VALUES ('a'),('b');
820
SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
821
create table t3 SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
822
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
823
show create table t3;
824
drop tables t1,t2,t3;
827
# Extended fix to Bug#10025 - the test above should result to mediumtext
828
# and the one below to longtext. Earlier above test resulted to longtext
832
CREATE TABLE t1 (a longtext);
833
CREATE TABLE t2 (b varchar(20));
834
INSERT INTO t1 VALUES ('a'),('b');
835
SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
836
create table t3 SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
837
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
838
show create table t3;
839
drop tables t1,t2,t3;
842
# Testing here that mediumtext converts into longtext if the result
843
# exceeds mediumtext maximum length
846
SELECT @tmp_max:= @@max_allowed_packet;
847
SET max_allowed_packet=25000000;
848
CREATE TABLE t1 (a mediumtext);
849
CREATE TABLE t2 (b varchar(20));
850
INSERT INTO t1 VALUES ('a');
851
CREATE TABLE t3 SELECT REPEAT(a,20000000) AS a FROM t1 UNION SELECT b FROM t2;
852
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
853
SHOW CREATE TABLE t3;
855
CREATE TABLE t1 (a tinytext);
856
INSERT INTO t1 VALUES ('a');
857
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
858
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
859
SHOW CREATE TABLE t3;
861
CREATE TABLE t1 (a mediumtext);
862
INSERT INTO t1 VALUES ('a');
863
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
864
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
865
SHOW CREATE TABLE t3;
867
CREATE TABLE t1 (a tinyblob);
868
INSERT INTO t1 VALUES ('a');
869
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
870
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
871
SHOW CREATE TABLE t3;
872
DROP TABLES t1,t2,t3;
873
SET max_allowed_packet:= @tmp_max;
876
# Bug #10032 Bug in parsing UNION with ORDER BY when one node does not use FROM
879
create table t1 ( id int not null auto_increment, primary key (id), col1 int);
880
insert into t1 (col1) values (2),(3),(4),(5),(6);
881
select 99 union all select id from t1 order by 1;
882
select id from t1 union all select 99 order by 1;
888
# Bug#12185: Data type aggregation may produce wrong result
890
create table t1(f1 char(1), f2 char(5), f3 blob, f4 blob, f5 timestamp, f6 varchar(1) collate utf8_general_ci, f7 text);
891
create table t2 as select *, f6 as f8 from t1 union select *, f7 from t1;
892
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
893
show create table t2;
897
# Bug#18175: Union select over 129 tables with a sum function fails.
899
(select avg(1)) union (select avg(1)) union (select avg(1)) union
900
(select avg(1)) union (select avg(1)) union (select avg(1)) union
901
(select avg(1)) union (select avg(1)) union (select avg(1)) union
902
(select avg(1)) union (select avg(1)) union (select avg(1)) union
903
(select avg(1)) union (select avg(1)) union (select avg(1)) union
904
(select avg(1)) union (select avg(1)) union (select avg(1)) union
905
(select avg(1)) union (select avg(1)) union (select avg(1)) union
906
(select avg(1)) union (select avg(1)) union (select avg(1)) union
907
(select avg(1)) union (select avg(1)) union (select avg(1)) union
908
(select avg(1)) union (select avg(1)) union (select avg(1)) union
909
(select avg(1)) union (select avg(1)) union (select avg(1)) union
910
(select avg(1)) union (select avg(1)) union (select avg(1)) union
911
(select avg(1)) union (select avg(1)) union (select avg(1)) union
912
(select avg(1)) union (select avg(1)) union (select avg(1)) union
913
(select avg(1)) union (select avg(1)) union (select avg(1)) union
914
(select avg(1)) union (select avg(1)) union (select avg(1)) union
915
(select avg(1)) union (select avg(1)) union (select avg(1)) union
916
(select avg(1)) union (select avg(1)) union (select avg(1)) union
917
(select avg(1)) union (select avg(1)) union (select avg(1)) union
918
(select avg(1)) union (select avg(1)) union (select avg(1)) union
919
(select avg(1)) union (select avg(1)) union (select avg(1)) union
920
(select avg(1)) union (select avg(1)) union (select avg(1)) union
921
(select avg(1)) union (select avg(1)) union (select avg(1)) union
922
(select avg(1)) union (select avg(1)) union (select avg(1)) union
923
(select avg(1)) union (select avg(1)) union (select avg(1)) union
924
(select avg(1)) union (select avg(1)) union (select avg(1)) union
925
(select avg(1)) union (select avg(1)) union (select avg(1)) union
926
(select avg(1)) union (select avg(1)) union (select avg(1)) union
927
(select avg(1)) union (select avg(1)) union (select avg(1)) union
928
(select avg(1)) union (select avg(1)) union (select avg(1)) union
929
(select avg(1)) union (select avg(1)) union (select avg(1)) union
930
(select avg(1)) union (select avg(1)) union (select avg(1)) union
931
(select avg(1)) union (select avg(1)) union (select avg(1)) union
932
(select avg(1)) union (select avg(1)) union (select avg(1)) union
933
(select avg(1)) union (select avg(1)) union (select avg(1)) union
934
(select avg(1)) union (select avg(1)) union (select avg(1)) union
935
(select avg(1)) union (select avg(1)) union (select avg(1)) union
936
(select avg(1)) union (select avg(1)) union (select avg(1)) union
937
(select avg(1)) union (select avg(1)) union (select avg(1)) union
938
(select avg(1)) union (select avg(1)) union (select avg(1)) union
939
(select avg(1)) union (select avg(1)) union (select avg(1)) union
940
(select avg(1)) union (select avg(1)) union (select avg(1)) union
941
(select avg(1)) union (select avg(1)) union (select avg(1));
944
# Bug #16881: password() and union select
945
# (The issue was poor handling of character set aggregation.)
947
select '12' union select '12345';
950
# Bug #26661: UNION with ORDER BY undefined column in FROM list
953
CREATE TABLE t1 (a int);
954
INSERT INTO t1 VALUES (3),(1),(2),(4),(1);
956
SELECT a FROM (SELECT a FROM t1 UNION SELECT a FROM t1 ORDER BY a) AS test;
958
SELECT a FROM (SELECT a FROM t1 UNION SELECT a FROM t1 ORDER BY c) AS test;
963
# Bug#23345: Wrongly allowed INTO in a non-last select of a UNION.
966
(select 1 into @var) union (select 1);
967
(select 1) union (select 1 into @var);
970
(select 2) union (select 1 into @var);
973
# Bug#27848: order-by of union clashes with rollup of select part
976
CREATE TABLE t1 (a int);
977
INSERT INTO t1 VALUES (10), (20);
978
CREATE TABLE t2 (b int);
979
INSERT INTO t2 VALUES (10), (50), (50);
983
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
988
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
993
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
994
ORDER BY a ASC LIMIT 3;
998
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
1001
--error ER_WRONG_USAGE
1004
(SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP ORDER BY a);
1006
--error ER_WRONG_USAGE
1009
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP ORDER BY a
1015
# Bug#32848: Data type conversion bug in union subselects in MySQL 5.0.38
1017
CREATE TABLE t1 (a INT);
1018
INSERT INTO t1 VALUES (1), (2), (3);
1020
CREATE TABLE t2 SELECT * FROM (SELECT NULL) a UNION SELECT a FROM t1;
1023
CREATE TABLE t3 SELECT a FROM t1 UNION SELECT * FROM (SELECT NULL) a;
1026
CREATE TABLE t4 SELECT NULL;
1029
CREATE TABLE t5 SELECT NULL UNION SELECT NULL;
1033
SELECT * FROM (SELECT * FROM (SELECT NULL)a) b UNION SELECT a FROM t1;
1036
DROP TABLE t1, t2, t3, t4, t5, t6;
1037
--echo End of 5.0 tests