sql query

sql query

  • (1) What is
    order by
  • (2) How to sort
    "" database query (sort: order by)
    SELECT * FROM table name ORDER BY sort field ASC (ascending order)|DESC (descending order);
  • (3) Features
    Specify columns
    Specify ascending or descending
    order by order by Sorting is only valid for numbers and English strings, not for Chinese characters.
    One or more columns can be specified as sorting conditions
  • (4) The dictionary order is invalid for Chinese
    admin delete

Initialization data

# 准备数据
drop database day13;
create database day13;
use day13;
# 执行
create table category (
  cid int primary key auto_increment,
  cname varchar(50)
);
create table products(
  pid int primary key ,
  pname varchar(50),
  price int,
  flag varchar(2),				#是否上架标记为:1表示上架、0表示下架
  category_id int
);
alter table products add foreign key (category_id) references category (cid);


#分类
insert into category(cid,cname) values(1,'家电');
insert into category(cid,cname) values(2,'服饰');
insert into category(cid,cname) values(3,'化妆品');
#商品
insert into products(pid, pname,price,flag,category_id) values('1','联想',5000,'1',1);
insert into products(pid, pname,price,flag,category_id) values('2','海尔',3000,'1',1);
insert into products(pid, pname,price,flag,category_id) values('3','雷神',5000,'1',1);

insert into products (pid, pname,price,flag,category_id) values('4','杰克琼斯',800,'1',2);
insert into products (pid, pname,price,flag,category_id) values('5','真维斯',200,'1',2);
insert into products (pid, pname,price,flag,category_id) values('6','花花公子',440,'1',2);
insert into products (pid, pname,price,flag,category_id) values('7','劲霸',2000,'1',2);

insert into products (pid, pname,price,flag,category_id) values('8','香奈儿',800,'1',2);
insert into products (pid, pname,price,flag,category_id) values('9','相宜本草',200,'1',2);

Case

1.使用价格排序(降序) -- 可以指定一个列

select * from products order by price desc;2.在价格排序(降序)的基础上,以分类排序(降序)-- 可以指定多个列

select * from products order by price desc,category_id desc;3.显示商品的价格(去重复),并排序(降序) -- distinct去掉重复

select distinct price from products order by price desc

sql query-aggregate function***

  • (1) What is an aggregate function?
    Aggregate function (similar to the method in Java: function name())
  • (2) What are the commonly used aggregate functions?
    Five aggregate functions:
    "count: count the number of rows of records where the specified column is not NULL;
    such as: count the number of rows in the pname column, if there is a NULL value, then no statistics
    " sum: calculate the value of the specified column, if the column type is specified If it is not a numeric type, then the calculation result is 0;
    "max: Calculate the maximum value of the specified column, if the specified column is a string type, then use string sorting operations;
    "min: Calculate the minimum value of the specified column, if the specified column is a character String type, then use string sorting operation;
    》avg: Calculate the average value of the specified column, if the specified column type is not a numeric type, then the calculation result is 0;
  • (3) Statement execution order
    from ->where ->count(*)
    SELECT COUNT(*) FROM product WHERE price > 200;
1.使用价格排序(降序) -- 可以指定一个列

select * from products order by price desc;2.在价格排序(降序)的基础上,以分类排序(降序)-- 可以指定多个列

select * from products order by price desc,category_id desc;3.显示商品的价格(去重复),并排序(降序) -- distinct去掉重复

select distinct price from products order by price desc

# person.eat() 方法或者函数  sql也是有函数 xxx()1 查询商品的总条数

select count(*) from products 

》2 查询价格大于200商品的总条数

select count(*) from products where price > 2003 查询分类为 1 的所有商品的总和

select sum(price) from products where category_id = 14 查询分类为 2 所有商品的平均价格

select avg(price) from products where category_id = 25 查询商品的最大价格和最小价格

select max(price),min(price) from products

sql query-grouping***

  • (1) What is grouping? Divide the
    data into N groups according to a certain rule or a certain feature.
    Generally, the id is not grouped.
    If the results after the grouping are filtered, you must use having instead of where
  • (2) How to group
    SELECT 字段1,字段2… FROM 表名 GROUP BY 分组字段 HAVING 分组条件;
    from> group by -> count(*) ->having -> select
  • (3) Case:
    1 Count the number of commodities in each category
    2 Count the number of commodities in each category, and only display the information with a number greater than 3
select 字段1,字段2from 表名 group by 分组字段 having 分组条件; 

1 统计各个分类商品的个数
select category_id ,count(*) from products group by category_id ;

2 统计各个分类商品的个数,且只显示个数大于3的信息
select category_id ,count(*) c from products group by category_id having c>3  ;

Pagination***

  • (1) What is paging?
    Database paging is also to write query statements in the database. The difference is that the query is for the specified number to the specified number of data , not all the data at once.
  • (2) How to page? ***
    SELECT field 1, field 2... FROM table name LIMIT M, N
    (M: indicates which row index (starting from 0) starts to display, N indicates how many rows to display)
    Page 1: N*(1- 1) Page 2: N*(2-1) Page page: N*(page-1)
  • (3) Display the first 5 rows of the prouct table
    Insert picture description here

sql practice

  • (1) Initialize data
create table student(
	id int,
	name varchar(20),
	chinese int,
	english int,
	math int
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,53,95);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);

topic

#Output after sorting math scores.
#Sort the total scores and output them, and then output them in the order from high to low #Sort and output the
scores of students surnamed Li

Reference answer

#对数学成绩排序后输出。

select math from student order by math desc;

#对总分排序后输出,然后再按从高到低的顺序输出

select chinese+english+math as z from student order by z desc;

#对姓李的学生成绩排序输出

select name,chinese+english+math as z  from student where name like '李%' order by z desc

sql exercise 2

  • (1) Initialize data
create table emp(
	empno		int, -- 编号
	ename		varchar(50), -- 员工姓名
	job		varchar(50), -- 岗位名称
	mgr		int, -- 上级领导编号
	hiredate	date,-- 入职日期
	sal		int, -- 工资
	comm		int, -- 奖金
	deptno		int  --  部门编号
) ;

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);

  • (2) Case 2
-- 1、按员工编号升序排列不在10号部门工作的员工信息

-- 2、查询姓名第二个字母不是”A”且薪水大于800元的员工信息,按年薪降序排列

-- 3、求每个部门的平均薪水

-- 4、求各个部门的最高薪水

-- 5、求每个部门每个岗位的最高薪水

-- 6、求平均薪水大于2000的部门编号

-- 7、将部门平均薪水大于1500的部门编号列出来,按部门平均薪水降序排列

Reference answer

-- 1、按员工编号升序排列不在10号部门工作的员工信息

select * from emp where deptno != 10 order by empno asc;

-- 2、查询姓名第二个字母不是”A”且薪水大于800元的员工信息,按年薪降序排列 not()取反

select * ,(sal*12+ifnull(comm,0)) nx from emp where sal > 800 and not(ename like '_A%') order by nx desc ;

-- 3、求每个部门的平均薪水

select deptno,avg(sal) from emp group by deptno;

-- 4、求各个部门的最高薪水

select deptno,max(sal) from emp group by deptno;

-- 5、求每个部门每个岗位的最高薪水

select deptno,job,max(sal) from emp group by deptno,job;

-- 6、求平均薪水大于2000的部门编号

select deptno, avg(sal) pingjun from emp group by deptno having pingjun > 2000

select deptno from emp group by deptno having  avg(sal)   > 2000

-- 7、将部门平均薪水大于1500的部门编号列出来,按部门平均薪水降序排列

select deptno, avg(sal) p from emp group by deptno having p > 1500 order by p desc

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108759575