SELECT中的MYSQL SELECT在同一个表上

时间:2013-02-04 05:13:53

标签: mysql select

我有一个SQL语句几乎可以完成我想做的事情。我需要的是找到 genus 这样的 pindent> 60 coverage> 60 表示 qseqid 值。我想我需要某种类型的联接,可能就像在question

中一样

这是我现在拥有的。哪个没达到我想要的效果。

SELECT qseqid, genus, species, txid, sgi, pindent, coverage 
FROM vmdavis.insecta10000
WHERE pindent > 60
AND coverage > 60
AND qseqid in ("diaci0.9_transcript_99990000013040", "diaci0.9_transcript_99990000022677")
ORDER BY  genus, species, qseqid, coverage, pindent;

以下是为什么这不起作用的一个例子。 Anchon满足上述qseqid for dia ... 040的标准但不适用于dia ... 677所以我不会这样做。

| diaci0.9_transcript_99990000013040 | Anchon           | sp. NYSM 95-02-01-35          |  265052 |   6467730 |   80.93 |  61.7597 |

以下是表格的示例

mysql> SELECT qseqid, genus, species, txid, pindent, coverage FROM vmdavis.insecta10000 limit 5;
+------------------------------------+---------+-------------+--------+---------+----------+
| qseqid                             | genus   | species     | txid   | pindent | coverage |
+------------------------------------+---------+-------------+--------+---------+----------+
| diaci0.9_transcript_99990000000055 | Apis    | florea      |   7463 |    97.5 |  2.58107 |
| diaci0.9_transcript_99990000000055 | Bombus  | impatiens   | 132113 |    97.5 |   3.3534 |
| diaci0.9_transcript_99990000000055 | Nasonia | vitripennis |   7425 |    97.5 |  1.58343 |
| diaci0.9_transcript_99990000000055 | Bombus  | terrestris  |  30195 |    97.5 |  3.41207 |
| diaci0.9_transcript_99990000000055 | Apis    | mellifera   |   7460 |    97.5 |  2.88889 |
+------------------------------------+---------+-------------+--------+---------+----------+

这是一个例子。在这种情况下,属Agetocera被列出两次,因为对于两个qseqid它都符合pindent和coverage的标准。如果Agetocera不符合pindent的条件,则应列出这些行中的其他行。 60和覆盖范围> 60为qseqid

| qseqid                             | genus     | species     | txid   | pindent | coverage
| diaci0.9_transcript_99990000013040 | Agetocera | mirablis    |  715820 | 291191497 |   82.37 |  60.7963 |
| diaci0.9_transcript_99990000022677 | Agetocera | mirablis    |  909986 | 309755769 |   77.52 |  78.6269 |

我是mysql的新手,我假设这个问题的答案可能存在于stackoverflow上。如果我发现它,我只是不知道要搜索或理解解决方案。如果问题可以更好地提出,或者您可以建议更好的标题,我会更新。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情 - 使用子查询只获得所需的属:

SELECT *
FROM insecta10000 i 
  JOIN 
  (
  SELECT genus
  FROM insecta10000
  WHERE pindent > 60
    AND coverage > 60
    AND qseqid in ("diaci0.9_transcript_99990000013040", "diaci0.9_transcript_99990000022677")
  GROUP BY genus
  HAVING COUNT(*) = 2
  ) i2 on i.genus = i2.genus 

这是SQL Fiddle

祝你好运。

答案 1 :(得分:0)

如果您想要满足Coverage>的记录60,pindent> 60,你已经得到了查询。但是,如果您正在查看类似的内容,union满足Coverage和pindent的记录,请尝试以下操作:

SELECT * FROM (
SELECT qseqid, genus, species, txid, sgi, pindent, coverage 
FROM vmdavis.insecta10000
WHERE pindent > 60    
UNION
SELECT qseqid, genus, species, txid, sgi, pindent, coverage 
FROM vmdavis.insecta10000
WHERE coverage > 60) x
WHERE x.qseqid in ("diaci0.9_transcript_99990000013040", "diaci0.9_transcript_99990000022677")
ORDER BY  x.genus, x.species, x.qseqid, x.coverage, x.pindent
;

现在您已经给出了预期的输出:(尽管列不同......稍微有点):

http://sqlfiddle.com/#!2/f89ce/4

SELECT qseqid, genus, species, txid, 
indent, coverage 
FROM demo
WHERE indent > 60
AND coverage > 60
AND qseqid in ("diaci0.9_transcript_99990000013040", "diaci0.9_transcript_99990000022677")
ORDER BY  genus, species, qseqid, coverage, indent;

|                             QSEQID |     GENUS |  SPECIES |   TXID | INDENT | COVERAGE |
------------------------------------------------------------------------------------------
| diaci0.9_transcript_99990000013040 | Agetocera | mirablis | 715820 |  82.37 |  60.7963 |
| diaci0.9_transcript_99990000022677 | Agetocera | mirablis | 909986 |  77.52 |  78.6269 |