sql 联合语法_SQL联合和联合所有运算符用语法示例解释

sql 联合语法

For this guide we’ll discuss the UNION Operator section of the SQL statement.

对于本指南,我们将讨论SQL语句的UNION运算符部分。

The UNION Operator is used to combine the results of multiple select statements into one result set.

UNION运算符用于将多个选择语句的结果合并为一个结果集。

The SQL statements must have the same number of columns in their Select Statement.

SQL语句的Select语句中的列数必须相同。

基本范例 (Basic Example)

SQL Statement

SQL语句

SELECT 'aaaaa'
UNION
SELECT 'bbbbbbbbb';

Output

输出量

+-----------+
| aaaaa     |
+-----------+
| aaaaa     |
| bbbbbbbbb |
+-----------+
2 rows in set (0.00 sec)

使用学生表的示例 (Example using the student tables)

SQL Statement

SQL语句

SELECT StudentID, FullName FROM student WHERE studentID BETWEEN 1 AND 5
UNION
SELECT studentID, studentEmailAddr FROM `student-contact-info` WHERE studentID BETWEEN 7 AND 8;

Output

输出量

+-----------+--------------------------------+
| StudentID | FullName                       |
+-----------+--------------------------------+
|         1 | Monique Davis                  |
|         2 | Teri Gutierrez                 |
|         3 | Spencer Pautier                |
|         4 | Louis Ramsey                   |
|         5 | Alvin Greene                   |
|         7 | Maximo.Smith@freeCodeCamp.org  |
|         8 | Michael.Roach@freeCodeCamp.ort |
+-----------+--------------------------------+
7 rows in set (0.00 sec)

SQL UNION ALL运算符 (SQL UNION ALL Operator)

The UNION ALL operator is an extension to UNION operator where it should result you a A+B of rows in the ouptput assuming A and B is your input, in simple terms UNION ALL doesn’t deduplicate.

UNION ALL运算符是UNION运算符的扩展,在这种情况下,假设A和B是您的输入,它应在输出中导致A + B的行,简单来说,UNION ALL不会进行重复数据删除。

基本语法 (Basic Syntax)

SQL Statement

SQL语句

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

As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。

I hope this at least gives you enough to get started.

我希望这至少能给您足够的入门。

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

翻译自: https://www.freecodecamp.org/news/sql-union-and-union-all-operators-explained-with-syntax-examples/

sql 联合语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值