当前位置:网站首页>3.1. Simplified supplement to DQL
3.1. Simplified supplement to DQL
2022-07-23 11:54:00 【Piglet vs Hengge】
Catalog
01、 Create table : Student list (stu)、 Employee list (emp)
02、 Create student table (stu)、 Employee list (emp) Complete data query operation
01、 Create table : Student list (stu)、 Employee list (emp)
# Create database myStudent
CREATE DATABASE IF NOT EXISTS `myStudent`;
# Create student table :stu
CREATE TABLE stu (
sid CHAR(6),
sname VARCHAR(50),
age INT,
gender VARCHAR(50)
);
# Add data : Show the students stu Add data to
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
INSERT INTO stu VALUES('S_1012', NULL, 10, 'female');
# Create an employee table :emp
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT
) ;
# Add data : Report to employees emp Add data to
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,200,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);02、 Create student table (stu)、 Employee list (emp) Complete data query operation
#1. Basic query
#1.1 Inquire about stu All fields in the table (--> That is, query all columns )
SELECT `sid`,`sname`,`age`,`gender` FROM stu;
SELECT * FROM stu; # When there are too many fields , Query all fields , among * Express all
#1.2 Check the student list stu The specified column in : Such as query sid、sname Column
SELECT `sid`,`sname` FROM stu;
#2. Conditions of the query
/*2.1 Introduction to condition query
A conditional query is given at query time where Clause , stay where The following operators and keywords are used in the clause :
-->: = != <> < <= > >=
-->: between...and
-->: IN(set)
-->: IS NULL
-->: AND
-->: OR
-->: NOT
*/
## Query XX record = Query all records of a piece of data
#2.2 Check gender for female , And older than 50 The record of
SELECT * FROM `stu` WHERE `gender`='female' AND `age`>50;
#2.3 Check gender for female , And older than 50 Employee number and name
SELECT `sid`,`sname` FROM `stu` WHERE `gender`='female' AND `age`>50;
#2.4 The student ID is S_1001, Or the name is lisi The record of
SELECT * FROM stu WHERE `sid`='S_1001' OR `sname`='lisi';
#2.5 The student ID is S_1001,S_1002,S_1003 The record of
SELECT * FROM stu WHERE `sid`='S_1001' OR `sid`='S_1002' OR `sid`='S_1003';
SELECT * FROM stu WHERE `sid` IN('S_1001','S_1002','S_1003');
#2.6 It's not S_1001,S_1002,S_1003 The record of
SELECT * FROM stu WHERE `sid`!='S_1001' AND `sid`!='S_1002' AND `sid`!='S_1003';
SELECT * FROM stu WHERE sid NOT IN('S_1001','S_1002','S_1003');
#2.7 Query age is null The record of
SELECT * FROM stu WHERE `age` IS NULL;
#2.8 The age of inquiry is 20 To 40 Between student records
SELECT * FROM stu WHERE `age`>=20 AND `age`<=40;
SELECT * FROM stu WHERE `age` BETWEEN 20 AND 40;
#2.9 Check the student records of non male students
SELECT * FROM stu WHERE `gender`='female';
SELECT * FROM stu WHERE `gender`!='male';
SELECT * FROM stu WHERE `gender`<>'male';
SELECT * FROM stu WHERE NOT `gender`='male';
#2.10 The search name is not null Student records of
SELECT * FROM stu WHERE `sname` IS NOT NULL;
SELECT * FROM stu WHERE NOT `sname` IS NULL;
#3. Fuzzy query ---LIKE
/*
When you want to search for names that contain a Students of characters need to use fuzzy query .
Fuzzy query needs to use keywords :LIKE
wildcard :
_ Represents any character
% Express arbitrarily 0~n Characters
*/
#3.1 Search name by 5 Student records in letters
SELECT * FROM stu WHERE `sname` LIKE '_____';
#3.2 Search name by 5 Letter composition , And No 5 The letters are "i" Student records of
SELECT * FROM stu WHERE `sname` LIKE '____i';
#3.3 Check the name with “z” The student record at the beginning
SELECT * FROM stu WHERE `sname` LIKE 'z%';
#3.4 Check the number of 2 The letters are “i” Student records of
SELECT * FROM stu WHERE `sname` LIKE '_i%';
#3.5 The query name contains “a” The student record of letters
SELECT * FROM stu WHERE `sname` LIKE '%a%';
#4. Field control query
#4.1 Remove duplicate records :DISTINCT
SELECT DISTINCT sal FROM emp;
#4.2 Check the sum of the employee's monthly salary and Commission Be careful : A number with NULL Carry out operations , The result is NULL
/*(1)
because sal and comm Both columns are numeric , So you can add .
If sal or comm One of the fields in is not a numeric type , Then there will be mistakes .
for example : There will be data errors
SELECT *,sal+comm FROM emp;
Be careful : A number with NULL Carry out operations , The result is NULL
(2)
comm There are many recorded values listed as NULL, Because of anything with NULL The sum is still NULL,
So the settlement result may appear NULL. Now we use a NULL Convert to value 0 Function of IFNULL:
*/
SELECT *,sal+IFNULL(comm,0) FROM emp;
#4.3 Add an alias to the column name , Use As keyword As Keywords can be omitted
SELECT *,sal+IFNULL(comm,0) AS ' The sum of monthly salary and Commission ' FROM emp;
SELECT *,sal+IFNULL(comm,0) ' The sum of monthly salary and Commission ' FROM emp;
#5. Sort ORDER BY ASC Ascending ( Default sort ) DESC Descending
#5.1 Check all student records , Sort by age in ascending order
SELECT * FROM stu ORDER BY `age` ASC;
#5.2 Check all student records , Sort by age in descending order
SELECT * FROM stu ORDER BY `age` DESC;
#5.3 Check all employees , Sort by monthly salary in descending order , If the monthly salary is the same , Sort by number in ascending order
SELECT * FROM emp ORDER BY `sal` DESC,`empno` ASC;
#5.4 Check all employees , Sort by monthly salary in descending order , If the monthly salary is the same , Sort by number in descending order
SELECT * FROM emp ORDER BY sal DESC,empno DESC;
#5.5 Check that the salary is higher than 1600 Employee records of , And sort in descending order according to salary
SELECT * FROM emp WHERE sal>1600 ORDER BY sal DESC;
#6. Aggregate functions
/*
Aggregate function is a function used for vertical operation
(1)COUNT(): The specified column of statistics is not NULL The number of record lines
(2)MAX(): Calculate the maximum value of the specified column , If the specified column is of string type , So use string sort operation
(3)MIN(): Calculate the minimum value of the specified column , If the specified column is of string type , So use string sort operation
(4)SUM(): Calculates the value of the specified column and , If the specified column type is not a numeric type , So the result is 0;
(5)AVG(): Calculate the average value of the specified column , If the specified column type is not a numeric type , So the result is 0;
*/
#(1)COUNT(): The specified column of statistics is not NULL The number of record lines
SELECT COUNT(comm) AS ' Number of people with Commission ' FROM emp;
SELECT COUNT(mgr) AS ' Number of employees with leadership ' FROM emp;
SELECT COUNT(*) AS ' Number of data ' FROM emp;
#(2)MAX(): Calculate the maximum value of the specified column , If the specified column is of string type , So use string sort operation
SELECT MAX(sal) AS ' Maximum wage ' FROM emp;
#(3)MIN(): Calculate the minimum value of the specified column , If the specified column is of string type , So use string sort operation ;
SELECT MIN(sal) AS ' minimum wage ' FROM emp;
#(4)SUM(): Calculates the value of the specified column and , If the specified column type is not a numeric type , So the result is 0;
SELECT SUM(sal) AS ' The sum of employees' wages ' FROM emp;
#(5)AVG(): Calculate the average value of the specified column , If the specified column type is not a numeric type , So the result is 0;
SELECT AVG(sal) AS ' The average wage of the employees ' FROM emp;
# Inquire about emp The monthly salary in the table is more than 2500 The number of people
SELECT COUNT(*) AS ' The monthly salary is greater than 2500 The number of people ' FROM emp WHERE sal>2500;
# The sum of monthly salary and commission is greater than 2500 The number of yuan
SELECT COUNT(sal+IFNULL(comm,0)) AS ' The sum of monthly salary and commission is greater than 2500 The number of yuan ' FROM emp WHERE sal+IFNULL(comm,0)>2500;
# Check the monthly salary of all employees + Commission and
SELECT SUM(sal+IFNULL(comm,0)) AS ' Monthly salary of all employees + Commission and ' FROM emp;
边栏推荐
猜你喜欢

NFT digital collection development: what are the possible application scenarios of digital collections in the future?

window下vmware使用无线网卡nat的方式上网

uni原生插件开发--友盟一键登录

Activiti7 Quick Start experience sharing

NFT digital collection system development: Shenzhen Evening News "good times travel" digital collection online seconds chime

九、实用类

3、DQL(数据查询语句)

Kubesphere haproxy+kept (I)
![[flick]flick on yarn's flick conf simplest configuration](/img/de/0ec23f3379148dba27fe77dc51e22f.png)
[flick]flick on yarn's flick conf simplest configuration

Mosaic the face part of the picture
随机推荐
CTF web common software installation and environment construction
NFT数字藏品系统开发,数字藏品的发展趋势
Unable to negotiate with port 51732: no matching host key type found. Their offer:
Review of knowledge points
NFT digital collection system development, development trend of Digital Collections
[untitled]
Ten year structure five year Life-02 first job
NFT digital collection development: what are the possible application scenarios of digital collections in the future?
Sqli lab pass 17-22 notes
[hudi]hudi compilation and simple use of Hudi & spark and Hudi & Flink
MySQL事务
Introduction to the process structure used by activiti workflow
Man in the middle attacks ARP Spoofing and its linkage with beef XSS
Digital collection development / meta universe digital collection development
MySQL备份
数仓4.0笔记——用户行为数据采集四
MySQL用户管理
1.认识数据库
Wordcount of the first Flink program
Kubesphere haproxy+kept (I)