如何使用任意列和行扩展SQL结果集?

时间:2014-07-20 11:09:40

标签: mysql sql

请允许我以一种冗长的方式解释我的问题。

我有蛋白质的内存关联,功能证据代码,它们告诉我们现在的情况,某些蛋白质具有所述功能。

示例

| Protein | Evidence Code | GO-Term    |
+---------+---------------+------------+
| prot_1  | 'EXP'         | GO:0030599 |
| prot_2  | 'IEA'         | GO:0030599 |

请注意,这些功能编码为Gene Ontology (GO) Terms。这是分层排列的蛋白质功能描述的数据库,其中随着深度的增加,给定的功能描述(GO术语)是比其祖先更详细或更精确的描述。事实上,GO术语以分层非循环图(GO DAG)排列。

以上GO术语GO:0030599有几个这样的祖先。请参阅Inferred Tree View here。术语的祖先可以从公共MySQL数据库

获得
mysql -h mysql.ebi.ac.uk -u go_select -P 4085 -pamigo go_latest

使用以下内容,相当长

查询

SELECT t.*, to_root.relation_distance, child.acc as child_acc
  FROM graph_path res LEFT JOIN term t ON t.id = res.term1_id
  LEFT JOIN graph_path to_root ON t.id = to_root.term2_id
  LEFT JOIN term child ON child.id = res.term2_id
  WHERE
  res.term1_id != (SELECT r.id FROM term r WHERE r.is_root = 1)
  AND child.acc in ('GO:0030599')
  AND to_root.term1_id =
  (SELECT r.id FROM term r WHERE r.is_root = 1)
  GROUP BY t.id ORDER BY to_root.relation_distance ASC

我可以扩展它,不幸的是到目前为止 一个蛋白质,这样我得到的东西就像期望的结果,这是内存关联与结果集合并:

查询

SELECT t.*, to_root.relation_distance, child.acc as child_acc,
  (CASE child.acc
    WHEN 'GO:0030599' THEN 'Prot_1'
    ELSE NULL END) as 'prot'
  FROM graph_path res LEFT JOIN term t ON t.id = res.term1_id
  LEFT JOIN graph_path to_root ON t.id = to_root.term2_id
  LEFT JOIN term child ON child.id = res.term2_id
  WHERE
  res.term1_id != (SELECT r.id FROM term r WHERE r.is_root = 1)
  AND child.acc in ('GO:0030599')
  AND to_root.term1_id =
  (SELECT r.id FROM term r WHERE r.is_root = 1)
  GROUP BY t.id ORDER BY to_root.relation_distance ASC

所需的示例结果

| Protein | Evidence Code | GO-Term                    |
+---------+---------------+----------------------------+
| prot_1  | 'EXP'         | GO:0030599 itself          |
| prot_1  | 'EXP'         | GO:0030599 first ancestor  |
| prot_1  | 'EXP'         | GO:0030599 second ancestor |
| prot_1  | 'EXP'         | GO:0030599 third ancestor  |

我的问题是:

如何修改上述SQL查询,使其复制上述示例结果行,但也复制蛋白prot_2

| Protein | Evidence Code | GO-Term                    |
+---------+---------------+----------------------------+
| prot_1  | 'EXP'         | GO:0030599 itself          |
| prot_1  | 'EXP'         | GO:0030599 first ancestor  |
| prot_1  | 'EXP'         | GO:0030599 second ancestor |
| prot_1  | 'EXP'         | GO:0030599 third ancestor  |
| prot_2  | 'IEA'         | GO:0030599 itself          |
| prot_2  | 'IEA'         | GO:0030599 first ancestor  |
| prot_2  | 'IEA'         | GO:0030599 second ancestor |
| prot_2  | 'IEA'         | GO:0030599 third ancestor  | 

我希望我的问题的编辑比原始版本更清晰。

非常感谢帮助。 非常感谢你!

1 个答案:

答案 0 :(得分:0)

  

有没有办法用普通的旧SQL来实现这个目标?

不,没有。 MySQL必须知道已婚夫妇的记忆内联系。除非存储在MySQL上的(临时)表中,否则您将不得不在可获得该数据的客户端进行此处理。