我不知道如何编写SQL查询。
鉴于数据:
几个特征标识符整数 - 比方说1和447。
我正在寻找domain_ids,在其中我将找到分配给特定特征的snp。当域名genomic_pos
与start
和end
之间的chr_id
相同时,SNP就位于域中。有两种情况任何和所有:
我已经解决了任何案例,但我坚持使用所有案例:(
以下是任何代码:
SELECT DISTINCT domains.domain_id
FROM snps JOIN traits_snps ON snps.snp_id = traits_snps.snp_id
JOIN traits ON traits.trait_id = traits_snps.trait_id
JOIN chromosomes ON snps.chr_id = chromosomes.chr_id
JOIN domains ON domains.chr_id = chromosomes.chr_id
WHERE traits.trait_id in (447,1 ) -- <-- HERE GOES TRAIT IDS
AND genomic_pos BETWEEN start AND end
AND domains.chr_id = snps.chr_id;
答案 0 :(得分:2)
如果我理解正确,您可以使用group by
和having
执行您想要的操作:
SELECT d.domain_id
FROM snps s JOIN
traits_snps ts
ON s.snp_id = ts.snp_id JOIN
traits t
ON t.trait_id = ts.trait_id JOIN
chromosomes c
ON s.chr_id = c.chr_id JOIN
domains d
ON d.chr_id = c.chr_id AND
d.chr_id = s.chr_id -- JOIN conditions should be in ON clauses
WHERE t.trait_id in (447, 1) AND -- <-- HERE GOES TRAIT IDS
genomic_pos BETWEEN start AND end
GROUP BY d.domain_id
HAVING COUNT(DISTINCT t.trait_id) = 2;
答案 1 :(得分:1)
要查找其他表中存在某些数据的域,请使用WHERE EXISTS
。
任何情况:查找在给定范围内与至少一个特征相关的具有snp的域:
select domain_id
from domains d
where exists
(
select *
from snps
where snps.chr_id = d.chr_id
and snps.genomic_pos between d.start and d.end
and snps.snp_id in (select snp_id from traits_snps where trait_id in (447, 1))
);
所有情况:查找在给定范围内与所有特征相关的具有snp的域:
select domain_id
from domains d
where exists
(
select *
from snps
where snps.chr_id = d.chr_id
and snps.genomic_pos between d.start and d.end
and snps.snp_id in
(
select snp_id
from traits_snps
where trait_id in (447, 1)
group by snp_id
having count(distinct trait_id) = 2
)
);