mysql> select count(id) total from applicants;
+-------+
| total |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

mysql> select count(id) total from jobs;
+-------+
| total |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

mysql> select count(id) total from applicants union select count(id) total from
jobs;
+-------+
| total |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

mysql>

不应该有两个带有“0”的记录。它的价值?

有帮助吗?

解决方案

Union All会发表两个声明。 Union尝试组合条目,或者更确切地说,删除重复的行。

其他提示

MySQL手册中引用:

  

UNION的默认行为是从中删除重复的行   结果。可选的DISTINCT   关键字除了以外没有任何影响   默认,因为它也指定   重复行删除。随着   可选的ALL关键字,重复行   不会发生移除和结果   包括所有匹配的行   SELECT语句。

这意味着,只有 UNION ,如果两行具有相同的值,则不会返回其中一行:

mysql> select 1 union select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

但是,如果使用 UNION ALL ,则不会删除重复项:

mysql> select 1 union all select 1;
+---+
| 1 |
+---+
| 1 |
| 1 |
+---+
2 rows in set (0.00 sec)

UNION删除重复项。请改用UNION ALL。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top