zoukankan      html  css  js  c++  java
  • mysql 连左查询

     1 create table teacher(
     2      tid int unsigned auto_increment primary key,
     3      tname varchar(30) not null
     4 )engine=myisam charset =utf8;
     5 insert into teacher values(null,'王老师');
     6 insert into teacher values(null,'李老师');
     7 insert into teacher values(null,'方老师');
     8 select * from teacher;
     9 -- 按照降序排序
    10 select * from teacher order by tid desc;
    11 -- 按照升序排列
    12 select * from teacher order by convert (tname using gbk) asc;
    13 create table student(
    14 sid int unsigned auto_increment primary key,
    15 sname varchar(10),
    16 sgender enum('','','保密'),
    17 sscore tinyint unsigned,
    18 sdept varchar(50),
    19 stid int unsigned  --  必须和老师的tid要用一致的类型
    20 )engine=myisam auto_increment=201501 charset utf8;
    21 insert into student values (null,'李四','',90,'法学',1);
    22 insert into student values (null,'李二','',null,'法学',1);
    23 insert into student values (null,'王二','',50,'法学',3);
    24 select * from student;
    25 -- 统计总共有多少人数
    26 select count(*) 人数 from student;
    27 -- 统计总共有多少男生人数
    28  select count(*) 男生人数 from student where sgender ='';
    29  -- 统计分数为空的学生有多少人
    30  select count(*) from student where sscore is null;
    31  -- 所有学生的平均,最高分,最低分, 成绩
    32  select count(*) 总人数 ,avg(sscore) 平均分 ,max(sscore),min(sscore)from student;
    33  -- 统计各专业的人数
    34 select sdept 专业,count(*) 人数,max(sscore) 最高分,min(sscore) 最低分
    35 from student where sgender='' group by sdept order by avg(sscore) desc;
    36 -- 清除重复的专业从student表里面
    37 select distinct sdept from student;
    38 -- 查找最高分和最低分
    39 select * from student where sscore in(select max(sscore)from student)
    40 union
    41 select * from student
    42 select * from teacher;
    43 where sscore in(select min(sscore)  from student)
    44 --  -- 显示学生学号 姓名 成绩 代课老师姓名
    45 select sid,sname,sscore,tname  代课老师  from student,teacher where stid = tid;
    46 -- 连左查询  和上面同样的效果
    47 select sid,sname,sscore,tname from student left join teacher on stid=tid;
    48 -- 显示老师id 姓名 学生 成绩
    49 select tid,tname ,sname,sscore from teacher lef join student on stid =tid
    50  order by tname asc;
    51 
    52 
    53 
    54  
    55  
  • 相关阅读:
    程序员书单合集,持续整理中
    informatica9.5.1后最一步出错(ICMD_10033,INFACMD_10053)
    Informatica9.5.1配置域名错误(ICMD_10033,INFASETUP_10002,RSVCSHARED_00021)
    程序员书单_UML篇
    程序员书单_J2EE专题
    程序员书单_求职面试
    程序员书单_java专项进阶篇
    程序员书单_HTML篇
    程序员书单_数据结构和算法篇
    程序员书单_HeadFirst系列
  • 原文地址:https://www.cnblogs.com/lsr111/p/4465088.html
Copyright © 2011-2022 走看看