-- a.请从表中查找出名称包含“企鹅”的所有商品
SELECT * FROM product WHERE NAME LIKE '%企鹅%';
-- b.查询Cash第二贵的物品的Name
SELECT NAME FROM product where Cash=(select distinct Cash from product order by Cash desc limit 1,1)
-- c.请插入一条名称为“角色卡”,Cash“100”的新数据
INSERT INTO product(NAME,Cash) VALUE('角色卡','100');
-- d.请更新“皇帝企鹅2代”的Cash,增加20
UPDATE product SET Cash=Cash+20 WHERE NAME='皇帝企鹅2代';
-- 1.用sql语句找出是计算机系并籍贯是北京的学生的所有记录
条件:
-- 多表关联
SELECT* FROM Test1
-- 普通字段过滤 where
WHERE Department = '计算机系'AND place='北京'
答:
SELECT * FROM Test1 WHERE Department = '计算机系'AND place='北京';
- 1.用sql语句把计算机系的学生的Department改为信息学院
条件:
-- 更新哪张表
UPDATE Test1
-- 修改那个字段
SET Department='信息学院'
-- 条件
WHERE Department='计算机系'
答:
UPDATE Test1 SET Department = '信息学院' WHERE Department = '计算机系';
-- 3.用sql连接查询找出成绩大于等于75分的学生的No,Name,Grade,Courses字段的记录
条件:
-- 多表关联
SELECT* FROM test1 a JOIN Test2 b ON a.No=b.No
-- 普通字段过滤
WHERE Grade>=75
-- 展示字段
a.no,a.Name,b.Grade,b.Courses
答:
SELECT a.no,a.Name,b.Grade,b.Courses
FROM TEST1 a JOIN Test2 b ON a.No=b.No
GROUP BY NAME
WHERE Grade>75
-- 4.用sql连接查询找出总分最高的学生的No,Name,总分数
条件:
-- 多表关联
SELECT* FROM test1 a JOIN Test2 b ON a.No=b.No
-- 分组group by
GROUP BY a.no
-- 排序
ORDER BY SUM(b.Grade)DESC
-- 使用limit
LIMIT 1
-- 展示字段
a.no,a.name
答:
SELECT SUM(b.Grade)
FROM test1 a JOIN Test2 b ON a.No=b.No
GROUP BY a.no
ORDER BY SUM(b.Grade)DESC
LIMIT 1;
SELECT a.no,a.name
FROM test1 a JOIN Test2 b ON a.No=b.No
GROUP BY a.no
HAVING SUM(b.Grade)
-- 4.1根据商品价格正序/倒序排列
SELECT prod_price FROM products ORDER BY prod_price ;
SELECT prod_price FROM products ORDER BY prod_price DESC;
-- 4.2选择商品价格最高的商品prod_id及vend_id
SELECT MAX(prod_price) FROM products;
SELECT prod_id,vend_id FROM products WHERE prod_price IN (SELECT MAX(prod_price) FROM products);
-- 4.3统计每个供应商的商品总价大于20的数据,并倒序排列
SELECT vend_id,SUM(prod_price)FROM products GROUP BY vend_id HAVING SUM(prod_price)>20
-- 4.4根据vent_id关联两张表,选出每个供应商的商品价格最高的数据vent_id,vend_name,prod_id,prode_name,prod_price
SELECT vend_id,MAX(prod_price)FROM products GROUP BY vend_id;
SELECT (a.vend_id,b.vend_name,a.prod_id,prod_name,prod_price)
FROM products a JOIN vendors b ON a.vend_id=b.vend_id
GROUP BY a.vend_id
ORDER BY a.prod_price DESC;
1. SELECT * FROM A a LEFT JOIN B b ON a.Num=b.Num;
SELECT * FROM A a RIGHT JOIN B b ON a.Num=b.Num;
-- 1.查询Level是L2的所有员工的Name和Department信息。如果有Grade信息,把Grade信息也查询出来,并按照Department升序排序。
SELECT NAME,Department
FROM Employeeinfo,Department
WHERE ID=EmployeeID,LEVEL='L2'
GROUP BY Department ASC;
-- 多表关联
SELECT*FROM Employeeinfo a JOIN Department b ON a.eid=b.pid
-- 对字段过滤
WHERE a.elevel='L2'
-- 排序
ORDER BY a.edepartemt
-- 展示字段
a.ename,a.edepartment,b.pgrade
-- 拼装成sql语句
SELECT a.ename,a.edepartment,b.pgrade FROM Employeeinfo a
-- 2.更新所有Department=CS的员工的所有绩效成绩为D。
条件 :
-- 更新哪张表
performance
-- 更新那个字段
SET pgrade='D'
-- 过滤条件
WHERE edepartment='CS'
-- 发现不在同一张表中,用嵌套查询
-- 1.只能根据上述的条件找到查询出跟另一张表有关系的字段
SELECT eid FROM Employeeinfo WHERE edepartment='CS'
-- 2.拿出查询出的结果,根据关联字段写更新条件
WHERE pid IN (SELECT eid FROM Employeeinfo WHERE edepartment='CS')
答:update performance SET pgrade='D'WHERE pid IN (SELECT eid FROM Employeeinfo WHERE edepartment='CS')
-- 3.统计有2次绩效成绩为A的员工Name和Department信息
条件:
-- 多表关联
SELECTFROM Employeeinfo a JOIN Performance b ON a.eid=b.pid
-- 分组,分组字段name
GROUP BY a.ename
-- 聚合函数过滤
HAVING COUNT()=2
-- 普通字段筛选
WHERE pgrade='A'
-- 展示字段Name Department
ename,edepartment
-- 拼装成sql
SELECT ename,edepartment FROM Employeeinfo a JOIN Performance b ON a.eid=b.pid WHERE pgrade='A'
-- 1.用sql语句找出是计算机系并籍贯是北京的学生的所有记录
条件:
-- 多表关联
SELECT* FROM Test1
-- 普通字段过滤 where
WHERE Department = '计算机系'AND place='北京'
答:
SELECT * FROM Test1 WHERE Department = '计算机系'AND place='北京';
-- 2.用sql语句把计算机系的学生的Department改为信息学院
条件:
-- 更新哪张表
UPDATE Test1
-- 修改那个字段
SET Department='信息学院'
-- 条件
WHERE Department='计算机系'
答:
UPDATE Test1 SET Department = '信息学院' WHERE Department = '计算机系';
-- 3.用sql连接查询找出成绩大于等于75分的学生的No,Name,Grade,Courses字段的记录
条件:
-- 多表关联
SELECT* FROM test1 a JOIN Test2 b ON a.No=b.No
-- 普通字段过滤
WHERE Grade>=75
-- 展示字段
a.no,a.Name,b.Grade,b.Courses
答:
SELECT a.no,a.Name,b.Grade,b.Courses
FROM TEST1 a JOIN Test2 b ON a.No=b.No
GROUP BY NAME
WHERE Grade>75
-- 4.用sql连接查询找出总分最高的学生的No,Name,总分数
条件:
-- 多表关联
SELECT* FROM test1 a JOIN Test2 b ON a.No=b.No
-- 分组group by
GROUP BY a.no
-- 排序
ORDER BY SUM(b.Grade)DESC
-- 使用limit
LIMIT 1
-- 展示字段
a.no,a.name
答:
SELECT SUM(b.Grade)
FROM test1 a JOIN Test2 b ON a.No=b.No
GROUP BY a.no
ORDER BY SUM(b.Grade)DESC
LIMIT 1;
SELECT a.no,a.name
FROM test1 a JOIN Test2 b ON a.No=b.No
GROUP BY a.no
HAVING SUM(b.Grade)
-- 1.查询书名“B”开头且2014年之后购入的书籍清单,显示字段 barcode,name,amount,date
条件:
-- 多表关联
SELECT*FROM A JOIN B ON a.Barcode=b.Barcode
-- 普通字段过滤
WHERE a.name LIKE 'B%'AND DATE>='2014-01-01'
-- 展示字段
a.barcode,a.name,b.amount,b.date
答:
SELECT a.barcode,a.name,b.amount,b.date FROM A JOIN B ON a.Barcode=b.Barcode WHERE a.name LIKE 'B%'AND DATE>='2014-01-01';
-- 2.统计每部书的总数量,显示字段:barcode,name,总量
条件:
-- 多表关联
SELECT*FROM A JOIN B ON a.Barcode=b.Barcode
-- 分组group by
GROUP BY A.barcode
-- 展示字段
a.barcode,a.NAME,SUM(Amount)
答:
SELECT SUM(Amount),a.barcode,a.NAME FROM A JOIN B ON a.Barcode=b.Barcode GROUP BY barcode SUM(Amount)
-- 3.统计购入次数大于1次的书,显示字段:barcode,name,购入次数
SELECT barcode,NAME
FROM A JOIN B ON a.Barcode=b.Barcode
GROUP BY a.Barcode
HAVING COUNT()>1;
-- 4.查询表中共有多少条操作记录
SELECT COUNT() FROM Audit;
-- 5.查询最近的10条操作记录的内容
SELECT * FROM Audit ORDER BY CreateTime DESC LIMIT 10;