初始化資料
# 準備資料
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);
案例
》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
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 > 200
》3 查詢分類為 1 的所有商品的總和
select sum(price) from products where category_id = 1
》4 查詢分類為 2 所有商品的平均價格
select avg(price) from products where category_id = 2
》5 查詢商品的最大價格和最小价格
select max(price),min(price) from products
SELECT 欄位1,欄位2… FROM 表名 GROUP BY 分組欄位 HAVING 分組條件;
select 欄位1,欄位2… from 表名 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 ;
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);
題目
#對數學成績排序後輸出。
#對總分排序後輸出,然後再按從高到低的順序輸出
#對姓李的學生成績排序輸出
參考答案
#對數學成績排序後輸出。
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
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);
-- 1、按員工編號升序排列不在10號部門工作的員工資訊
-- 2、查詢姓名第二個字母不是」A」且薪水大於800元的員工資訊,按年薪降序排列
-- 3、求每個部門的平均薪水
-- 4、求各個部門的最高薪水
-- 5、求每個部門每個崗位的最高薪水
-- 6、求平均薪水大於2000的部門編號
-- 7、將部門平均薪水大於1500的部門編號列出來,按部門平均薪水降序排列
參考答案
-- 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