Mysql 使用 if 语句聚合数据
Mysql aggregating data with if statement
我试图解决的问题的简化示例:
编辑 - 使用 PK
sqlfiddle: http://sqlfiddle.com/#!2/7be5d/1
---
我有两个table:
People
--------
| Name |
--------
| Mike |
| Jim |
| Fred |
--------
HasBanana
------------------------
| Name | Has_a_banana |
------------------------
| Mike | N |
| Mike | N |
| Mike | N |
| Mike | N |
| Mike | Y |
| Mike | N |
| Jim | N |
| Jim | N |
| Jim | Y |
| Jim | N |
| Jim | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
------------------------
还有一个查询:
select * from People
left join HasBanana on HasBanana.name = People.name;
这会生成与上面的 HasBanana table 完全相同的 table。我想要生成的是 table,按 People.name 分组,如果此人曾被标记为有香蕉,则显示 Y。
所以像这样:
HadBanana
------------------------
| Name | Had_a_banana |
------------------------
| Mike | Y |
| Jim | Y |
| Fred | N |
------------------------
我知道我可以使用以下内容按名称分组
group by people.name;
但是如果 has_a_banana 曾经是 Y,则在执行每个名称的逻辑时遇到问题,然后将 had_a_banana 设置为 Y。
select
p.name
, case when x.name is null then 'N' else 'Y' end as had_a_banana
from people p
left outer join
(
select
name
from bananas
where has_a_banana = 'Y'
group by name
) x
on x.name = p.name
group by p.name, x.name
我已经使用您的 SQLFiddle link 进行了检查,尽管您的 Fiddle 数据与您的问题不符,所以我得到:
Fred N
Jim Y
Mike N
(这似乎与您的 Fiddle 数据匹配)。
</p>
<pre><code>select people.id, people.name, case a.has_a_banana when 'Y' then 'Y' else 'N' end as has_a_banana from people
left join
(select people.id, people.name, bananas.has_a_banana from people
left join bananas on bananas.name = people.name
where has_a_banana = 'Y'
group by people.name) as a
on a.name = people.name
应该让你得到你想要的。
替代方案(有点丑陋的解决方案):
select people.name, max(has_a_banana) from people
left join bananas on bananas.name = people.name
group by people.name;
w=14=WILL.y.:w=12=w
w=10=sh
w=14=shw=11=shw=12=sh
我试图解决的问题的简化示例:
编辑 - 使用 PK
sqlfiddle: http://sqlfiddle.com/#!2/7be5d/1
---
我有两个table:
People
--------
| Name |
--------
| Mike |
| Jim |
| Fred |
--------
HasBanana
------------------------
| Name | Has_a_banana |
------------------------
| Mike | N |
| Mike | N |
| Mike | N |
| Mike | N |
| Mike | Y |
| Mike | N |
| Jim | N |
| Jim | N |
| Jim | Y |
| Jim | N |
| Jim | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
| Fred | N |
------------------------
还有一个查询:
select * from People
left join HasBanana on HasBanana.name = People.name;
这会生成与上面的 HasBanana table 完全相同的 table。我想要生成的是 table,按 People.name 分组,如果此人曾被标记为有香蕉,则显示 Y。
所以像这样:
HadBanana
------------------------
| Name | Had_a_banana |
------------------------
| Mike | Y |
| Jim | Y |
| Fred | N |
------------------------
我知道我可以使用以下内容按名称分组
group by people.name;
但是如果 has_a_banana 曾经是 Y,则在执行每个名称的逻辑时遇到问题,然后将 had_a_banana 设置为 Y。
select
p.name
, case when x.name is null then 'N' else 'Y' end as had_a_banana
from people p
left outer join
(
select
name
from bananas
where has_a_banana = 'Y'
group by name
) x
on x.name = p.name
group by p.name, x.name
我已经使用您的 SQLFiddle link 进行了检查,尽管您的 Fiddle 数据与您的问题不符,所以我得到:
Fred N
Jim Y
Mike N
(这似乎与您的 Fiddle 数据匹配)。
</p>
<pre><code>select people.id, people.name, case a.has_a_banana when 'Y' then 'Y' else 'N' end as has_a_banana from people
left join
(select people.id, people.name, bananas.has_a_banana from people
left join bananas on bananas.name = people.name
where has_a_banana = 'Y'
group by people.name) as a
on a.name = people.name
应该让你得到你想要的。
替代方案(有点丑陋的解决方案):
select people.name, max(has_a_banana) from people
left join bananas on bananas.name = people.name
group by people.name;