按条件计数

时间:2012-03-19 17:58:09

标签: mysql

我有一张表格,其中包含不同国家/地区的不同值,例如:

id| country  |
===================
1 | Argelia  | 
2 | USA      |
1 | China    |
1 | Italy    |
1 | Italy    |
1 | USA      |
4 | USA      |
1 | Argelia  |

我只对一个国家/地区的计数和总数感兴趣,但是我无法提出单个查询来执行此操作。 id 1的此查询结果为:

id| value_in_Italy | total
==========================
1 | 2              | 6

如您所见,我获得了意大利的价值和总价值。对于类似的表,什么样的查询会产生如上所述的行?

4 个答案:

答案 0 :(得分:2)

我能想到的最简单的MySQL查询是:

select id, sum(country = 'Italy') values_in_Italy, count(*) Total from t
where id = 1

MySQL不会强迫您group by id,因为它会不确定地占用一个ID,但where子句强制该列只有一个ID

答案 1 :(得分:1)

SELECT A.ID,B.value_in_Italy,(SELECT COUNT(DISTINCT Country) AS total FROM YOURTABLE) AS          total
FROM YOURTABLE A,
(SELECT Country,COUNT(*) AS value_in_Italy
FROM YOURTABLE
WHERE COUNTRY='Italy'
GROUP BY Country) B
WHERE A.Country=B.Country

答案 2 :(得分:1)

以下是加载的示例数据

mysql> drop database if exists luqita;
Query OK, 1 row affected (0.03 sec)

mysql> create database luqita;
Query OK, 1 row affected (0.01 sec)

mysql> use luqita
Database changed
mysql> create table countrydata
    -> (
    ->     id int not null,
    ->     country varchar(32),
    ->     value int not null
    -> );
Query OK, 0 rows affected (0.10 sec)

mysql> insert into countrydata (id,country) values
    -> (1,'Argelia'  ),
    -> (2,'USA'      ),
    -> (1,'China'    ),
    -> (1,'Italy'    ),
    -> (1,'Italy'    ),
    -> (1,'USA'      ),
    -> (4,'USA'      ),
    -> (1,'Argelia'  );
Query OK, 8 rows affected, 1 warning (0.06 sec)
Records: 8  Duplicates: 0  Warnings: 1

mysql> select * from countrydata;
+----+---------+-------+
| id | country | value |
+----+---------+-------+
|  1 | Argelia |     0 |
|  2 | USA     |     0 |
|  1 | China   |     0 |
|  1 | Italy   |     0 |
|  1 | Italy   |     0 |
|  1 | USA     |     0 |
|  4 | USA     |     0 |
|  1 | Argelia |     0 |
+----+---------+-------+
8 rows in set (0.00 sec)

mysql>

只计算意大利

select id,SUM(IF(country='Italy',1,0)) italy_count,COUNT(IF(id=1,1,0)) id_count
from countrydata WHERE id=1;

以下是针对样本数据的查询

mysql> select id,SUM(IF(country='Italy',1,0)) italy_count,COUNT(IF(id=1,1,0)) id_count
 ->    from countrydata WHERE id=1;
+----+-------------+----------+
| id | italy_count | id_count |
+----+-------------+----------+
|  1 |           2 |        6 |
+----+-------------+----------+
1 row in set (0.00 sec)

mysql>

以下是全包查询

select B.*,
    SUM(IF(A.country=B.country,1,0)) country_count,
    SUM(IF(B.id=A.id,1,0)) id_count
from
    countrydata A,
    (select distinct id,country from countrydata) B
group by B.id,B.country;

以下是执行的全包查询

mysql> select B.*,
    ->     SUM(IF(A.country=B.country,1,0)) country_count,
    ->     SUM(IF(B.id=A.id,1,0)) id_count
    -> from
    ->     countrydata A,
    ->     (select distinct id,country from countrydata) B
    -> group by B.id,B.country;
+----+---------+---------------+----------+
| id | country | country_count | id_count |
+----+---------+---------------+----------+
|  1 | Argelia |             2 |        6 |
|  1 | China   |             1 |        6 |
|  1 | Italy   |             2 |        6 |
|  1 | USA     |             3 |        6 |
|  2 | USA     |             3 |        1 |
|  4 | USA     |             3 |        1 |
+----+---------+---------------+----------+
6 rows in set (0.00 sec)

mysql>

答案 3 :(得分:0)

试试这个:

select 1 id, sum(country = 'Italy') values_in_Italy, count(*) Total from t