mysql select 包含 group_concat 的语句

mysql select statement that includes group_concat

我有一个 mysql 这样的陈述

SELECT distinct mk.gene as gene, qs.rs as snp, CONCAT (qs.A1, qs.A2) as genotype
FROM dallas.QS_base qs  cross join   
dallas.staging mk 
     ON qs.rs = mk.rs 
WHERE qs.sampleID = 'mydna' 
order by gene ASC;

哪个returns这种类型的输出

'ACE'    'RS4343',    'AA'
'ACTN3'  'RS1815739'  'TC'

来自这种 table (dallas.staging)

'heart health', 'ACE', 'RS4343'
'skin health', 'ACE', 'RS4343'
'sports performance', 'ACE', 'RS4343'
'sports performance', 'ACTN3', 'RS1815739'
'longevity', 'ACTN3', 'RS1815739'

和这个 (dallas.QS_base)

'mydna','RS4343','A','A'
'mydna','RS1815739','T','C'

我应该如何更改上面的 mysql 语句才能获得此输出?我相信我需要使用 group_concat 命令。

'ACE'    'RS4343',    'AA' '(heart health, sports performance, skin health)'
'ACTN3'  'RS1815739'  'TC' '(sports performance, longevity)'

此查询应该为您完成这项工作(请注意 mk.gene 上的 distinct 不是必需的,因为您正在按它分组):

SELECT mk.gene as gene, qs.rs as snp, CONCAT (qs.A1, qs.A2) as genotype, GROUP_CONCAT(mk.condition) AS conditions
FROM QS_base qs  cross join   
staging mk 
     ON qs.rs = mk.rs 
WHERE qs.sampleID = 'mydna'
group by gene
order by gene ASC;

输出:

gene    snp         genotype    conditions
ACE     RS4343      AA          heart health,sports performance,skin health
ACTN3   RS1815739   TC          sports performance,longevity