mysql中的一堆多查询
MySQL中关于一堆多查询,是指使用一条SELECT语句从多个表格中查询数据,并且将这些表格联合起来。下面是一些MySQL中一堆多查询的例子。
--查询臭名昭著的黑色猫的信息,包括其所有文章 SELECT cats.name, articles.title, articles.content FROM cats INNER JOIN articles ON cats.id = articles.cat_id WHERE cats.name = 'Black Cat'; --查询所有文章的标题、作者和评论数量 SELECT articles.title, authors.name, COUNT(comments.id) as 'comment_count' FROM articles INNER JOIN authors ON articles.author_id = authors.id LEFT JOIN comments ON articles.id = comments.article_id GROUP BY articles.id; --查询所有科目及其对应的学生数 SELECT subjects.subject_name, COUNT(students.id) as 'student_count' FROM subjects LEFT JOIN student_subject ON subjects.id = student_subject.subject_id LEFT JOIN students ON student_subject.student_id = students.id GROUP BY subjects.id; --查询所有学生及其选课的科目数 SELECT students.name, COUNT(subjects.id) as 'subject_count' FROM students LEFT JOIN student_subject ON students.id = student_subject.student_id LEFT JOIN subjects ON student_subject.subject_id = subjects.id GROUP BY students.id;