union.test 3.63 KB
Newer Older
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
1 2 3 4
#
# Test of unions
#

5
drop table if exists t1,t2,t3;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6 7 8 9 10 11 12 13 14 15 16 17 18
CREATE TABLE t1 (a int not null, b char (10) not null);
insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c');
CREATE TABLE t2 (a int not null, b char (10) not null);
insert into t2 values (3,'c'),(4,'d'),(5,'f'),(6,'e');

select a,b from t1 union select a,b from t2;
select a,b from t1 union all select a,b from t2;
select a,b from t1 union all select a,b from t2 order by b;
select a,b from t1 union all select a,b from t2 union select 7,'g';
select 0,'#' union select a,b from t1 union all select a,b from t2 union select 7,'gg';
select a,b from t1 union select a,b from t1;
select 't1',b,count(*) from t1 group by b UNION select 't2',b,count(*) from t2 group by b;

19 20
#test alternate syntax for unions 
(select a,b from t1 limit 2)  union all (select a,b from t2 order by a) limit 4;
21 22
(select a,b from t1 limit 2)  union all (select a,b from t2 order by a limit 1);
(select a,b from t1 limit 2)  union all (select a,b from t2 order by a limit 1) order by b desc;
23
explain (select a,b from t1 limit 2)  union all (select a,b from t2 order by a limit 1) order by b desc;
24 25
(select sql_calc_found_rows  a,b from t1 limit 2)  union all (select a,b from t2 order by a) limit 2;
select found_rows();
26
# Test some error conditions with UNION
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
27 28
explain select a,b from t1 union all select a,b from t2;

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
29 30 31 32 33 34
--error  1054
explain select xx from t1 union select 1;
explain select a,b from t1 union select 1;
explain select 1 union select a,b from t1 union select 1;
explain select a,b from t1 union select 1 limit 0;

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
35
--error 1221
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
36
select a,b from t1 into outfile 'skr' union select a,b from t2;
37

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
38
--error 1221
39 40
select a,b from t1 order by a union select a,b from t2;

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
41
--error 1221
42 43
insert into t3 select a from t1 order by a union select a from t2;

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
44
--error 1222
45 46
create table t3 select a,b from t1 union select a from t2;

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
47
--error 1222
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
48
select a,b from t1 union select a from t2;
49

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
50
--error 1222
51 52
select * from t1 union select a from t2;

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
53
--error 1222
54 55
select a from t1 union select * from t2;

56
# Test CREATE, INSERT and REPLACE
57 58
create table t3 select a,b from t1 union all select a,b from t2;
insert into t3 select a,b from t1 union all select a,b from t2;
59 60
replace into t3 select a,b as c from t1 union all select a,b from t2;

61
drop table t1,t2,t3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75

#
# Test bug reported by joc@presence-pc.com
#

CREATE TABLE t1 (
  `pseudo` char(35) NOT NULL default '',
  `pseudo1` char(35) NOT NULL default '',
  `same` tinyint(1) unsigned NOT NULL default '1',
  PRIMARY KEY  (`pseudo1`),
  KEY `pseudo` (`pseudo`)
) TYPE=MyISAM;
INSERT INTO t1 (pseudo,pseudo1,same) VALUES ('joce', 'testtt', 1),('joce', 'tsestset', 1),('dekad', 'joce', 1);
SELECT pseudo FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo FROM t1 WHERE pseudo='joce';
76
SELECT pseudo1 FROM t1 WHERE pseudo1='joce' UNION SELECT pseudo1 FROM t1 WHERE pseudo='joce';
77
SELECT * FROM t1 WHERE pseudo1='joce' UNION SELECT * FROM t1 WHERE pseudo='joce' order by pseudo desc,pseudo1 desc;
78 79 80
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT pseudo FROM t1 WHERE pseudo1='joce';
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION ALL SELECT pseudo FROM t1 WHERE pseudo1='joce';
SELECT pseudo1 FROM t1 WHERE pseudo='joce' UNION SELECT 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
81
drop table t1;
82 83 84 85 86 87 88
drop table if exists t1,t2;
create table t1 (a int);
create table t2 (a int);
insert into t1 values (1),(2),(3),(4),(5);
insert into t2 values (11),(12),(13),(14),(15);
(select * from t1 limit 2) union (select * from t2 limit 3) limit 4;
(select * from t1 limit 2) union (select * from t2 limit 3);
89 90 91 92
(select * from t1 limit 2) union (select * from t2 limit 20,3);
set SQL_SELECT_LIMIT=2;
(select * from t1 limit 2) union (select * from t2 limit 3);
set SQL_SELECT_LIMIT=DEFAULT;
93
drop table t1,t2;