通过MySQL SELECT控制组中的数据

时间:2022-07-22 04:23:31

I have a table like this:

我有这样一张桌子:

+------------------------+
| Id IdGroup Name Status |
+------------------------+
| 0  P.1     Foo  Ok     |
| 1  P.1     Foo No     |
| 2  P.2     Foo3 No     |
+------------------------+

I want to make a select with group by like this:

我想用这样的组进行选择:

SELECT IdGroup, Name, Status FROM footable GROUP BY IdGroup

and if all "Status" values of grouped rows are ok. The result that I want is:

如果分组行的所有“状态”值都可以。我想要的结果是:

+--------------------+
| P.1     Foo  Maybe |
+--------------------+
| P.2     Foo3 No    |
+--------------------+

It's possible?

这是可能的?

1 个解决方案

#1


0  

Compare the count of Ok rows with the total count of rows in each group.

将Ok行的数量与每个组中的总行数进行比较。

SELECT IdGroup, Name, IF(COUNT(*) = SUM(Status = 'Ok'), 'Maybe', 'No') As Status
FROM footable
GROUP BY IdGroup

Note that this will select a random Name from each group. The question didn't indicate how the name should be selected.

请注意,这将从每个组中选择一个随机名称。问题没有说明应该如何选择名称。

#1


0  

Compare the count of Ok rows with the total count of rows in each group.

将Ok行的数量与每个组中的总行数进行比较。

SELECT IdGroup, Name, IF(COUNT(*) = SUM(Status = 'Ok'), 'Maybe', 'No') As Status
FROM footable
GROUP BY IdGroup

Note that this will select a random Name from each group. The question didn't indicate how the name should be selected.

请注意,这将从每个组中选择一个随机名称。问题没有说明应该如何选择名称。