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;
28
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;
29
(select sql_calc_found_rows a,b from t1 limit 2) union all (select a,b from t2 order by a) limit 2;
31
select sql_calc_found_rows a,b from t1 union all select a,b from t2 limit 2;
35
# Test some error conditions with UNION
38
explain select a,b from t1 union all select a,b from t2;
41
explain select xx from t1 union select 1;
43
explain select a,b from t1 union select 1;
45
explain select 1 union select a,b from t1 union select 1;
47
explain select a,b from t1 union select 1 limit 0;
50
select a,b from t1 into outfile 'skr' union select a,b from t2;
53
select a,b from t1 order by a union select a,b from t2;
56
insert into t3 select a from t1 order by a union select a from t2;
59
create table t3 select a,b from t1 union select a from t2;
62
select a,b from t1 union select a from t2;
65
select * from t1 union select a from t2;
68
select a from t1 union select * from t2;
71
select * from t1 union select SQL_BUFFER_RESULT * from t2;
73
# Test CREATE, INSERT and REPLACE
74
create table t3 select a,b from t1 union all select a,b from t2;
75
insert into t3 select a,b from t1 union all select a,b from t2;
76
# PS can't handle REPLACE ... SELECT
77
replace into t3 select a,b as c from t1 union all select a,b from t2;
82
# Test some unions without tables
85
select * union select 1;
86
select 1 as a,(select a union select a);
88
(select 1) union (select 2) order by 0;
89
SELECT @a:=1 UNION SELECT @a:=@a+1;
91
(SELECT 1) UNION (SELECT 2) ORDER BY (SELECT a);
92
(SELECT 1,3) UNION (SELECT 2,1) ORDER BY (SELECT 2);
95
# Test bug reported by joc@presence-pc.com
99
`pseudo` char(35) NOT NULL default '',
100
`pseudo1` char(35) NOT NULL default '',
101
`same` tinyint(1) unsigned NOT NULL default '1',
102
PRIMARY KEY (`pseudo1`),
103
KEY `pseudo` (`pseudo`)
105
INSERT INTO t1 (pseudo,pseudo1,same) VALUES ('joce', 'testtt', 1),('joce', 'tsestset', 1),('dekad', 'joce', 1);
106
SELECT pseudo FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo FROM t1 WHERE pseudo='joce';
107
SELECT pseudo1 FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo1 FROM t1 WHERE pseudo='joce';
108
SELECT * FROM t1 WHERE pseudo1='joce' UNION SELECT * FROM t1 WHERE pseudo='joce' order by pseudo desc,pseudo1 desc;
109
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT pseudo FROM t1 WHERE pseudo1='joce';
110
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION ALL SELECT pseudo FROM t1 WHERE pseudo1='joce';
111
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT 1;
114
create table t1 (a int);
115
create table t2 (a int);
116
insert into t1 values (1),(2),(3),(4),(5);
117
insert into t2 values (11),(12),(13),(14),(15);
118
(select * from t1 limit 2) union (select * from t2 limit 3) limit 4;
119
(select * from t1 limit 2) union (select * from t2 limit 3);
120
(select * from t1 limit 2) union (select * from t2 limit 20,3);
121
set SQL_SELECT_LIMIT=2;
122
(select * from t1 limit 1) union (select * from t2 limit 3);
123
set SQL_SELECT_LIMIT=DEFAULT;
127
# Test error with left join
131
cid smallint(5) unsigned NOT NULL default '0',
132
cv varchar(250) NOT NULL default '',
136
INSERT INTO t1 VALUES (8,'dummy');
138
cid bigint(20) unsigned NOT NULL auto_increment,
139
cap varchar(255) NOT NULL default '',
144
gid bigint(20) unsigned NOT NULL auto_increment,
145
gn varchar(255) NOT NULL default '',
146
must tinyint(4) default NULL,
150
INSERT INTO t3 VALUES (1,'V1',NULL);
152
uid bigint(20) unsigned NOT NULL default '0',
153
gid bigint(20) unsigned default NULL,
154
rid bigint(20) unsigned default NULL,
155
cid bigint(20) unsigned default NULL,
156
UNIQUE KEY m (uid,gid,rid,cid),
160
KEY container (gid,rid,cid)
162
INSERT INTO t4 VALUES (1,1,NULL,NULL);
164
rid bigint(20) unsigned NOT NULL auto_increment,
165
rl varchar(255) NOT NULL default '',
170
uid bigint(20) unsigned NOT NULL auto_increment,
171
un varchar(250) NOT NULL default '',
172
uc smallint(5) unsigned NOT NULL default '0',
174
UNIQUE KEY nc (un,uc),
177
INSERT INTO t6 VALUES (1,'test',8);
179
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";
180
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";
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 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");
182
drop table t1,t2,t3,t4,t5,t6;
185
# Test insert ... SELECT with UNION
188
CREATE TABLE t1 (a int not null, b char (10) not null);
189
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
190
CREATE TABLE t2 (a int not null, b char (10) not null);
191
insert into t2 values (3,'c'),(4,'d'),(5,'f'),(6,'e');
192
create table t3 select a,b from t1 union select a,b from t2;
193
create table t4 (select a,b from t1) union (select a,b from t2) limit 2;
194
insert into t4 select a,b from t1 union select a,b from t2;
195
insert into t3 (select a,b from t1) union (select a,b from t2) limit 2;
198
drop table t1,t2,t3,t4;
201
# Test of SQL_CALC_FOUND_ROW handling
203
create table t1 (a int);
204
insert into t1 values (1),(2),(3);
205
create table t2 (a int);
206
insert into t2 values (3),(4),(5);
209
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2) LIMIT 1;
211
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2) LIMIT 2;
214
# Test cases where found_rows() should return number of returned rows
215
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION all (SELECT * FROM t2);
217
(SELECT SQL_CALC_FOUND_ROWS * FROM t1) UNION all (SELECT * FROM t2 LIMIT 1);
219
# This used to work in 4.0 but not anymore in 4.1
221
(SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1) UNION SELECT * FROM t2 LIMIT 1;
222
#select found_rows();
224
# In these case found_rows() should work
225
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION all SELECT * FROM t2 LIMIT 2;
227
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION all SELECT * FROM t2 LIMIT 2;
230
# The following examples will not be exact
231
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2;
233
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 100;
235
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 100 UNION SELECT * FROM t2;
237
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2;
239
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1 UNION SELECT * FROM t2 LIMIT 2;
241
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
243
SELECT SQL_CALC_FOUND_ROWS * FROM t1 limit 2,2 UNION SELECT * FROM t2;
246
# Test some limits with ORDER BY
247
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
248
(SELECT * FROM t1 ORDER by a) UNION ALL (SELECT * FROM t2 ORDER BY a) ORDER BY A desc LIMIT 4;
252
(SELECT * FROM t1) UNION all (SELECT SQL_CALC_FOUND_ROWS * FROM t2) LIMIT 1;
254
create temporary table t1 select a from t1 union select a from t2;
255
drop temporary table t1;
257
create table t1 select a from t1 union select a from t2;
259
select a from t1 union select a from t2 order by t2.a;
263
# Problem with alias '*' (BUG #1249)
266
select length(version()) > 1 as `*` UNION select 2;
269
# Bug #4980: problem with explain
272
create table t1 (a int);
273
insert into t1 values (0), (3), (1), (2);
274
explain (select * from t1) union (select * from t1) order by a;
277
# Test for another bug with UNION and LEFT JOIN
279
CREATE TABLE t1 ( id int(3) unsigned default '0') ENGINE=MyISAM;
280
INSERT INTO t1 (id) VALUES("1");
281
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;
282
INSERT INTO t2 (id, id_master, text1, text2) VALUES("1", "1",
284
INSERT INTO t2 (id, id_master, text1, text2) VALUES("2", "1",
286
INSERT INTO t2 (id, id_master, text1, text2) VALUES("3", "1", NULL,
288
INSERT INTO t2 (id, id_master, text1, text2) VALUES("4", "1",
290
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;
291
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;
292
drop table if exists t1,t2;
295
# Test of bug when using the same table multiple times
297
create table t1 (a int not null primary key auto_increment, b int, key(b));
298
create table t2 (a int not null primary key auto_increment, b int);
299
insert into t1 (b) values (1),(2),(2),(3);
300
insert into t2 (b) values (10),(11),(12),(13);
302
explain extended (select * from t1 where a=1) union (select * from t2 where a=1);
303
(select * from t1 where a=5) union (select * from t2 where a=1);
304
(select * from t1 where a=5 and a=6) union (select * from t2 where a=1);
305
(select t1.a,t1.b from t1,t2 where t1.a=5) union (select * from t2 where a=1);
306
(select * from t1 where a=1) union (select t1.a,t2.a from t1,t2 where t1.a=t2.a);
307
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);
308
explain (select * from t1 where a=1) union (select * from t1 where b=1);
310
create table t1 ( id int not null auto_increment, primary key (id) ,user_name text );
311
create table t2 ( id int not null auto_increment, primary key (id) ,group_name text );
312
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) );
313
insert into t1 (user_name) values ('Tester');
314
insert into t2 (group_name) values ('Group A');
315
insert into t2 (group_name) values ('Group B');
316
insert into t3 (user_id, group_id) values (1,1);
317
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;
318
drop table t1, t2, t3;
323
create table t1 (mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, matintnum CHAR(6) NOT NULL, test MEDIUMINT UNSIGNED NULL);
324
create table t2 (mat_id MEDIUMINT UNSIGNED NOT NULL, pla_id MEDIUMINT UNSIGNED NOT NULL);
325
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);
326
insert into t2 values (1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104), (3, 101), (3, 102), (3, 105);
327
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;
333
create table t1 SELECT "a" as a UNION select "aa" as a;
335
show create table t1;
337
create table t1 SELECT 12 as a UNION select "aa" as a;
339
show create table t1;
341
create table t1 SELECT 12 as a UNION select 12.2 as a;
343
show create table t1;
346
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);
347
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');
349
create table t1 SELECT it2 from t2 UNION select it1 from t2;
351
show create table t1;
353
create table t1 SELECT it2 from t2 UNION select i from t2;
355
show create table t1;
357
create table t1 SELECT i from t2 UNION select f from t2;
359
show create table t1;
361
create table t1 SELECT f from t2 UNION select d from t2;
363
show create table t1;
365
create table t1 SELECT ib from t2 UNION select f from t2;
367
show create table t1;
369
create table t1 SELECT ib from t2 UNION select d from t2;
371
show create table t1;
373
create table t1 SELECT f from t2 UNION select y from t2;
375
show create table t1;
377
create table t1 SELECT f from t2 UNION select da from t2;
379
show create table t1;
381
create table t1 SELECT y from t2 UNION select da from t2;
383
show create table t1;
385
create table t1 SELECT y from t2 UNION select dt from t2;
387
show create table t1;
389
create table t1 SELECT da from t2 UNION select dt from t2;
391
show create table t1;
393
create table t1 SELECT dt from t2 UNION select trim(sc) from t2;
394
select trim(dt) from t1;
395
show create table t1;
397
create table t1 SELECT dt from t2 UNION select sv from t2;
399
show create table t1;
401
create table t1 SELECT sc from t2 UNION select sv from t2;
403
show create table t1;
405
create table t1 SELECT dt from t2 UNION select b from t2;
407
show create table t1;
409
create table t1 SELECT sv from t2 UNION select b from t2;
411
show create table t1;
413
create table t1 SELECT i from t2 UNION select d from t2 UNION select b from t2;
415
show create table t1;
417
create table t1 SELECT sv from t2 UNION select tx from t2;
419
show create table t1;
421
create table t1 SELECT b from t2 UNION select tx from t2;
423
show create table t1;
425
create table t1 select 1 union select -1;
427
show create table t1;
430
create table t1 select _latin1"test" union select _latin2"testt" ;
431
create table t1 select _latin2"test" union select _latin2"testt" ;
432
show create table t1;
436
# conversion memory->disk table
438
create table t1 (s char(200));
439
insert into t1 values (repeat("1",200));
440
create table t2 select * from t1;
441
insert into t2 select * from t1;
442
insert into t1 select * from t2;
443
insert into t2 select * from t1;
444
insert into t1 select * from t2;
445
insert into t2 select * from t1;
446
set local tmp_table_size=1024;
447
select count(*) from (select * from t1 union all select * from t2 order by 1) b;
448
select count(*) from t1;
449
select count(*) from t2;
451
set local tmp_table_size=default;
456
create table t1 (a int, index (a), b int);
457
insert t1 values (1,1),(2,2),(3,3),(4,4),(5,5);
458
insert t1 select a+1, a+b from t1;
459
insert t1 select a+1, a+b from t1;
460
insert t1 select a+1, a+b from t1;
461
insert t1 select a+1, a+b from t1;
462
insert t1 select a+1, a+b from t1;
464
show status like 'Slow_queries';
465
select count(*) from t1 where a=7;
466
show status like 'Slow_queries';
467
select count(*) from t1 where b=13;
468
show status like 'Slow_queries';
469
select count(*) from t1 where b=13 union select count(*) from t1 where a=7;
470
show status like 'Slow_queries';
471
select count(*) from t1 where a=7 union select count(*) from t1 where b=13;
472
show status like 'Slow_queries';
473
# additional test for examined rows
475
select a from t1 where b not in (1,2,3) union select a from t1 where b not in (4,5,6);
476
show status like 'Slow_queries';
480
# Column 'name' cannot be null (error with union and left join) (bug #2508)
482
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;
483
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');
484
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);
488
# Bug #2809 (UNION fails on MyIsam tables when index on second column from
491
create table t1 (col1 tinyint unsigned, col2 tinyint unsigned);
492
insert into t1 values (1,2),(3,4),(5,6),(7,8),(9,10);
493
select col1 n from t1 union select col2 n from t1 order by n;
494
alter table t1 add index myindex (col2);
495
select col1 n from t1 union select col2 n from t1 order by n;
499
# Incorrect handling of UNION ALL (Bug #1428)
501
create table t1 (i int);
502
insert into t1 values (1);
503
select * from t1 UNION select * from t1;
504
select * from t1 UNION ALL select * from t1;
505
select * from t1 UNION select * from t1 UNION ALL select * from t1;
507
select 1 as a union all select 1 union all select 2 union select 1 union all select 2;
508
set sql_select_limit=1;
509
select 1 union select 2;
510
(select 1) union (select 2);
511
(select 1) union (select 2) union (select 3) limit 2;
512
set sql_select_limit=default;
517
create table t1 (a int);
518
insert into t1 values (100), (1);
519
create table t2 (a int);
520
insert into t2 values (100);
521
select a from t1 union select a from t2 order by a;
522
SET SQL_SELECT_LIMIT=1;
523
select a from t1 union select a from t2 order by a;
525
set sql_select_limit=default;
528
# nonexisting column in global ORDER BY
530
CREATE TABLE t1 (i int(11) default NULL,c char(1) default NULL,KEY i (i));
531
CREATE TABLE t2 (i int(11) default NULL,c char(1) default NULL,KEY i (i));
533
explain (select * from t1) union (select * from t2) order by not_existing_column;
539
CREATE TABLE t1 (uid int(1));
540
INSERT INTO t1 SELECT 150;
541
SELECT 'a' UNION SELECT uid FROM t1;
545
# parser stack overflow
547
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));
549
CREATE TABLE t2 ( ID int(3) unsigned NOT NULL DEFAULT '0' , DATA1 timestamp DEFAULT '0000-00-00 00:00:00' , PRIMARY KEY (ID));
550
(SELECT * FROM t1 AS PARTITIONED, t2 AS
551
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
552
(SELECT * FROM t1 AS PARTITIONED, t2 AS
553
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
554
(SELECT * FROM t1 AS PARTITIONED, t2 AS
555
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
556
(SELECT * FROM t1 AS PARTITIONED, t2 AS
557
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
558
(SELECT * FROM t1 AS PARTITIONED, t2 AS
559
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
560
(SELECT * FROM t1 AS PARTITIONED, t2 AS
561
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
562
(SELECT * FROM t1 AS PARTITIONED, t2 AS
563
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
564
(SELECT * FROM t1 AS PARTITIONED, t2 AS
565
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
566
(SELECT * FROM t1 AS PARTITIONED, t2 AS
567
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
568
(SELECT * FROM t1 AS PARTITIONED, t2 AS
569
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
570
(SELECT * FROM t1 AS PARTITIONED, t2 AS
571
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1) UNION
572
(SELECT * FROM t1 AS PARTITIONED, t2 AS
573
PARTITIONED_B WHERE PARTITIONED_B.ID=PARTITIONED.ID1);
577
# merging ENUM and SET fields in one UNION
579
create table t1 (a ENUM('Yes', 'No') NOT NULL);
580
create table t2 (a ENUM('aaa', 'bbb') NOT NULL);
581
insert into t1 values ('No');
582
insert into t2 values ('bbb');
583
create table t3 (a SET('Yes', 'No') NOT NULL);
584
create table t4 (a SET('aaa', 'bbb') NOT NULL);
585
insert into t3 values (1);
586
insert into t4 values (3);
587
select "1" as a union select a from t1;
588
select a as a from t1 union select "1";
589
select a as a from t2 union select a from t1;
590
select "1" as a union select a from t3;
591
select a as a from t3 union select "1";
592
select a as a from t4 union select a from t3;
593
select a as a from t1 union select a from t4;
594
drop table t1,t2,t3,t4;
597
# Bug #6139 UNION doesn't understand collate in the column of second select
600
(select _latin1'test') union
601
(select _latin1'TEST') union
602
(select _latin1'TeST');
603
show create table t1;
604
select count(*) from t1;
608
(select _latin1'test' collate latin1_bin) union
609
(select _latin1'TEST') union
610
(select _latin1'TeST');
611
show create table t1;
612
select count(*) from t1;
616
(select _latin1'test') union
617
(select _latin1'TEST' collate latin1_bin) union
618
(select _latin1'TeST');
619
show create table t1;
620
select count(*) from t1;
624
(select _latin1'test') union
625
(select _latin1'TEST') union
626
(select _latin1'TeST' collate latin1_bin);
627
show create table t1;
628
select count(*) from t1;
632
a char character set latin1 collate latin1_swedish_ci,
633
b char character set latin1 collate latin1_german1_ci);
636
(select a from t2) union
639
(select a collate latin1_german1_ci from t2) union
641
show create table t1;
644
(select a from t2) union
645
(select b collate latin1_german1_ci from t2);
646
show create table t1;
649
(select a from t2) union
650
(select b from t2) union
651
(select 'c' collate latin1_german1_ci from t2);
652
show create table t1;
657
# Bug 6931: Date Type column problem when using UNION-Table.
659
create table t1(a1 int, f1 char(10));
661
select f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) a
663
select f2,a1 from (select a1, CAST('2004-12-31' AS DATE) f2 from t1) a
665
show columns from t2;
668
create table t1 (f1 int);
669
create table t2 (f1 int, f2 int ,f3 date);
670
create table t3 (f1 int, f2 char(10));
673
select t2.f3 as sdate
675
left outer join t2 on (t1.f1 = t2.f1)
676
inner join t3 on (t2.f2 = t3.f1)
677
order by t1.f1, t3.f1, t2.f3
681
select cast('2004-12-31' as date) as sdate
683
left outer join t2 on (t1.f1 = t2.f1)
684
inner join t3 on (t2.f2 = t3.f1)
686
order by t1.f1, t3.f1, t2.f3
689
show columns from t4;
690
drop table t1, t2, t3, t4;
693
# Bug #2435 UNION with parentheses not supported
695
create table t1 (a int not null, b char (10) not null);
696
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
697
select * from ((select * from t1 limit 1)) a;
698
select * from ((select * from t1 limit 1) union (select * from t1 limit 1)) a;
699
select * from ((select * from t1 limit 1) union (select * from t1 limit 1) union (select * from t1 limit 1)) a;
700
select * from ((((select * from t1))) union (select * from t1) union (select * from t1)) a;
701
select * from ((select * from t1) union (((select * from t1))) union (select * from t1)) a;
705
# Bugs#6519 UNION with collation binary and latin1_swedish_ci fails
708
select concat('value is: ', @val) union select 'some text';
711
# Bug#15949 union + illegal mix of collations (IMPLICIT + COERCIBLE)
713
select concat(_latin1'a', _ascii'b' collate ascii_bin);
714
create table t1 (foo varchar(100)) collate ascii_bin;
715
insert into t1 (foo) values ("foo");
716
select foo from t1 union select 'bar' as foo from dual;
723
a ENUM('ďż˝','ďż˝','ďż˝') character set utf8 not null default 'ďż˝',
724
b ENUM("one", "two") character set utf8,
727
show create table t1;
728
insert into t1 values ('ďż˝', 'one', 'one'), ('ďż˝', 'two', 'one'), ('ďż˝', NULL, NULL);
729
create table t2 select NULL union select a from t1;
730
show columns from t2;
732
create table t2 select a from t1 union select NULL;
733
show columns from t2;
735
create table t2 select a from t1 union select a from t1;
736
show columns from t2;
738
create table t2 select a from t1 union select c from t1;
740
create table t2 select a from t1 union select b from t1;
741
show columns from t2;
745
# Bug #14216: UNION + DECIMAL wrong values in result
747
create table t1 (f1 decimal(60,25), f2 decimal(60,25));
748
insert into t1 values (0.0,0.0);
749
select f1 from t1 union all select f2 from t1;
750
select 'XXXXXXXXXXXXXXXXXXXX' as description, f1 from t1
752
select 'YYYYYYYYYYYYYYYYYYYY' as description, f2 from t1;
754
create table t1 (f1 decimal(60,24), f2 decimal(60,24));
755
insert into t1 values (0.0,0.0);
756
select f1 from t1 union all select f2 from t1;
757
select 'XXXXXXXXXXXXXXXXXXXX' as description, f1 from t1
759
select 'YYYYYYYYYYYYYYYYYYYY' as description, f2 from t1;
763
# Test that union with VARCHAR produces dynamic row tables
766
create table t1 (a varchar(5));
767
create table t2 select * from t1 union select 'abcdefghijkl';
768
show create table t2;
769
select row_format from information_schema.TABLES where table_schema="test" and table_name="t2";
770
alter table t2 ROW_FORMAT=fixed;
771
show create table t2;
775
# correct conversion long string to TEXT (BUG#10025)
778
CREATE TABLE t1 (a mediumtext);
779
CREATE TABLE t2 (b varchar(20));
780
INSERT INTO t1 VALUES ('a'),('b');
781
SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
782
create table t3 SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
783
show create table t3;
784
drop tables t1,t2,t3;
787
# Extended fix to Bug#10025 - the test above should result to mediumtext
788
# and the one below to longtext. Earlier above test resulted to longtext
792
CREATE TABLE t1 (a longtext);
793
CREATE TABLE t2 (b varchar(20));
794
INSERT INTO t1 VALUES ('a'),('b');
795
SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
796
create table t3 SELECT left(a,100000000) FROM t1 UNION SELECT b FROM t2;
797
show create table t3;
798
drop tables t1,t2,t3;
801
# Testing here that mediumtext converts into longtext if the result
802
# exceeds mediumtext maximum length
805
SELECT @tmp_max:= @@max_allowed_packet;
806
SET max_allowed_packet=25000000;
807
CREATE TABLE t1 (a mediumtext);
808
CREATE TABLE t2 (b varchar(20));
809
INSERT INTO t1 VALUES ('a');
810
CREATE TABLE t3 SELECT REPEAT(a,20000000) AS a FROM t1 UNION SELECT b FROM t2;
811
SHOW CREATE TABLE t3;
813
CREATE TABLE t1 (a tinytext);
814
INSERT INTO t1 VALUES ('a');
815
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
816
SHOW CREATE TABLE t3;
818
CREATE TABLE t1 (a mediumtext);
819
INSERT INTO t1 VALUES ('a');
820
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
821
SHOW CREATE TABLE t3;
823
CREATE TABLE t1 (a tinyblob);
824
INSERT INTO t1 VALUES ('a');
825
CREATE TABLE t3 SELECT REPEAT(a,2) AS a FROM t1 UNION SELECT b FROM t2;
826
SHOW CREATE TABLE t3;
827
DROP TABLES t1,t2,t3;
828
SET max_allowed_packet:= @tmp_max;
831
# Bug #10032 Bug in parsing UNION with ORDER BY when one node does not use FROM
834
create table t1 ( id int not null auto_increment, primary key (id), col1 int);
835
insert into t1 (col1) values (2),(3),(4),(5),(6);
836
select 99 union all select id from t1 order by 1;
837
select id from t1 union all select 99 order by 1;
843
# Bug#12185: Data type aggregation may produce wrong result
845
create table t1(f1 char(1), f2 char(5), f3 binary(1), f4 binary(5), f5 timestamp, f6 varchar(1) character set utf8 collate utf8_general_ci, f7 text);
846
create table t2 as select *, f6 as f8 from t1 union select *, f7 from t1;
847
show create table t2;
851
# Bug#18175: Union select over 129 tables with a sum function fails.
853
(select avg(1)) union (select avg(1)) union (select avg(1)) union
854
(select avg(1)) union (select avg(1)) union (select avg(1)) union
855
(select avg(1)) union (select avg(1)) union (select avg(1)) union
856
(select avg(1)) union (select avg(1)) union (select avg(1)) union
857
(select avg(1)) union (select avg(1)) union (select avg(1)) union
858
(select avg(1)) union (select avg(1)) union (select avg(1)) union
859
(select avg(1)) union (select avg(1)) union (select avg(1)) union
860
(select avg(1)) union (select avg(1)) union (select avg(1)) union
861
(select avg(1)) union (select avg(1)) union (select avg(1)) union
862
(select avg(1)) union (select avg(1)) union (select avg(1)) union
863
(select avg(1)) union (select avg(1)) union (select avg(1)) union
864
(select avg(1)) union (select avg(1)) union (select avg(1)) union
865
(select avg(1)) union (select avg(1)) union (select avg(1)) union
866
(select avg(1)) union (select avg(1)) union (select avg(1)) union
867
(select avg(1)) union (select avg(1)) union (select avg(1)) union
868
(select avg(1)) union (select avg(1)) union (select avg(1)) union
869
(select avg(1)) union (select avg(1)) union (select avg(1)) union
870
(select avg(1)) union (select avg(1)) union (select avg(1)) union
871
(select avg(1)) union (select avg(1)) union (select avg(1)) union
872
(select avg(1)) union (select avg(1)) union (select avg(1)) union
873
(select avg(1)) union (select avg(1)) union (select avg(1)) union
874
(select avg(1)) union (select avg(1)) union (select avg(1)) union
875
(select avg(1)) union (select avg(1)) union (select avg(1)) union
876
(select avg(1)) union (select avg(1)) union (select avg(1)) union
877
(select avg(1)) union (select avg(1)) union (select avg(1)) union
878
(select avg(1)) union (select avg(1)) union (select avg(1)) union
879
(select avg(1)) union (select avg(1)) union (select avg(1)) union
880
(select avg(1)) union (select avg(1)) union (select avg(1)) union
881
(select avg(1)) union (select avg(1)) union (select avg(1)) union
882
(select avg(1)) union (select avg(1)) union (select avg(1)) union
883
(select avg(1)) union (select avg(1)) union (select avg(1)) union
884
(select avg(1)) union (select avg(1)) union (select avg(1)) union
885
(select avg(1)) union (select avg(1)) union (select avg(1)) union
886
(select avg(1)) union (select avg(1)) union (select avg(1)) union
887
(select avg(1)) union (select avg(1)) union (select avg(1)) union
888
(select avg(1)) union (select avg(1)) union (select avg(1)) union
889
(select avg(1)) union (select avg(1)) union (select avg(1)) union
890
(select avg(1)) union (select avg(1)) union (select avg(1)) union
891
(select avg(1)) union (select avg(1)) union (select avg(1)) union
892
(select avg(1)) union (select avg(1)) union (select avg(1)) union
893
(select avg(1)) union (select avg(1)) union (select avg(1)) union
894
(select avg(1)) union (select avg(1)) union (select avg(1)) union
895
(select avg(1)) union (select avg(1)) union (select avg(1));
898
# Bug #16881: password() and union select
899
# (The issue was poor handling of character set aggregation.)
901
select _utf8'12' union select _latin1'12345';
904
# Bug #26661: UNION with ORDER BY undefined column in FROM list
907
CREATE TABLE t1 (a int);
908
INSERT INTO t1 VALUES (3),(1),(2),(4),(1);
910
SELECT a FROM (SELECT a FROM t1 UNION SELECT a FROM t1 ORDER BY a) AS test;
912
SELECT a FROM (SELECT a FROM t1 UNION SELECT a FROM t1 ORDER BY c) AS test;
917
# Bug#23345: Wrongly allowed INTO in a non-last select of a UNION.
920
(select 1 into @var) union (select 1);
921
(select 1) union (select 1 into @var);
924
(select 2) union (select 1 into @var);
927
# Bug#27848: order-by of union clashes with rollup of select part
930
CREATE TABLE t1 (a int);
931
INSERT INTO t1 VALUES (10), (20);
932
CREATE TABLE t2 (b int);
933
INSERT INTO t2 VALUES (10), (50), (50);
937
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
942
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
947
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
948
ORDER BY a ASC LIMIT 3;
952
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP
955
--error ER_WRONG_USAGE
958
(SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP ORDER BY a);
960
--error ER_WRONG_USAGE
963
SELECT b, COUNT(*) FROM t2 GROUP BY b WITH ROLLUP ORDER BY a
969
# Bug#32848: Data type conversion bug in union subselects in MySQL 5.0.38
971
CREATE TABLE t1 (a INT);
972
INSERT INTO t1 VALUES (1), (2), (3);
974
CREATE TABLE t2 SELECT * FROM (SELECT NULL) a UNION SELECT a FROM t1;
977
CREATE TABLE t3 SELECT a FROM t1 UNION SELECT * FROM (SELECT NULL) a;
980
CREATE TABLE t4 SELECT NULL;
983
CREATE TABLE t5 SELECT NULL UNION SELECT NULL;
987
SELECT * FROM (SELECT * FROM (SELECT NULL)a) b UNION SELECT a FROM t1;
990
DROP TABLE t1, t2, t3, t4, t5, t6;
991
--echo End of 5.0 tests