数据库Group和Join

创建category表:

create table category
(
  categoryId   int         not null
   primary key,
  categoryName varchar(50) null
);

原始数据:


origincat.png

创建product表:

create table product
(
  productId   int auto_increment
    primary key,
  productName varchar(50) null,
  categoryId  int         null,
  price       int         null,
  constraint product_category_categoryId_fk
  foreign key (categoryId) references category (categoryId)
);

create index product_category_categoryId_fk
  on product (categoryId);

原始数据:


originprod.png

只写join:

select * from `product` join category
output

只写join是做了笛卡尔积,4*2=8,每个product都拼上两种category
真正想要的是categoryId相等的情况

select * from `product` join category on product.categoryId = category.categoryId
output.png

注意:没有nike的记录,因为没有对应的categoryId
如果想把没有对应categoryId的产品也显示出来,用left join(左外连接)

select * from `product` left join category on product.categoryId = category.categoryId
output.png

也就是productcategoryIdnull的产品会被显示

小插曲:mysql5.7.5以上版本group by查询问题解决:
group by 报错解决

select  * from product left join category c on product.categoryId = c.categoryId group by product.categoryId;
out.png
select  product.categoryId,categoryName, count(*) from product left join category c on product.categoryId = c.categoryId group by product.categoryId
out2.png

选择根据categoryName分类后的最便宜的产品:

select  product.categoryId,categoryName, MIN(price) from product left join category c on product.categoryId = c.categoryId group by product.categoryId
output.png

把nike对应的categoryId设置为1后,想要得到最便宜产品的名字:

select  product.categoryId,categoryName,productName, MIN(price) from product left join category c on product.categoryId = c.categoryId group by product.categoryId
wrong.png

正确写法是使用子查询:

select * from product join (
select  product.categoryId,categoryName, MIN(price) from product left join category c
    on product.categoryId = c.categoryId group by product.categoryId
  ) as cat_min
on product.categoryId = cat_min.categoryId

ok1.png
select * from product join (
select  product.categoryId,categoryName, MIN(price) as min_price from product left join category c
    on product.categoryId = c.categoryId group by product.categoryId,categoryName
  ) as cat_min
on product.categoryId = cat_min.categoryId
where product.price = cat_min.min_price

ok2.png

优化:

select product.productName,cat_min.categoryName,cat_min.min_price from product join (
select  product.categoryId,categoryName, MIN(price) as min_price from product left join category c
    on product.categoryId = c.categoryId group by product.categoryId,categoryName
  ) as cat_min
on product.categoryId = cat_min.categoryId
where product.price = cat_min.min_price
final.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 转 # https://www.cnblogs.com/easypass/archive/2010/12/ 08/...
    吕品㗊阅读 13,334评论 0 44
  • php面试题及答案(转载)收藏 基础题: 1.表单中 get与post提交方法的区别? 答:get是发送请求HTT...
    积_渐阅读 5,792评论 0 37
  • 当幸福来敲门》 ——拼尽全力地活着 也许生的欲望只有在被逼到绝境时才能悉数迸发,就像克里斯·迦纳一样。 失业,...
    83114ba97142阅读 1,837评论 0 0
  • 山河悸动的呼啸江南的风,格外凛冽我以为第二夜会是海哭颤动的思念一盏灯,随我入眠 窗外人群的蹿动江南的夜,别样森然我...
    买金矿阅读 3,658评论 19 37
  • 前言:学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,...
    赵亦晨阅读 4,229评论 0 1

友情链接更多精彩内容