"SQL must know and know" (4, 5, 6): filter data, advanced data filter, wildcard filter

In the previous section, the third lesson sorted and retrieved data

Lesson 4: Filtering the data

4.1 Using the where clause

Database tables usually contain a lot of data, it is rarely necessary to check all the rows, but also to avoidFull table scan. We need to use restrictions to find the data we need, that is, filtering.
Search criteria filter conditionIt's all the same thing.
Use the where clause to filter in the select statement.

SELECT prod_id,prod_price,prod_name from products
where prod_price=3.49
;

Insert picture description hereThe equality test is simply used here. SQL can do more things, let's see the decomposition later.

Data can also be filtered at the application layer, but we generally don’t do that. If filtering on the client side, the server has to send excess data, and the client’s work on the database will also become larger, affecting application performance and wasting network bandwidth. .We generally choose to optimize the database to filter data efficiently

4.2 where clause operator

Insert picture description here

SELECT prod_name,prod_price FROM Products
WHERE prod_price BETWEEN 5 AND 10;

Insert picture description here

4.3 Summary

This lesson introduces how to use the WHERE clause of the SELECT statement to filter the returned data. We learned how to check for equality, inequality, greater than, less than, range of values, and NULL values.

Lesson 5: Advanced data filtering

5.1 Combined where clause

Operator: The keywords used to connect or change the clauses in the where clause are also called logical operators. Usually and, or;

5.1.1 and operator

It means that both conditions are satisfied, and and is the principle of proximity, which is described in detail below.

SELECT prod_id,prod_name,prod_price FROM Products
WHERE vend_id = 'DLL01' and prod_price <= 4;

Insert picture description here

5.1.2 or operator

Either condition is satisfied.

SELECT prod_id,prod_name,prod_price FROM Products
WHERE vend_id = 'DLL01' or prod_price <= 4;

Insert picture description here

5.1.13 Evaluation order

The reason why and is the principle of proximity is that and is executed first before or.

SELECT prod_name,prod_price FROM Products
WHERE vend_id = 'DLL01'OR vend_id = 'BRS01'
AND prod_price != 10;

Insert picture description here
Of course, it would be different if the brackets were added. The parentheses have high priority.

5.2 in operator

Identify the scope of the condition.

SELECT prod_name,prod_price FROM Products
wHERE vend_id IN ( 'DLLO1','BRS01') 
ORDER BY prod_name;

Insert picture description here
Note the difference between and between and, which are two numerical ranges.

The effect of in and or is basically the same. Why use in?

  • When there are many legal options, the syntax of the IN operator is clearer and more intuitive.
  • When IN is used in combination with other AND and OR operators, the order of evaluation is easier to manage. The IN operator is generally faster than a set of OR operators (in the above example with few legal options, you can't see a performance difference).
  • The biggest advantage of IN is that it can contain other SELECT statements and can create WHERE clauses more dynamically. This will be explained in detail in Lesson 11.

5.3 NOT operator

Negative meaning, negate;

SELECT prod_name FROM Products
WHERE NOT vend_id = 'DLLO1'ORDER BY prod_name;

Insert picture description here
You can also use <> or! = Instead

Lesson 6: Wildcard filtering

6.1 like operator

All the operators introduced earlier filter against known values. Whether it is matching one value or multiple values, checking greater than or less than a known value, or checking a range of values, the common point is that the values ​​used in filtering are all known. But it can't meet the needs of reality, so you have to find all the students whose surname is Wu, and the operator can't meet them.

Wildcard: Part of the special characters used to match the value.
Search pattern: A search condition consisting of literal values, wildcards, or a combination of both.
To use wildcards, you must use them like! ! !
Predicate: When is an operator not an operator? The answer is when it is used as a predicate. Technically speaking, LIKE is a predicate rather than an operator. Although the end result is the same, you should understand this term so that you don’t get confused when you encounter this term in SQL literature or manuals.
Wildcard search can only be used in text fields (strings)

6.1.1 Percent sign% wildcard

% Can represent any character and number.

SELECT prod_id,prod_name FROM Products
WHERE prod_name LIKE 'Fish%';

Find the beginning of Fish,
Insert picture description here

Note: Please note that the NULL
wildcard% looks like it can match anything, but there is one exception, which is NULL. The clause WHERE prod_name LIKE'%' will not match rows where the product name is NULL.

6.1.2 Underscore

Similar to %, _ means to match a single character instead of multiple.
And it cannot be zero. There can only be one, no more, no less.

6.1.3 Square brackets []

The square brackets [] wildcard is used to specify a character set, and it must match a character in the specified position (the position of the wildcard).

SELECT cust_contact
FROM customers
WHERE cust_contact 
LIKE '[JM]%'
ORDER BY cust_contact;

这里很正常的遇到坑了,被坑了好久,5555~
The above statement, other DBMS may be possible, but I am using MySQL, just not applicable! ! ! Let's take a look at how MySQL is used! !

select cust_contact 
from customers
where cust_contact 
rlike '^[JM]';
-- where cust_contact regexp '^[JM]';

The difference with the above is that there is an extra rsum ^. Why is this?
Hey, [] is actually borrowed from the usage of regular expressions in sql. [] is not supported in MySQL, and only regular expressions can be used. So.

Tips for using wildcards

Although wildcards are good, don't be greedy!

Wildcards are more cost-intensive in terms of performance. Here are some tips:

  1. Don't overuse wildcards. If other operators can achieve the same purpose, other operators should be used.
  2. When you really need to use wildcards, try not to use them at the beginning of the search pattern. Put the wildcard at the beginning, the search is the slowest.
  3. Pay careful attention to the location of wildcards. If it is misplaced, the desired data may not be returned.

In short, wildcard is an extremely important and useful search tool, we will often use it in the future.

summary

This time, I talked about what wildcards are and how to use them: everything in the
world is true, and the passage is not only useless, but it is harmful.
The ashes are gone, hhh between talking and laughing

Guess you like

Origin blog.csdn.net/qq_43600467/article/details/112974532