1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
| select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a join score b on a.s_id=b.s_id and b.c_id='01' left join score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score
select a.*,b.s_score as 01_score,c.s_score as 02_score from student a,score b,score c where a.s_id=b.s_id and a.s_id=c.s_id and b.c_id='01' and c.c_id='02' and b.s_score>c.s_score
select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING avg_score >=60;
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b left join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING avg_score <60 union select a.s_id,a.s_name,0 as avg_score from student a where a.s_id not in ( select distinct s_id from score);
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from student a left join score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name;
select count(t_id) from teacher where t_name like '李%';
select a.* from student a join score b on a.s_id=b.s_id where b.c_id in( select c_id from course where t_id =( select t_id from teacher where t_name = '张三'));
select * from student c where c.s_id not in( select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三'));
select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';
select a.* from student a where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02')
select s.* from student s left join Score s1 on s1.s_id=s.s_id group by s.s_id having count(s1.c_id)<(select count(*) from course)
select * from student where s_id not in( select s_id from score t1 group by s_id having count(*) =(select count(distinct c_id) from course))
select * from student where s_id in( select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id='01') );
SELECT Student.* FROM Student WHERE s_id IN (SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(s_id) = ( #下面的语句是找到'01'同学学习的课程数 SELECT COUNT(c_id) FROM Score WHERE s_id = '01' ) ) AND s_id NOT IN ( #下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们 SELECT s_id FROM Score WHERE c_id IN( #下面的语句是找到‘01’同学没学过的课程 SELECT DISTINCT c_id FROM Score WHERE c_id NOT IN ( #下面的语句是找出‘01’同学学习的课程 SELECT c_id FROM Score WHERE s_id = '01' ) ) GROUP BY s_id ) #下面的条件是排除01同学 AND s_id NOT IN ('01')
SELECT t3.* FROM ( SELECT s_id, group_concat(c_id ORDER BY c_id) group1 FROM score WHERE s_id <> '01' GROUP BY s_id ) t1 INNER JOIN ( SELECT group_concat(c_id ORDER BY c_id) group2 FROM score WHERE s_id = '01' GROUP BY s_id ) t2 ON t1.group1 = t2.group2 INNER JOIN student t3 ON t1.s_id = t3.s_id
select a.s_name from student a where a.s_id not in ( select s_id from score where c_id = (select c_id from course where t_id =( select t_id from teacher where t_name = '张三')));
select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from student a left join score b on a.s_id = b.s_id where a.s_id in( select s_id from score where s_score<60 GROUP BY s_id having count(1)>=2) GROUP BY a.s_id,a.s_name
select a.*,b.c_id,b.s_score from student a,score b where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC;
select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文, (select s_score from score where s_id=a.s_id and c_id='02') as 数学, (select s_score from score where s_id=a.s_id and c_id='03') as 英语, round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC;
SELECT a.s_id,MAX(CASE a.c_id WHEN '01' THEN a.s_score END ) 语文, MAX(CASE a.c_id WHEN '02' THEN a.s_score END ) 数学, MAX(CASE a.c_id WHEN '03' THEN a.s_score END ) 英语, avg(a.s_score),b.s_name FROM Score a JOIN Student b ON a.s_id=b.s_id GROUP BY a.s_id ORDER BY 5 DESC
select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2), ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率, ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率, ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率, ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率 from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name
select a.s_id,a.c_id, @i:=@i +1 as i保留排名, @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名, @score:=a.s_score as score from ( select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s
(select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank FROM score t1 where t1.c_id='01' order by t1.s_score desc) t1) union (select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank FROM score t1 where t1.c_id='02' order by t1.s_score desc) t2) union (select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank FROM score t1 where t1.c_id='03' order by t1.s_score desc) t3)
select a.s_id, @i:=@i+1 as i, @k:=(case when @score=a.sum_score then @k else @i end) as rank, @score:=a.sum_score as score from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a, (select @k:=0,@i:=0,@score:=0)s
select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a left join score b on a.c_id=b.c_id left join teacher c on a.t_id=c.t_id GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;
select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id='01' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id='02' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id='03' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3;
select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from score a left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`, ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)b on a.c_id=b.c_id left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`, ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)c on a.c_id=c.c_id left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`, ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)d on a.c_id=d.c_id left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`, ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)e on a.c_id=e.c_id left join course f on a.c_id = f.c_id
select a.s_id, @i:=@i+1 as '不保留空缺排名', @k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名', @avg_score:=avg_s as '平均分' from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b;
select a.s_id,a.c_id,a.s_score from score a left join score b on a.c_id = b.c_id and a.s_score<b.s_score group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3 ORDER BY a.c_id,a.s_score DESC
select c_id,count(s_id) from score a GROUP BY c_id
select s_id,s_name from student where s_id in( select s_id from score GROUP BY s_id HAVING COUNT(c_id)=2);
select s_sex,COUNT(s_sex) as 人数 from student GROUP BY s_sex
select * from student where s_name like '%风%';
select a.s_name,a.s_sex,count(*) from student a JOIN student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex GROUP BY a.s_name,a.s_sex
select s_name from student where s_birth like '1990%'
select c_id,ROUND(AVG(s_score),2) as avg_score from score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC
select a.s_id,b.s_name,ROUND(avg(a.s_score),2) as avg_score from score a left join student b on a.s_id=b.s_id GROUP BY s_id HAVING avg_score>=85
select a.s_name,b.s_score from score b join student a on a.s_id=b.s_id where b.c_id=( select c_id from course where c_name ='数学') and b.s_score<60
select a.s_id,a.s_name, SUM(case c.c_name when '语文' then b.s_score else 0 end) as '语文', SUM(case c.c_name when '数学' then b.s_score else 0 end) as '数学', SUM(case c.c_name when '英语' then b.s_score else 0 end) as '英语', SUM(b.s_score) as '总分' from student a left join score b on a.s_id = b.s_id left join course c on b.c_id = c.c_id GROUP BY a.s_id,a.s_name
select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id left join student a on a.s_id=c.s_id where c.s_score>=70
select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id where a.s_score<60
select a.s_id,b.s_name from score a LEFT JOIN student b on a.s_id = b.s_id where a.c_id = '01' and a.s_score>80
select count(*) from score GROUP BY c_id;
select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三' select MAX(s_score) from score where c_id='02' select a.*,b.s_score,b.c_id,c.c_name from student a LEFT JOIN score b on a.s_id = b.s_id LEFT JOIN course c on b.c_id=c.c_id where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三') and b.s_score in (select MAX(s_score) from score where c_id='02')
select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score
select a.s_id,a.c_id,a.s_score from score a where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id
select c_id,count(*) as total from score GROUP BY c_id HAVING total>5 ORDER BY total,c_id ASC
select s_id,count(*) as sel from score GROUP BY s_id HAVING sel>=2
select * from student where s_id in( select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course))
select s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - (case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age from student;
select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth) select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d')) select WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))
select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth)
select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(s_birth)
select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(s_birth)
|