过滤加入 table
Filtering a joined table
我有两张桌子
Events
id - published
--------------
1 - T
2 - T
3 - F
Attributes
id - event_id - type
---------------------
1 - 1 - large
2 - 1 - large
3 - 1 - medium
4 - 2 - small
5 - 2 - small
6 - 3 - large
7 - 3 - large
我想做的是在 Events.id 和 Attributes.event_id 上加入这两个表并像这样过滤:
For all events where Events.published = True and
where Attributes.type != large or medium
get the percentage of those events which fit the filters / all events
output of sql query
event_type - totals - event_ids - percentage
-----------------------------------------
"events that fit filter" - 2 - [1,2] - 66.66% -- (2/3)
"events that dont fit filter" - 1 - [3] - 33.33% -- (1/3)
For all events where Events.published = True and
where Attributes.type != large or medium
get the percentage of those events which fit the filters / all events
select avg( e.published and a.type not in ('large', 'medium') )
from events e join
attributes a
on e.id = a.event_id;
这使用了这样一个事实,即 MySQL 将布尔值视为 1
表示真,0
表示假。
编辑:
哦,你想要这个每个事件。为此,使用两个聚合级别:
select avg(flag)
from (select e.id,
max( e.published and a.type not in ('large', 'medium') ) as flag
from events e join
attributes a
on e.id = a.event_id
group by e.id
) e;
select
case when e.published and a.type not in('large','medium') then 'Met condition' else 'Not met condition' end conditions
, count(distinct e.id) totals
, group_concat(distinct e.id) event_ids
, count(distinct e.id) * 100.0 / x.total_cnt percentage
from events e
join Attributes a on a.event_id = e.id
cross join (select count(*) as total_cnt from events) x
group by case when e.published and a.type not in('large','medium') then 'Met condition' else 'Not met condition' end, total_cnt
db<>fiddle here
我有两张桌子
Events
id - published
--------------
1 - T
2 - T
3 - F
Attributes
id - event_id - type
---------------------
1 - 1 - large
2 - 1 - large
3 - 1 - medium
4 - 2 - small
5 - 2 - small
6 - 3 - large
7 - 3 - large
我想做的是在 Events.id 和 Attributes.event_id 上加入这两个表并像这样过滤:
For all events where Events.published = True and
where Attributes.type != large or medium
get the percentage of those events which fit the filters / all events
output of sql query
event_type - totals - event_ids - percentage
-----------------------------------------
"events that fit filter" - 2 - [1,2] - 66.66% -- (2/3)
"events that dont fit filter" - 1 - [3] - 33.33% -- (1/3)
For all events where Events.published = True and where Attributes.type != large or medium get the percentage of those events which fit the filters / all events
select avg( e.published and a.type not in ('large', 'medium') )
from events e join
attributes a
on e.id = a.event_id;
这使用了这样一个事实,即 MySQL 将布尔值视为 1
表示真,0
表示假。
编辑:
哦,你想要这个每个事件。为此,使用两个聚合级别:
select avg(flag)
from (select e.id,
max( e.published and a.type not in ('large', 'medium') ) as flag
from events e join
attributes a
on e.id = a.event_id
group by e.id
) e;
select
case when e.published and a.type not in('large','medium') then 'Met condition' else 'Not met condition' end conditions
, count(distinct e.id) totals
, group_concat(distinct e.id) event_ids
, count(distinct e.id) * 100.0 / x.total_cnt percentage
from events e
join Attributes a on a.event_id = e.id
cross join (select count(*) as total_cnt from events) x
group by case when e.published and a.type not in('large','medium') then 'Met condition' else 'Not met condition' end, total_cnt
db<>fiddle here