練習一:
//創建種類表:
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(31)
);
insert into category values(null,'手機數碼','電子產品'),
(null,'鞋靴箱包','江南皮革廠'),
(null,'香菸酒水','二鍋頭'),
(null,'酸奶','哇哈哈'),
(null,'小零食','辣條');
//創建商品表:
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
pdate timestamp.
cno int
);
insert into product values
(null,'小米',998,null,1),
(null,'錘子',2888,null,1),
(null,'阿迪王',99,null,2),
(null,'老村長',88,null,3),
(null,'勁酒',35,null,3),
(null,'小熊餅乾',1,null,4),
(null,'衛龍辣條',1,null,5),
(null,'旺旺大餅',1,null,5);
查詢:
select …
from…
是先執行from在執行select
表別名:
select p.pname,p.price from product as p;
列別名:
select pname as 商品名稱,price as 商品價格 from product ;
查詢所有價格:
select price from product;
去重:
select distinct price from product;
select運算查詢:僅僅在查詢結果上做運算
select *,price*1.5 as 折後價 from product;
條件查詢 where關鍵字
select * from product where price > 60;
<> : 不等於,標準sql語句
!= : 不等於,非標準sql語句
select * from product where price <> 88;
查詢價格10到100的:
select * from product where price > 10 and price <100;
between…and…
select * from product where price between 10 and 100;
邏輯運算 and or not
–like :模糊查詢
_ :代表的是一個字符
% :代表的是多個字符
查詢出名字帶有餅的所有商品
select * from product where pname like '%餅%';
查詢第二個名字是熊的所有商品
select * from product where pname like '_熊%';
in在某個範圍中獲得值
查詢出商品分類ID在1,4,5裏面的所有商品
select * from product where cno in (1,4,5);
排序查詢: order by 關鍵字
asc: ascend 升序(默認)
desc: descend 降序
1,查詢所有商品,按照價格排序
select * from product order by price ;
select * from product order by price desc;
2,查詢名稱中包含 小的商品,按照價格升序排序
select * from product where pname like '%小%' order by price asc;
–聚合函數:
sum() : 求和
avg() : 求平均值
count() : 統計數量
max() : 最大值
min() : 最小值
1,獲取商品價格的總和
select sum(price) from product;
2,獲取商品價格的平均值
select avg(price) from product;
3,獲取所有商品的個數
select count(*) from product;
注意:where 條件後面不能接聚合函數
4,查出商品價格大於平均價格的所有商品
select * from product where price >(select avg(price) from product);
分組:group by
1,根據cno字段分組,分組後統計商品的個數
select cno,count(*) from product group by cno;
2,根據cno分組,分組統計每組商品的平均價格,並且商品平均價格>60
select cno,avg(price) from product group by cno having avg(price)>60;
having 關鍵字 可以接聚合函數的 出現在分組之後
where 關鍵字 不可以聚合函數 出現在分組之前
–編寫順序
– S…F…W…G…H…O
–執行順序
F..W..G..H..S..O
from.. where.. group by.. having.. select.. order by
練習二:
員工信息表
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
) ;
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);
INSERT INTO emp values(7981,'MILLER','CLERK',7788,'1992-01-23',2600,500,20);
部門信息表
CREATE TABLE dept(
deptno INT,
dname varchar(14),
loc varchar(13)
);
INSERT INTO dept values(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept values(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept values(30, 'SALES', 'CHICAGO');
INSERT INTO dept values(40, 'OPERATIONS', 'BOSTON');
基本查詢
--所有員工的信息
select e.*,d.dname,d.loc from emp e inner join dept d on d.deptno = e.deptno;
--薪資大於等於1000並且小於等於2000的員工信息
select e.*,d.dname,d.loc from emp e inner join dept d on d.deptno = e.deptno and e.sal>=1000
and e.sal<=2000;
--從員工表中查詢出所有的部門編號
select distinct e.deptno from emp e;
--查詢出名字以A開頭的員工的信息
select e.*,d.dname,d.loc from emp e inner join dept d on d.deptno = e.deptno and e.ename like 'A%';
--查詢出名字第二個字母是L的員工信息
select * from emp where ename like '_L%';
--查詢出沒有獎金的員工信息
select * from emp where comm is null;
--所有員工的平均工資
select avg(sal) as 平均工資 from emp;
--所有員工的工資總和
select sum(sal) as 總和 from emp;
--所有員工的數量
select count(*) from emp;
--最高工資
select max(sal) from emp;
--最少工資
select min(sal) from emp;
--最高工資的員工信息
select * from emp where sal = (select max(sal) from emp)
--最低工資的員工信息
select * from emp where sal = (select min(sal) from emp)
分組查詢
--每個部門的平均工資
select deptno,avg(sal) as 平均工資 from emp group by deptno;
子查詢
-- 單行子查詢(> < >= <= = <>)
-- 查詢出高於10號部門的平均工資的員工信息
select * from emp where sal >(select avg(sal) from emp where deptno=10);
-- 多行子查詢(in not in any all) >any >all
-- 查詢出比10號部門任何員工薪資高的員工信息
select * from emp where sal >(select max(sal) from emp where deptno=10);
-- 多列子查詢(實際使用較少) in
-- 和10號部門同名同工作的員工信息
select * from emp where deptno<>10 and (ename,job) in (select ename,job from emp where deptno=10)
-- Select接子查詢
-- 獲取員工的名字和部門的名字
select e.ename , d.dname from emp e,dept d where e.deptno = d.deptno
-- from後面接子查詢
-- 查詢emp表中經理信息
select * from emp where job='MANAGER';
-- where 接子查詢
-- 薪資高於10號部門平均工資的所有員工信息
select * from emp where sal>(select avg(sal) from emp where deptno=10)
-- having後面接子查詢
-- 有哪些部門的平均工資高於30號部門的平均工資
select deptno, avg(sal) from emp group by deptno having avg(sal)
>(select avg(sal) from emp where deptno=30)
-- 工資>JONES工資
select * from emp where sal >(select sal from emp where ename='JONES')
-- 查詢與SCOTT同一個部門的員工
select * from emp where deptno = (select deptno from emp where ename='SCOTT')
-- 工資高於30號部門所有人的員工信息
select * from emp where sal >(select max(sal) from emp where deptno =30)
SQL查詢的綜合案例
--查詢出高於本部門平均工資的員工信息
select * from emp e where sal > (select avg(sal) from emp where e.deptno = deptno)
--列出達拉斯加工作的人中,比紐約平均工資高的人
select * from emp where deptno =(select deptno from dept where loc='DALLAS')
and sal>(select avg(sal) from emp where deptno=(select deptno from dept where loc='NEW YORK'))
--查詢出各個部門薪水最高的員工所有信息
select * from emp e where sal =(select max(sal) from emp where deptno=e.deptno)