MySQL SQL实验:高级数据查询与分析

需积分: 0 81 下载量 107 浏览量 更新于2024-08-03 收藏 20KB DOCX 举报
本次实验主要围绕MySQL数据库中的SQL查询语句展开,旨在提升学生对SQL语言的掌握,包括存在量词的应用、查询表的操作以及复杂数据汇总。以下是实验中涉及的关键知识点: 1. 查询最高订单金额: 学生需要使用子查询找出订单明细表`orderdetail`中订单金额的最大值,然后在外部查询中找到具有这个最大金额的订单记录。SQL语句为: ``` SELECT * FROM ordermaster WHERE orderSum = (SELECT MAX(orderSum) FROM ordermaster); ``` 2. 统计至少被订购3次的商品信息: 通过`GROUP BY`和`HAVING`子句,找出商品编号`productNo`、订单编号`orderNo`,以及订货数量和金额,按照订货数量降序排序: ``` SELECT productNo AS '商品编号', orderNo AS '订单编号', SUM(quantity) AS '订货数量', price AS '订货金额' FROM orderdetail GROUP BY productNo, orderNo HAVING SUM(quantity) > 2 ORDER BY SUM(quantity) DESC; ``` 3. 筛选订购商品数量不超过10个的客户: 联合`customer`和`ordertail`表,依据客户编号和订货数量条件筛选: ``` SELECT a.customerNo, a.customerName FROM customer a JOIN orderdetail b ON a.customerNo = b.customerNo JOIN ordermaster c ON a.customerNo = c.customerNo GROUP BY a.customerNo, a.customerName HAVING SUM(b.quantity) < 11; ``` 4. 查找至少订购3种商品的客户信息: 这需要结合多个表并使用`INSTR`函数来检查订购的商品种类数量,SQL语句示例未给出,但应包含`COUNT(DISTINCT productNo)`来计算不同商品。 5. 查询总销售金额最高的销售员信息: 同样使用子查询找出销售员的最高销售金额,然后获取对应信息: ``` SELECT s.salesmanNo, o.orderNo, o.orderDate, o.orderSum FROM salesman s JOIN ordermaster o ON s.salesmanNo = o.salesmanNo WHERE o.orderSum = (SELECT MAX(orderSum) FROM ordermaster); ``` 6. 计算每位客户每种商品的总数量和平均单价: 需要进行多表联接并分组统计: ``` SELECT customerNo, productNo, SUM(quantity) AS '总数量', AVG(price) AS '平均单价' FROM orderdetail GROUP BY customerNo, productNo ORDER BY customerNo, productNo; ``` 7. 业绩最好的业务员及其总销售金额: 类似于第5步,找到销售员总销售额最高者: ``` SELECT salesmanNo, salesmanName, SUM(orderSum) AS '总销售金额' FROM salesman JOIN ordermaster ON salesman.salesmanNo = ordermaster.salesmanNo GROUP BY salesmanNo, salesmanName ORDER BY SUM(orderSum) DESC LIMIT 1; ``` 8. 查找无订货记录的客户: 使用存在量词`EXISTS`来检测客户是否存在订货记录: ``` SELECT customerName FROM customer WHERE NOT EXISTS (SELECT * FROM orderdetail WHERE customer.customerNo = orderdetail.customerNo); ``` 9. 包含“手环”商品的订单信息: 通过商品名称筛选特定商品的订单: ``` SELECT orderNo, customerName, productName, quantity, price FROM orderdetail WHERE productName LIKE '%手环%' ORDER BY orderNo; ``` 10. 同时订购“酷睿四核”和“华为手环”的客户: 需要联合多个商品信息并筛选条件: ``` SELECT customerNo, orderNo, orderSum FROM ( SELECT customerNo, orderNo, orderSum FROM orderdetail WHERE productName = '酷睿四核' ) AS core_orders JOIN ( SELECT customerNo, orderNo, orderSum FROM orderdetail WHERE productName = '华为手环' ) AS huawei_orders ON core_orders.customerNo = huawei_orders.customerNo AND core_orders.orderNo = huawei_orders.orderNo; ``` 通过这些实验,学生将深入理解SQL语句在处理大量数据和复杂查询时的灵活性和实用性,同时提升其数据处理和分析能力。