gpt4 book ai didi

MySQL:多行到多个字段?

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:01 25 4
gpt4 key购买 nike

我有这张表:

userid | property
1 | propA
1 | propC
2 | propA
2 | propB

并且需要此输出用于 csv 导出/导入

userid;propA;propB;probC
1;yes;no;yes
2;yes;yes;no

如果没有像 php 这样的脚本语言,这可能吗?如果"is"和“否”只是“1”和“0”(例如),那对我来说完全没问题,但重要的是我只需要为每个用户一行,为每个属性只需要一个字段。

最佳答案

您可以使用条件聚合。但是,您的格式不是 CSV(提示:“C”表示逗号)。

要获得该格式,您可以:

select t.userid,
max(case when t.property = 'propA' then 'Yes' else 'No' end) as PropA,
max(case when t.property = 'propB' then 'Yes' else 'No' end) as PropB,
max(case when t.property = 'propC' then 'Yes' else 'No' end) as PropC
from table t
group by t.userid;

这恰好有效,因为"is"在字母表中比“否”晚。就个人而言,我只会使用数字 0 和 1:

select t.userid,
max(t.property = 'propA') as PropA,
max(t.property = 'propB') as PropB,
max(t.property = 'propC') as PropC
from table t
group by t.userid;

关于MySQL:多行到多个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31320528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com