当前位置:网站首页>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;
边栏推荐
- First hand evaluation of Reza electronics rz/g2l development board
- Test shift right: Elk practice of online quality monitoring
- 1038 Recover the Smallest Number
- [overview of AUTOSAR three RTE]
- 全志A40i/T3如何通过SPI转CAN
- 基于ARM RK3568的红外热成像体温检测系统
- [flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
- [C language] branch and loop statements (Part 1)
- 18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
- Deep analysis of data storage in memory
猜你喜欢
随机推荐
【FH-GFSK】FH-GFSK信号分析与盲解调研究
Inversion de l'intervalle spécifié dans la liste des liens
【爱死机】《吉巴罗》被忽略的细节
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
MySQL基础用法02
攻克哈希的基本概念与实现
Leetcode-2115: find all the dishes that can be made from the given raw materials
异步、邮件、定时三大任务
数据分析思维分析犯法和业务知识——分析方法(一)
Infrared thermography temperature detection system based on arm rk3568
How wide does the dual inline for bread board need?
On Fibonacci sequence
【无标题】
[shutter] image component (cached_network_image network image caching plug-in)
Every k nodes in the linked list are flipped
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
链表内指定区间反转
excel IF公式判断两列是否相同
【C语言】分支和循环语句(上)