当前位置:网站首页>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;
边栏推荐
- R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
- 1038 Recover the Smallest Number
- Key wizard play strange learning - front desk and Intranet send background verification code
- ROS2之ESP32简单速度消息测试(极限频率)
- Test shift right: Elk practice of online quality monitoring
- 正确甄别API、REST API、RESTful API和Web Service之间的异同
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- 按键精灵打怪学习-多线程后台坐标识别
- JS inheritance and prototype chain
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
猜你喜欢

Assets, vulnerabilities, threats and events of the four elements of safe operation

Daily topic: movement of haystack

Strongly connected components of digraph

Foundations of data science is free to download

(C language) data storage

Infrared thermography temperature detection system based on arm rk3568
![1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]](/img/bf/ab6838e34a3074130eac0a9992e77c.png)
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]

How wide does the dual inline for bread board need?

excel表格计算时间日期的差值,并转化为分钟数

Initial order of pointer (basic)
随机推荐
有向图的强连通分量
[AUTOSAR VI description document]
MySQL基础用法02
matlab 多普勒效应产生振动信号和处理
Initial order of pointer (basic)
机器学习术语
excel表格计算时间日期的差值,并转化为分钟数
Excel if formula determines whether the two columns are the same
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
matlab查找某一行或者某一列在矩阵中的位置
【C语言】分支和循环语句(上)
Leetcode-2280: represents the minimum number of line segments of a line graph
How wide does the dual inline for bread board need?
Leetcode-934: the shortest Bridge
(C语言)数据的存储
Merge K sorted linked lists
Excel calculates the difference between time and date and converts it into minutes
[introduction to AUTOSAR seven tool chain]
无向图的割点
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement