Count common rows between two tables in mysql - php

so here's my question...
Hi have two tables in mysql, called go_H and go_J, both looking like this:
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
+------------+
| 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.
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...
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)
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?

Hope this helps.
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
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
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
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:
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
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.

Related

Mysql concat unequal table field

why I can't Select a row of a table with an unequal concat construction? I'll show you an example.
Table1
| id | area |
| 1 | items_labeling_small |
| 2 | items_labeling_big |
Table2
| id | area | kat |
| 1 | small | labeling |
| 2 | big | labeling |
SELECT Table1.area FROM Table1, Table2 WHERE Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
No results have to be shown, because both are matching with the concat construction. But they're shown in result. I've no idea why.. and how I can change the query that it works.
They are show because each row don't match the other so you have the rows that not match
SELECT Table1.area
FROM Table1
INNER JOIN Table2 ON Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
could be you want a not in
SELECT Table1.area
FROM Table1
where Table1.area NOT IN (
SELECT CONCAT('items_', Table2.kat, '_', Table2.area)
FROM Table2
)
and as a suggestion you should not use the (old) implict join sintax based on where clause ...use explicit join sintax ..

PDO Query : Count related and repeated values then inner join

I work with PHP and PDO.
So I have 2 tables like,
Table 1
| id | name | age |
| 1 | John | 25 |
| 2 | Tom | 32 |
| 3 | James| 45 |
Table 2
| id | Comment | Link |
| 1 | some text | 3 |
| 2 | some text | 3 |
| 3 | some text | 1 |
So, Link column numbers represent id's in table1. For example Link = 3s in table 2 represent James in table 1. I need a query which brings all table1's data and also a number of repeated value for related Link column which comes from table2.
For example, the query should give me (let's choose James),
| id | name | age | Value |
| 3 | James | 45 | 2 |
value=2, because there are two 3s in link column which related to James
I tried somethings but got lots of errors.
I think you just need the GROUP BY
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
GROUP BY a.id, a.name, a.age
If you really want just one row then add WHERE
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
WHERE a.name = 'James'
GROUP BY a.id, a.name, a.age
or use subquery
SELECT a.id,
a.name,
a.age,
(SELECT count(*) FROM table2 b WHERE a.id = b.link) as value
FROM table1 a
WHERE a.name = 'James'

MYSQL statement: lookup how many records exist how many times in other table

I have 2 database tables:
Table 1:
+---------+-------+-------------+
| Page | Title | Description |
+---------+-------+-------------+
| Apple | ..... | ........... |
| Orange | ..... | ........... |
| Pear | ..... | ........... |
| Grapes | ..... | ........... |
+---------+-------+-------------+
Table 2:
+----------+-------------+
| Link | Page |
+----------+-------------+
| Website1 | Apple |
| Website2 | Orange |
| Website3 | Apple |
| Website4 | Orange |
| Website5 | Apple |
| Website6 | Pear |
| Website7 | Apple |
| Website8 | Grapes |
| Website9 | Grapes |
+----------+-------------+
I want to know/return how many pages from Table 1 are referenced in Table 2 and how many times they are referenced. (I DON'T want to know how many times EACH page in Table 1 is referenced in Table 2).
So in this example:
1 page is referenced 1 time (Pear),
2 pages are referenced 2 times (Grapes and Orange) &
1 page is referenced 4 times.
What kind of SQL statement would I use to get this?
Following query should do..
SELECT COUNT(1) NoOfPages,CNT ReferencedTimes
FROM
(
SELECT T2.PAGE,COUNT(1) CNT
FROM TABLE1 T1 INNER JOIN TABLE2 T2 ON T1.PAGE = T2.PAGE
GROUP BY T2.PAGE
)T
GROUP BY CNT
I think the following statement will fit:
SELECT count(*) FROM Table2 WHERE (Table2.Page IN (SELECT Page FROM Table1));
Use This query
Select table2.page,cnt(table2.page)
from table1 inner join table2
On table1.Page=table2.Page group by table2.page
SELECT (GROUP_CONCAT(DISTINCT page)) AS Page,page_count
FROM
(SELECT table1.Page as page,COUNT(*) as page_count
FROM table1 INNER JOIN table2 ON table1.Page=table2.Page
GROUP BY table1.Page)
as T GROUP BY page_count
Hope this helps
If what you are seeking is X page was referenced N times, the below query will achieve that:
SELECT COUNT(t1.page), t2.count
FROM table1 t1
INNER JOIN (SELECT page,COUNT(*) AS count FROM table2 GROUP BY page) t2 ON t1.page=t2.page
GROUP BY t2.count
Try this query, it will make a left join and tell you how many times item is referenced in table2, if count is zero than no reference in the other table
SELECT table1.Page, count(table2.Page) as count
FROM table1
LEFT JOIN table2 ON table2.Page = table1.Page
GROUP BY table1.Page

MySQL Multiple Conditions on Group By / Having Clause

I have three tables that are all inter-related with the following structure.
ModuleCategory Table:
+------------------+----------------+------------+
| ModuleCategoryID | ModuleCategory | RequireAll |
+------------------+----------------+------------+
| 90 | Cat A | YES |
| 91 | Cat B | NO |
+------------------+----------------+------------+
ModuleCategorySkill Table:
+------------------+---------+
| ModuleCategoryID | SkillID |
+------------------+---------+
| 90 | 1439 |
| 90 | 3016 |
| 91 | 1440 |
| 91 | 3016 |
+------------------+---------+
EmployeeSkill Table:
+---------+---------+
| EmpName | SkillID |
+---------+---------+
| Emp1 | 1439 |
| Emp1 | 3016 |
| Emp2 | 1440 |
| Emp2 | 3016 |
| Emp3 | 1439 |
| Emp4 | 3016 |
+---------+---------+
Desired Output:
+------------------+-------+
| ModuleCategory | Count |
+------------------+-------+
| Cat A | 1 |
| Cat B | 3 |
+------------------+-------+
I am trying to group by ModuleCategoryID's and get the count of employees which have the skills being tracked.
Normally, I can do the following query to obtain the numbers:
select mc.ModuleCategory, Count(*) as Count from ModuleCategory as mc
join ModuleCategorySkill as mcs on mc.ModuleCategoryID = mcs.ModuleCategoryID join EmployeeSkill as es on es.SkillID= mcs.SkillID
group by mc.ModuleCategoryID
However, I have a column RequireAll in the ModuleCategory table which if it is set to 'YES' should only count employees as 1 only if they have all the skills in the category. If it is set to NO then it can count each row normally and increase the count by the number of rows it groups by.
I can achieve this by writing separate queries for each modulecategoryID and using a having Count() > 1 (which will find me anyone that has all the skills for ModuleCategoryID 90). If there were 3 skills than I would have to change it to Having Count() > 2. If there isn't anyone that has all the skills specified, the count should be 0.
I need a dynamic way of being able to do this since there is a lot of data and writing one query for each ModuleCategoryID isn't the proper approach.
Also, I am using PHP so I can loop through and create a sql string that can help me achieve this. But I know I will run into performance issues on big tables with a lot of skills and modulecategoryID's.
Any guidance on how to achieve this is much appreciated.
You can do it by joining on the total category counts, and then using conditional aggregation:
select modulecategory,
count(case when requireall = 'yes'
then if(s = t, 1, null)
else s
end)
from (
select modulecategory,empname, requireall, count(*) s, min(q.total) t
from employeeskill e
inner join modulecategoryskill mcs
on e.skillid = mcs.skillid
inner join modulecategory mc
on mcs.modulecategoryid = mc.modulecategoryid
inner join (
select modulecategoryid, count(*) total
from modulecategoryskill
group by modulecategoryid
) q
on mc.modulecategoryid = q.modulecategoryid
group by modulecategory, empname
) qq
group by modulecategory;
demo here
This operates under the assumption an employee isn't going to be allocated the same skill twice, if that is something that may happen, this query is alterable to support it, but it seems like a broken scenario to me.
What we have here is an inner query that collates all the information we need (category name, employee name, whether or not all skills are required, how many skills are in the group per employee, and how many there in the group total), with an outer query that uses a conditional count to change how the rows are tallied, based on the value of requireall.

JOIN rows from 3 tables WHERE tableA.column1 = tableB.column1 = tableC.column1

I have 3 tables, structured like so:
TABLE A:
ID | PCID | ACTIVE | COHORT | WEEKLY_MEETING_TIME | FYE_ID | RC | AGREEMENT_SIGNED | RELEASE_SIGNED | NOTES | FACULTY_ADVISOR
TABLE B:
ID | QUARTER | OFFICE | WRITING_CENTER | etc.. | etc.. | etc.. |
TABLE C:
ID | QUARTER | WEEK | EMAIL | etc.. | etc.. | etc.. |
The common element between all 3 tables is the ID field.
I need to SELECT from all 3 tables, and have each row represent one ID and all the values associated with that ID.
So, for example, each output row should look like a combination of the three tables:
RESULTS:
ID | PCID | ACTIVE | COHORT | WEEKLY_MEETING_TIME | FYE_ID | RC | AGREEMENT_SIGNED | RELEASE_SIGNED | NOTES | FACULTY_ADVISOR | QUARTER | OFFICE | WRITING_CENTER | etc.. | etc.. | etc.. | WEEK | EMAIL | etc.. | etc.. | etc.. |
I have no idea how to structure a query like this. I suspect it involves using JOINs but my attempts have proved futile.
how can I combine the data from 3 tables, based on shared ID field?
SELECT * FROM TABLEA
INNER JOIN TABLEB ON TABLEA.ID = TABLEB.ID
INNER JOIN TABLEC ON TABLEA.ID = TABLEC.ID
If you don't need all the values substitute the "*" with the names of the fields you need (ex. TABLEA.ID, TABLEB.QUARTER, TABLEC.WEEK...)
NATURAL JOIN works in MySQL:
select * from A natural join B natural join C
Try this :
SELECT / WHAT YOU NEED TO SELECT - NOT * !!!/
FROM TABLEA
INNER JOIN TABLEB USING (ID)
INNER JOIN TABLEC USING (ID)

Categories