假设 表a(id,班级名,班主任)班级与表b(id,aid,学生名称,地址),
方式1:在列中进行查询 (效率低)
select
a.* ,
(select count(*) from b where a.id =b.id) as 学生人数
from a
方式2:在关联表查询 (比方式1快)
select
a.*,
b.学生人数
from a ,(select id ,count(id) as 学生人数 from b group by id) as b
假设 表a(id,班级名,班主任)班级与表b(id,aid,学生名称,地址),
方式1:在列中进行查询 (效率低)
select
a.* ,
(select count(*) from b where a.id =b.id) as 学生人数
from a
方式2:在关联表查询 (比方式1快)
select
a.*,
b.学生人数
from a ,(select id ,count(id) as 学生人数 from b group by id) as b