【问题标题】:How can I write a loop bash script with a mysql query including subqueries?如何使用包含子查询的 mysql 查询编写循环 bash 脚本?
【发布时间】:2013-02-16 00:46:18
【问题描述】:

所以我有这个非常丑陋(但功能强大)的多子查询 mysql Select 命令。

select myt_taxon_list, count(myt_taxon_list)
from (select t1.taxon_list as myt_taxon_list, t1.query_prot_id,
             count(t1.query_prot_id) / (select count(t.id)
                                        from taxa t, stax st
                                        where t.systematics like concat('%; ', st.taxon_list, '%')
                                              and t.taxon_name not like '%thaliana%'
                                              and st.taxon_list = t1.taxon_list
                                        group by t1.taxon_list) as taxfrac
      from task1 t1
      where t1.taxon_list = 'Stramenopiles'
      group by t1.query_prot_id) myt 
where taxfrac > 0.5

在最后一行中,您可以阅读“Stramenopiles”。其结果是一个简单的计数。现在我想编写一个 bash 脚本,该脚本不仅为 Stramenopiles 还为该列中的所有条目写入计数 - 不是总和,而是每个分隔的条目。我需要将它与 bash 中的迭代循环结合起来,但我以前从未编写过脚本。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 这似乎是两个问题合而为一。您需要先以某种方式提取列中的条目,然后才能编写循环来遍历它们(这是单独的问题)。你知道第一部分怎么做吗?
  • 感谢您的澄清!不幸的是,我真的不知道该怎么做:(
  • SELECT DISTINCT taxon_list FROM task1 呢?
  • 我必须放在哪里?你的意思是不是“t1.taxon_list = 'Stramenopiles'”?

标签: mysql bash loops select count


【解决方案1】:

我根本不会在 bash 中这样做。如果你删除

where t1.taxon_list = 'Stramenopiles'

您可以在外部选择中使用group by myt_taxon_list

select myt_taxon_list, count(myt_taxon_list)
from (select t1.taxon_list as myt_taxon_list, t1.query_prot_id,
             count(t1.query_prot_id) / (select count(t.id)
                                        from taxa t, stax st
                                        where t.systematics like concat('%; ', st.taxon_list, '%')
                                              and t.taxon_name not like '%thaliana%'
                                              and st.taxon_list = t1.taxon_list
                                        group by t1.taxon_list) as taxfrac
      from task1 t1
      group by t1.query_prot_id, t1.taxon_list) myt 
where taxfrac > 0.5
group by myt_taxon_list

它可以很好地计算出所有 taxon_list 条目。

【讨论】:

  • 我以前试过这个;结果是错误的计数,并且缺少许多列条目 =/
  • @Wicked_sue 那么taxon_list 和query_prot_id 有什么关系呢?这应该是 11 关系。
  • 啊,似乎有问题。对于 taxon_list 条目,存在超过 1 个 query_prot_id 条目。我对它们出现的次数很感兴趣。
  • @Wicked_sue 您必须同时按taxon_list 和query_prot_id 分组,否则您会得到任意结果。请查看修改后的答案。
  • 完美运行!非常非常非常感谢您的快速回答;我在这玩了 3 天了,我快要生气了:D
猜你喜欢
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2011-09-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多