第五章:排序数据
>SELECT prod_name FROM products ORDER BY prod_name;(单行排列)
SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price, prod_name;(多行排列)

An ORDER BY Shortcut: Instead of type the column names in ORDER BY, you can 
also type the column number specifying its sequence in the SELECTstatement
使用order by的语句中,可以数字的方式来调用前面的字段名,达到快捷目的.

>SELECT prod_id, prod_price, prod_name FROM products ORDER BY 2, 3;

To sort by descending order, the keyword DESC must be specified.
必须声明DESC,才可进行逆序排列.

To sort by descending order, the keyword DESC must be specified.

第六章:过滤数据

Within a SELECT statement, data is filtered by specifying search criteria in the 
WHERE clause. The WHERE clause is specified right after the table name (the FROM clause) as follows:
>SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50

where子句可限定条件,从而过滤数据

where子句的操作符
比较:
    =
    <>
    !=
    <
    <=
    >
    >=
    BETWEEN

 

SELECT prod_name, prod_price
FROM products
WHERE prod_price BETWEEN 5 AND 10;
between + and 可限定一个范围

SELECT prod_name
FROM products
WHERE prod_price IS NULL;
用is来判定是否为空值;

第七章:高级数据过滤

Combining WHERE Clauses:联合的where子句

SELECT prod_id, prod_price, prod_name
FROM products
WHERE vend_id = 1003 AND prod_price <= 10;
and操作符的使用.

SELECT prod_name, prod_price
FROM products
WHERE vend_id = 1002 OR vend_id = 1003;
or操作符的使用.

 order of evaluation: SQL (like most languages) processesANDoperators before ORoperators.
 当碰到有多个and or连接子句时,SQL会优先计算and ,然后计算or

 IN操作符
    The IN operator is used to specify a range of conditions, any of which can be matched. IN takes a comma-delimited list of valid values, all enclosed within parentheses.
    当需要匹配多个确定的值,而不是范围时,用多个where子句搭配or就显得啰嗦了,IN操作符非常适合这种情况.

    SELECT prod_name, prod_price
    FROM products
    WHERE vend_id IN (1002,1003)
    ORDER BY prod_name;

    The biggest advantage of IN is that the IN operator can contain another SELECT statement, enabling you to build highly dynamic WHERE clauses. 
    IN最大的优势在于IN操作符可以包含另外一个select语句(放在子查询部分讲解)

NOT操作符
    MariaDB supports the use of NOT to negate IN, BETWEEN, and EXISTS clauses. This is different from most other DBMSs that allow NOTto be used to negate any conditions.
    MariaDB仅允许翻转IN,BETWEEN,EXISTS 3个子句,其他数据库一般可翻转任意条件.

    SELECT prod_name, prod_price
    FROM products
    WHERE vend_id NOT IN (1002,1003)
    ORDER BY prod_name;

第八章:通配符

LIKE操作符
    All the previous operators we studied filter against known values
    前几章介绍的,全是运用已知的,确定的值来操作.当你想要得到一个带有模糊信息的数据怎么办呢?

    Using wildcards, you can create search patterns that can be compared against your data.
    运用通配符,你可以创建模板去跟数据比较从而找到需要的数据

    Wildcards: Special characters used to match parts of a value.
    通配符:用来匹配值的一部分的特殊字符

    Search pattern A search condition made up of literal text, wildcard characters, or any combination of the two. 
    搜索原型:由一般字符加上通配符构成的模板.

    百分号(%)
        The most frequently used wildcard is the percent sign (%). Within a search string, %means match any number of occurrences of any character. 
        百分号用来代表任意数量的任意字符


        >SELECT prod_id, prod_name
        FROM products
        WHERE prod_name LIKE 'jet%';

        While it may seem that the %wildcard matches anything, there is one 
        exception,NULL.
        "%"不能匹配到NULL


        The underscore is used just like %, but instead of matching multiple characters, the underscore matches just a 
        single character.
        "_"仅匹配单一的任意字符

        SELECT prod_id, prod_name FROM products WHERE prod_name LIKE '_ ton anvil';

        ■ Don’t overuse wildcards. If another search operator will do, use it instead.
        ■ When you do use wildcards, try to not use them at the beginning 
        of the search pattern unless absolutely necessary. Search patterns that 
        begin with wildcards are the slowest to process.
        ■ Pay careful attention to the placement of the wildcard symbols. If they 
        are misplaced, you might not return the data you intended.
        不要过度使用通配符,如果其他的搜索操作符能做,就替换掉它
        不要在模板的开头就使用通配符,执行的会很慢
        注意通配符的位置