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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?