当前位置:网站首页>MySQL learning record (4)
MySQL learning record (4)
2022-07-02 21:29:00 【White_ Silence (Learning version)】
SQL Query and sort
1.SELECT sentence
1.1 Select data from the table
Data retrieval process
SELECT < Name >, ……
FROM < Table name >
WHERE < Conditional expression >;
-- Take the matching from a table WHERE A column of data for the condition
1.2 Select qualified data from the table
WHERE sentence
When you don't need to take out all the data , But when selecting data that meets certain conditions , Use WHERE sentence .
SELECT Statement passing WHERE Clause to specify the conditions for querying data . stay WHERE Clause can specify “ The value of a column is equal to this string ” perhaps “ The value of a column is greater than this number ” Equal conditions . Execute... With these conditions SELECT sentence , Query only the records that meet this condition .
-- Used to select product type List as clothes ’ The record of SELECT sentence
SELECT product_name, product_type
FROM product
WHERE product_type = ' clothes ';
-- You can also select columns that are not query criteria ( The condition column is different from the output column )
SELECT product_name
FROM product
WHERE product_type = ' clothes ';1.3 The law of correlation
- asterisk (*) For all columns .
- SQL You can use line breaks at will in , Does not affect statement execution ( But don't insert a blank line ).
- You need to use double quotation marks when setting Chinese alias (") Cover up .
- stay SELECT Use in statement DISTINCT Sure Delete duplicate lines .
- Comments are SQL The part of a statement used to identify instructions or precautions . It is divided into 1 Line notes "-- " And multiline comments "/* */".
-- When you want to query all columns , You can use asterisks representing all columns (*). SELECT * FROM < Table name >; -- SQL Statement can be used AS Keyword to alias a column ( Double quotation marks are required when using Chinese (“”)). SELECT product_id AS id, product_name AS name, purchase_price AS " Purchase unit price " FROM product; -- Use DISTINCT Delete product_type Duplicate data in column SELECT DISTINCT product_type FROM product;2. Arithmetic operators and comparison operators
2.1 Arithmetic operator :+ - * /
2.2 Comparison operator := <> ( and ~ It's not equal ) > >= < <=
2.3 Common rules :
SELECT You can use constants or expressions in clauses .
When using the comparison operator, be sure to pay attention to the position of the unequal sign and the equal sign .
String type data is sorted in dictionary order in principle , It can't be confused with the size order of numbers .
Hope to choose NULL When recording , You need to use... In conditional expressions IS NULL Operator . I hope the choice is not NULL When recording , You need to use... In conditional expressions IS NOT NULL Operator .
-- SQL You can also use operational expressions in statements
SELECT product_name, sale_price, sale_price * 2 AS "sale_price x2"
FROM product;
-- WHERE Clause can also be used in conditional expressions
SELECT product_name, sale_price, purchase_price
FROM product
WHERE sale_price-purchase_price >= 500;
-- from product Take the selling price minus the cost price from the table >=500 The goods , Output its product name , The price is , Purchase price .
/* Use an unequal sign on a string
First create chars And insert data
Select a value greater than ‘2’ Of SELECT sentence */
-- DDL: Create table
CREATE TABLE chars
(chr CHAR(3)NOT NULL,
PRIMARY KEY(chr));
-- Fixed length string length 3 Non empty constraint Primary key constraint
-- Select a value greater than '2' Data. SELECT sentence ('2' For the string )
SELECT chr
FROM chars
WHERE chr > '2';
-- selection NULL The record of
SELECT product_name,purchase_price
FROM product
WHERE purchase_price IS NULL;
-- Select not NULL The record of
SELECT product_name,purchase_price
FROM product
WHERE purchase_price IS NOT NULL;3. Logical operators
3.1 NOT Operator
notes :NOT Not to be used alone
-- The selected sales unit price is greater than or equal to 1000 The record of Japanese yen
SELECT product_name, product_type, sale_price
FROM product
WHERE sale_price >= 1000;
-- To the code list 2-30 Add... To the query conditions of NOT Operator
SELECT product_name, product_type, sale_price
FROM product
WHERE NOT sale_price >= 1000;3.2 AND OR Operator
Use parentheses to prioritize
notes : Without parentheses ,AND Prior to the OR
lookup “ The commodity category is office supplies ” also “ The registration date is 2009 year 9 month 11 Day or 2009 year 9 month 20 Japan ”
-- Make... By using parentheses OR The operator precedes AND Operator execution
SELECT product_name, product_type, regist_date
FROM product
WHERE product_type = ' Office Supplies '
AND ( regist_date = '2009-09-11'
OR regist_date = '2009-09-20');3.3 Truth table
How to understand complex operations ?
When you encounter a statement with complex conditions , It's not easy to understand the meaning of a sentence , You can use Truth table To sort out the logical relationship .
What is true value ?
The three operators described in this section NOT、AND and OR Called logical operators . The logic mentioned here means to operate on the truth value . Truth value Is the value is true (TRUE) Or false (FALSE) Value of one of them .
for example , about sale_price >= 3000 For this query condition , because product_name As a ' motion T T-shirt ' The record of sale_price Is the value of the column 2800, So it returns false (FALSE), and product_name As a ' pressure cooker ' The record of sale_price Is the value of the column 5000, So back to true (TRUE).
AND Operator **:** Returns true when the truth values on both sides are true , In addition, all return false .
OR Operator **:** As long as one of the truth values on both sides is not false, it returns true , False is returned only if the true values on both sides are false .
NOT Operator **:** Just simply convert true into false , Convert false to true .


3.5 contain NULL True value at
NULL The truth result of is neither true , It's not false , Because I don't know such a value .
How to express it ?
At this time, the true value is the third value besides true and false —— Not sure (UNKNOWN). This third value does not exist in general logical operations .SQL Other languages basically use only true and false truth values . In contrast to the usual logical operation called binary logic , Only SQL The logical operation in is called ternary logic .
Exercises 1
Write a SQL sentence , from product( goods ) Select from the table “ Registration date (regist stay 2009 year 4 month 28 After the day ” The goods , Query results should contain product_name and regist_date Two .
select product_name, regist_date
from product
WHERE regist_date >= "2009-04-28";
Exercises 3
Code list 2-22(2-2 section ) Medium SELECT Statements can be made from product Take out from the table “ Unit sales price (saleprice) Compared with the purchase price (purchase price) Higher than 500 Above JPY ” The goods . Please write two items that can get the same result SELECT sentence . The results are shown below .
-- The first way of writing
select product_name, sale_price,purchase_price
from product
where sale_price-purchase_price >= 500;
-- Article 2 writing
select product_name,sale_price,purchase_price,sale_price-purchase_price >=500 AS "salepurchase_price"
from product
where purchase_price is NOT NULL;Exercises 4
Please write a SELECT sentence , from product Select from the table to meet “ After 10% discount on unit sales price, the profit is higher than 100 Japanese yen office supplies and kitchen utensils ” Record of conditions . The query results should include product_name Column 、product_type And the profit after 10% discount on the sales unit price ( The alias is set to profit).
SELECT product_name, product_type, sale_price*0.9-purchase_price AS "profit"
from product
WHERE (product_type =" Office Supplies " OR " Kitchenware ") AND sale_price*0.9-purchase_price >= 100;

边栏推荐
- 7. Build native development environment
- [hands on deep learning]02 softmax regression
- Research Report on the overall scale, major manufacturers, major regions, products and applications of battery control units in the global market in 2022
- Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
- [shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)
- Unexpectedly, there are such sand sculpture code comments! I laughed
- ctf-HCTF-Final-Misc200
- Customized Huawei hg8546m restores Huawei's original interface
- Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
- 1005 spell it right (20 points) "PTA class a exercise"
猜你喜欢

Write the content into the picture with type or echo and view it with WinHex

How is LinkedList added?

Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
![[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)](/img/04/4070d51ce8b7718db609ef2fc8bcd7.jpg)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
![The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article](/img/8f/6759b4685a129f9d10d6ea1dc8e61e.jpg)
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article

How does esrally perform simple custom performance tests?

MySQL learning notes (Advanced)
![[dynamic planning] p1220: interval DP: turn off the street lights](/img/b6/405e29ca88fac40caee669a3b7893f.jpg)
[dynamic planning] p1220: interval DP: turn off the street lights

Spend more time with your computer on this special holiday, HHH
![[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)](/img/45/735431f59a84e9554225a72a551ab8.jpg)
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
随机推荐
[12] the water of the waves is clear, which can wash my tassel. The water of the waves is muddy, which can wash my feet
Codeworks global round 19 (CF 1637) a ~ e problem solution
Analysis of enterprise financial statements [1]
Go web programming practice (1) -- basic syntax of go language
Makefile: usage of control functions (error, warning, info)
China's crude oil heater market trend report, technological innovation and market forecast
Internal/validators js:124 throw new ERR_ INVALID_ ARG_ Type (name, 'string', value) -- solution
26 FPS video super-resolution model DAP! Output 720p Video Online
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
[CV] Wu Enda machine learning course notes | Chapter 12
Customized Huawei hg8546m restores Huawei's original interface
Add two numbers of leetcode
Research Report on market supply and demand and strategy of China's plastic pump industry
Construction and maintenance of business website [2]
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
Write the content into the picture with type or echo and view it with WinHex
Research Report on market supply and demand and strategy of Chinese garden equipment industry
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022
Happy Lantern Festival! Tengyuanhu made you a bowl of hot dumplings!