mysql 从 where 子句中获取总值和总数
mysql get total values and total number from where clause
我正在尝试编写一个 mysql 查询,它将 return 列中值的总数以及基于同一列中 where 子句的值总数。
我有一个 table 这样的:
+------------------------+-------+
| color | code |
+------------------------+-------+
| red | 200 |
| red | 202 |
| blue | 105 |
| yellow | 136 |
| green | 561 |
| red | 198 |
| blue | 414 |
| green | 11 |
| yellow | 600 |
| green | 155 |
| red | 865 |
| blue | 601 |
| green | 311 |
+------------------------+-------+
如果我运行这个查询:
select
color,
count(*) as count
from colors
where code > 0 &&
code <= 500
group by color
order by count(*) desc;
我得到了这个很棒的结果,因为它几乎就是我想要的:
+------------------------+-------+
| color | count |
+------------------------+-------+
| red | 3 |
| green | 3 |
| blue | 2 |
| yellow | 1 |
+------------------------+-------+
我还需要 returned 是列中值的总数,所以结果 table 看起来像这样。
+------------------------+--------------+-------+
| color | total | count |
+------------------------+--------------+-------+
| red | 4 | 3 |
| green | 4 | 3 |
| blue | 3 | 2 |
| yellow | 2 | 1 |
+------------------------+--------------+-------+
所以total就是color列中每个值的个数,count就是匹配where子句的总数。
谢谢:)
这是 SQLFiddle 的 link。
您需要使用条件聚合来处理计数并让引擎处理总数。
SELECT color
, count(*) as Total
, sum(case when code > 0 and code <= 500 then 1 else 0 end) as cnt
FROM colors
GROUP BY color
ORDER BY cnt desc;
您可以 JOIN
您的查询与另一个查询,例如:
select color, count(*) as count , a.total
from colors JOIN (
SELECT color, count(*) as `total` FROM colors GROUP BY color
) a ON colors.code = a.color
where code > 0 && code <= 500
group by color
order by count(*) desc;
您的案例使用 case when 语句来仅计算满足您条件的项目。像这样:
Select
color
, count(*) as total
, SUM(CASE WHEN code > 0 && code <= 500 THEN 1 ELSE 0 END ) as Count
group by color order by count(*) desc;
您可以使用连接查询和 table 别名
select t1.color,t1.total,t2.count
FROM
(select color, count(*) as total from colors group by color) t1,
(select color, count(*) as `count` from colors where `code` > 0 && `code` <= 500 group by color) t2
WHERE
t1.color=t2.color order by `count` desc;
这是 SQLFiddle 的 link。
您可以使用条件聚合:
select color,
count(*) as Total,
sum(code > 0 and code <= 500) as count_0_to_500
from colors
group by color
order by count_0_to_500 desc
它利用了 MySQL 中 true 的计算结果为 1 而 false 的计算结果为 0 的事实。
这基本上就是 xQbert 回答的内容,只是没有 case 表达式。
我正在尝试编写一个 mysql 查询,它将 return 列中值的总数以及基于同一列中 where 子句的值总数。
我有一个 table 这样的:
+------------------------+-------+
| color | code |
+------------------------+-------+
| red | 200 |
| red | 202 |
| blue | 105 |
| yellow | 136 |
| green | 561 |
| red | 198 |
| blue | 414 |
| green | 11 |
| yellow | 600 |
| green | 155 |
| red | 865 |
| blue | 601 |
| green | 311 |
+------------------------+-------+
如果我运行这个查询:
select
color,
count(*) as count
from colors
where code > 0 &&
code <= 500
group by color
order by count(*) desc;
我得到了这个很棒的结果,因为它几乎就是我想要的:
+------------------------+-------+
| color | count |
+------------------------+-------+
| red | 3 |
| green | 3 |
| blue | 2 |
| yellow | 1 |
+------------------------+-------+
我还需要 returned 是列中值的总数,所以结果 table 看起来像这样。
+------------------------+--------------+-------+
| color | total | count |
+------------------------+--------------+-------+
| red | 4 | 3 |
| green | 4 | 3 |
| blue | 3 | 2 |
| yellow | 2 | 1 |
+------------------------+--------------+-------+
所以total就是color列中每个值的个数,count就是匹配where子句的总数。
谢谢:)
这是 SQLFiddle 的 link。
您需要使用条件聚合来处理计数并让引擎处理总数。
SELECT color
, count(*) as Total
, sum(case when code > 0 and code <= 500 then 1 else 0 end) as cnt
FROM colors
GROUP BY color
ORDER BY cnt desc;
您可以 JOIN
您的查询与另一个查询,例如:
select color, count(*) as count , a.total
from colors JOIN (
SELECT color, count(*) as `total` FROM colors GROUP BY color
) a ON colors.code = a.color
where code > 0 && code <= 500
group by color
order by count(*) desc;
您的案例使用 case when 语句来仅计算满足您条件的项目。像这样:
Select
color
, count(*) as total
, SUM(CASE WHEN code > 0 && code <= 500 THEN 1 ELSE 0 END ) as Count
group by color order by count(*) desc;
您可以使用连接查询和 table 别名
select t1.color,t1.total,t2.count
FROM
(select color, count(*) as total from colors group by color) t1,
(select color, count(*) as `count` from colors where `code` > 0 && `code` <= 500 group by color) t2
WHERE
t1.color=t2.color order by `count` desc;
这是 SQLFiddle 的 link。
您可以使用条件聚合:
select color,
count(*) as Total,
sum(code > 0 and code <= 500) as count_0_to_500
from colors
group by color
order by count_0_to_500 desc
它利用了 MySQL 中 true 的计算结果为 1 而 false 的计算结果为 0 的事实。
这基本上就是 xQbert 回答的内容,只是没有 case 表达式。