在MariaDB数据库中,UNION运算符用于合并两个或多个SELECT语句的结果集。它删除各种SELECT语句之间的重复行。
语法:
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
让无涯教程以MariaDB UNION运算符为例,该运算符从多个SELECT语句返回一个字段。 (两个表都具有相同的公共字段)。
无涯教程有两个表" Student"和" Students"。
Student:
Students:
SELECT student_name FROM Student UNION SELECT student_name FROM Students;
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/mariadb/mariadb-union-operator.html
来源:LearnFk无涯教程网
它显示了两个表中的student_name,其中删除了重复的名称。
将UNION运算符与ORDER BY子句一起使用可从两个表中检索多个列。
SELECT student_id, student_name FROM Students WHERE student_name = 'Komal' UNION SELECT student_id, salary FROM Student WHERE student_id > 4 ORDER BY 1;
输出:
链接:https://www.learnfk.comhttps://www.learnfk.com/mariadb/mariadb-union-operator.html
来源:LearnFk无涯教程网
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)