zoukankan      html  css  js  c++  java
  • 数据库MySQL--条件查询/排序查询

    一、条件查询

    条件查询:满足条件的字段被筛选出来

    语法:select 查询列表字段 from 表名 where 筛选条件;

    条件查询的条件分类:

      1.按条件表达式筛选:条件运算符:>, <, =, !=, >=, <= , <>(不等于)

      2.按逻辑表达式筛选:逻辑运算符:&&(and), ||( or ) ,!( not )

      3.模糊查询:like, between and, in, is null

    模糊查询使用:

    like:

      select * from 表名 where 字段名 like '_a%';   # 查询字段里包含a字母的

      (ike 一般和通配符搭配使用:% 任意多个字符,包含0个字符, _  任意单个字符)

      (注:当要查询包含特殊符号(&,_)时,可用转义字#符   , 也可使用 escape 关键词)

      例:

        ...........

          where last_name like '_$_%' escape '$';  # 这里指定 $ 为转义符

    between..and:

      select * from 表名 where 字段名 between 100 and 120;    # 这里表示是查询的大于100,小于120 的值

      (注:使用between..and..则表示包含两个值的临界值,并且临界值不能调换位置)

    in:判断某字段的值是否属于in列表中的某一项

      select * from 表名 where 字段值 IN()

      (注:in 列表的值类型必须兼容(相同))

    is null / is not null:

      例:.........

          where 字段名 is null;   # 判断字段值是null 的

          where 字段名 is not null; # 判断字段值不为null的

      (注:=或<> 不能用于判断null值,所以出现了is null关键字)

    安全等于(<=>):

      可以用于判断null值,或判断是否等于,若等于则返回true

    一、排序查询

    语法:

    select 查询列表

    from 表

    where 筛选条件(可限制可不限制)

    order by 排序列表  (asc<升序>| desc<降序>)(若不写默认为升序)

    (注1:order by 后可支持单个字段,多个字段,表达式,函数等)

    (注2:order by 子句一般是放在查询语句的最后面,limit子句除外)

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/Vera-y/p/10919299.html
Copyright © 2011-2022 走看看