Skip to content
Advertisement

How to select only rows from a table one clumns value is more than b table count value in MySQL

Table A:

id, name, school_code
-------------------
1,David,10601
2,Jack,10602
3,John,10603
4,Hank,10602
5,Daisy,10601
6,Kelly,10602
7,May,10602
8,Mayme,10602

Table B:

id, school_code,max_stu
------------------------
1,10601,10
2,10602,5
3,10603,6

How select table B rows value of max_stu more than table A count(*) where a.school_code=b.school_code ?

Expected result:

id, school_code,max_stu
------------------------
1,10601,10
3,10603,6

Advertisement

Answer

One method would use a correlated subquery. That looks pretty similar to how you already formulated it.

SELECT *
       FROM `table b` `b`
       WHERE `b`.`max_stu` > (SELECT count(*)
                                     FROM `table a` `a`
                                     WHERE `a`.`school_code` = `b`.`school_code`);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement