質問

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"を持つ2つのレコードがあるはずではありません。その値として?

役に立ちましたか?

解決

Union Allは2つのステートメントを提供します。 Unionはエントリを結合しようとするか、重複する行を削除します。

他のヒント

MySQLマニュアルから引用:

  

UNIONのデフォルトの動作では、重複する行が   結果。オプションのDISTINCT   キーワードは、   デフォルトも指定するため   重複行の削除。とともに   オプションのALLキーワード、重複行   除去は行われず、結果   すべての一致する行をすべて含む   SELECTステートメント。

これは、 UNION のみで、2つの行が同じ値を持っている場合、そのうちの1つが返されないことを意味します:

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