I have two tables with the same structure:
Table -1:
+----------------------+--------------+-----+
| Field | Type | Key |
+----------------------+--------------+-----+
| id | int(5) | PRI |
| country | varchar(500) | |
+----------------------+--------------+-----|
Table -2:
+----------------------+--------------+-----+
| Field | Type | Key |
+----------------------+--------------+-----+
| id | int(5) | PRI |
| country | varchar(500) | |
+----------------------+--------------+-----|
The data in the table will be as following: Table -1:
+----+---------+
|id | country |
+----+---------+
| 1 | A |
| 2 | B |
| 3 | A |
| 4 | A |
| 5 | B |
+----+---------+
Table -2:
+----+---------+
|id | country |
+----+---------+
| 1 | A |
| 2 | B |
| 3 | B |
| 4 | B |
| 5 | B |
+----+---------+
When i use the following query statement :
SELECT country, COUNT(*) AS result FROM table-1 GROUP BY country UNION SELECT country, COUNT(*) AS result FROM table-2 GROUP BY country;
I get the result as following:
+--------+---------+
|country | result |
+--------+---------+
| A | 3 |
| B | 2 |
| A | 1 |
| B | 4 |
+--------+---------+
Which shows the count of table-1 and table-2 seperately. But i want the count to be combined as following:
+--------+---------+
|country | result |
+--------+---------+
| A | 4 |
| B | 6 |
+--------+---------+
Thanks in advance.
select t.country,count(t.country)
from
(
select * from table1
union all
select * from table2
) t group by t.country
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.