Mysql SQL垂直分组

Mysql SQL垂直分组,mysql,sql,relational-database,Mysql,Sql,Relational Database,我有以下RDB表: ID Feature 1 1 1 2 2 1 3 1 3 2 3 3 我想要的是以下输出: ID Feature1 Feature2 Feature3 1 true true false 2 true false false 3 true true true 要实现这一点,最简单的SQL查询是什么?使用交叉联接和左联接,获取所有功能组合的所

我有以下RDB表:

ID  Feature
1   1
1   2
2   1
3   1
3   2
3   3
我想要的是以下输出:

ID  Feature1    Feature2    Feature3
1   true        true        false
2   true        false       false
3   true        true        true

要实现这一点,最简单的SQL查询是什么?

使用
交叉联接和
左联接,获取所有功能组合的所有id,从而获得所需的结果

select i.id,
max(case when f.feature=1 and t.feature is not null then 'true' else 'false' end) as feature1,
max(case when f.feature=2 and t.feature is not null then 'true' else 'false' end) as feature2,
max(case when f.feature=3 and t.feature is not null then 'true' else 'false' end) as feature3
from (select distinct feature from t) f --replace this with the feature table if you have one
cross join (select distinct id from t) i
left join t on t.id=i.id and t.feature=f.feature
group by i.id
如果您只需要布尔值1,0表示True,那么可以将查询简化为False

select i.id,
max(f.feature=1 and t.feature is not null) as feature1,
max(f.feature=2 and t.feature is not null) as feature2,
max(f.feature=3 and t.feature is not null) as feature3
from (select distinct feature from t) f --replace this with the feature table if you have one
cross join (select distinct id from t) i
left join t on t.id=i.id and t.feature=f.feature
group by i.id

使用
交叉连接
左连接
获取所有特征组合的所有id,并将原始表添加到此上,以获得所需的结果

select i.id,
max(case when f.feature=1 and t.feature is not null then 'true' else 'false' end) as feature1,
max(case when f.feature=2 and t.feature is not null then 'true' else 'false' end) as feature2,
max(case when f.feature=3 and t.feature is not null then 'true' else 'false' end) as feature3
from (select distinct feature from t) f --replace this with the feature table if you have one
cross join (select distinct id from t) i
left join t on t.id=i.id and t.feature=f.feature
group by i.id
如果您只需要布尔值1,0表示True,那么可以将查询简化为False

select i.id,
max(f.feature=1 and t.feature is not null) as feature1,
max(f.feature=2 and t.feature is not null) as feature2,
max(f.feature=3 and t.feature is not null) as feature3
from (select distinct feature from t) f --replace this with the feature table if you have one
cross join (select distinct id from t) i
left join t on t.id=i.id and t.feature=f.feature
group by i.id

我相信您只需要一个简单的pivot查询。在这里,我使用布尔值来标记一个特性是否存在(1表示true,0表示false)


我相信您只需要一个简单的pivot查询。在这里,我使用布尔值来标记一个特性是否存在(1表示true,0表示false)


您只需要条件聚合:

select id, max(feature = 1) as feature1, max(feature = 2) as feature2,
       max(feature = 3) as feature3
from t
group by id;
上面返回0和1。如果确实需要字符串
true
false
,可以执行以下操作:

select id,
       (case when max(feature = 1) then 'true' else 'false' end) as feature1,
       (case when max(feature = 2) then 'true' else 'false' end) as feature2,
       (case when max(feature = 3) then 'true' else 'false' end) as feature3
from t
group by id;

您只需要条件聚合:

select id, max(feature = 1) as feature1, max(feature = 2) as feature2,
       max(feature = 3) as feature3
from t
group by id;
上面返回0和1。如果确实需要字符串
true
false
,可以执行以下操作:

select id,
       (case when max(feature = 1) then 'true' else 'false' end) as feature1,
       (case when max(feature = 2) then 'true' else 'false' end) as feature2,
       (case when max(feature = 3) then 'true' else 'false' end) as feature3
from t
group by id;

只有3个功能,或者你可以有更多功能吗?越通用越好,但现在,我们可以安全地假设只有3个功能这是mysql,对吗?只有3个功能,或者你可以有更多功能吗?越通用越好,但现在,我们可以安全地假设只有3个功能这是mysql,对吗?