How to count rows in one table based on another table in mysql

后端 未结 4 1207
慢半拍i
慢半拍i 2021-01-19 18:17

I have two tables in a MySQL database. The first one has a list of department names.

departments
    abbreviation | name
    -------------|-------------
             


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-19 18:40

    I quick solution, but not the best, could be:

    SELECT abbreviation, 
    (SELECT COUNT(*) FROM courses C WHERE D.abbreviation = SUBSTRING(C.abbreviation, 0, 3)) AS c 
    FROM departments D;
    

提交回复
热议问题