[英]group or select distinct columns in mysql
I have a table like this 我有这样的桌子
records columnA columnB columnC columnD gary 2011 0 a gary 2011 0 b gary 2010 1 a mary 2011 0 a mary 2010 1 b mary 2010 1 c
i want to parse the table and if i find multiple lines where columnA columnB columnC are common to display the values from these three columns once followed by values found on columnD from each records where those columns ABC where the same. 我想解析表,如果我发现多行,其中columnA columnB columnC是通用的,则一次显示这三列的值,然后是从每条记录的columnD上找到的值,其中那些ABC列相同。
Something like this 像这样
print output gary 2011 0 a,b gary 2010 1 a mary 2011 0 a mary 2010 1 b,c
I am using mysql, I tried with distinct but it duplicates the prints when found similar ABC columns. 我使用的是mysql,我尝试了与众不同的方法,但是当发现相似的ABC列时,它会重复打印。 for example instead of displaying
例如,而不是显示
gary 2011 0 a,b
once, it will disaply this line twice and so on three, four ... times, for each similar lines found... 一次,它将使该行失效两次,以此类推,对于发现的每条相似行,重复三,四次...
Thank you, Mozley 谢谢你,莫兹利
You're looking for GROUP_CONCAT
: 您正在寻找
GROUP_CONCAT
:
SELECT columnA, columnB, columnC,
GROUP_CONCAT(DISTINCT columnD
ORDER BY columnD ASC SEPARATOR ', ') AS columnD
FROM records
GROUP BY columnA, columnB, columnC
Result 结果
| COLUMNA | COLUMNB | COLUMNC | COLUMND | ----------------------------------------- | gary | 2010 | 1 | a | | gary | 2011 | 0 | a, b | | mary | 2010 | 1 | b, c | | mary | 2011 | 0 | a |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.