Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL的Join语句_Sql_Mysql_Select_Join - Fatal编程技术网

MySQL的Join语句

MySQL的Join语句,sql,mysql,select,join,Sql,Mysql,Select,Join,我有一个MySQL数据库,其中包含以下表: Table: Professor Attributes: ID, ProfessorName Table: Class Attributes: ID, ClassName, SubjectID (secondary) Table: Subject Attributes: ID, SubjectName Table: DocCategory Attributes: ID, DocCategoryName Table: Document Attri

我有一个MySQL数据库,其中包含以下表:

Table: Professor
Attributes: ID, ProfessorName

Table: Class
Attributes: ID, ClassName, SubjectID (secondary)

Table: Subject
Attributes: ID, SubjectName

Table: DocCategory
Attributes: ID, DocCategoryName

Table: Document
Attributes: ID, DocName, ProfessorID (secondary), ClassID (secondary), DocCategoryID (secondary)
是否有人可以通过查询为我指出正确的方向,以便我得到一份包含以下内容的报告:

所有ProfessorName的列表,第二列包含文档表中与该教授相关的文档计数

提前谢谢

使用:

  SELECT p.professorname,
         COUNT(*) AS numDocuments
    FROM PROFESSOR p
    JOIN DOCUMENT d ON d.professorid = p.id
GROUP BY p.professorname
要查看所有教授的列表(没有文档的教授的列表值为零),请使用:


太好了!但有一件事是,它只显示带文档的教授姓名,我是否可以添加一个语句,在没有关系的旁边显示“0”?
   SELECT p.professorname,
          COALESCE(COUNT(d.id), 0) AS numDocuments
     FROM PROFESSOR p
LEFT JOIN DOCUMENT d ON d.professorid = p.id
 GROUP BY p.professorname