据库查询

前面是想要查询什么东西,后面是根据什么查询

举列子的表格
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班     
009    百里守约    男    湖南    21    1班     
010    妲己    女    广东    26    2班    340322199607247654
011    李白    男    北京    30    4班    340322199005267754
012    孙膑    男    新疆    26    3班    340322199000297655
4. 3 查询 
    挑选 所有展示 从 students表中展示
        select * from   students
        挑选  学号 姓名 性别 从学生表中展示   students
        -- select studentNo,name,sex from   students



        -- 查询指定字段
        -- 在select后面的列名部分,可以使用 as为列起别名,这个别名出现在结果集
            给指定字段起别名便于区分
        -- select studentNo  as 学号,name  as 姓名,sex  as 性别 from students


         不写关键字as也是可以实现的
         -- select studentNo  学号1,name 姓名1,sex  性别1 from students
          都用s 来代表这样方便一些后面的直接指定查询s就可以了
        -- select  s.studentNo , s.name , s.sex  from students as   s



        - - 消除重复行

        -- 在select后面列前使用 distinct可以消除重复的行
        -- select distinct 列1,... from 表名;

        -- 查询学生的性别   这样做把重复的全部去掉
        --   select distinct sex from students



        -- 查询学生的班级和性别,不显示重复数据
        挑选,去除重复,挑选字段为性别,班级,从学生表中查找
        --   select distinct sex,class from students

挑选,去重,所有,从,学生表中查找
          select distinct * from students
这样做整个表都可以去除重复了



          比较运算符

        等于:   =
        大于:   >
        大于等于:  >=
        小于:   <
        小于等于:   <=
        不等于:  !=  或 <>
        例1:查询小乔的年龄
        挑选,年龄字段,从,学生表中,的,姓名为‘小乔’查询
          select age from students where name='小乔'

        例2:查询20岁以下的学生
      挑选,*,从,学生表,的年龄<=20 查找
          select * from students where age<=20
        例3:查询家乡不在北京的学生
    挑选,*,从,学生表,的,地址字段不为背景的,全部查找出来
          select * from students where hometown!='北京'

        -- 1、查询学号是'007'的学生的身份证号
         挑选,省份证字段,从,学生表中,的,学号为007;查找出来
        --   select card from students where studentNo='007' 
        -- 2、查询'1班'以外的学生信息
         挑选*,从,学生表中,的,班级字段!=1班的;数据都查找出来
          -- select * from students where class!='1班'
        -- 3、查询年龄大于20的学生的姓名和性别
       挑选 name字段,从学生表中 的 年龄>20 ;全部查找出来
          select name,sex from students where age>20

         逻辑运算符
     并且
          and
    或者
          or
    不等于
          not
            例1:查询年龄小于20的女同学

            年龄小于20且性别为女
         挑选 * 从 学生表中 的 年龄 <20 并且 性别=女
             select * from students where age<20 and sex='女'
            例2:查询女学生或'1班'的学生


            性别为女 或 班级为‘1班’
         挑选 * 从 学生表 的 性别=女 或者 班级=1班 查找出来
             select * from students where sex='女' or class='1班'
            例3:查询非天津的学生
        挑选 * 从 学生表 的地址!=天津 的都查找出来
              select * from students where hometown!='天津'
          挑选 * 从 学生表 的 不等于 地址=天津  全部查找出来
            select * from students where not hometown='天津'


          模糊查询

          like
          %表示任意多个任意字符
         _表示一个任意字符
            例1:查询姓孙的学生
         挑选 * 从 学生表中 的 name 模糊查询 '孙%'  都查找出来
              select * from students where name like '孙%'

            例2:查询姓孙且名字是一个字的学生
         挑选 * 从 学生表中 的name 模糊查询‘孙_’一个字位置 ; 都查找出来
              select * from students where name like '孙_'

            例3:查询叫乔的学生
        挑选 * 从 学生表 的 name 模糊 结尾是乔的; 都查询出来
              select * from students where name like '%乔'

            例4:查询姓名含白的学生
      挑选 * 从 学生表中 的 name 模糊 前面后面有白的;都查询出来
              select * from students where name like '%白%'

         范围查询
        
        in表示在一个非连续的范围内
            例1:查询家乡是北京或上海或广东的学生
         挑选 * 从 学生表 的 地址=背景 或者 地址=上海 或者 地址=广东;都查询出来
             select * from students where hometown='北京' or hometown='上海' or hometown='广东'
            挑选 * 从 学生表 的 地址字段 范围(北京,上海,广东);都查出来
             select * from students where hometown in ('北京','上海','广东')
    
              between ... and ...表示在一个连续的范围内
            例2:查询年龄为18至20的学生
         挑选 * 从 学生表 的 年龄 在 18 并且 20 范围内的;都查询出来
              select * from students where age between 18 and 20
      挑选 * 从 学生表 的 年龄>=18 并且 年龄<=20; 这样也是可以的
              -- select * from students where age>=18 and age<=20

        空判断

        注意:null与''是不同的
        判空is null
            例1:查询没有填写身份证的学生
          挑选 * 从 学生表中 的身份证 是 null ;查询出来
             select * from students where card is null
            例2:查询填写了身份证的学生
      挑选 * 从 学生表中 的 身份 不是 null ;查询出来
              select * from students where card is not null


            此语句实现不了
            -- select * from students where card!=''


        练习题


            -- 1、查询河南或河北的学生
            -- select * from students where hometown='河南' or hometown='河北'
            -- 2、查询'1班'的'上海'的学生
            -- select * from students where class='1班' and hometown='上海'
            -- 3、查询非20岁的学生
            -- select * from students where not age=20

            -- 1、查询姓名为两个字的学生
            -- select * from students where name like '__'
            -- 2、查询姓百且年龄大于20的学生
            -- select * from students where name like '百%' and age>20
            -- 3、查询学号以1结尾的学生
            -- select * from students  where studentNo like '%1'

            -- 1、查询年龄在18或19或22的女生
            -- select * from students where age in(18,19,22) and sex='女'
            -- 2、查询年龄在20到25以外的学生
            -- select * from students where not age between 20 and 25


         排序

        为了方便查看数据,可以对数据进行排序
        语法:
        select * from 表名
        order by 列1 asc|desc,列2   asc|desc,...
        将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
        默认按照列值从小到大排列
        asc从小到大排列,即升序
        desc从大到小排序,即降序

            例1:查询所有学生信息,按年龄从小到大排序
      挑选 * 从 学生表 默认升序 年龄字段 ;全部年龄按着升序查询
             select * from students order by age

            例2: 查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序
              select * from students order by age desc,studentNo
         聚合函数

        为了快速得到统计数据,经常会用到如下5个聚合函数
        count(*)表示计算总行数,括号中写星与列名,结果是相同的
        聚合函数不能在 where 中使用

            例1:查询学生总数
     挑选 总数(*)从 学生表中  ;查询
             select count(*) from students
            -- select count(name) from students
            --  如果count统计某一列时,这列如果有null的值,不会统计
            -- select count(card) from students
            select count(*) from students

            max(列)表示求此列的最大值
            例2:查询女生的最大年龄
     挑选 最大值(年龄)从 学生表 的 性别=女;全部查询出来
             select max(age) from students where sex='女'

            min(列)表示求此列的最小值
            例3:查询1班的最小年龄
      挑选 最小值(年龄)从 学生表 的 班级=1班;的查找;查询
             select min(age) from students where class='1班'

            sum(列)表示求此列的和
            例4:查询北京学生的年龄总和
     挑选 相加(年龄)从 学生表 的 地址=背景;
              select sum(age) from students where hometown='北京'

            avg(列)表示求此列的平均值
            例5:查询女生的平均年龄
     挑选 平均(年龄)从学生表 的 性别=女;
             select avg(age) from students where sex='女'

             select max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄,sum(age) as 年龄总和 from students

            1、查询所有学生的最大年龄、最小年龄、平均年龄
            select max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from students
            2、一班共有多少个学生
            select count(*) from students where class='1班'
            3、查询3班年龄小于18岁的同学有几个
条件写在后面了,前面是所有学生
              select count(*) from students where class='3班' and age<18


          分组

        按照字段分组,表示此字段相同的数据会被放到一个组中
        分组后,分组的依据列会显示在结果集中,其他列不会显示在结果集中
        可以对分组后的数据进行统计,做聚合运算
        语法:
        select 列1,列2,聚合... from 表名 group by 列1,列2...
            例1:查询各种性别的人数
挑选 * 从 学生表中 分组 去 性别
              select * from students group by sex

            例2:查询各种年龄的人数
挑选 数量(*)从 学生表 分组 去 年龄
              select count(*) from students group by age

            一条记录如果性别和班级都相等,分到一组
     挑选 数量(*),性别,班级,从 学生表中 分组 性别 ,班级
              select count(*),sex,class from students group by sex,class

        分组后的数据筛选

        语法:
        select 列1,列2,聚合... from 表名
        group by 列1,列2,列3...
        having 列1,...聚合...
        having后面的条件运算符与where的相同
            例1:查询男生总人数
     挑选 数量(*),性别,从 学生表 分组 去 性别  条件 性别=男
             select count(*),sex from students group by sex having sex='男'
     这个样写也可以
             select count(*) from students where sex='男'


        查询各个班级学生的平均年龄、最大年龄、最小年龄
     挑选 班级,平均(年龄),最大(年龄),最小(年龄)从 学生表 分组 去 班级 ;按照班级分组的意思
          select class,avg(age),max(age),min(age) from students group by class

        查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄
挑选 班级,平均(年龄),最大(年龄),最小(年龄)从 学生表 分组 班级 指向 班级!=1班 ;的查询
         select class,avg(age),max(age),min(age) from students group by class having class!='1班'

          获取部分行

        当数据量过大时,在一页中查看数据是一件非常麻烦的事情
        语法
        select * from 表名
        limit start,count
        从start开始,获取count条数据
        start索引从0开始

            例1:查询前3行学生信息

            select * from students  limit  0,3

            查询第4到第6行学生信息

            select * from students  limit  3,3


            每页显示4条数据

            第一页  (1-1) * 4  select * from students limit 0,4

            第二页    (2-1) * 4 select * from students limit 4,4

            第三页    (3-1) * 4  select * from students limit 8,4


            每页显示5条数据,显示每一页的数据

            select * from students limit 0,5

            select * from students limit 5,5

            select * from students limit 10,5


         连接查询
         
            例1:查询学生信息及学生的成绩

            方式一

                select * from 表1,表2 where 表1.列=表2.列

            select * from students,scores where students.studentNo=scores.studentNo

            select name,score,sc.studentNo from  
            students as stu,scores as sc  
            where stu.studentNo=sc.studentNo

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值