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 GO 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?
任何幫助嗎?
4 个解决方案
#1
1
Hope this helps.
希望這個有幫助。
select count(*) from go_J j join go_H h on h.GoCode=j.GoCode;
#2
2
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)
內連接=>行,基於連接列(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:
查看以下答案了解連接:
內連接、左連接、右連接和全連接的區別是什么?
#3
1
To find how many rows are similar between 2 table
找出兩個表之間有多少行是相似的
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中有多少行不在go_J中
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中有多少行不在go_H中
SELECT COUNT(*) totalCount
FROM go_J a
LEFT JOIN go_H b
ON a.GoCode = b.GoCode
WHERE b.GoCode IS NULL
#4
0
You can achieve this just in SQL by running a query similar to this:
您可以通過運行類似於以下的查詢來實現這一點:
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來檢查兩個表
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.
這不是最有效的方法,但它將產生結果。