记一次sql查询

效果图

要查询出如上图的效果

知识点.

1.多表嵌套查询.

2.输出查询结果,group_concat函数

3.关联查询

<!--表3,使用group_concat( t1.姓名 ) 输出姓名-->

SELECT
	t1.学校,
	t1.年级,
	t1.班级,
	group_concat( t1.姓名 ) '未交费学生' 
FROM
<!--嵌套表2,关联查询表1.使用left join 保持表1的完整性,查询出监护人的信息-->
      (
SELECT
	u.tel "监护人手机号",
	u.id,
	si.学校,
	si.姓名,
	si.识别码,
	si.年级,
	si.班级 
FROM
	(
	<!--嵌套表1,查询全体学生(含未注册)的姓名,学校,识别码.-->
SELECT
	s.NAME "学校",
	stu.studentName "姓名",
	stu.studentId "识别码",
CASE
	
	WHEN stu.studentSession = 2017 THEN
	'1年级' 
	WHEN stu.studentSession = 2016 THEN
	'2年级' 
	WHEN stu.studentSession = 2015 THEN
	'3年级' 
	WHEN stu.studentSession = 2014 THEN
	'4年级' 
	WHEN stu.studentSession = 2013 THEN
	'5年级' 
	WHEN stu.studentSession = 2012 THEN
	'6年级' ELSE '错误' 
	END "年级",
	stu.studentClass "班级" 
FROM
	met_student stu,
	met_school s 
WHERE
	SUBSTRING( stu.studentId, 1, 6 ) = s.activation_code 
	AND s.NAME LIKE '%尚义%' 
	) AS si
	LEFT JOIN met_user u ON si.识别码 = u.studentid 
	) AS t1
	LEFT JOIN met_shopv2_order so ON t1.id = so.uid 
	AND so.search LIKE '%五月份%' 
	AND so.state IN ( 2, 4 ) 
WHERE
	isnull( so.search ) 
GROUP BY
  t1.`学校`,
	t1.年级,
	t1.班级 
ORDER BY
	t1.年级,
t1.班级

猜你喜欢

转载自blog.csdn.net/ljxzdn/article/details/80177659