简体   繁体   English

计算MySQL中两个表之间的公共行

[英]Count common rows between two tables in mysql

so here's my question... 所以这是我的问题

Hi have two tables in mysql, called go_H and go_J, both looking like this: 您好,在mysql中有两个表,分别称为go_H和go_J,看起来都像这样:

go_H go_H

+---------------+------------+
| gene          | GoCode     |
+---------------+------------+
| DNAJC25-GNG10 | GO:0004871 |
| DNAJC25-GNG10 | GO:0005834 |
| DNAJC25-GNG10 | GO:0007186 |
| LOC100509620  | GO:0005215 |
| LOC100509620  | GO:0006810 |
| LOC100509620  | GO:0016021 |
| PPIAL4E       | GO:0000413 |
| PPIAL4E       | GO:0003755 |
| PPIAL4E       | GO:0005737 |
| PPIAL4E       | GO:0006457 |
| LOC105371242  | GO:0000413 |
+----------------------------+

go_J go_J

+------------+
| GoCode     |
+------------+
| GO:0007254 |
| GO:0007256 |
| GO:0007257 |
| GO:0042655 |
| GO:0043506 |
| GO:0043507 |
| GO:0043508 |
| GO:0046328 |
| GO:0046329 |
| GO:0046330 |
+------------+

Basically what I want to achieve is to see what GoCode values from go_J appear in GoCode from Go_H, and count them, so as I get a total number o GO ids that are present in both tables. 基本上,我想要实现的是查看go_J的GoCode值出现在Go_H的GoCode中,并对它们进行计数,以便获得两个表中都存在的o ID总数。

I have come to select go_H.GoCode and go_J.GoCode, but I don't know how to compare them to find common rows and then count them... 我来选择go_H.GoCode和go_J.GoCode,但是我不知道如何比较它们以找到常见的行,然后对它们进行计数...

Any help? 有什么帮助吗?

SELECT COUNT(*) FROM go_H
INNER JOIN go_J USING GoCode

INNER JOIN => Rows that are in both tables based on the join column (GoCode) INNER JOIN =>基于联接列(GoCode)的两个表中的行

Alternative: 替代方案:

SELECT COUNT(*) FROM go_H h
INNER JOIN go_J ON j.GoCode = h.GoCode

Check this answer out to learn about joins: 查看此答案以了解有关联接的信息:

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? INNER JOIN,LEFT JOIN,RIGHT JOIN和FULL JOIN有什么区别?

希望这可以帮助。

select count(*) from go_J j join  go_H h on h.GoCode=j.GoCode;

To find how many rows are similar between 2 table 查找2个表之间有多少行相似

SELECT  COUNT(*) totalCount
FROM    go_H a 
        INNER JOIN go_J b
            ON a.GoCode = b.GoCode

To find how many rows from go_H are not in go_J 查找go_H中没有多少行

SELECT  COUNT(*) totalCount
FROM    go_H a 
        LEFT JOIN go_J b
            ON a.GoCode = b.GoCode
WHERE   b.GoCode IS NULL

To find how many rows from go_J are not in go_H 查找go_J中没有多少行

SELECT  COUNT(*) totalCount
FROM    go_J a 
        LEFT JOIN go_H b
            ON a.GoCode = b.GoCode 
WHERE   b.GoCode IS NULL

You can achieve this just in SQL by running a query similar to this: 您可以通过运行类似于以下查询的方式在SQL中实现此目的:

SELECT
 *,
count (GoCode)
 FROM (    
SELECT GoCode FROM go_H
    UNION
    SELECT GoCode FROM go_H )a
group by a.gocode

This will provide you a table with each code in a column and then the amount of times it is present across both tables 这将为您提供一个表格,其中每个代码都在一个列中,然后在两个表格中均出现该代码的次数

An alternative with PHP would be get both tables into an array by using PDO and use in_array to check PHP的替代方法是使用PDO将两个表都放入数组,并使用in_array进行检查

<?php
foreach ($go_H as $GoCode) {
    if (in_array($GoCode, $go_J)) {
        // handle codes in both tables
    }
}

This is not the most efficient method but it will yeild results. 这不是最有效的方法,但会产生结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM