Mysql数据库入门---数据查询常用方法(包含条件查询和排序查询)

Mysql数据库入门—数据查询

Author:qyan.li

Date:2022.2.21



1. select关键字

select意为选择之意,可以借助于select关键字完成数据库内容的查询

  • 借助于关键字查询

    基本用法:select 查询列表 from 表名

    # 数据查询
    select 100; # 查询变量100
    select 1000 + 100# 查询表达式
    
    # 数据库字段查询
    select owner from student_information.pet # 查询单个字段
    slelect owner,name from student_information.pet # 查询多个字段
    select * from student_information.pet # 查询所有字段
    
  • 借助于查询函数查询

    查询函数可以辅助用户完成数据库、用户基本情况的查询

    select now(); # 显示当前时间
    select user(); # 用户名和ip
    select version(); # 显示Mysql版本
    select database(); # 显示数据库
    
  • 小Tips:

    1. 字段别名:

      select name as 姓名 from student_information.pet;上述语句表明为name起别名为姓名

      • 此处的as可以简略,直接表示为select name 姓名 from student_information.pet;

      • 别名可以与其他sql结合形成更加复杂的语句

        select now() as 当前时间

        select name 宠物名,owner as 主人 from student_information.pet;

    2. 去除重复数据:

      数据库应用中,某些时刻会需要提炼出某字段中所有不相重复的数据,此时需要distinct关键字tinct

      select name from student_information.pet;
      select distinct name from student_information.pet;
      
2.条件查询

数据库查询过程中,经常会遇到筛选出满足特定条件的数据,此时会需要where关键字,后加筛选条件完成数据的查询,一般形式为select 字段名 from 数据表名称 where 筛选条件

  • Mysql运算符:

    Mysql中运算符一般包括 1. 算术运算符 2.比较运算符 3. 逻辑运算符 4.位运算符

    • 算术运算符:

      加减乘除(除法借助于/或者div表示)、取余(%或者mod表示)比较运算符:

    • 比较运算符:

      小于、大于、小于等于、大于等于较为常规

      不等于(借助于<>或者!=表示)

      为空或者不为空(is null 或者 is not null)

      在两值之间(between a and b)

      是否在集合中(in 或者 not in)

      是否模糊匹配(like或者not like)

      正则表达式匹配regexp

    • 逻辑运算符:

      与或非(and(&&) or(||) not(!))

      异或(xor)

    • 位运算符:

      应用并不是非常广泛,不做讨论

    简单示例:

    select owner from student_information.pet where name = 'liqiyan';
    select gnp/gnpold,name from country where gnp/gnpold > 1.1;
    select * from student_information.pet where not(name = 'liqiyan' and sex = 'm');
    
  • 运算符筛选查询:

    运算符筛选种类、组合多样,可根据需要自行组合,此处仅介绍重难点筛选方法

    • 模糊匹配查询

      模糊匹配查询一般借助于like(not like)和通配符进行实现

      通配符主要包含两种_表示任意单个字符%表示任意多个字符

      简单示例:

      # 查询国家名称中包含字母INA
      select * from world.country where name like '&INA%'
      # 查询国家名称中最后一个字母为IA
      select name from world.country where name like '%IA'
      # 查询国家名称中第一个字母为CH
      select * from world.country where name like 'CH%'
      # 查询国家名称中第三个字母为_的记录(借助于转义字符完成)
      select name from world.country where name like '__\_%'
      # 转义字符一般借助于\进行表示,但是该字符可以自定义,借助于escape关键字
      select name from world.country where name like '__#_%' escape '#';
      
    • 关键字查询

      此处提及的关键字,主要包含上述运算符介绍中提及的between and 和 is null 和 in(not in)等等

      1. between and关键字

        # 查询预期寿命在80-85岁之间的国家和地区
        select name,LifeExpectancy from world.country where LifeExpectancy >= 80 and LifeExpectancy <= 85;
        select name,LifeExpectancy from world.country where LifeExpectancy between 80 and 85;
        
      2. 集合查询in(not in)关键字

        # 查询独立年份在1971,1981和1991年的国家
        select name,IndepYear from world.country where IndepYear = 1971 or IndepYear = 1981 or IndepYear = 1991;
        select name,IndepYear from world.country where IndepYear in (1971,981,1991);
        
      3. 判断是否为空(is null或者is not null)

        select * from world.country where IndepYear is null;
        

        此处需要提及null和空字符串的区别:

        二者是不等价的两个概念,null更像是一个占位,表示什么都没有,而空字符串表示此处包含有内容,只不过内容为空而已。

        select name,HeadOfState from world.country where HeadOfState = '';
        select name,HeadOfState from world.country where HeadOfState is null;
        
        • 安全等号(<=>不必区分is和普通等号的区别,即在此处空字符串和null可以混用)

          select name,HeadOfState from world.country where HeadOfState <=> '';
          select name,HeadOfState from world.country where HeadOfState <=> null;
          
3.排序查询

在数据库查询操作中,经常会遇到数据需要按照一定的规则进行排序,此时会应用到sql语句中的排序查询关键字order by,结合asc和desc可以实现降序、升序的排列,一般语法规则为select <查询列表> from <数据表名称> where <筛选条件> order by 排序列表

在排序中,存在asc和desc两种排序规则,分别为升序和降序,默认情况下为降序

简单示例:

# 按照人口进行降序排列,显示国家名称
select name,population from world.country order by population desc;
# 按照GDP增长的比例升序排列
select name,GNP,GNPOld,GNP/GNPOld GNP增长率 from world.country order by GNP增长率 asc;
  • 小Tips:

    1. 结合函数可以实现更高级别的排序

      select name,length(name) as 字符个数 from world.country order by 字符个数;

    2. 排序过程中可以限制多条件完成排序

      select name,length(name) as 字符个数 from world.country order by 字符个数,Population desc;

    3. 排序可以直接指定列号进行排序

      select * from world.country order by 2 desc;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值