无涯教程-MariaDB - Union All语句

104 阅读1分钟

MariaDB UNION ALL运算符与UNION运算符相同,但不会删除重复的记录。它返回查询中的所有行,并且不会删除各个SELECT语句之间的重复行。

语法:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]; 

Note: 在MariaDB UNION ALL运算符中,每个SELECT语句在具有相似数据类型的结果集中必须具有相同数量的字段。

UNION ALL返回单个字段

SELECT student_name
FROM Student
UNION ALL 
SELECT student_name
FROM Students; 

输出:

MariaDB Union all operator 1

您会看到它不会删除重复的记录。

ORDER BY 运算符

将UNION ALL运算符与ORDER BY子句一起使用可从两个表中检索多个列。

SELECT student_id, student_name
FROM Students
WHERE student_name = Komal
UNION ALL 
SELECT student_id, salary
FROM Student
WHERE student_id > 4
ORDER BY 1;

输出:

MariaDB Union all operator 2

参考链接

www.learnfk.com/mariadb/mar…