sql—将多行数据串联在一行中,并用逗号分隔

xn1cxnb4  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(268)

我有一个表,我想合并相同的问题,他们的答案。这个问题的答案需要用逗号隔开

tbl_data
Userid       question           answer      email
1032         Marital status     Single      Something@something.com
1032         Requirements       eco house   Something@something.com
1032         Requirements       garage      Something@something.com
1032         requirements       garden      Something@something.com
1032         Household income?  over 80k    Something@something.com
1033         Martial status     Married     harry@something.com

我想查看这样的数据,并结合要求的问题和答案

tbl_data
    Userid       question           answer                    email
    1032         Marital status     Single                    Something@something.com
    1032         Requirements       eco house,garage,garden   Something@something.com
    1032         Household income?  over 80k                  Something@something.com
    1033         Martial status     Married                   harry@something.com

我在这里尝试了这个解决方案,但没有任何运气,因为它只是将所有问题的答案添加到每个尝试解决方案的答案中

eivnm1vs

eivnm1vs1#

SELECT Userid, GROUP_CONCAT(question SEPARATOR ',') as questions, answer, email FROM tbl_data GROUP BY question;

相关问题