跳到主要内容

合并查询

2024年06月12日
柏拉文
越努力,越幸运

一、认识


unionunioin all 操作符用于合并两个或多个 select 语句的结果集。请注意,unionunioin all 内部的每个 select 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 select 语句中的列的顺序必须相同。

  • union: 操作符选取不同的值, 去除重复的值

  • union all: 允许重复的值

二、语法


select column_name(s) from table1
uniion
select column_name(s) from table2;

三、用法


3.1 基于 A 表和 B 表选取所有不同的 c

select c from A union select c from B order by c;

// 带有 where

select c,d from A where c='xx' union select c,e from B where c='xx' order by c;