2012-11-08 70 views
0

如何修改此查詢以使用存在?在SQL中使用EXIST

SELECT allele.allele as allele,site.allele_id as allele_id, site.genome_site_id as genome_site_id, popl.popl_id as popl_id,popl.frequency as frequency 
FROM kref.allele as allele 
    join kref.genome_site_allele as site using (allele_id) 
    LEFT JOIN kref.genome_site_allele_popl as popl USING (ref_genome_id, allelism_start, allelism_end, chrom_id, allele_id) 
WHERE ref_genome_id= 2 AND allelism_start=25710014 AND allelism_end=25710015 
    AND chrom_id ='22' 
    AND site.allele_id IN (
     SELECT allele_id 
     from kref.allele 
     where allele IN ('-','C') 
    ) 
+0

你能eleborate對什麼是你的目標只是爲了檢查,如果你得到任何結果或其他什麼東西? – Jester

回答

1

不管什麼原因,你可能想的是,這裏是一個使用語義上等效查詢存在:

SELECT allele.allele as allele,site.allele_id as allele_id, 
     site.genome_site_id as genome_site_id, popl.popl_id as popl_id, 
     popl.frequency as frequency 
    FROM kref.allele as allele 
    JOIN kref.genome_site_allele as site using (allele_id) 
    LEFT JOIN kref.genome_site_allele_popl as popl 
     USING (ref_genome_id, allelism_start, allelism_end, chrom_id, allele_id) 
WHERE ref_genome_id= 2 
    AND allelism_start=25710014 
    AND allelism_end=25710015 
    AND chrom_id ='22' 
    AND EXISTS (
    SELECT TRUE 
    FROM kref.allele 
    WHERE allele IN ('-','C') 
    AND allele_id = site.allele_id 
    )