当前位置:网站首页>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 <= 1000Attention 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, 3LIMIT 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
边栏推荐
- Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
- 2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
- Promethus
- Class loading process
- Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- Cross compile opencv with contrib
- Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
- 1.5 learn to find mistakes first
猜你喜欢

2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration

2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程

kubernetes集群搭建efk日志收集平台

Commands related to files and directories

2.2 integer

Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation

PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?

Ae/pr/fcpx super visual effects plug-in package fxfactory

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
随机推荐
Part 27 supplement (27) buttons of QML basic elements
Rd file name conflict when extending a S4 method of some other package
2166. Design bit set
Make a simple text logo with DW
2.7 format output of values
Global and Chinese market of rubidium standard 2022-2028: Research Report on technology, participants, trends, market size and share
6006. Take out the minimum number of magic beans
AI enhanced safety monitoring project [with detailed code]
BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
Change deepin to Alibaba image source
Win10 share you don't have permission
Basic command of IP address configuration ---ip V4
Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que
2.4 conversion of different data types
kubernetes集群搭建efk日志收集平台
Typora charges, WTF? Still need support
Camera calibration (I): robot hand eye calibration
Strict data sheet of new features of SQLite 3.37.0
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
6. Data agent object Defineproperty method