3 用SQL块编程完成下列查询要求: (1)求购买了供应商"宝洁"产品的所有顾客; (2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名); (3)求牙膏卖出数量最多的供应商。 (4)将所有的牙膏商品单价增加10%。(SQL语句) (5) 删除从未被购买的商品记录。(SQL语句)
时间: 2024-03-17 18:40:12 浏览: 133
SQL 实现数据查询
1. 求购买了供应商"宝洁"产品的所有顾客:
```
SELECT DISTINCT c.name FROM customer c, orders o, order_details od, product p, supplier s
WHERE c.customer_id = o.customer_id
AND o.order_id = od.order_id
AND od.product_id = p.product_id
AND p.supplier_id = s.supplier_id
AND s.supplier_name = '宝洁';
```
2. 求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名):
```
SELECT DISTINCT c.name FROM customer c, orders o, order_details od, product p
WHERE c.customer_id = o.customer_id
AND o.order_id = od.order_id
AND od.product_id = p.product_id
AND p.product_name IN (
SELECT DISTINCT p.product_name FROM customer c, orders o, order_details od, product p
WHERE c.name = 'Dennis'
AND c.customer_id = o.customer_id
AND o.order_id = od.order_id
AND od.product_id = p.product_id
);
```
3. 求牙膏卖出数量最多的供应商:
```
SELECT s.supplier_name FROM supplier s, product p, order_details od
WHERE s.supplier_id = p.supplier_id
AND p.product_id = od.product_id
AND p.product_name = '牙膏'
GROUP BY s.supplier_name
ORDER BY SUM(od.quantity) DESC
LIMIT 1;
```
4. 将所有的牙膏商品单价增加10%:
```
UPDATE product SET price = price * 1.1 WHERE product_name = '牙膏';
```
5. 删除从未被购买的商品记录:
```
DELETE FROM product WHERE product_id NOT IN (SELECT product_id FROM order_details);
```
阅读全文