当前位置:网站首页>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;

边栏推荐
- Golang embeds variables in strings
- Web3js method to obtain account information and balance
- [shutter] statefulwidget component (pageview component)
- JDBC | Chapter 3: SQL precompile and anti injection crud operation
- The metamask method is used to obtain account information
- Market trend report, technical innovation and market forecast of China's Micro pliers
- Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
- [CV] Wu Enda machine learning course notes | Chapter 12
- qwb2018_ core kernel_ rop
- JDBC | Chapter 4: transaction commit and rollback
猜你喜欢
![[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)](/img/a7/0b87fa45ef2edd6fac519b40adbeae.gif)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)

kernel_ uaf

7. Build native development environment

Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition

Check the confession items of 6 yyds

Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class

Detailed upgrade process of AWS eks

Baidu sued a company called "Ciba screen"

6 pyspark Library

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
随机推荐
Import a large amount of data to redis in shell mode
Cardinality sorting (detailed illustration)
Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
Analysis of enterprise financial statements [1]
Happy Lantern Festival! Tengyuanhu made you a bowl of hot dumplings!
In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028
Research Report on the overall scale, major manufacturers, major regions, products and applications of friction dampers in the global market in 2022
Construction and maintenance of business websites [6]
Roommate, a king of time, I took care of the C language structure memory alignment
Construction and maintenance of business website [2]
Add two numbers of leetcode
Sweet talk generator, regular greeting email machine... Open source programmers pay too much for this Valentine's day
Analysis of enterprise financial statements [2]
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
Analyze comp-206 advanced UNIX utils
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
Welfare | Hupu isux11 Anniversary Edition is limited to hand sale!
This team with billions of data access and open source dreams is waiting for you to join
MySQL learning notes (Advanced)
Go cache of go cache series