UNION运算符可以将两个或两个以上上SELECT语句的查询结果集合合并成一个结果集合显示,即执行联合查询。UNION的语法格式为
SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE
联合查询时,查询结果的列标题为第一个查询语句的列标题。因此,要定义列标题必须在第一个查询语句中定义。要对联合查询结果排序时,也必须使用第一查询语句中的列名、列标题或者列序号。
在使用UNION 运算符时,应保证每个联合查询语句的选择列表中有相同数量的表达式,并且每个查询选择表达式应具有相同的数据类型,或是可以自动将它们转换为相同的数据类型。在自动转换时,对于数值类型,系统将低精度的数据类型转换为高精度的数据类型。
实例
mysql教程>
mysql>
mysql> CREATE TABLE Employee(
-> id int,
-> first_name VARCHAR(15),
-> last_name VARCHAR(15),
-> start_date DATE,
-> end_date DATE,
-> salary FLOAT(8,2),
-> city VARCHAR(10),
-> description VARCHAR(15)
-> );
Query OK, 0 rows affected (0.02 sec)mysql>
mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values (1,'Jason', 'Martin', '19960725', '20060725', 1234.56, 'Toronto', 'Programmer');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(2,'Alison', 'Mathews', '19760321', '19860221', 6661.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(3,'James', 'Smith', '19781212', '19900315', 6544.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(4,'Celia', 'Rice', '19821024', '19990421', 2344.78, 'Vancouver','Manager');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(5,'Robert', 'Black', '19840115', '19980808', 2334.78, 'Vancouver','Tester');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(6,'Linda', 'Green', '19870730', '19960104', 4322.78,'New York', 'Tester');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(7,'David', 'Larry', '19901231', '19980212', 7897.78,'New York', 'Manager');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
-> values(8,'James', 'Cat', '19960917', '20020415', 1232.78,'Vancouver', 'Tester');
Query OK, 1 row affected (0.00 sec)mysql>
mysql> select * from Employee;
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| id | first_name | last_name | start_date | end_date | salary | city | description |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| 1 | Jason | Martin | 1996-07-25 | 2006-07-25 | 1234.56 | Toronto | Programmer |
| 2 | Alison | Mathews | 1976-03-21 | 1986-02-21 | 6661.78 | Vancouver | Tester |
| 3 | James | Smith | 1978-12-12 | 1990-03-15 | 6544.78 | Vancouver | Tester |
| 4 | Celia | Rice | 1982-10-24 | 1999-04-21 | 2344.78 | Vancouver | Manager |
| 5 | Robert | Black | 1984-01-15 | 1998-08-08 | 2334.78 | Vancouver | Tester |
| 6 | Linda | Green | 1987-07-30 | 1996-01-04 | 4322.78 | New York | Tester |
| 7 | David | Larry | 1990-12-31 | 1998-02-12 | 7897.78 | New York | Manager |
| 8 | James | Cat | 1996-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
8 rows in set (0.00 sec)mysql>
mysql>
mysql>
mysql>
mysql>
mysql> SELECT first_name
-> FROM employee
-> WHERE (first_name LIKE 'J%')
-> UNION
-> SELECT first_name
-> FROM employee
-> WHERE (first_name LIKE 'R%');
+------------+
| first_name |
+------------+
| Jason |
| James |
| Robert |
+------------+
3 rows in set (0.02 sec)mysql>
mysql>
Which is identical as using two where conditions.
mysql>
mysql> SELECT first_name
-> FROM employee
-> WHERE ((first_name LIKE 'J%') || (first_name LIKE 'R%'));
+------------+
| first_name |
+------------+
| Jason |
| James |
| Robert |
| James |
+------------+
4 rows in set (0.00 sec)相关文章
精彩推荐
下载我的博物馆故事 官方安卓版v1.61.2
模拟经营 我的博物馆故事 官方安卓版v1.61.2我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
下载专业模拟飞行10 手机版v12.2.4
模拟经营 专业模拟飞行10 手机版v12.2.4专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
下载动物起义战斗模拟器二琳同款 最新版v4.1.1
模拟经营 动物起义战斗模拟器二琳同款 最新版v4.1.1动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
下载迷你世界七周年 安卓手机版v1.43.0
模拟经营 迷你世界七周年 安卓手机版v1.43.0迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
下载劫后公司无限资源版 v1.0.5.1
模拟经营 劫后公司无限资源版 v1.0.5.1劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内