lulu_programmer 2022-07-11 15:31 采纳率: 75%
浏览 166
已结题

sql join和correlated subenquiry的区别

题目要求生成如下表格:

img

相关表格两张:

img

img

正确答案:

img

我的答案,我没有使用join链接两张表格,而是在select中用where把两张表的product id链接起来,为什么结果不对?

img

  • 写回答

1条回答 默认 最新

  • sinJack 2022-07-11 15:41
    关注
    select 
    p.product_id,
    p.name,
    (select count(*) from order_items oi where oi.product_id = p.product_id) as orders,
    if ((select count(*) from order_items oi where oi.product_id = p.product_id) > 1, 'Many times', 'Once') as frequency
    from products p
    group by product_id, name
    

    方式二:

    select 
    temp.product_id,
    temp.name,
    temp.orders,
    if (tmep.orders>1, 'Many times', 'Once') as frequency
    from(
    select 
    p.product_id,
    p.name,
    (select count(*) from order_items oi where oi.product_id = p.product_id) as orders
    from products p
    group by product_id, name
    ) temp
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月19日
  • 已采纳回答 7月11日
  • 创建了问题 7月11日

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)