zoukankan      html  css  js  c++  java
  • mysql常用语句

    创建数据表
    drop table if exists students;
    create table students (
     studentNo varchar(10) primary key,
     name varchar(10),
     sex varchar(1),
     hometown varchar(20),
     age tinyint(4),
     class varchar(10),
     card varchar(20)
    )
    
    准备数据
    insert into students values
    ('001', '王昭君', '', '北京', '20', '1班', '340322199001247654'),
    ('002', '诸葛亮', '', '上海', '18', '2班', '340322199002242354'),
    ('003', '张飞', '', '南京', '24', '3班', '340322199003247654'),
    ('004', '白起', '', '安徽', '22', '4班', '340322199005247654'),
    ('005', '大乔', '', '天津', '19', '3班', '340322199004247654'),
    ('006', '孙尚香', '', '河北', '18', '1班', '340322199006247654'),
    ('007', '百里玄策', '', '山西', '20', '2班', '340322199007247654'),
    ('008', '小乔', '', '河南', '15', '3班', null),
    ('009', '百里守约', '', '湖南', '21', '1班', ''),
    ('010', '妲己', '', '广东', '26', '2班', '340322199607247654'),
    ('011', '李白', '', '北京', '30', '4班', '340322199005267754'),
    ('012', '孙膑', '', '新疆', '26', '3班', '340322199000297655')


    drop table if exists courses;
    create table courses (
     courseNo int(10) unsigned primary key auto_increment,
     name varchar(10)
    );
    
    insert into courses values
    ('1', '数据库'),
    ('2', 'qtp'),
    ('3', 'linux'),
    ('4', '系统测试'),
    ('5', '单元测试'),
    ('6', '测试过程');
    
    drop table if exists scores;
    create table scores (
     id int(10) unsigned primary key auto_increment,
     courseNo int(10),
     studentNo varchar(10),
     score tinyint(4)
    );
    insert into scores values
    ('1', '1', '001', '90'),
    ('2', '1', '002', '75'),
    ('3', '2', '002', '98'),
    ('4', '3', '001', '86'),
    ('5', '3', '003', '80'),
    ('6', '4', '004', '79'),
    ('7', '5', '005', '96'),
    ('8', '6', '006', '80');
    1、关于内连接
    
    select students.name,courses.name,scores.score from courses 
    inner join scores on courses.courseno=scores.courseno
    inner join students on scores.studentNo=students.studentNo
    where courses.name='数据库' and students.name='王昭君'
    以上的含义是查询王昭君的数据库成绩,显示姓名、课程名,成绩
    
    select students.name,courses.name,scores.score from courses 
    inner join scores on courses.courseno=scores.courseno
    inner join students on scores.studentNo=students.studentNo
    where students.sex=''
    order by scores.score desc
    limit 1
    查询男生中最高成绩,显示姓名、课程名,成绩
    
    2、关于左连接(join前面查询的结果作为左表,左连接就表示显示左表的全部记录,而右表不存在的数据用null填充)
    
    select * from students
    left join scores on scores.studentNo=students.studentNo
    以上的含义是查出所有学生的成绩,包括没有成绩的学生。
    
    select * from students
    left join scores on scores.studentNo=students.studentNo
    left join courses on scores.courseNo=courses.courseNo
    以上的含义是查询所有学生的成绩,包括没有成绩的学生,需要显示课程名
    
    3、关于右连接(join前面查询的结果作为左表,右连接就表示显示右表的全部记录,而左表不存在的数据用null填充)
    
    select * from scores
    right join courses on scores.courseNo=courses.courseNo
    以上的含义是查出所有课程的成绩,包括没有成绩的课程
    
    select * from scores
    right join courses on scores.courseNo=courses.courseNo
    right join students on students.studentNo=scores.studentNo
    查询所有课程的成绩,包括没有成绩的课程,包括学生信息
    
    4、标量子查询---括号内子查询的结果只能有一列一行
     select * from scores where studentNo=(select studentNo from students where name='王昭君')
    and courseNo=(select courseNo from courses where name='数据库')
    以上的含义是查出王昭君的数据库成绩。
    可以认为是select * from scores where studentNo='001'  and  courseNo='1'
    
    5、列子查询---括号内子查询的结果有一列多行
    select * from scores where studentNo in (select studentNo from students where sex='' and age='18')
    以上的含义是查询18岁男生成绩,要求显示成绩。
    可以认为是select * from scores where studentNo in ('002')
    
    6、表子查询---括号内子查询的结果有多行多列(其实是一个表)
    select * from scores
    inner join (select * from courses where name in('数据库','系统测试')) as c on scores.courseno=c.courseno
    以上的含义是查询数据库跟系统测试的成绩
    1、统计每个班级中每种性别的学生人数,并按照班级降序排序(统计每个/每种)就需要用分组group by
    select class,sex,count(*) from students group by class,sex order by class  desc
    
    
    2、查询学生表中百里守约或百里玄策的信息
    select * from students where name='百里守约' or name='百里玄策'
    
    3、查询1班学生中的最大年龄中是多少
    select max(age) from students where class='1班'
    
    4、查询2班男女生各有多少人
    select class,sex,count(*) from students where class='2班' group by sex
    
    5、查询价格大于或等于‘超极本’价格的商品,并按价格降序排序
    select * from goods where price >=any (select price from goods where cate='超级本') order by price desc
    
    如果是列子查询,那么=   <     >    <=    >=  这些符号后面还要加关键字(any some all in)
    any 表示可以查出比括号内子查询的任意一个结果都大的记录。
    some 等同于any。
    all 表示可以查出比括号内子查询的最大值还要大的记录。 
    
    

    drop table if exists courses;create table courses ( courseNo int(10) unsigned primary key auto_increment, name varchar(10));
    insert into courses values('1', '数据库'),('2', 'qtp'),('3', 'linux'),('4', '系统测试'),('5', '单元测试'),('6', '测试过程');
    drop table if exists scores;create table scores ( id int(10) unsigned primary key auto_increment, courseNo int(10), studentNo varchar(10), score tinyint(4));insert into scores values('1', '1', '001', '90'),('2', '1', '002', '75'),('3', '2', '002', '98'),('4', '3', '001', '86'),('5', '3', '003', '80'),('6', '4', '004', '79'),('7', '5', '005', '96'),('8', '6', '006', '80');

  • 相关阅读:
    2020软件工程作业00——问题清单
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
    2020软件工程作业00
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/huaniaoyuchong/p/13931649.html
Copyright © 2011-2022 走看看