当前位置:网站首页>MySQL learning notes - single table query
MySQL learning notes - single table query
2022-07-03 20:05:00 【Cristal0412】
1. Select statement The SELECT Statement:
SELECT yes Column / Field Select statement , Selectable column , Mathematical expression between columns , Specific value or text , You can use AS Keyword to set column aliases (AS Omission ), Be careful DISTINCT
Use of keywords .
Be careful
SQL Will completely ignore case ( Case in most cases )、 Extra space ( More than one space )、 Indent and wrap ,SQL Statements are completely separated by semicolons ;
Division , Indent with 、 Line breaks are just to make the code look more beautiful and the structure clearer , These and Python Very different , it is to be noted that .
2. WHERE
WHERE Is the row filter condition , Actually, it's line by line / One record after another verifies whether it meets the conditions , Screening
Comparison operator :
> < = >= <=
!=/<> It's not equal to
3. AND,OR,NOT Operator
example
USE sql_store;
SELECT *
FROM customers
WHERE birth_date > '1990-01-01' AND points > 1000
/WHERE birth_date > '1990-01-01' OR
points > 1000 AND state = 'VA'
AND Priority over OR, But it's best to put parentheses , Clearer
WHERE birth_date > '1990-01-01' OR
(points > 1000 AND state = 'VA')
NOT Usage of
WHERE NOT (birth_date > '1990-01-01' OR points > 1000)
The parenthesized equivalent is converted to
WHERE birth_date <= '1990-01-01' AND points <= 1000
Attention priority : mathematics → Compare → Logic
SELECT Clause ,WHERE Clause and the following ORDER BY Clauses, etc. can use mathematical expressions between columns
4. IN Operator
use IN Operator will be a certain attribute With multiple values ( A series of values ) Compare ; The essence is the simplification of multiple equal comparison operation conditions
use IN Operator simplifies the condition
WHERE state IN ('va', 'fl', 'ga')
coca NOT
WHERE state NOT IN ('va', 'fl', 'ga')
It's available here NOT Why : You can see that ,IN sentence IN ('va', 'fl', 'ga')
It's a judgment of whether it meets the conditions , It can be regarded as a special comparison operation , What you get is a logical value , So it can be used NOT To reverse
5. BETWEEN Operator
Used to express Range Type condition
Be careful
- use AND Not parentheses
- Closed interval , Include both ends
- It can also be used for dates , After all, dates are also numerical in nature , Dates also have sizes ( Sooner or later ), Comparable operations
- Same as IN equally ,BETWEEN Essence is also a specific Simplification of multiple comparison operation conditions
6. LIKE Operator
Fuzzy search , Find records of strings with certain patterns / That's ok
Be careful
- Outdated usage ( But sometimes it's easier to use , Later, I found that it seems to be used more ), Regular expressions in the next lesson are more flexible and powerful
- Note that like regular expressions, strings are wrapped in quotation marks
USE sql_store;
SELECT * FROM customers
WHERE last_name like 'brush%' / 'b____y'
Describe the desired string pattern in quotation marks , Be careful SQL( almost ) Case insensitive in any case
Two kinds of wildcards :
%
Any number ( Include 0 individual ) The characters of ( Use more )any number of characters_
Single character single character
practice
Choose customers who meet the following conditions :
1. The address contains 'TRAIL' or 'AVENUE'
2. The telephone number is in 9 end
USE sql_store;
select *
from customers
where address like '%Trail%' or
address like '%avenue%'
LIKE The execution priority is after the logical operator , After all IN BETWEEN LIKE The essence can be seen as the simplification of comparison operators , It should be at the same level as the comparison operation , mathematics → Compare → Logic , Always remember this order , The above one will be much simpler if regular expressions are used
where phone like '%9'
/where phone not like '%9'
LIKE The judgment result of is also a TRUE/FASLE The problem of , Any logical value / Brin value can be pre NOT To take the opposite
7. REGEXP Operator
Regular expressions , More powerful in searching strings , You can search for more complex templates
example
USE sql_store;
select * from customers
where last_name like '%field%'
-- Equivalent to
where last_name regexp 'field'
regexp yes regular expression( Regular expressions ) Abbreviation
Regular expressions can be combined to express more complex string patterns
where last_name regexp '^mac|field$|rose'
where last_name regexp '[gi]e|e[fmq]' -- Find the ge/ie or ef/em/eq Of
where last_name regexp '[a-h]e|e[c-j]'
Regular expression summary :
Symbol | significance |
---|---|
^ | start beginning |
$ | ending end |
[abc] | contain abc |
[a-c] | contain a To c |
| | logical or |
practice
Choose customers who meet the following conditions :
1. first names yes ELKA or AMBUR
2. last names With EY or ON end
3. last names With MY start Or contain SE
4. last names contain BR or BU
select *
from customers
where first_name regexp 'elka|ambur'
/where last_name regexp 'ey$|on$'
/where last_name regexp '^my|se'
/where last_name regexp 'b[ru]'/'br|bu'
8. IS NULL and IS NOT NULL Operator
Find the null value , Find records with some missing attributes
Be careful
yes IS NULL and IS NOT NULL here NOT Not preceded by brin value , It is more in line with English grammar be After verb
9. ORDER BY Sort
Sort statement , and SELECT ……
It's like :
- Multi column
- It can be a mathematical expression between columns
- Can include any column , Include unselected columns (MySQL characteristic , Other DBMS There may be a mistake ),
- It can be a previously defined alias column (MySQL characteristic , It can even be a column alias set with a constant )
- Any sort by column Can be added DESC
Be careful
Better not use ORDER BY 1, 2
( Said to SELECT ……
Select the... In the column 1、2 List as sorting basis ) This implicit basis , because SELECT When the selected column changes, it is easy to make mistakes , It is better to write the column names explicitly as the basis for sorting
notes :workbench The middle wrench icon opens the design mode of the table , View or modify the columns in the table ( attribute ), You can see who is the primary key . If the sorting statement is omitted, it will be sorted by primary key by default
10. LIMIT Clause
Limit the number of records that return results ,“ front N individual ” or “ skip M The last one is the first one N individual ”
USE sql_store;
select * from customers
limit 3 / 300 / 6, 3
LIMIT 6, 3 Before you jump 6 individual , Take the first place 7~9 individual ,6 It's the offset ,
Such as : Page paging Every time 3 Records show one page The first 3 The record that the page should display is limit 6, 3
Be careful
SELECT The sentence is over , The order of the clauses inside is fixed, and you should remember , If the order is disordered, an error will be reported select
from
where
+ order by
limit
Vertical selection column , Determination table , Select the row horizontally ( The writing and combination of various conditions should be clear and familiar ), Finally, sort and limit
边栏推荐
- AI enhanced safety monitoring project [with detailed code]
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
- Typora charges, WTF? Still need support
- Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Microservice knowledge sorting - search technology and automatic deployment technology
- 2.1 use of variables
- Realize user registration and login
- Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
- Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
Today's work summary and plan: February 14, 2022
The 15 year old interviewer will teach you four unique skills that you must pass the interview
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
2.3 other data types
Machine learning support vector machine SVM
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
IPv6 experiment
Acquisition and transmission of parameters in automatic testing of JMeter interface
随机推荐
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
4. Data splitting of Flink real-time project
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
Ae/pr/fcpx super visual effects plug-in package fxfactory
2.1 use of variables
AI enhanced safety monitoring project [with detailed code]
Day11 - my page, user information acquisition, modification and channel interface
Print linked list from end to end
IP address is such an important knowledge that it's useless to listen to a younger student?
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
Find a line in a file and remove it
CMD implements the language conversion of locale non Unicode programs
Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Difference between surface go1 and surface GO2 (non professional comparison)