当前位置:网站首页>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;
边栏推荐
- 按鍵精靈打怪學習-多線程後臺坐標識別
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- MySQL
- kivy教程之在 Kivy App 中使用 matplotlib 的示例
- 递归处理组织的几种情况
- 链表中的节点每k个一组翻转
- R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
- 机器学习术语
- Initial order of pointer (basic)
- 基于ARM RK3568的红外热成像体温检测系统
猜你喜欢
![[introduction to AUTOSAR seven tool chain]](/img/cf/ed0ccf39d38e0b4fc3d97d4fd58a7e.png)
[introduction to AUTOSAR seven tool chain]

基于ARM RK3568的红外热成像体温检测系统

MySQL

Deep analysis of data storage in memory

12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet

基本远程连接工具Xshell

详解RDD基本概念、RDD五大属性

FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟

Assets, vulnerabilities, threats and events of the four elements of safe operation
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)
随机推荐
用Go+绘制爱心给心爱的她表白
Asynchronous, email and scheduled tasks
MongoDB系列之MongoDB常用命令
电话网络问题
按键精灵打怪学习-自动寻路回打怪点
ROS2之ESP32简单速度消息测试(极限频率)
异步、郵件、定時三大任務
【C语言】分支和循环语句(上)
1038 Recover the Smallest Number
matlab 多普勒效应产生振动信号和处理
数学建模之线性规划(含MATLAB代码)
Understanding and distinguishing of some noun concepts in adjustment / filtering
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
有向图的强连通分量
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
Key wizard play strange learning - multithreaded background coordinate recognition
1038 Recover the Smallest Number
【爱死机】《吉巴罗》被忽略的细节
MySQL