当前位置:网站首页>MySQL-DQL
MySQL-DQL
2022-06-21 11:47:00 【Jue Niu thunder plough hot blade】
/* DQL Data query language , The full English name is Data Query Language, Used to query the data records of tables in the database . Grammar format : select [all|distinct] < The expression of the target column 1> [ Alias ], < The expression of the target column 2> [ Alias ]... from < Table or view name > [ Alias ],< Table or view name > [ Alias ]... [where< Conditional expression >] [group by < Name > [having < Conditional expression >]] [order by < Name > [asc|desc]] [limit < Number or list >]; Simplified syntax : select *| Name from surface where Conditions */
-- Create a product list :
create table product
(
pid int primary key auto_increment, -- Product id
pname varchar(20) not null, -- Commodity name
price double, -- commodity price
category_id varchar(20) -- Classification of goods
);
insert into product
values (null, ' Haier washing machine ', 5000, 'c001');
insert into product
values (null, ' Midea refrigerator ', 3000, 'c001');
insert into product
values (null, ' Gree air conditioning ', 5000, 'c001');
insert into product
values (null, ' Jiuyang rice cooker ', 200, 'c001');
insert into product
values (null, ' Woodpecker shirt ', 300, 'c002');
insert into product
values (null, ' Hengyuanxiang trousers ', 800, 'c002');
insert into product
values (null, ' Playboy jacket ', 440, 'c002');
insert into product
values (null, ' Jinba casual pants ', 266, 'c002');
insert into product
values (null, ' Hailan home sweater ', 180, 'c002');
insert into product
values (null, ' Jack Jones Sweatpants ', 430, 'c002');
insert into product
values (null, ' Lancome cream ', 300, 'c003');
insert into product
values (null, ' Estee Lauder essence water ', 200, 'c003');
insert into product
values (null, ' Chanel perfume ', 350, 'c003');
insert into product
values (null, 'SK-II GHb ', 350, 'c003');
insert into product
values (null, ' Shiseido foundation solution ', 180, 'c003');
insert into product
values (null, ' Old Beijing instant noodles ', 56, 'c004');
insert into product
values (null, ' Liangpin shop kelp silk ', 17, 'c004');
insert into product
values (null, ' Three squirrel nuts ', 88, null);
-- 1. Query all products .
select *
from product;
-- 2. Query the product name and price .
select pname, price
from product;
-- 3. Alias query . The keywords used are as(as Omission ).
-- 3.1 Table alias :
# select * from product as p;
select *
from product p;
-- 3.2 Column alias :
select pname pn
from product;
-- 4. Remove the repetition value .
select distinct price
from product;
# When all columns are the same , duplicate removal
# select distinct *
# from product;
-- 5. The query result is an expression ( Arithmetic query ): Put the price of all the goods +10 Yuan to display .
select pname, price + 10 as new_price
from product;
# Conditions of the query
-- The product name is “ Haier washing machine ” All information about our products
select *
from product
where pname = ' Haier washing machine ';
-- The inquiry price is 800 goods
select *
from product
where price = 800;
-- Inquiry price is not 800 All of our products
select *
from product
where price != 800;
select *
from product
where price <> 800;
select *
from product
where not price = 800;
-- The inquiry commodity price is greater than 60 All the product information of yuan
select *
from product
where price > 60;
-- Inquire about the price of goods in 200 To 1000 Between all the goods
select *
from product
where price >= 200
and price <= 1000;
select *
from product
where price >= 200
&& price <= 1000;
select *
from product
where price between 200 and 1000;
-- The inquiry of commodity price is 200 or 800 All of our products
select *
from product
where price = 200
or price = 800;
select *
from product
where price = 200
|| price = 800;
select *
from product
where price in (200, 800);
/* _: Match any single character %: Match any number of characters */
-- Query contains ' pants ' All the goods of the word
select *
from product
where pname like '% pants %';
-- Query to ' The sea ' All the merchandise at the beginning
select *
from product
where pname like ' The sea %';
-- The second word is ' Cardamom ' All of our products
select *
from product
where pname like '_ Cardamom %';
-- Inquire about category_id by null The goods
select *
from product
where category_id is null;
-- Inquire about category_id Not for null Classified Goods
select *
from product
where category_id is not null;
# For the minimum
select least(15, 48, 2, 45) min;
# contain null Will not be compared , The result is directly null
select least(15, null, 2, 45) min;
# For maximum
select greatest(15, 48, 89, 45) max;
# contain null Will not be compared , The result is directly null
select greatest(15, 48, null, 45) max;
# Sort query
# Price in descending order
select *
from product
order by price desc;
# On the basis of price descending order , In descending order of classification
# At the same price , Sort by category
select *
from product
order by price desc, category_id desc;
# Sort the de duplicated prices in descending order
select distinct price
from product
order by price desc;
# Aggregate query
# count() The specified column of statistics is not NULL The number of record lines ;
# sum() Calculates the value of the specified column and , If the specified column type is not a numeric type , So the result is 0;
# max() Calculate the maximum value of the specified column , If the specified column is of string type , So use string sort operation ;
# min() Calculate the minimum value of the specified column , If the specified column is of string type , So use string sort operation ;
# avg() Calculate the average value of the specified column , If the specified column type is not a numeric type , So the result is 0
# Query the total number of items
select count(*)
from product;
# The inquiry price is greater than 200 The total number of items
select count(*)
from product
where price > 200;
# The query is classified as 'c001' The sum of the prices of all the goods
select sum(price)
from product
where category_id = 'c001';
# Query the maximum price of goods
select max(price) max_price
from product;
# Query the minimum price of goods
select min(price) min_price
from product;
# The query is classified as 'c002' The average price of all goods
select avg(price)
from product
where category_id = 'c002';
/* 1、count Function pair null Value handling If count The argument of the function is asterisk (*), Then count the number of all records . If the parameter is a field , Excluding statistics null Number of records of value . 2、sum and avg Function pair null Value handling These two functions ignore null Existence of value , It's like the record doesn't exist . 3、max and min Function pair null Value handling max and min Both functions also ignore null Existence of value . */
# Group query
# Count the number of commodities in each category
# If you want to group , be select After that, only group fields and aggregate functions can appear
select category_id, count(*)
from product
group by category_id;
# Count the number of commodities in each category , And only show the number greater than 4 Product information of
select category_id, count(*) cnt
from product
group by category_id
having cnt > 4
order by cnt;
# Query exercise
create table student
(
id int,
gender varchar(10),
name varchar(20),
chinese int,
english int,
math int
);
insert into student
values (1, ' male ', ' Zhang Xiaoming ', 89, 78, 90);
insert into student
values (2, ' male ', ' Li Jin ', 67, 53, 95);
insert into student
values (3, ' male ', ' Wang Wu ', 87, 78, 77);
insert into student
values (4, ' male ', ' Li Yi ', 88, 98, 92);
insert into student
values (5, ' male ', ' Li Laicai ', 82, 84, 67);
insert into student
values (6, ' Woman ', ' Zhang Jinbao ', 55, 85, 45);
insert into student
values (7, ' Woman ', ' Huang Rong ', 75, 65, 30);
insert into student
values (7, ' Woman ', ' Huang Rong ', 75, 65, 30);
-- Query the information of all students in the table
select *
from student;
-- Look up the names of all the students and their English scores
select name, english
from student;
-- Filter duplicate data in table
select distinct *
from student;
-- Count the total score of each student
select name, chinese + english + math total_score
from student;
-- Add... To the total score of all students 10 Points of specialty points of
select name, chinese + english + math + 10 total_score
from student;
-- Use aliases to indicate student scores
select name, chinese chi, english eng, math
from student;
-- Query English score greater than 90 Classmate
select *
from student
where english > 90;
-- Total query score greater than 200 All students
select *, chinese + english + math
from student
where chinese + english + math > 200;
-- Query English scores in 80-90 Between the students
select *
from student
where english >= 80
and english <= 90;
select *
from student
where english between 80 and 90;
-- Query English score is not in 80-90 Between the students
select *
from student
where english < 80
or english > 90;
select *
from student
where english not between 80 and 90;
-- Query math score is 89,90,91 Classmate
select *
from student
where math in (89, 90, 91);
select *
from student
where math = 89
or math = 90
or math = 91;
-- Check the English scores of all students surnamed Li
select name, english
from student
where name like ' Li %';
-- Query math score 80 And Chinese is divided into 80 Classmate
select *
from student
where math = 80
and chinese = 80;
-- Query English 80 Or total score 200 Classmate
select *
from student
where english = 80
or chinese + english + math = 200;
-- Sort the math grades and output them
select *
from student
order by math desc;
-- After sorting the total score, output , And then output it from high to low
select *, chinese + english + math
from student
order by chinese + english + math desc;
-- Sort the students' grades by their surname Li
select *, chinese + english + math
from student
where name like ' Li %'
order by chinese + english + math desc;
# How many boys and girls are there , Sort by number of people in descending order , The number of people detected is greater than 4 Gender of
select gender, count(*) cnt
from student
group by gender
having cnt > 4
order by cnt desc;
create table emp
(
empno int, -- Employee number
ename varchar(50), -- Employee name
job varchar(50), -- Job name
mgr int, -- Superior leader number
hiredate date, -- Date of entry
sal int, -- Salary
comm int, -- Bonus
deptno int -- Department number
);
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, 0, 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);
-- 1、 It is not listed in ascending order of employee number 10 Employee information of department No
select *
from emp
where deptno != 10
order by empno;
-- 2、 The second letter of the query name is not ”A” And the salary is greater than 800 Employee information for , In descending order of annual salary
select *, (sal * 12 + ifnull(comm, 0)) year_sal
from emp
where ename not like '_a%'
and sal > 800
order by year_sal desc;
-- 3、 Average salary for each department
select deptno, avg(sal)
from emp
group by deptno;
-- 4、 Ask for the highest salary in each department
select deptno, max(sal)
from emp
group by deptno;
-- 5、 Ask for the highest salary for each position in each department
select deptno, job, max(sal)
from emp
group by deptno, job;
-- 6、 The average salary is greater than 2000 Department number of
select deptno, avg(sal) avg_sal
from emp
group by deptno
having avg_sal > 2000;
-- 7、 The average salary of the Department will be greater than 1500 The department number of the , Rank by department average salary in descending order
select deptno, avg(sal) avg_sal
from emp
group by deptno
having avg_sal > 1500
order by avg_sal desc;
-- 8、 Choose the name of the employee in the company who has the bonus , Wages
select ename, sal, comm
from emp
where comm is not null;
-- 9、 Find out the difference between the maximum wage and the minimum wage
select max(sal) - min(sal)
from emp;
边栏推荐
- 矩形覆盖面积
- Design and implementation of server security audit system
- [comprehensive pen test] sword finger offer II 114 Alien dictionary
- It is the German oscilloscope software and the keysight oscilloscope upper computer software ns-scope
- 旅行不能治愈心灵
- 【哈尔滨工业大学】考研初试复试资料分享
- 2022 safety officer-b certificate retraining question bank and simulated examination
- Flynk CDC reads MySQL 8 hours late. Set the servertimezone parameter
- Shell process control - 35. Multi branch case conditional statements
- Second harmonyos training
猜你喜欢
随机推荐
矩形覆盖面积
5 best practices for perfect security code auditing
QT operation on SQLite database multithreading
A Kuan food: the battle for "the first share of convenience food" continues
One article quick learning - playing with MySQL time operation function and detailed explanation of time matching operation + instance code
Use huggingface to quickly load pre training models and datasets in the moment pool cloud
中国企业海外业务DDoS防护探索
Five steps to successfully complete threat modeling
对文件夹下所有的文件一键改名
Flynk CDC reads MySQL 8 hours late. Set the servertimezone parameter
IMU选型、标定误差分析、AHRS组合导航
Factory mode implementation
Scholar magic changes QT creator plug-in framework (with examples)
Machine learning 2-linear regression
Implementation of qcustomplot based on qtquick
When gdpr knocks
服务器被入侵了怎么办
有意思的鼠标指针交互探究
WPF 使用 MAUI 的自绘制逻辑
2022 safety officer-b certificate retraining question bank and simulated examination




![SSD [target detection]](/img/f5/1a4a9868cddb24fb08db9fae4db290.png)




![[yolov5s target detection] opencv loads onnx model for reasoning on GPU](/img/87/49a54dfaf325fe104287235fe533c5.png)