• <<SQL必知必会>> -- 笔记2


    按照书的指南,选用MySQL作为DBMS,在使用例句之前需要先创建表
    在官网下载的脚本在MAC上运行有报ASC字符的错,新建文件复制后解决
    以下是去掉注释的SQL脚本
    百度网盘链接
    密码:okgi

    检索数据

    SELECT

    单列
       SELECT prod_name
       FROM Products;
    
    多列
       SELECT prod_id, prod_name, prod_price
       FROM Products;
    
    所有列

    通配符*

       SELECT *
       FROM Products;
    
    检索不同的值

    如果结果有相同的值,只出现一次

       SELECT DISTINCT vend_id
       FROM Products;
    
    限制检索结果

    返回不超过X行的结果

       SELECT prod_name 
       FROM Products 
       LIMIT 5 OFFSET 5;
    

    后面的5表示从第五行算起
    上述也可以写成

       SELECT prod_name 
       FROM Products 
       LIMIT 5,5;
    

    三种注释

    --#/**/



    排序检索结果

    ORDER BY

    排序
      SELECT prod_name
      FROM Products
      ORDER BY prod_name;
    
    按多个列排序
      SELECT prod_id, prod_price, prod_name 
      FROM Products 
      ORDER BY prod_price, prod_name;
    
    按列的位置排序
      SELECT prod_id, prod_price, prod_name 
      FROM Products 
      ORDER BY 2,3;
    
    指定排序方向

    ASCDESC,默认升序

      SELECT prod_id, prod_price, prod_name 
      FROM Products 
      ORDER BY prod_price DESC;
    


    过滤数据

    WHERE

    单个值
      SELECT prod_name, prod_price
      FROM Products 
      WHERE prod_price = 3.49;
    
    不匹配
      SELECT prod_name, prod_price
      FROM Products 
      WHERE prod_price <> 'DLLO1';
    
    范围值
      SELECT prod_name, prod_price
      FROM Products 
      WHERE prod_price BETWEEN 5 AND 10;
    
    空值
      SELECT cust_name
      FROM CuStomers
      WHERE cust_email IS NULL;
    


    高级数据过滤

    组合WHERE

    AND
      SELECT prod_id, prod_price, prod_name
      FROM Products
      WHERE vend_id = 'DLL01' AND prod_price <= 4;
    
    OR
      SELECT prod_name, prod_price 
      FROM Products 
      WHERE vend_id = 'DLL01' OR vend_id = 'BRS01';
    

    AND的优先级高于OR,必要时用括号确定优先级

      SELECT prod_name, prod_price 
      FROM Products
      WHERE (vend_id = 'DLL01' OR vend_id = 'BRS01')
      AND prod_price >= 10;
    
    IN
      SELECT prod_name, prod_price 
      FROM Products 
      WHERE vend_id IN ( 'DLL01', 'BRS01' )
      ORDER BY prod_name;
    
    NOT
      SELECT prod_name 
      FROM Products 
      WHERE NOT vend_id = 'DLL01'
      ORDER BY prod_name DESC;
    


    通配符

    LIKE

    %通配符

    表示任何字符出现任意次数,包括0次

      SELECT prod_id, prod_name 
      FROM Products 
      WHERE prod_name LIKE 'Fish%';
    
    _通配符

    匹配任意单个字符

      SELECT prod_id, prod_name 
      FROM Products 
      WHERE prod_name LIKE '__ inch teddy bear';
    
    []通配符

    指定一个字符集
    在MySQL 8中使用似乎没有用,使用RLIKE和REGEXP和方括号都没用
    建议查看手册,关于正则表达式部分

      SELECT cust_contact
      FROM Customers 
      WHERE cust_contact regexp 'J%'
      ORDER BY cust_contact;
    -- after test, the '[]' is useless
    
  • 相关阅读:
    django 自定义用户身份验证
    登录验证算法
    Scrapy
    爬虫性能相关
    Beautifulsoup模块
    selenium模块
    Cookie&Session
    Django Admin 本质
    JavaScript自执行函数和jquery扩展方法
    JS作用域与词法分析
  • 原文地址:https://www.cnblogs.com/burymyname/p/11907256.html
Copyright © 2020-2023  润新知