[英]Mysql subquery for group and count records by specific field
I have this problem: 我有这个问题:
I have a car
table, each car
record has a state
field, state
field value can be: 我有一个
car
表,每个car
记录都有一个state
字段, state
字段值可以是:
1 = Enable or 2 = Disable 1 = 启用或2 = 禁用
For example, in this case I need to show all cars grouped by color
field and counted by color
, this is not problem really :) 例如,在这种情况下,我需要显示的分组的所有汽车
color
领域,并通过计算 color
,这不是真正的问题:)
Here the SQL statement : 这里是SQL语句:
SELECT
`id`,
`model_family`,
`color`,
COUNT(`color`) AS 'quantity',
`state`
FROM `auto`
WHERE `model_family` = 'Sedan'
GROUP BY `color`
+-----+--------------+------------+----------+---+
| id | model_family | color | quantity | state |
+-----+--------------+--------+----------+-------+
| 77 | Sedan | Red | 2 | 2 |
| 42 | Sedan | Blue | 3 | 2 |
| 97 | Sedan | Green | 5 | 1 |
+-----+--------------+--------+----------+-------+
Results show two Disabled records and one Enabled record. 结果显示两个已禁用记录和一个已启用记录。
Well, the questions is : 那么问题是:
How can I can show the cars disabled and enabled but For example, if for first grouped record if state
is disabled ( state
= 2) then quantity
field will appear as "0" (because it not exists cars enabled) else quantity
= n 我如何能显示汽车禁用和启用,但举例来说,如果第一分组记录,如果
state
是禁用( state
= 2),那么quantity
字段将显示为“0”(因为它不存在汽车启用)其他quantity
= N
Something like this: 像这样的东西:
+-----+--------------+------------+----------+-------+
| id | model_family | color | quantity | state |
+-----+--------------+------------+----------+-------+
| 77 | Sedan | Red | 0 | 2 |
| 42 | Sedan | Blue | 0 | 2 |
| 97 | Sedan | Green | 5 | 1 |
+-----+--------------+------------+----------+-------+
Regads ! 注册!
SELECT
`id`,
`model_family`,
`color`,
SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled,
SUM(CASE WHEN state = 2 THEN 1 ELSE 0 END) as disabled,
`state`
FROM `auto`
WHERE `model_family` = 'Sedan'
GROUP BY `color`
You have to use case statements to group the items together. 您必须使用案例语句将项目组合在一起。 When it finds that the state is 1 you will need to sum together all of those records which is why I am doing the Then 1 Else 0.
当它发现状态为1时,你需要将所有这些记录加在一起,这就是为什么我要做的然后1 Else 0。
This was a quick example of how to do it. 这是如何做到的一个简单示例。 I haven't tested to make sure it works, but it should.
我没有测试过以确保它有效,但它应该。
If you want all disabled groups to be replaced with 0, you can just put a single case statement in your select clause. 如果要将所有禁用的组替换为0,则只需在select子句中放置一个case语句即可。 Something like this:
像这样的东西:
SELECT id, model_family, color, (CASE WHEN state = 1 THEN COUNT(*) ELSE 0 END) AS quantity, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;
It tested fine with your data on SQL Fiddle . 它在SQL Fiddle上的数据测试得很好。
EDIT Note, since you're grouping by color, you can just use COUNT(*) instead of COUNT(color) because they are going to count the same thing. 编辑注意,由于您按颜色进行分组,因此您可以使用COUNT(*)而不是COUNT(颜色),因为它们将计算相同的内容。
EDIT 2 Also important to note that if a color has some enabled vehicles and some disabled vehicles, it still returns 0 because there is at least one disabled. 编辑2同样重要的是要注意,如果颜色有一些启用的车辆和一些残疾车辆,它仍然返回0,因为至少有一个禁用。 If you want a count of the enabled ones, you can do something like this:
如果您想要计算已启用的数量,您可以执行以下操作:
SELECT id, model_family, color, SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) AS quantityEnabled, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.