练习1

CREATE TABLE product(

pid INT PRIMARY KEY,#主键ID

pname VARCHAR(20),#商品名称

price DOUBLE,#商品价格

category_name VARCHAR(32)#商品分类名称

);

INSERT INTO product(pid,pname,price,category_name) VALUES(1,'联想电脑',5000,'电脑办公');

INSERT INTO product(pid,pname,price,category_name) VALUES(2,'海尔电脑',3000,'电脑办公');

INSERT INTO product(pid,pname,price,category_name) VALUES(3,'雷神电脑',5000,'电脑办公');

INSERT INTO product(pid,pname,price,category_name) VALUES(4,'JACK JONES',800,'服装');

INSERT INTO product(pid,pname,price,category_name) VALUES(5,'真维斯',200,'服装');

INSERT INTO product(pid,pname,price,category_name) VALUES(6,'花花公子',440,'服装');

INSERT INTO product(pid,pname,price,category_name) VALUES(7,'劲霸',2000,'服装');

INSERT INTO product(pid,pname,price,category_name) VALUES(8,'香奈儿',800,'女士用品');

INSERT INTO product(pid,pname,price,category_name) VALUES(9,'相宜本草',200,'女士用品');

INSERT INTO product(pid,pname,price,category_name) VALUES(10,'面霸',5,'女士用品');

INSERT INTO product(pid,pname,price,category_name) VALUES(11,'雪碧',56,'饮料饮品');

INSERT INTO product(pid,pname,price,category_name) VALUES(12,'香飘飘奶茶',1,'饮料饮品');

INSERT INTO product(pid,pname,price,category_name) VALUES(13,'iPhone9',8000,NULL);

#查询product表中所有记录

SELECT * FROM product

#查询product表中pid和pname字段

SELECT pid,pname FROM product

#查询product表中所有的电脑办公记录

SELECT * FROM product WHERE category_name="电脑办公"

#查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.

SELECT pname AS "名称",price+10 AS "价格" FROM product

#查询商品名称为“花花公子”的商品所有信息:

SELECT *FROM product WHERE pname="花花公子"

#查询价格为800商品

SELECT *FROM product WHERE price=800

#查询价格不是800的所有商品

SELECT *FROM product WHERE price!=800

#查询商品价格大于60元的所有商品信息

SELECT *FROM product WHERE price>60

#查询商品价格在200到1000之间所有商品

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

#查询商品价格是200或800或者2000的所有商品

SELECT *FROM product WHERE price=20 OR price=800 OR price=2000

SELECT *FROM product WHERE price=20|| price=800 || price=2000

SELECT *FROM product WHERE price IN(20,800,2000)

#查询含有'霸'字的所有商品

SELECT *FROM product WHERE pname LIKE '%霸%'

#查询以'香'开头的所有商品

SELECT *FROM product WHERE pname LIKE '香%'

#查询第二个字为'想'的所有商品

SELECT *FROM product WHERE pname LIKE '_想%'

#商品没有分类的商品

SELECT *FROM product WHERE category_name IS NULL

#查询有分类的商品

SELECT *FROM product WHERE category_name IS NOT NULL

#1.使用价格排序(降序)

SELECT *FROM product ORDER BY price DESC

#2.在价格排序(降序)的基础上

#若价格相同,相同价格的数据以pid降序排序

SELECT *FROM product ORDER BY price DESC,pid ASC

#1 查询商品的总条数

#查看商品总价格、最大价格、最小价格、价格的平均值

SELECT SUM(price) AS "总价格",MAX(price),MIN(price),AVG(price) FROM product

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

SELECT COUNT(1) FROM product WHERE price >200

#3 查询分类为'电脑办公'的所有商品的总记录

SELECT COUNT(1) FROM product WHERE category_name="电脑办公"

#4 查询分类为'服装'所有商品的平均价格

SELECT AVG(price) FROM product WHERE category_name="服装"

# 统计各个分类下商品的个数

SELECT * FROM product WHERE pid<=2

SELECT * FROM product LIMIT 4,2

找id=5 他在第三页(3-1)*2=4 (索引始为零)

#统计各个分类商品的个数,有且 只显示分类名称不为空值的数据

SELECT * FROM product WHERE category_name="女士用品" GROUP BY category_name HAVING category_name

IS NOT NULL ORDER BY pid ASC LIMIT 0,6

练习2

CREATE TABLE s (

sidCHAR(6),

snameVARCHAR(50),

ageINT,

genderVARCHAR(50)

);

INSERT INTO s VALUES('S_1001', 'liuYi', 35, 'male');

INSERT INTO s VALUES('S_1002', 'chenEr', 15, 'female');

INSERT INTO s VALUES('S_1003', 'zhangSan', 95, 'male');

INSERT INTO s VALUES('S_1004', 'liSi', 65, 'female');

INSERT INTO s VALUES('S_1005', 'wangWu', 55, 'male');

INSERT INTO s VALUES('S_1006', 'zhaoLiu', 75, 'female');

INSERT INTO s VALUES('S_1007', 'sunQi', 25, 'male');

INSERT INTO s VALUES('S_1008', 'zhouBa', 45, 'female');

INSERT INTO s VALUES('S_1009', 'wuJiu', 85, 'male');

INSERT INTO s VALUES('S_1010', 'zhengShi', 5, 'female');

INSERT INTO s VALUES('S_1011', 'xxx', NULL, NULL);

--  查询性别为女,并且年龄大于等于50的记录

SELECT *FROM s WHERE gender="female"&&age>50

--  查询学号为S_1001,或者姓名为liSi的记录

SELECT *FROM s WHERE sid="s_1001" ||sname="liSi"

--  查询学号为S_1001,S_1002,S_1003的记录

SELECT *FROM s WHERE sid="S_1001" OR sid="S_1002" OR sid="S_1003"

--  查询学号不是S_1001,S_1002,S_1003的记录

SELECT *FROM s WHERE sid!="S_1001" && sid!="S_1002" && sid!="S_1003"

--  查询年龄为null的记录

SELECT *FROM s WHERE age IS NULL

--  查询年龄在20到40之间的学生记录

SELECT *FROM s WHERE age>20&&age<=40

--  查询性别非男的学生记录

SELECT *FROM s WHERE gender!="male"

--  查询姓名不为null的学生记录

SELECT *FROM s WHERE sname IS NOT NULL

--  查询姓名由5个字母构成的学生记录

SELECT *FROM s WHERE sname LIKE "_____"

--  查询姓名由5个字母构成,并且第5个字母为“i”的学生记录

SELECT *FROM s WHERE sname LIKE "_____" &&sname NOT LIKE "____i"

--  查询姓名以“z”开头的学生记录

SELECT *FROM s WHERE sname LIKE "z%"

--  查询姓名中第2个字母为“i”的学生记录

SELECT *FROM s WHERE sname LIKE "_i%"

--  查询姓名中包含“a”字母的学生记录

SELECT *FROM s WHERE sname LIKE "%a%"

练习3

CREATE DATABASE test1;

USE test1;

##部门表

#DROP IF EXISTS TABLE DEPT;

CREATE TABLE DEPT(

DEPTNO INT PRIMARY KEY,##部门编号

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

##员工表

#DROP IF EXISTS TABLE EMP;

CREATE TABLE EMP(

EMPNO INT PRIMARY KEY,#员工编号

ENAME VARCHAR(10),#员工姓名

JOB VARCHAR(9),#员工工作

MGR INT, #员工直属领导编号

HIREDATE DATE, #入职时间

SAL DOUBLE,#工资

COMM DOUBLE,#奖金

DEPTNO INT#对应dept表的外键

);

## 添加 部门 和 员工 之间的主外键关系

ALTER TABLE EMP ADD CONSTRAINT FOREIGN KEY EMP(DEPTNO) REFERENCES DEPT (DEPTNO);

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-07-03',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-07-13',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,'1981-01-23',1300,NULL,10);

#1.查找部门是30的员工详细信息。

SELECT * FROM emp WHERE DEPTNO="30"

#2.找出从事clerk工作的员工的编号、姓名、部门号。

SELECT EMPNO,ENAME,JOB FROM emp WHERE JOB="CLERK"

#3.检索出奖金多于基本工资的员工信息。

SELECT * FROM emp WHERE COMM>SAL

#4.检索出奖金多于基本工资60%的员工信息。

SELECT * FROM emp WHERE comm>(SAL*0.6)

#5.找出10部门的经理、20部门的职员 的员工信息。

SELECT * FROM emp WHERE DEPTNO=10&&JOB="MANAGER"||DEPTNO=20&&JOB="CLERK"

#6.找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。

SELECT * FROM emp WHERE DEPTNO=10&&JOB="MANAGER"||DEPTNO=20&&JOB="CLERK"||JOB!="MANAGER"&&JOB!="CLERK"&&SAL>2000

#7.找出获得奖金的员工的信息。

SELECT * FROM emp WHERE COMM!=""

#8.找出奖金少于100或者没有获得奖金的员工的信息。

SELECT * FROM emp WHERE COMM<100||COMM IS NULL||COMM=""

#9.找出姓名以A、B、S开始的员工信息。

SELECT * FROM emp WHERE ENAME LIKE "A%"||ENAME LIKE "B%"||ENAME LIKE "C%"

#10.找到名字长度为6个字符的员工信息。

SELECT * FROM emp WHERE ENAME LIKE "______"

#11.名字中不包含R字符的员工信息。

SELECT * FROM emp WHERE ENAME NOT LIKE "%R%"

#12.返回员工的详细信息并按姓名排序。

SELECT * FROM emp ORDER BY ENAME ASC

#13.返回员工的信息并按工作降序工资升序排列。

SELECT * FROM emp ORDER BY SAL ASC,ENAME DESC

#14.计算员工的日薪(按30天)。

SELECT ENAME,(SAL/30) FROM emp

#15.找出姓名中包含A的员工信息。

SELECT * FROM emp WHERE ENAME LIKE "%A%"

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐