SQL根据商品表(sh_goods)与商品评论表(sh_goods_comment )
1、查询商品id等于8且有效的评论内容。
2、查询每个用户评论的商品数量。
3、查询最新发布的5条有效商品评论信息。
4、查询评论过两种以上不同商品的用户id及对应的商品id。
5、查询没有任何评论信息的商品id和name。
6、查询商品评分为5星的商品评论信息。
select * from shop.sh_goods_comment;
select goods_id,content,is_show
from shop.sh_goods_comment where goods_id=8 and is_show=1;
-- 根据商品表(sh goods)与商品评论表(sh goods comment)查询商品id等于8且有效的评论内容
select goods_id,content,is_show
from shop.sh_goods_comment where goods_id=8 and is_show=1;
-- 查询每个用户评论的商品数量
SELECT user_id, COUNT(DISTINCT goods_id) as num_comments
FROM shop.sh_goods_comment
GROUP BY user_id;
-- 查询最新发布的5条有效商品评论信息
SELECT id, goods_id, content, create_time
FROM shop.sh_goods_comment
WHERE is_show = 1
ORDER BY create_time DESC
LIMIT 5;
-- 5、查询评论过两种以上不同商品的用户id和对应商品id
select user_id,goods_id,content
from sh_goods_comment
where user_id in (select user_id from sh_goods_comment group by user_id);
-- 6、查询没有任何评论信息的商品id和name
select id ,name from sh_goods
where id not in (select distinct(goods_id) from sh_goods_comment);
-- 7、查询商品评分为5星的商品评论信息
select content from sh_goods_comment
where goods_id in (select id from sh_goods where score=5);