需求:查询客户列表,需要带上客户的最后跟进时间、最后的跟进内容
客户表结构
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)