效果图
要查询出如上图的效果
知识点.
1.多表嵌套查询.
2.输出查询结果,group_concat函数
3.关联查询
SELECT t1.学校,
CASE
WHEN t1.年级 = 2017 THEN
'1年级'
WHEN t1.年级 = 2016 THEN
'2年级'
WHEN t1.年级 = 2015 THEN
'3年级'
WHEN t1.年级 = 2014 THEN
'4年级'
WHEN t1.年级 = 2013 THEN
'5年级'
WHEN t1.年级 = 2012 THEN
'6年级'
ELSE
'错误'
END "年级",
t1.班级,s1.总计'全班人数',count(if(isnull(so.search),t1.姓名,null))'未交人数',
case when isnull(group_concat(if(isnull(so.search),t1.姓名,null))) then '已交齐' else group_concat(if(isnull(so.search),t1.姓名,null)) end '未交费学生'
FROM
(
SELECT
u.tel "监护人手机号",
u.id,
si.学校,
si.姓名,
si.识别码,
si.年级,
si.班级
FROM
(
SELECT
s.NAME "学校",
stu.studentName "姓名",
stu.studentId "识别码",
stu.studentSession "年级",
stu.studentClass "班级"
FROM
met_student stu,
met_school s
WHERE
SUBSTRING(stu.studentId, 1, 6) = s.activation_code
AND s.name LIKE '%贵阳市第二十四中学%'
ORDER BY stu.studentSession,stu.studentClass
)as si LEFT JOIN met_user u ON si.识别码 = u.studentid
-- and u.tel!=110,在多用户绑定同一识别码时,将额外的用户tel字段添加110作为标识,以排除重复人员。
and u.tel!=110 ) as t1 LEFT JOIN met_shopv2_order so ON t1.id=so.uid
AND
so.search LIKE '%五月份%'
AND so.state in (2,4) ,
(select
count(stu.studentName)"总计",