Chapter VII of the data filtering
Combining WHERE clause
AND operator
SELECT prod_price,prod_name
FROM products
WHERE vend_id = 1003 AND prod_price <=10;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223132219939.png)
OR operator
SELECT prod_price,prod_name
FROM products
WHERE vend_id = 1003 OR prod_price <=10;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223132331580.png)
Calculation of total time
SELECT prod_price,prod_name,vend_id
FROM products
WHERE vend_id = 1003 OR vend_id = 1002 AND prod_price <=10;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223133351308.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI0NjkwOQ==,size_16,color_FFFFFF,t_70)
SELECT prod_price,prod_name,vend_id
FROM products
WHERE (vend_id = 1003 OR vend_id = 1002) AND prod_price <=10;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223133511937.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI0NjkwOQ==,size_16,color_FFFFFF,t_70)
IN operator
SELECT prod_price,prod_name,vend_id
FROM products
WHERE vend_id IN (1002,1003)
ORDER BY prod_name;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223133732736.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTI0NjkwOQ==,size_16,color_FFFFFF,t_70)
NOT operator
SELECT prod_price,prod_name,vend_id
FROM products
WHERE vend_id NOT IN (1002,1003)
ORDER BY prod_name;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223134029648.png)
Chapter VIII filtered with a wildcard
LIKE operator
- Wildcard: for special characters that are part of the matching value
- Search mode: combination of literals, or two persons to a wildcard search criteria
Percent%: Any characters appear any number of times
SELECT prod_id,prod_name
FROM products
WHERE prod_name LIKE 'jet%';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223135250446.png)
_ Underscore: any single character
SELECT prod_id,prod_name
FROM products
WHERE prod_name LIKE '_ ton anvil';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223135803283.png)
When using a wildcard, if not necessary, do not place the wildcard in the beginning of the search pattern
With regular expression search
Use MySQL Regular Expressions
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '1000';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223162657885.png)
(.) Matches any character
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '.000';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223162926411.png)
Be OR (|) match
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '1000|2000';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223163215419.png)
[] Matches one of the few characters
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '[123]000';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223163436445.png)
Matching range
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '[1-5] Ton';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223163829209.png)
Matching special characters
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '\\.';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223164026298.png)
Metacharacters |
Explanation |
\\f |
Feed |
\\n |
Wrap |
\\r |
Enter |
\\t |
tabulation |
\\ v |
Vertical tab |
Examples of the plurality of matching
Metacharacters |
Explanation |
* |
0 or more matches |
+ |
One or more matching |
? |
Match 0 or 1 |
{n} |
Specifies the number of matches |
{n, } |
Less than a specified number of matches |
{n,m} |
Number matches the specified range |
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '\\([0-9] sticks?\\)';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223171141596.png)
Locator
Metacharacters |
Explanation |
^ |
The beginning of the text |
$ |
End of text |
[[:<;]] |
The beginning of the word |
[[:>;]] |
End of a word |
SELECT prod_id,prod_name
FROM products
WHERE prod_name REGEXP '^[0-9\\.]';
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223172345847.png)
Create calculated fields
Splicing field
Concat () splice two columns
SELECT Concat(vend_name,'(',vend_country,')')
FROM vendors
ORDER BY vend_name;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223180033487.png)
RTrim () data to the right to delete extra spaces
SELECT RTrim(vend_country)
FROM vendors
ORDER BY vend_name;
The LTrim () Delete data for the left extra spaces
Trim () Delete data side two extra spaces
AS alias
Perform arithmetic calculations
SELECT prod_id,
quantity,
item_price,
quantity*item_price AS 总价
FROM orderitems
WHERE order_num = 20005;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20200223181313846.png)