当前位置:网站首页>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 .
边栏推荐
- VMware vSphere esxi 7.0 installation tutorial
- 抖音的兴趣电商已经碰到流量天花板?
- mime. Type file content
- 灵活的IP网络测试工具——— X-Launch
- 一套系统,减轻人流集中地10倍的通行压力
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
- What is a low code development platform? Why is it so hot now?
- Animal breeding production virtual simulation teaching system | Sinovel interactive
- 2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
- BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
猜你喜欢

Galaxy Kirin system LAN file sharing tutorial

Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)

基于 TensorRT 的模型推理加速

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

White whoring red team goby & POC, how do you call white whoring?

No wonder people chose apifox instead of postman

Here are 12 commonly used function formulas for you. All used ones are good

GoLand permanently activated

SQL Server for循环用法

一套系统,减轻人流集中地10倍的通行压力
随机推荐
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
SQL server for circular usage
White whoring red team goby & POC, how do you call white whoring?
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Tutorial | fNIRS data processing toolkit homer2 download and installation
抖音的兴趣电商已经碰到流量天花板?
Best practice: optimizing Postgres query performance (Part 2)
Squid proxy server
Love math experiment | phase VI - Financial anti fraud case study
ARCS模型介绍
Flexible IP network test tool -- x-launch
Animal breeding production virtual simulation teaching system | Sinovel interactive
分享下我是如何做笔记的
Dictionary tree (review)
SQL必需掌握的100个重要知识点:组合 WHERE 子句
分享|智慧环保-生态文明信息化解决方案(附PDF)
Recommended practice sharing of Zhilian recruitment based on Nebula graph
GFS distributed file system
大促场景下,如何做好网关高可用防护