老是忘的sql格式,写过几次还是忘的!

1. 

select (select type_name from time_set where id=sa.time_set_id)as time_set_name,"

+"(select sub_type_name from offense where sub_type=sa.attend_type and type=1)as attend_name,sa.*,o.*,s.*,o.score as offense_score from (select * from student_attendance  sa where sa.student_id=? and date_format(sa.attend_time,'%Y-%m-%d')=?)sa left join student s on sa.student_id=s.id "

+"left join (select so.student_id,o.score,o.type,o.sub_type,o.type_name,o.sub_type_name,so.time_set_id from student_offense so,offense o where so.offense_id=o.id)o on sa.student_id=o.student_id and sa.time_set_id = o.time_set_id  limit ?,?";

2.

" select b.*,s.student_name,s.student_no,t.type_name from ("

 +" select a.*,group_concat(sub_type_name      separator   '|')as sub_type_name_s,group_concat(attend_type      separator   '|')as attend_type_s,group_concat(score    separator   '|')as score_s,group_concat(description    separator   '|')as description_s from ("
+" select sa.*,o.sub_type_name,     o.type from (select student_id,attend_type,attend_time,score,time_set_id,description  from student_attendance  sa where sa.student_id=? and date_format(sa.attend_time,'%Y-%m-%d')>? and date_format(sa.attend_time,'%Y-%m-%d')<?)sa left join (select * from offense o where o.school_id =(select school_id from student where id=?))o on o.sub_type = sa.attend_type"
+" union all"
+" select so.student_id,o.sub_type,so.offense_time,o.score,so.time_set_id,so.description,o.sub_type_name ,      o.type from (select * from student_offense so  where so.student_id=? and date_format(so.offense_time,'%Y-%m-%d')>? and date_format(so.offense_time,'%Y-%m-%d')<?)so left join (select * from offense o where o.school_id =(select school_id from student where id=?)) o on o.id = so.offense_id"
+"  )a group by date_format(a.attend_time,'%Y-%m-%d'),time_set_id"
 +"  )b "
+"   left join student s on s.id = b.student_id"

 +"  left join time_set t on t.id = b.time_set_id  ORDER BY b.attend_time desc  limit ?,?";

3.

"SELECT mm.*,nn.warning_score FROM (SELECT (cc.total + cc.downs) / cc.num finallyScore, cc.grade_name,cc.grade_id,ws.type_score,ws.warn_score FROM (SELECT aa.*, IFNULL(down, 0) AS downs FROM ( SELECT count(gs.student_id) num, count(gs.student_id) * 100 AS total, gs.grade_id, g.grade_name FROM grade_student gs LEFT JOIN grade g ON gs.grade_id = g.id WHERE g.school_id = ? GROUP BY gs.grade_id ) aa "

+"LEFT JOIN ( SELECT grade_score.id, SUM(grade_score.total_score) down, grade_score.grade_name FROM ( SELECT s.student_name, sid, total_score, g.grade_name, g.id FROM ( SELECT IFNULL(s1, s2) AS sid, ( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score FROM (SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so,offense o WHERE"
+" so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa RIGHT JOIN ( SELECT sa.student_id AS s2,sum(sa.score) AS attend_score FROM student_attendance sa WHERE date_format(sa.attend_time, '%Y-%m') < ? AND"
+" date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id) o ON o.s2 = sa.s1 UNION SELECT IFNULL(s1, s2) AS sid,( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score FROM ( SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so,offense o WHERE so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? "
+"AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa LEFT JOIN (SELECT sa.student_id AS s2,sum(sa.score) AS attend_score FROM student_attendance sa WHERE date_format(sa.attend_time, '%Y-%m') < ? AND date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id ) o ON o.s2 = sa.s1 ) al LEFT JOIN grade_student gs ON gs.student_id = sid "

+"LEFT JOIN grade g ON g.id = gs.grade_id LEFT JOIN student s ON sid = s.id WHERE g.school_id = ? ) grade_score GROUP BY grade_score.id ) bb ON aa.grade_id = bb.id) cc LEFT JOIN warning_score ws ON cc.grade_id = ws.grade_id) mm LEFT JOIN (SELECT ws.*,ws.warn_score warning_score from warning_score ws WHERE ws.type_score = '2' ) nn ON nn.school_id = ? GROUP BY mm.grade_id limit ? , ?";

4.

"SELECT avd.warning_score, AVG(finallyScore) as avgscore FROM ( SELECT mm.*, nn.warning_score FROM ( SELECT (cc.total + cc.downs) / cc.num finallyScore, cc.grade_name, cc.grade_id, ws.type_score, ws.warn_score FROM ( SELECT aa.*, IFNULL(down, 0) AS downs FROM "

+"( SELECT count(gs.student_id) num, count(gs.student_id) * 100 AS total, gs.grade_id, g.grade_name FROM grade_student gs LEFT JOIN grade g ON gs.grade_id = g.id WHERE g.school_id = ? GROUP BY gs.grade_id ) aa LEFT JOIN ( SELECT grade_score.id, SUM(grade_score.total_score) down, grade_score.grade_name FROM "
+"( SELECT s.student_name, sid, total_score, g.grade_name, g.id FROM ( SELECT IFNULL(s1, s2) AS sid,( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score FROM ( SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so, offense o WHERE so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa "
+"RIGHT JOIN ( SELECT sa.student_id AS s2, sum(sa.score) AS attend_score FROM student_attendance sa WHERE date_format(sa.attend_time, '%Y-%m') < ? AND date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id ) o ON o.s2 = sa.s1 UNION SELECT IFNULL(s1, s2) AS sid, ( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score "
+"FROM ( SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so, offense o WHERE so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa LEFT JOIN ( SELECT sa.student_id AS s2, sum(sa.score) AS attend_score FROM student_attendance sa "
+"WHERE date_format(sa.attend_time, '%Y-%m') < ? AND date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id ) o ON o.s2 = sa.s1 ) al LEFT JOIN grade_student gs ON gs.student_id = sid LEFT JOIN grade g ON g.id = gs.grade_id LEFT JOIN student s ON sid = s.id WHERE g.school_id = ? ) grade_score GROUP BY grade_score.id ) bb ON aa.grade_id = bb.id ) cc "

+"LEFT JOIN warning_score ws ON ws.grade_id = cc.grade_id ) mm LEFT JOIN ( SELECT ws.*, ws.warn_score warning_score FROM warning_score ws WHERE ws.type_score = '2' ) nn ON nn.school_id = ? GROUP BY mm.grade_id) avd limit ?,?";

5.

SELECT mm.*,nn.warning_score FROM (SELECT (cc.total + cc.downs) / cc.num finallyScore, cc.grade_name,cc.grade_id,ws.type_score,ws.warn_score FROM (SELECT aa.*, IFNULL(down, 0) AS downs FROM ( SELECT count(gs.student_id) num, count(gs.student_id) * 100 AS total, gs.grade_id, g.grade_name FROM grade_student gs LEFT JOIN grade g ON gs.grade_id = g.id WHERE g.school_id = ? GROUP BY gs.grade_id ) aa "

+"LEFT JOIN ( SELECT grade_score.id, SUM(grade_score.total_score) down, grade_score.grade_name FROM ( SELECT s.student_name, sid, total_score, g.grade_name, g.id FROM ( SELECT IFNULL(s1, s2) AS sid, ( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score FROM (SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so,offense o WHERE"
+" so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa RIGHT JOIN ( SELECT sa.student_id AS s2,sum(sa.score) AS attend_score FROM student_attendance sa WHERE date_format(sa.attend_time, '%Y-%m') < ? AND"
+" date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id) o ON o.s2 = sa.s1 UNION SELECT IFNULL(s1, s2) AS sid,( IFNULL(attend_score, 0) + IFNULL(offense_score, 0) ) AS total_score FROM ( SELECT so.student_id AS s1, sum(o.score) AS offense_score FROM student_offense so,offense o WHERE so.offense_id = o.id AND date_format(so.offense_time, '%Y-%m') < ? "
+"AND date_format(so.offense_time, '%Y-%m') > ? GROUP BY student_id ) sa LEFT JOIN (SELECT sa.student_id AS s2,sum(sa.score) AS attend_score FROM student_attendance sa WHERE date_format(sa.attend_time, '%Y-%m') < ? AND date_format(sa.attend_time, '%Y-%m') > ? GROUP BY student_id ) o ON o.s2 = sa.s1 ) al LEFT JOIN grade_student gs ON gs.student_id = sid "
+"LEFT JOIN grade g ON g.id = gs.grade_id LEFT JOIN student s ON sid = s.id WHERE g.school_id = ? ) grade_score GROUP BY grade_score.id ) bb ON aa.grade_id = bb.id) cc LEFT JOIN warning_score ws ON cc.grade_id = ws.grade_id) mm LEFT JOIN (SELECT ws.*,ws.warn_score warning_score from warning_score ws WHERE ws.type_score = '2' ) nn ON nn.school_id = ? GROUP BY mm.grade_id order by finallyScore desc limit ? , ?";

猜你喜欢

转载自blog.csdn.net/qq_40237472/article/details/80176290