zoukankan      html  css  js  c++  java
  • WHERE子句

    WHERE常见语法

    • WHERE主要是给选择语句添加条件, 进行筛选, 涉及到一些常见的运算符语句
    # IN 运算
    SELECT *
    FROM customers
    WHERE state='VA' OR state='GA' OR state='FL';
    
    SELECT *
    FROM customers
    WHERE state IN ('VA', 'GA', 'FL');
    # 练习
    -- Return products with quantity in stock equal to 49, 38, 72
    SELECT *
    FROM products
    WHERE quantity_in_stock IN (49, 38, 72);
    
    # BETWEEN 
    SELECT *
    FROM customers
    WHERE points >= 1000 AND points <= 3000;
    
    SELECT *
    FROM customers
    WHERE points BETWEEN 1000 AND 3000;
    # 练习
    -- return customers born between 1/1/1990 and 1/1/2000
    SELECT *
    FROM customers
    WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01';
    
    # LIKE 匹配字符串, % 多个任意字符, _ 单个任意字符
    SELECT *
    FROM customers
    WHERE last_name LIKE 'b%';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '%b%';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '%y';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '%y';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '%y';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '_y';
    
    SELECT *
    FROM customers
    WHERE last_name LIKE '_____y';
    -- % any number of characters
    -- _ single character
    
    # 练习
    -- GET the customers whose
    -- address contain TRAIL or AVENUE
    
    SELECT *
    FROM customers
    WHERE address LIKE '%TRAIL%' OR address LIKE '%AVENUE%';
    -- phone numbers end with 9
    SELECT *
    FROM customers
    WHERE phone LIKE '%9';
    SELECT *
    FROM customers
    WHERE phone NOT LIKE '%9';
    
    -- REGEPX
    SELECT *
    FROM customers
    WHERE last_name LIKE '%field%';
    
    SELECT *
    FROM customers
    WHERE last_name REGEXP 'field';
    -- 以field 开头
    SELECT *
    FROM customers
    WHERE last_name REGEXP '^field';
    -- 以 field 结尾
    SELECT *
    FROM customers
    WHERE last_name REGEXP 'field$';
    -- 搜寻多个单词
    SELECT *
    FROM customers
    WHERE last_name REGEXP 'field$|mac|rose';
    -- 前面包含多个选项[]
    SELECT *
    FROM customers
    WHERE last_name REGEXP '[gim]e';
    
    SELECT *
    FROM customers
    WHERE last_name REGEXP '[a-h]e[lv]';
    # exerise
    -- GET the customers whose
    -- First names are ELKA or AMBUR
    SELECT *
    FROM customers
    WHERE first_name LIKE '%ELKA%' OR first_name LIKE '%AMBUR';
    
    SELECT *
    FROM customers
    WHERE first_name REGEXP 'ELKA|AMBUR';
    -- last names end with EY or ON
    SELECT *
    FROM customers
    WHERE last_name REGEXP 'EY$|ON$';
    -- last names start with MY or contains SE
    SELECT *
    FROM customers
    WHERE last_name REGEXP '^MY|SE';
    -- last names contain B followed by R or U
    SELECT *
    FROM customers
    WHERE last_name REGEXP 'b[ru]';
    
    -- NULL
    SELECT *
    FROM customers
    WHERE phone IS NULL;
    
    SELECT *
    FROM customers
    WHERE phone IS NOT NULL;
    # Exercise 
    -- Get the orders that are not shipped
    SELECT *
    FROM orders
    WHERE shipped_date IS NULL;
    # WHERE 用法
    USE sql_store;
    SELECT *
    FROM customers
    WHERE points > 3000;
    
    SELECT *
    FROM customers
    WHERE state = 'va';
    
    SELECT *
    FROM customers
    WHERE state != 'va';
    
    SELECT *
    FROM customers
    WHERE birth_date > '1990-01-01';
    
    # 练习
    -- Get the orders placed this year
    SELECT *
    FROM orders
    WHERE order_date >= '2019-01-01';
    SELECT *
    FROM orders
    WHERE YEAR(order_date) = '2019';
    
    # WHERE多条件
    SELECT *
    FROM customers
    WHERE birth_date > '1990-01-01' AND points > 1000;
    
    SELECT *
    FROM customers
    WHERE birth_date > '1990-01-01' OR points > 1000;
    
    SELECT *
    FROM customers
    WHERE birth_date > '1990-01-01' OR (points > 1000 AND state = 'VA');
    
    SELECT *
    FROM customers
    WHERE NOT(birth_date > '1990-01-01' OR points > 1000);
    
    # 练习
    -- FROM the order_items table, get the items for order #6
    -- where the local price is greater than 30
    SELECT *
    FROM order_items
    WHERE order_id = 6 AND (quantity * unit_price) > 30;
    
  • 相关阅读:
    LINUX监控一:监控命令
    Kettle并行
    KETTLE集群搭建
    Solr报错Index locked for write for core '***'. Solr now longer supports forceful unlocking via 'unlockOnStartup'
    Solr json,xml等文件数据导入(添加索引)linux下操作
    python对solr进行查询、插入操作(GETPOST)
    Solr-5.3.1 dataimport 导入mysql数据
    解决MySQL数据导入报错Got a packet bigger than‘max_allowed_packet’bytes
    解决防火墙限制远程连接MySQL(导致错误10060可能之一)
    gensim加载word2vec训练结果(bin文件)并进行相似度实验
  • 原文地址:https://www.cnblogs.com/jly1/p/12977261.html
Copyright © 2011-2022 走看看