5.分组查询 在图书读者数据库中: 1)按图书类别查找,每类图书中的最高定价,平均定价2)求机械工业出版社出版的各类图书的平均定价,用GROUPBY表示 3)求计算机类图书的总册数和总价格 4)列出借阅次数超过4次的读者编号及借阅次数 5)查询机械工业出版社计算机类图书的数量
时间: 2024-02-05 19:14:21 浏览: 109
1) 按图书类别查找,每类图书中的最高定价和平均定价:
```
SELECT category, MAX(price) AS max_price, AVG(price) AS avg_price
FROM books
GROUP BY category;
```
2) 求机械工业出版社出版的各类图书的平均定价,用GROUP BY表示:
```
SELECT category, AVG(price) AS avg_price
FROM books
WHERE publisher = '机械工业出版社'
GROUP BY category;
```
3) 求计算机类图书的总册数和总价格:
```
SELECT SUM(quantity) AS total_quantity, SUM(quantity * price) AS total_price
FROM books
WHERE category = '计算机';
```
4) 列出借阅次数超过4次的读者编号及借阅次数:
```
SELECT reader_id, COUNT(*) AS borrow_times
FROM borrow_records
GROUP BY reader_id
HAVING borrow_times > 4;
```
5) 查询机械工业出版社计算机类图书的数量:
```
SELECT COUNT(*) AS num_of_books
FROM books
WHERE publisher = '机械工业出版社' AND category = '计算机';
```
阅读全文