当前位置:网站首页>100 important knowledge points that SQL must master: filtering data

100 important knowledge points that SQL must master: filtering data

2022-06-27 21:29:00 Guge academic

4.1 Use WHERE Clause
Database tables generally contain a large amount of data , It is rarely necessary to retrieve all rows in the table . Usually only
Extract a subset of table data according to the needs of a specific operation or report . To retrieve only the required data, you need to refer to
Define search criteria (search criteria), Search criteria are also called filter criteria (filter condition).
stay SELECT In the sentence , The data is based on WHERE Filter the search criteria specified in clause .
WHERE Clause in table name ( FROM Clause ) Then give , As shown below :
Input ▼
SELECT prod_name, prod_price
FROM Products
WHERE prod_price = 3.49;
analysis ▼
This statement starts from products Two columns in the search table , But not all rows are returned , Only return
prod_price The value is 3.49 The line of , As shown below :

Output ▼
prod_name prod_price
------------------- ----------
Fish bean bag toy 3.49
Bird bean bag toy 3.49
Rabbit bean bag toy 3.49
This simple example uses the equality test : Check whether the value of this column is the specified value , Accordingly
Filtering data . however ,SQL No, you can only test that it is equal to , There's more to be done .

Tips : How many 0?
When you practice this example , You will find that the displayed result may be 3.49、3.490、3.4900
etc. . This is the case , It's often because DBMS Specifies the data type and
Its default behavior . therefore , If your output may be a little different from that in the book , Don't worry ,
After all, mathematically speaking ,3.49 and 3.4900 It's the same .

Tips :SQL Filtering and application filtering
Data can also be filtered at the application layer . So ,SQL Of SELECT Statement for the client application
Retrieve more data than you actually need , The client code then loops through the returned data ,
Extract the required line .
Usually , This is extremely inappropriate . After optimizing the database, the data can be entered more quickly and effectively
Line filter . And let the client application ( Or development language ) The work of dealing with databases will be enormous
Affect the performance of the application , And make the created application completely non scalable . Besides ,
If you filter data on the client , The server has to send excess data over the network , this
This will result in a waste of network bandwidth .

Be careful : WHERE Position of clause
Use... At the same time ORDER BY and WHERE When clause , Should let ORDER BY be located
WHERE after , Otherwise, there will be errors ( About ORDER BY Use , see also
The first 3 course ).

4.2  WHERE Clause operator
When we did the equality test, we saw the first WHERE Clause , It determines whether a column contains
The specified value .SQL Support table 4-1 All the conditional operators listed .

 

Be careful : Operator compatibility
surface 4-1 Some of the operators listed in are redundant ( Such as < > And != identical , !< amount to  >= ).
Not all DBMS All support these operators . Want to make sure your DBMS What operations are supported
operator , Please refer to the corresponding documentation . 

4.2.1 Check individual values
We've seen examples of checking equality , Now let's look at a few examples of using other operators .
The first example is to list all prices less than 10 Dollar products .
Input ▼
SELECT prod_name, prod_price
FROM Products
WHERE prod_price < 10;

Output ▼
prod_name prod_price
------------------- ----------
Fish bean bag toy 3.49
Bird bean bag toy 3.49
Rabbit bean bag toy 3.49
8 inch teddy bear 5.99
12 inch teddy bear 8.99
Raggedy Ann 4.99
King doll 9.49
Queen doll 9.49
The next statement retrieves all prices less than or equal to 10 Dollar products ( Because no price happens to be
10 Dollar products , So the result is the same as the previous example ):
Input ▼
SELECT prod_name, prod_price
FROM Products
WHERE prod_price <= 10;
4.2.2 Mismatch check
This example lists all non suppliers DLL01 Manufactured products :
Input ▼
SELECT vend_id, prod_name
FROM Products
WHERE vend_id <> 'DLL01';
Output ▼
vend_id prod_name
---------- ------------------
BRS01 8 inch teddy bear
BRS01 12 inch teddy bear
BRS01 18 inch teddy bear
FNG01 King doll
FNG01 Queen doll

Tips : When to use quotation marks
If you look closely at the above WHERE The condition in clause , You will see some values enclosed in single quotes ,
Some values are not enclosed . Single quotation marks are used to qualify strings . If you compare a value with a string type
Column to compare , You need to limit quotation marks . Values used to compare with numeric columns are not quoted . 

Here's the same example , It uses != instead of <> The operator :
Input ▼
SELECT vend_id, prod_name
FROM Products
WHERE vend_id != 'DLL01';

Be careful : yes != still <> ?
!= and <> Usually interchangeable . however , Not all DBMS Both support these two kinds of operations
An allograph . If in doubt , Please refer to the corresponding DBMS file . 

4.2.3 Check the range of values
To check the value of a range , have access to BETWEEN The operator . Its grammar is similar to others WHERE
Clause has slightly different operators , Because it needs two values , That is, the start value and end value of the range .
for example , BETWEEN The operator can be used to retrieve the price in 5 The dollar and 10 All assets between dollars
product , Or all dates between the specified start date and end date .
The following example shows how to use BETWEEN The operator , It retrieves the price at 5 The dollar and 10
All products between dollars .

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

Output ▼
prod_name prod_price
------------------- ----------
8 inch teddy bear 5.99
12 inch teddy bear 8.99
King doll 9.49
Queen doll 9.49
analysis ▼
As you can see from this example , In the use of BETWEEN when , Two values must be specified —— Required range
The low and high values of the circumference . These two values must use AND Keyword separation . BETWEEN matching
All values in the range , Include the specified start and end values .
4.2.4 Null check
When the table is created , The table designer can specify whether the columns can contain no values . Not in a column
When containing values , Say it contains null values NULL .

NULL
No value (no value), It is contained with the field 0、 Empty strings or just contain Spaces differently . 

Determine if the value is NULL , You can't simply check whether it's equal to NULL . SELECT The statement has a
A special WHERE Clause , Can be used to check the presence of NULL Columns of values . This WHERE Clause
Namely IS NULL Clause . The syntax is as follows :
Input ▼
SELECT prod_name
FROM Products
WHERE prod_price IS NULL;
This statement returns all without price ( empty prod_price Field , Not at a price of 0 ) Of
product , Because there is no such row in the table , So no data is returned . however , Customers The table does contain a with NULL Columns of values : If you don't have an email address , be cust_email
The column will contain NULL value :
Input ▼
SELECT cust_name
FROM Customers
WHERE cust_email IS NULL;
Output ▼ 
cust_name
----------
Kids Place
The Toy Store

Tips :DBMS Unique operators
many DBMS Extends the standard set of operators , Provides more advanced filtering options . more
Refer to the corresponding DBMS file .

Be careful : NULL And unmatched
When all rows that do not contain the specified value are selected by filtering , You may want to return with NULL It's worth it
That's ok . But this cannot be done . because NULL A special , So we're doing matching filtering or non matching
When equipped with filtration , These results will not be returned . 

原网站

版权声明
本文为[Guge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271909450103.html