Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select count(id) from one table where that id is present in another table MYSQL

Tags:

mysql

EDIT: Detailed description

Detailed Description:

*in table_a there are 100 members but only 50 of them have records in table_b and only 25 have records in table_b where approved = 1 THEREFORE the value I will need returned by the query is 25*

Hey everyone here is the query I am trying to resolve it will need to return a single result count so I can access with mysql_result($query, 0).

SELECT COUNT(id) FROM table_a WHERE (THIS IS WHERE I AM STUCK)

I need to check if the( count of memberID in table_b WHERE memberID matching each id in table_a and approved in table_b = 1) - is greater than 1

The final result needs to be a count of the number of members that have an entry in table_b.

Sample of table columns that need to access

table_a
-----------------
id

table_b
------------------
id
memberID
approved

Let me know if you need any more details.

like image 891
MadScientist Avatar asked Oct 26 '25 10:10

MadScientist


1 Answers

Problem Solved

Had to think of it backwards

    SELECT COUNT( DISTINCT memberID )
    FROM table_b
    WHERE approved =1

I do not need to even look at table_a seeing as I am counting the memberID based on table_b

Sometimes the solution is so simple and right in front of you.

Thanks for all the help! I hope this helps other in the future.

like image 132
MadScientist Avatar answered Oct 28 '25 23:10

MadScientist