简体   繁体   English

从一个表中选择count(id),其中另一个表中存在该ID

[英]Select count(id) from one table where that id is present in another table 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* *在table_a中有100个成员,但其中只有50个成员在table_b中有记录,而只有25个成员在table_b中有已批准= 1的记录,因此我需要查询返回的值是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). 嘿,这里的每个人都是我要解决的查询,它需要返回一个结果计数,以便可以使用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 我需要检查((table_b中的memberID的数量与table_a中的每个ID匹配并在table_b中批准的MemberID = 1)-大于1

The final result needs to be a count of the number of members that have an entry in table_b. 最终结果必须是在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. 让我知道是否需要更多详细信息。

I'm assuming you want to get the number of records in table_a that match to table_b, as long as the value in table_b is approved. 我假设您要获取table_a中与table_b匹配的记录数,只要table_b中的值被批准即可。 Therefore, I would join the 2 tables. 因此,我将加入2个表。 The inner join ensures that you only consider rows that are in both tables, and the where statement takes care of the approved requirement. 内部联接确保您只考虑两个表中的行,而where语句将满足批准的要求。

select count(a.id) 
from table_a a
join table_b b
on a.id = b.memberID
where b.approved=1
 SELECT b.ID, a.ID 

 FROM table_a a, table_b b 

 WHERE a.ID = b.ID AND b.ID = 1

I'm a bit rusty in SQL, but can this be achieved by specifying the two tables in the query like this: 我对SQL有点生锈,但是可以通过在查询中指定两个表来实现:

SELECT COUNT(DISTINCT table_a.id) 
  FROM table_a, table_b 
  WHERE table_a.id = table_b.MemberID 
    AND table_b.approved = 1;

I believe that meets your select criteria. 我相信符合您的选择标准。

SELECT COUNT(*) AS cnt
FROM table_a AS a
WHERE EXISTS
      ( SELECT *
        FROM table_b AS b
        WHERE b.memberID = a.id
          AND b.approved = 1
      )

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 我什至不需要查看table_a,因为我正在基于table_b计数memberID

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. 我希望这对将来有帮助。

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

相关问题 从一个表中选择,从另一个表中计数,其中 id 未链接 - select from one table, count from another where id is not linked MySQL SELECT COUNT从一个表和ID从另一个表? - MySQL SELECT COUNT from one table and ID from another? 如果另一个表中存在id,则MYSQL在一个表中选择count(*) - MYSQL select count(*) in one table if id exists in another table 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists MySql:从另一个表插入表SELECT中,其中first_table ID = another_table.id - MySql: insert into table SELECT from another table where first_table ID =another_table.id 从表中选择字段,其中id不在mysql的另一个表中[不起作用] - select fields from table where id not in another table in mysql [not working] 从一个表中选择,从id链接的另一个表中计数 - select from one table, count from another where id's linked Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible mysql查询-仅从另一个表中选择id = id,而该表中的字段=值 - mysql query - only select where id = id from another table and a field in that table = a value MySQL SELECT * FROM表WHERE ID IN(子查询) - MySQL SELECT * FROM table WHERE id IN (subquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM