MySQL查询最客户最后一次的跟进记录

需求:查询客户列表,需要带上客户的最后跟进时间、最后的跟进内容

客户表结构
CREATE TABLE `sale_customer` (
  `id` bigint(20) NOT NULL COMMENT '主键',
  `name` varchar(255) DEFAULT NULL COMMENT '客户名称',
  `short_name` varchar(255) DEFAULT NULL COMMENT '客户简称',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `customer_status` bigint(20) DEFAULT NULL COMMENT '客户状态',
  `customer_stage` bigint(20) DEFAULT NULL COMMENT '客户阶段',
  `churn_reason` bigint(20) DEFAULT NULL COMMENT '流失原因',
  `churn_remark` varchar(500) DEFAULT NULL COMMENT '流失备注',
  `cooperate` varchar(255) DEFAULT NULL COMMENT '协同人',
  `customer_type` bigint(20) DEFAULT NULL COMMENT '客户类型',
  `customer_nature` bigint(20) DEFAULT NULL COMMENT '客户性质',
  `customer_level` bigint(20) DEFAULT NULL COMMENT '客户级别',
  `customer_industry` bigint(20) DEFAULT NULL COMMENT '客户行业',
  `importance` varchar(255) DEFAULT NULL COMMENT '重要程度',
  `customer_source` bigint(20) DEFAULT NULL COMMENT '客户来源',
  `customer_profile` varchar(500) DEFAULT NULL COMMENT '客户简介',
  `remark` varchar(502) DEFAULT NULL COMMENT '备注',
  `belong_id` bigint(20) DEFAULT NULL COMMENT '归属人',
  `create_user` bigint(20) DEFAULT NULL COMMENT '创建人',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_user` bigint(20) DEFAULT NULL COMMENT '修改人',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  `status` int(11) DEFAULT NULL COMMENT '状态',
  `org_id` bigint(20) DEFAULT NULL COMMENT '机构ID',
  `company_id` bigint(20) DEFAULT NULL COMMENT '公司ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='企业客户';
客户跟进记录表
CREATE TABLE `sale_customer_record` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `customer_id` bigint(20) DEFAULT NULL COMMENT '客户ID',
  `record_type` bigint(20) DEFAULT NULL COMMENT '字典:跟进方式',
  `record_time` datetime DEFAULT NULL COMMENT '跟进时间',
  `record_content` varchar(800) DEFAULT NULL COMMENT '跟进内容',
  `next_record` int(11) DEFAULT '0' COMMENT '下次跟进提醒 0否 1是',
  `create_user` bigint(20) DEFAULT NULL COMMENT '创建人',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_user` bigint(20) DEFAULT NULL COMMENT '修改人',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  `status` int(11) DEFAULT NULL COMMENT '状态',
  `org_id` bigint(20) DEFAULT NULL COMMENT '机构ID',
  `company_id` bigint(20) DEFAULT NULL COMMENT '公司ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1518145306573680643 DEFAULT CHARSET=utf8 COMMENT='客户跟进记录信息';
查询数据的SQL
SELECT scr.id,scr.record_time,scr.record_content,sc.name
from
    sale_customer sc left join
  sale_customer_record  scr on scr.customer_id = sc.id
WHERE  scr.record_time =(SELECT MAX(s2.record_time)
              FROM sale_customer_record s2
              WHERE scr.customer_id = s2.customer_id and s2.status = 0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是福强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值