1.sql中将查询到的结果集作为一个表去连接多个结果集查询想要的字段,适用于查询复杂逻辑的字段;
举例:
select dd.base_grade_value,count(distinct dd.userid) from
(select cc.school_id,bb.userid,cc.base_grade_value from
(select a.create_time,a.student_id,a.school_id,c.base_grade_value,b.clzss_id from
q.student a,
q.rel b,
q.school_grade c
where a.id=b.school_student_id and a.school_id=b.school_id and b.grade_id=c.id and b.school_id=c.school_id) cc
join
(select aa.userid from (
select a.userid,a.createtime from input a
where substring(a.createtime,1,7) ='2018-06' )aa
join
ac_student c on aa.userid=c.id
and c.status=1 and substring(c.create_time,1,7)= substring(aa.createtime,1,7) and substring(c.activate_at,1,7)= substring(aa.createtime,1,7) )bb
on bb.userid=cc.student_id)dd join
org_school d on d.id=dd.school_id
and d.school_name not regexp '【.*'
and d.status in(1,3) group by dd.base_grade_value;