首页 > 数据库技术 > 详细

SQL查询小案例

时间:2019-10-23 23:27:50      阅读:137      评论:0      收藏:0      [点我收藏+]

这是一篇自学MySQL的小案例,下面是部分数据信息:goods表

技术分享图片

 

 

1、查询cate_name为‘超级本’的商品名称、价格


SELECT
`name`,
price
FROM
goods
WHERE cate_name like ‘超级本‘;


-- 2、查询商品的种类


SELECT
cate_name
FROM
goods
GROUP BY
cate_name;


-- 3、查询所有电脑产品的平均价格,并且保留两位小数

SELECT
ROUND(avg(price), 2) AS avg_price
FROM
goods;


-- 4、查询每种商品的平均价格

SELECT
cate_name,
avg(price) AS avg_price
FROM
goods
GROUP BY
cate_name;


-- 5、查询每种商品中的最高价格、最低价格,平均价、数量

SELECT
cate_name,
MAX(price) AS ma_price,
MIN(price) AS mi_price,
avg(price) AS avg_price,
COUNT(*)
FROM
goods
GROUP BY
cate_name;


-- 6、查询所有价格大于平均价格的商品、并且按价格降序排列

SELECT
id,
NAME,
cate_name,
brand_name,
price
FROM
goods
WHERE
price > (
SELECT
ROUND(AVG(price), 2)
FROM
goods
)
ORDER BY
price DESC;


-- 7、查询每种类型中最贵的电脑信息


SELECT
*
FROM
goods
INNER JOIN (
SELECT
cate_name,
MAX(price) AS max_price,
MIN(price) AS min_price,
COUNT(*)
FROM
goods
GROUP BY
cate_name
) AS goods_new_info ON goods.cate_name = goods_new_info.cate_name
AND goods.price = goods_new_info.max_price;

SQL查询小案例

原文:https://www.cnblogs.com/liuxiaomin/p/11728981.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!