数据库基础查询

102 阅读1分钟

「这是我参与2022首次更文挑战的第3天,活动详情查看:2022首次更文挑战」。

1.基础数据库查询

      concat拼接有null数据全为null
      Desc显示表的结构
      Distinct不重复

2.进阶查询

条件查询       
      select
      查询列表
      from
      表命
      where
    筛选条件;

3.分类

    3.1按条件表达式筛选
    运算符:> < = != >= <=
    例:select *
    from hotel
    where
         room_id=5;
    3.2按逻辑查询
    运算符:|| && !
    与:一假必须假(and) 或:一真必真(or) 非:取反()
    例:select *
        from lianxi
        where
       not( 条件1 and 条件
   3.3模糊查询
   3.31 like
    一般和通配符(%任意字符 _代表单个字符)搭配使用
      例:select*
    from hotel
    where like '%a%'//包含a的所有数据 %代表通配符
    select *
    from hotel
    where 
    like '_$_%'ESCAPE'$'
    output:第二个字符为_的结果($escape '$'代表转意取消特殊意思)
    3.32 between and
    (包含临界值>=.<=)等价于[a,b]
    select *
    from hotel
    where
     hotel_id between 99 and 111 
    3.33 in
    判断某字段值是否属于in列表(同类型或者兼容)中的一项
    不支持通配符
     select last_name,hotel_id
     from hotel
     where
     last_name in(‘张华’,‘李四)
     output:
    is null或者is not null
    null不能用<>=来判断
    查询有什么值和没有什么值,
    安全等于可以判断<=>任意值
    例:select last_name,time
    from hotel
    wehere time is null
   output:timenull的last_name
   面试:select *from hotel和select * from hotel where hotel_id like'%%'and last_name like '%%'得到的结果一样吗
   不一样
   可能会存在null值;第二种无法显示存在null

4.排序查询

   基本语法
   select 查询列表
   fromwhere
   oder by 排序列表【desc||asc】(默认升序)
   ex1:查询费用并且按照费用降序显示
   select *,time*12*(1-ifnull(count,0))费用
   from hotel
   oder by 费用 desc
   ex2:按长度升序或者降序显示某表的信息
   select length(time*12*(1-ifnull(count,0)))
   from hotel
   oder by length(time*12*(1-ifnull(count,0))) 
   一般Oder by放在最后,limit语句除外
   总结:这是最近回忆学习的一些数据库查询方面的基础知识,希望可以和大家多交流沟通。加油!!