简体   繁体   中英

mysql select statement that includes group_concat

Ive a mysql statement like this

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;

which returns this type of output

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

from this type of table (dallas.staging)

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

and this (dallas.QS_base)

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

How should I change the mysql statement above to enable me to get this output? I believe I need to use group_concat command.

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

This query should do the job for you (note the distinct on mk.gene is not necessary as you are grouping by it):

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;

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM