tags:

views:

71

answers:

3
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>

Isn't there supposed to be two records with "0" as its value?

+5  A: 

Union All would give two statements. Union attempts to combine the entries, or rather, remove the duplicate rows.

CodeByMoonlight
+1  A: 

UNION removes duplicates. Try UNION ALL instead.

David Andres
+3  A: 

Hi,

Quoting from the MySQL manual :

The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.

Which means that, with only UNION, if two lines have the same value, one of them will not be returned :

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

But, if using UNION ALL, duplicates are not removed :

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