【发布时间】:2018-12-12 10:19:22
【问题描述】:
我有三张桌子School, Grade, Student。表Grade 有一个Foreign Key 列s_id 引用Primary Key 列id 的School 和表Student 有一个Foreign Key 列g_id 引用@987645332@ 列@987643331@ @ 表Grade。我需要选择school_name,身高低于170或高于180的学生人数。现在,我这样写选择查询。
SELECT School.name, School.id as sid,
(Select count(*) from Student inner join Grade on
Student.g_id=Grade.id inner join School on
Grade.s_id=School.id where School.id=sid and
Student.height < 170) as under_170,
(Select count(*) from Student inner join Grade on
Student.g_id=Grade.id inner join School on
Grade.s_id=School.id where School.id=sid and Student.height > 180) as over_180
from School
我必须为每一列使用join。有更好的方法吗?
【问题讨论】: