zoukankan      html  css  js  c++  java
  • mysql数据库(二):查询(SELECT)

    一. 数据库查询—查询(SELECT)

    单表查询

    多表联合查询

    二. 查询—单表查询

    查询特定字段:

    select <字段1,字段2,...> from <表名>;

    示例:查询学生表里所有学生的id和名字

    select id, name from student;

    查询所有字段:

    select * from <表名>;

    示例:查询学生表的所有学生信息

    select * from student;

    按条件进行查询:

    select ... from <表名> where 表达式A and 表达式B and ...;

    示例:查询id为1401001的学生信息

    select * from student where id=1401001;

    示例:查询学生表里class_id为1401而且sex为女的学生

    select * from student where class_id=1401 and sex='女';

    对结果集进行排序:

    select ... from <表名> where ... order by 字段A asc(desc);

    select ... from <表名> where ... order by 字段A asc(desc), 字段B asc(desc);

    asc:升序

    desc:降序

    示例:查询学生表里class_id为1401而sex为女的学生并按学生编号升序(降序)排列

    select * from student where class_id=1401 and sex='女' order by id asc;

    select * from student where class_id=1401 and sex='女' order by id desc;

    课堂练习

    1. 查询学生表(student)学生编号id为1403001的学生信息

    select * from student where id=1403001;

    2. 查询分数表(score)的学生编号为(student_id)为1403001的分数成绩

    select score from score where student_id=1403001;

    三. 查询—多表联合查询

    问题一:

      对于不同类型的信息该怎么存储呢?是放一个表呢,还是不同类型数据放不同表

      放在一个表的缺点:思考(仓库里的货物是杂乱的堆在一起好呢,还是分类存储好一些?哪个更利于我们去找一件货物?)

      1. 字段过多不好维护

      2. 表数据庞大

      3. 数据不好维护

      4. 数据冗余,重复数据多

    解决方案:

      不同类型数据放不同表,学生信息放在student表,成绩放在score表

    问题二:

      既然不同的数据放在了不同的表,比如:学生的信息以及学生的成绩分别放在了student表和score表,那如果要查询某个学生的信息以及他的成绩,那sql要怎么写?

    解决方案:

      设计表的时候两个表之间维持一个关联即可

    举例说明:

      去超市买东西时将东西存在储物柜,离开时,取东西的时候就是拿个票据去找柜子,然后再拿到柜子里的东西,也就是说找柜子拿里面的东西是需要根据一个票据信息的。那既然这样,咱们成绩表如果保存了这样一个票据信息,比如说,保存了学生的id,那这样就建立了一个关联关系。到时候我们要查询一个学生的姓名,性别,以及他的成绩,那我们就可以先根据student表里找到他的姓名和性别,然后再根据这个id去score表找到他的成绩

     

    关联查询的语法:

    select t1.字段A, t1.字段B, t2.字段C, t2.字段D, t2.字段E...from <表1名字> t1, <表2名字> t2,...,<表n的名字> tn where 关联条件

    示例:查询id为1403001的同学的名字和性别以及她的各科成绩

    分析:

    1. 涉及表:学生表(student)、成绩表(score)

    2. 查询的字段:学生表(name, sex),成绩表(score)

    3. 关联关系:student.id = score.student_id

    4. 过滤条件:student.id=1403001

    select t1.name, t1.sex, t2.score from student t1, score t2 where t1.id=t2.student_id and t1.id=1403001;

    select t1.name, t1.sex, t2.score from student t1, score t2 where t1.id=t2.student_id and t1.id=1403001;

    拓展:

    select t1.name, t1.sex, t3.name, t2.score from student t1, score t2, course t3 where t1.id=t2.student_id and t2.course_id=t3.id and t1.id=1403001;

    select t1.name, t1.sex, t2.score, t3.name from student t1, score t2, course t3 where t1.id=t2.student_id and t2.course_id=t3.id and t1.id=1403001;

    我们要进行查询的表之间的关系

    1. 学生表(student)跟班级表(class)关联,关联字段(student.class_id=class.id)

    2. 班级表(class)跟年级表(grade)关联,关联字段(class.grade_id=grade.id)

    3. 学生表(student)跟成绩表(score)关联,关联字段(score.student_id=student.id)

    4. 成绩表跟课程表关联,关联字段(score.course_id=course.id)

    四. 课堂练习

    1. 查询出"高一年级"下面的所有班级的信息;

    select t2.* from grade t1, class t2 where t1.id=t2.grade_id and t1.name='高一年级';

    2. 查询出"高一年级"下面的所有班级里面的学生信息(学生的姓名,学号,性别,住址);

    select t3.name,t3.id,t3.sex,t3.address from grade t1,class t2,student t3 where t1.id=t2.grade_id and t2.id=t3.class_id and t1.name='高一年级';

  • 相关阅读:
    Debian apt-get 无法补全
    Python 字典排序
    Python 替换字符串
    Debian 7 64位安装 wine
    Python Virtualenv 虚拟环境
    ASP.NET MVC ModelState
    Oracle存储过程写法
    利用ODBC从SQLServer向Oracle中导数据
    web自定义控件UserControl
    工作笔记
  • 原文地址:https://www.cnblogs.com/my_captain/p/9445559.html
Copyright © 2011-2022 走看看