dongshan1959 2017-01-22 12:59
浏览 84
已采纳

MYSQL如何按最后日期为每个客户获取单行策略

i have two table

vehicle_data

 id chassis name      number
  1 00001   customer1 123456
  2 00002   customer2 654321
  3 00003   customer3 645421

insurance_data

 id chassis policy_number expiry_date
  1 00001           11111 22-01-2015
  2 00002           22222 22-01-2015
  3 00003           33333 22-01-2015
  4 00001           44444 22-01-2016
  5 00002           55555 22-01-2017
  6 00001           66666 22-01-2017

what i want

 chassis name      policy_number expiry_date
   00001 customer1         66666 22-01-2017
   00002 customer2         55555 22-01-2017
   00003 customer3         33333 22-01-2015
  • 写回答

1条回答 默认 最新

  • dongou3286 2017-01-22 13:03
    关注

    I think this is what you intend:

    select id.*
    from insurance_data id
    where id.expiry_date = (select max(id2.expiry_date)
                            from insurance_data id2
                            where id.name = id2.name and
                                  month(id2.expiry_date) = 1 
                           );
    

    This returns the most recent row in the table for each name for the given month of expiry.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集