select 1 union ( select 2 union select 3); 1 1 2 3 explain extended select 1 union ( select 2 union select 3); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4 UNION ALL NULL NULL NULL NULL 2 100.00 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1 */ select 1 AS `1` union /* select#4 */ select `__4`.`2` AS `2` from (/* select#2 */ select 2 AS `2` union /* select#3 */ select 3 AS `3`) `__4` select 1 union ( select 1 union select 1); 1 1 explain extended select 1 union ( select 1 union select 1); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4 UNION ALL NULL NULL NULL NULL 2 100.00 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1 */ select 1 AS `1` union /* select#4 */ select `__4`.`1` AS `1` from (/* select#2 */ select 1 AS `1` union /* select#3 */ select 1 AS `1`) `__4` select 1 union all ( select 1 union select 1); 1 1 1 explain extended select 1 union all ( select 1 union select 1); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4 UNION ALL NULL NULL NULL NULL 2 100.00 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1 */ select 1 AS `1` union all /* select#4 */ select `__4`.`1` AS `1` from (/* select#2 */ select 1 AS `1` union /* select#3 */ select 1 AS `1`) `__4` select 1 union ( select 1 union all select 1); 1 1 explain extended select 1 union ( select 1 union all select 1); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 4 UNION ALL NULL NULL NULL NULL 2 100.00 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1 */ select 1 AS `1` union /* select#4 */ select `__4`.`1` AS `1` from (/* select#2 */ select 1 AS `1` union all /* select#3 */ select 1 AS `1`) `__4` select 1 union select 1 union all select 1; 1 1 1 explain extended select 1 union select 1 union all select 1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 2 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1 */ select 1 AS `1` union /* select#2 */ select 1 AS `1` union all /* select#3 */ select 1 AS `1` (select 1 as a) union (select 2) order by a; a 1 2 explain extended (select 1 as a) union (select 2) order by a; id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 2 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Using filesort Warnings: Note 1003 (/* select#1 */ select 1 AS `a`) union (/* select#2 */ select 2 AS `2`) order by `a` /* select#1 */ select 1 AS `a` union /* select#2 */ select 2 AS `2` order by `a`; a 1 2 explain extended /* select#1 */ select 1 AS `a` union /* select#2 */ select 2 AS `2` order by `a`; id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 2 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Using filesort Warnings: Note 1003 /* select#1 */ select 1 AS `a` union /* select#2 */ select 2 AS `2` order by `a` select 1 union ( select 1 union (select 1 union (select 1 union select 1))); 1 1 explain extended all select 1 union ( select 1 union (select 1 union (select 1 union select 1))); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used 8 UNION ALL NULL NULL NULL NULL 2 100.00 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 7 UNION ALL NULL NULL NULL NULL 2 100.00 3 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 6 UNION ALL NULL NULL NULL NULL 2 100.00 4 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used 5 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 /* select#1/0 Filter Select: select `1` AS `1` */ select 1 AS `1` union /* select#8/0 */ select `__8`.`1` AS `1` from (/* select#2/1 Filter Select: select `1` AS `1` */ select 1 AS `1` union /* select#7/1 */ select `__7`.`1` AS `1` from (/* select#3/2 Filter Select: select `1` AS `1` */ select 1 AS `1` union /* select#6/2 */ select `__6`.`1` AS `1` from (/* select#4/3 Filter Select: select `1` AS `1` */ select 1 AS `1` union /* select#5/3 */ select 1 AS `1`) `__6`) `__7`) `__8` # # MDEV-6341: INSERT ... SELECT UNION with parenthesis # create table t1 (a int, b int); insert into t1 (select 1,1 union select 2,2); select * from t1 order by 1; a b 1 1 2 2 delete from t1; insert into t1 select 1,1 union select 2,2; select * from t1 order by 1; a b 1 1 2 2 drop table t1; CREATE OR REPLACE TABLE t1 AS SELECT 1 AS a UNION SELECT 2; select * from t1 order by 1; a 1 2 drop table t1; CREATE OR REPLACE TABLE t1 AS (SELECT 1 AS a UNION SELECT 2); select * from t1 order by 1; a 1 2 drop table t1; CREATE OR REPLACE VIEW v1 AS (SELECT 1 AS a); show create view v1; View Create View character_set_client collation_connection v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS (select 1 AS `a`) latin1 latin1_swedish_ci drop view v1; CREATE OR REPLACE VIEW v1 AS SELECT 1 AS a UNION SELECT 2; show create view v1; View Create View character_set_client collation_connection v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 1 AS `a` union select 2 AS `2` latin1 latin1_swedish_ci drop view v1; CREATE OR REPLACE VIEW v1 AS (SELECT 1 AS a UNION SELECT 2); show create view v1; View Create View character_set_client collation_connection v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 1 AS `a` union select 2 AS `2` latin1 latin1_swedish_ci drop view v1; # # MDEV-10028: Syntax error on ((SELECT ...) UNION (SELECT ...)) # CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (10); INSERT INTO t1 VALUES (20); INSERT INTO t1 VALUES (30); ((SELECT a FROM t1) UNION (SELECT a FROM t1)); a 10 20 30 (SELECT * FROM t1 UNION SELECT * FROM t1); a 10 20 30 ((SELECT a FROM t1) LIMIT 1); a 10 SELECT * FROM (SELECT 1 UNION (SELECT 2 UNION SELECT 3)) t1; 1 1 2 3 DROP TABLE t1; # # test of several levels of ORDER BY / LIMIT # create table t1 (a int, b int); insert into t1 (a,b) values (1, 100), (2, 200), (3,30), (4,4); select a,b from t1 order by 1 limit 3; a b 1 100 2 200 3 30 (select a,b from t1 order by 1 limit 3) order by 2 limit 2; a b 3 30 1 100 (select 10,1000 union select a,b from t1 order by 1 limit 3) order by 2 limit 2; 10 1000 3 30 1 100 ((select a,b from t1 order by 1 limit 3) order by 2 limit 2) order by 1 limit 1; a b 1 100 ((select a,b from t1 order by 1 limit 3) order by 2 limit 2) order by 1; a b 1 100 3 30 drop table t1; # # MDEV-16359: union with 3 selects in brackets # select 1 union select 1 union select 1; 1 1 (select 1 union select 1 union select 1); 1 1 ((select 1) union (select 1) union (select 1)); 1 1 # # MDEV-16357: union in brackets with tail # union with tail in brackets # CREATE TABLE t1 (a int); INSERT INTO t1 VALUES(1),(2),(3),(4); CREATE TABLE t2 (a int); INSERT INTO t2 VALUES (4),(5),(6),(7); (SELECT a FROM t1 UNION SELECT a FROM t2) LIMIT 1; a 1 (SELECT a FROM t1 UNION SELECT a FROM t2) ORDER BY a DESC; a 7 6 5 4 3 2 1 (SELECT a FROM t1 UNION SELECT a FROM t2 LIMIT 1); a 1 DROP TABLE t1,t2; # # MDEV-19324: ((SELECT ...) ORDER BY col ) LIMIT n # create table t1 (a int); insert into t1 values (10),(20),(30); select a from t1 order by a desc limit 1; a 30 explain extended select a from t1 order by a desc limit 1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using filesort Warnings: Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` order by `test`.`t1`.`a` desc limit 1 explain format=json select a from t1 order by a desc limit 1; EXPLAIN { "query_block": { "select_id": 1, "read_sorted_file": { "filesort": { "sort_key": "t1.a desc", "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100 } } } } } (select a from t1 order by a desc) limit 1; a 30 explain extended (select a from t1 order by a desc) limit 1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using filesort Warnings: Note 1003 (select `test`.`t1`.`a` AS `a` from `test`.`t1` order by `test`.`t1`.`a` desc limit 1) explain format=json (select a from t1 order by a desc) limit 1; EXPLAIN { "query_block": { "select_id": 1, "read_sorted_file": { "filesort": { "sort_key": "t1.a desc", "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100 } } } } } (select a from t1 where a=20 union select a from t1) order by a desc limit 1; a 30 explain extended (select a from t1 where a=20 union select a from t1) order by a desc limit 1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where 2 UNION t1 ALL NULL NULL NULL NULL 3 100.00 NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Using filesort Warnings: Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 20 union /* select#2 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` order by `a` desc limit 1 explain format=json (select a from t1 where a=20 union select a from t1) order by a desc limit 1; EXPLAIN { "query_block": { "union_result": { "table_name": "", "access_type": "ALL", "query_specifications": [ { "query_block": { "select_id": 1, "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100, "attached_condition": "t1.a = 20" } } }, { "query_block": { "select_id": 2, "operation": "UNION", "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100 } } } ] } } } ((select a from t1 where a=20 union select a from t1) order by a desc) limit 1; a 30 explain extended ((select a from t1 where a=20 union select a from t1) order by a desc) limit 1; id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where 2 UNION t1 ALL NULL NULL NULL NULL 3 100.00 NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Using filesort Warnings: Note 1003 (/* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 20) union /* select#2 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` order by `a` desc limit 1 explain format=json ((select a from t1 where a=20 union select a from t1) order by a desc) limit 1; EXPLAIN { "query_block": { "union_result": { "table_name": "", "access_type": "ALL", "query_specifications": [ { "query_block": { "select_id": 1, "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100, "attached_condition": "t1.a = 20" } } }, { "query_block": { "select_id": 2, "operation": "UNION", "table": { "table_name": "t1", "access_type": "ALL", "rows": 3, "filtered": 100 } } } ] } } } drop table t1; # # MDEV-19363: ((SELECT ...) ORDER BY col ) LIMIT n UNION ... # create table t1 (pk int); insert into t1 values (5),(4),(1),(2),(3); ((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); pk 1 2 5 explain extended ((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using filesort 2 UNION t1 ALL NULL NULL NULL NULL 5 100.00 Using where NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1003 (/* select#1 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` order by `test`.`t1`.`pk` limit 2) union (/* select#2 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` where `test`.`t1`.`pk` > 4) explain format=json ((select * from t1 order by pk) limit 2) union (select * from t1 where pk > 4); EXPLAIN { "query_block": { "union_result": { "table_name": "", "access_type": "ALL", "query_specifications": [ { "query_block": { "select_id": 1, "read_sorted_file": { "filesort": { "sort_key": "t1.pk", "table": { "table_name": "t1", "access_type": "ALL", "rows": 5, "filtered": 100 } } } } }, { "query_block": { "select_id": 2, "operation": "UNION", "table": { "table_name": "t1", "access_type": "ALL", "rows": 5, "filtered": 100, "attached_condition": "t1.pk > 4" } } } ] } } } drop table t1; # # MDEV-18689: parenthesis around table names and derived tables # select * from ( mysql.db ); Host Db User Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Grant_priv References_priv Index_priv Alter_priv Create_tmp_table_priv Lock_tables_priv Create_view_priv Show_view_priv Create_routine_priv Alter_routine_priv Execute_priv Event_priv Trigger_priv Delete_history_priv % test Y Y Y Y Y Y N Y Y Y Y Y Y Y Y N N Y Y Y % test\_% Y Y Y Y Y Y N Y Y Y Y Y Y Y Y N N Y Y Y create table t1 (a int); insert into t1 values (7), (2), (7); select * from (t1); a 7 2 7 select * from ((t1)); a 7 2 7 select * from (t1 t) where t.a > 5; a 7 7 select * from ((t1 t)) where t.a > 5; a 7 7 select * from ((select a, sum(a) from t1 group by a) t); a sum(a) 2 2 7 14 select * from (((select a, sum(a) from t1 group by a) t)); a sum(a) 2 2 7 14 update (t1 t) set t.a=t.a+1; select * from t1; a 8 3 8 drop table t1; # End of 10.4 tests