当前位置:网站首页>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;
边栏推荐
- 18_ The wechat video number of wechat applet scrolls and automatically plays the video effect to achieve 2.0
- 465. 最优账单平衡 DFS 回溯
- The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- kivy教程之在 Kivy App 中使用 matplotlib 的示例
- (C language) data storage
- 合并K个已排序的链表
- tail -f 、tail -F、tailf的区别
- leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
- 瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
猜你喜欢
电话网络问题
Asynchronous, email and scheduled tasks
[AUTOSAR VI description document]
Advanced pointer (I)
Basic use of sringcloud & use of component Nacos
Explain the basic concepts and five attributes of RDD in detail
Draw love with go+ to express love to her beloved
Initial order of pointer (basic)
测试右移:线上质量监控 ELK 实战
机器学习术语
随机推荐
[自我管理]时间、精力与习惯管理
Leetcode-224: basic calculator
ROS2之ESP32简单速度消息测试(极限频率)
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
Assets, vulnerabilities, threats and events of the four elements of safe operation
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
Excel removes the data after the decimal point and rounds the number
按键精灵打怪学习-自动寻路回打怪点
Basic use of sringcloud & use of component Nacos
[AUTOSAR five methodology]
First hand evaluation of Reza electronics rz/g2l development board
Specified interval inversion in the linked list
【爱死机】《吉巴罗》被忽略的细节
这不平凡的两年,感谢我们一直在一起!
合并K个已排序的链表
Merge K sorted linked lists
基于ARM RK3568的红外热成像体温检测系统
Matlab Doppler effect produces vibration signal and processing
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)