IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

二十四、MySQL UNION 操作符查询多张表

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=4905

MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中

多个 SELECT 语句会删除重复的数据

UNION 操作符语法

MySQL UNION 操作符的语法格式如下

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
参数 说明
expression1, expression2, … expression_n 要检索的列
tables 要检索的数据表
WHERE conditions 可选, 检索条件
DISTINCT 可选,删除结果集中重复的数据
默认情况下 UNION 操作符已经删除了重复数据
所以 DISTINCT 修饰符对结果没啥影响
ALL 可选,返回所有结果集,包含重复数据

范例数据

可以在 mysql> 命令行中运行以下语句填充范例数据

DROP TABLE IF EXISTS `tbl_language`;
DROP TABLE IF EXISTS `tbl_rank`;

CREATE TABLE IF NOT EXISTS `tbl_language`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `name` VARCHAR(64) NOT NULL,
   `url` VARCHAR(128) NOT NULL,
   `founded_at` DATE,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `tbl_rank`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `name` VARCHAR(64) NOT NULL,
   `month` VARCHAR(7) NOT NULL,
   `rank` TINYINT NOT NULL,
   `rate` VARCHAR(32) NOT NULL,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `tbl_language` VALUES
    (1,'Python','https://tech.souyunku.com','1991-2-20'),
    (2,'PHP','http://www.php.net','1994-1-1'),
    (3,'Ruby','https://www.ruby-lang.org/','1996-12-25');

INSERT INTO `tbl_rank` VALUES
    (1, 'Python','2018-04',4,'5.083%'),
    (2, 'PHP','2018-04',6,'4.218%'),
    (3, 'Ruby','2018-04',11,'2.018%'),
    (4, 'Java','2018-04',1,'15.777%'),
    (5, 'Python','2018-03',4,'5.869%'),
    (6, 'PHP','2018-03',7,'4.010%'),
    (7, 'Ruby','2018-03',12,'2.744%'),
    (8, 'Java','2018-03',1,'14.941'),
    (9, 'Python','2018-02',4,'5.168%'),
    (10, 'PHP','2018-02',7,'3.420%'),
    (11, 'Ruby','2018-02',10,'2.534%'),
    (12, 'Java','2018-02',1,'14.988%');

tbl_language 表中的数据如下

+----+--------+----------------------------+------------+
| id | name   | url                        | founded_at |
+----+--------+----------------------------+------------+
|  1 | Python | https://tech.souyunku.com        | 1991-02-20 |
|  2 | PHP    | http://www.php.net         | 1994-01-01 |
|  3 | Ruby   | https://www.ruby-lang.org/ | 1996-12-25 |
+----+--------+----------------------------+------------+

tbl_rank 表中的数据如下

+----+--------+---------+------+---------+
| id | name   | month   | rank | rate    |
+----+--------+---------+------+---------+
|  1 | Python | 2018-04 |    4 | 5.083%  |
|  2 | PHP    | 2018-04 |    6 | 4.218%  |
|  3 | Ruby   | 2018-04 |   11 | 2.018%  |
|  4 | Java   | 2018-04 |    1 | 15.777% |
|  5 | Python | 2018-03 |    4 | 5.869%  |
|  6 | PHP    | 2018-03 |    7 | 4.010%  |
|  7 | Ruby   | 2018-03 |   12 | 2.744%  |
|  8 | Java   | 2018-03 |    1 | 14.941  |
|  9 | Python | 2018-02 |    4 | 5.168%  |
| 10 | PHP    | 2018-02 |    7 | 3.420%  |
| 11 | Ruby   | 2018-02 |   10 | 2.534%  |
| 12 | Java   | 2018-02 |    1 | 14.988% |
+----+--------+---------+------+---------+

SQL UNION

下面的 SQL 语句从 tbl_languagetbl_rank 表中选取所有 不同的 name(只有不同的值)

SELECT name FROM tbl_language UNION SELECT name FROM tbl_rank  ORDER BY name;

执行以上 SQL 输出结果如下

MariaDB [souyunku]> SELECT name FROM tbl_language UNION SELECT name FROM tbl_rank  ORDER BY name;
+--------+
| name   |
+--------+
| Java   |
| PHP    |
| Python |
| Ruby   |
+--------+
4 rows in set (0.00 sec)

UNION 不能用于列出两个表中所有的 name ,UNION 只会选取不同的值

如果要选取重复的值,可以使用 UNION ALL

SQL UNION ALL

下面的 SQL 语句从 tbl_languagetbl_rank 表中选取所有的 name(可能有重复的值)

SELECT name FROM tbl_language UNION ALL SELECT name FROM tbl_rank  ORDER BY name;

执行以上 SQL 输出结果如下

MariaDB [souyunku]> SELECT name FROM tbl_language UNION ALL SELECT name FROM tbl_rank  ORDER BY name;
+--------+
| name   |
+--------+
| Java   |
| Java   |
| Java   |
| PHP    |
| PHP    |
| PHP    |
| PHP    |
| Python |
| Python |
| Python |
| Python |
| Ruby   |
| Ruby   |
| Ruby   |
| Ruby   |
+--------+
15 rows in set (0.00 sec)

带有 WHERE 的 SQL UNION ALL

下面的 SQL 语句从 tbl_languagetbl_rank 表中选取所有的 Python(可能有重复的值)

SELECT name FROM tbl_language WHERE name="Python" UNION ALL SELECT name FROM tbl_rank WHERE name="Python"  ORDER BY name;

执行以上 SQL 输出结果如下

MariaDB [souyunku]> SELECT name FROM tbl_language WHERE name="Python" UNION ALL SELECT name FROM tbl_rank WHERE name="Python"  ORDER BY name;
+--------+
| name   |
+--------+
| Python |
| Python |
| Python |
| Python |
+--------+
4 rows in set (0.00 sec)

干货推荐

本站推荐:精选优质专栏

附录:MySQL 教程:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(88) 打赏



未经允许不得转载:搜云库技术团队 » 二十四、MySQL UNION 操作符查询多张表

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367