当前位置:网站首页>Database SQL language 01 where condition
Database SQL language 01 where condition
2022-07-03 01:12:00 【Super brother 1986】
DQL Language
DQL( Data Query Language Data query language )
- Query database data , Such as SELECT sentence
- Simple single table query or complex query and nested query of multiple tables are the core of database language , The most important statement
- The most frequently used statement
SELECT grammar
SELECT [ALL | DISTINCT]
{
* | table.* | [table.field1[as alias1][,table.field2[as alias2]][,...]]}
FROM table_name [as table_alias]
[left | right | inner join table_name2] -- The joint query
[WHERE ...] -- Specify the conditions to be met for the result
[GROUP BY ...] -- Specifies which fields the results are grouped by
[HAVING] -- Filtering the grouped records must meet the secondary conditions
[ORDER BY ...] -- Specifies that query records are sorted by one or more criteria
[LIMIT {
[offset,]row_count | row_count OFFSET offset}];
-- Specify which records to query from
Be careful : [ ] Parentheses represent optional , { } The brackets mean you have to choose
-- Query all data column results in the table , use **" \* "** Symbol ;
-- Check all student information
SELECT * FROM student;
-- Query specified column ( Student number , full name )
SELECT studentno,studentname FROM student;
AS Clause as alias
effect :
- You can take a new alias for the data column
- A new alias can be given to the table
- The calculated or summarized results can be replaced by another new name
-- Here is the alias for the column ( Of course as Key words can be omitted )
SELECT studentno AS Student number ,studentname AS full name FROM student;
-- Use as You can also alias a table
SELECT studentno AS Student number ,studentname AS full name FROM student AS s;
-- Use as, Give the query a new name
-- CONCAT() Function concatenation string
SELECT CONCAT(' full name :',studentname) AS New name FROM student;
DISTINCT Use of keywords
effect : Get rid of SELECT Duplicate records in the returned record result of query ( Returns the same value for all columns ) , Just return one
-- # Check which students took the exam ( Student number ) Remove duplicates
SELECT * FROM result; -- Check exam results
SELECT studentno FROM result; -- Check which students took the exam
SELECT DISTINCT studentno FROM result; -- understand :DISTINCT Remove duplicates , ( The default is ALL)
Columns that use expressions
Expressions in the database : It's usually a text value , The column value , NULL , Functions and operators, etc
Application scenarios :
- SELECT Statement return result column
- SELECT Statement ORDER BY , HAVING Use in sub sentences
- DML Statement where Using expressions in conditional statements
-- selcet Expressions can be used in queries
SELECT @@auto_increment_increment; -- Query self increasing step size
SELECT VERSION(); -- Query version No
SELECT 100*3-1 AS The result of the calculation is ; -- expression
-- Students' test scores will be raised by one point
SELECT studentno,StudentResult+1 AS ' After scoring ' FROM result;
avoid SQL The return result contains ’ . ’ , ’ * ’ And parentheses interfere with the development of language programs .
where Conditional statements
effect : Used to retrieve data from a data table eligible The record of
The search criteria may consist of one or more logical expressions , The result is usually true or false .
Logical operators 
Example :
-- Queries that meet the criteria (where)
SELECT Studentno,StudentResult FROM result;
-- Check the test results in 95-100 Between
SELECT Studentno,StudentResult
FROM result
WHERE StudentResult>=95 AND StudentResult<=100;
-- AND Or you could write it as &&
SELECT Studentno,StudentResult
FROM result
WHERE StudentResult>=95 && StudentResult<=100;
-- Fuzzy query ( The corresponding words : Precise query )
SELECT Studentno,StudentResult
FROM result
WHERE StudentResult BETWEEN 95 AND 100;
-- except 1000 Classmate No , I want the results of other students
SELECT studentno,studentresult
FROM result
WHERE studentno!=1000;
-- Use NOT
SELECT studentno,studentresult
FROM result
WHERE NOT studentno=1000;
Fuzzy query : Comparison operator

Be careful :
- Only records of numeric data type can perform arithmetic operation ;
- Only data of the same data type can be compared ;
test :
-- Fuzzy query between and \ like \ in \ null
-- =============================================
-- LIKE
-- =============================================
-- Check the student number and name of the student surnamed Liu
-- like Wildcard used in combination : % ( representative 0 To any character ) _ ( A character )
SELECT studentno,studentname FROM student
WHERE studentname LIKE ' Liu %';
-- Inquire about Liu , There's only one word behind it
SELECT studentno,studentname FROM student
WHERE studentname LIKE ' Liu _';
-- Inquire about Liu , There are only two words behind it
SELECT studentno,studentname FROM student
WHERE studentname LIKE ' Liu ';
-- The query name contains Jia The word
SELECT studentno,studentname FROM student
WHERE studentname LIKE '% Jia %';
-- If the name contains special characters, you need to use escape symbols '\'
-- Custom escape key : ESCAPE ':'
-- =============================================
-- IN
-- =============================================
-- The student ID is 1000,1001,1002 Student name of
SELECT studentno,studentname FROM student
WHERE studentno IN (1000,1001,1002);
-- Inquiry address in Beijing , nanjing , Students in Luoyang, Henan Province
SELECT studentno,studentname,address FROM student
WHERE address IN (' Beijing ',' nanjing ',' Luoyang, Henan ');
-- =============================================
-- NULL empty
-- =============================================
-- The student's date of birth was not filled in
-- Don't write directly =NULL , It's a mistake , use is null
SELECT studentname FROM student
WHERE BornDate IS NULL;
-- Query the students who filled in the date of birth
SELECT studentname FROM student
WHERE BornDate IS NOT NULL;
-- Search for students who don't have a home address ( Empty string not equal to null)
SELECT studentname FROM student
WHERE Address='' OR Address IS NULL;
边栏推荐
- 【无标题】
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- Rk3568 development board evaluation (II): development environment construction
- 递归处理组织的几种情况
- Infrared thermography temperature detection system based on arm rk3568
- mysql 多表联合删除
- MySQL basic usage 02
- 解决ReactNative使用webView存在缓存问题
- Excel if formula determines whether the two columns are the same
- 异步、邮件、定时三大任务
猜你喜欢
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)

测试右移:线上质量监控 ELK 实战
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]

Basic use of sringcloud & use of component Nacos

Infrared thermography temperature detection system based on arm rk3568

Telephone network problems

FPGA - 7 Series FPGA internal structure clocking -04- multi area clock

异步、郵件、定時三大任務

excel去除小数点后面的数据,将数字取整
随机推荐
Strongly connected components of digraph
Basic use of sringcloud & use of component Nacos
excel去除小数点后面的数据,将数字取整
Leetcode-2115: find all the dishes that can be made from the given raw materials
Key wizard play strange learning - front desk and Intranet send background verification code
Compare version number
解决ReactNative使用webView存在缓存问题
MySQL multi table joint deletion
tail -f 、tail -F、tailf的区别
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
基于ARM RK3568的红外热成像体温检测系统
Solve the cache problem of reactnative using WebView
Key wizard play strange learning - multithreaded background coordinate recognition
Leetcode-849: maximum distance to the nearest person
按键精灵打怪学习-自动回城路线的判断
[AUTOSAR twelve mode management]
[自我管理]时间、精力与习惯管理
MySQL
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
拥抱平台化交付的安全理念