mysql怎么根据系统标识优先取最新系统的数据?
简单描述:需要展示机构名称、机构编码(可能是ORG_CODE,也可能是BASE_ORG_CODE,优先取值BASE_ORG_CODE),有三个系统,各系统数据优先级:3 > 2 > 1
需要支持:
1.数据优先级:系.统3 > 系,统2 > 系统1(不同系统之间部门名称可能不一致,如系统3部门名称为X_aa,系统2部门名称为X_bb)
2.在第1条的基础上优先取BASE_ORG_CODE字段作为机构编码,如果没有则取ORG_CODE字段
3.需根据机构名称进行右模糊查询
4.需根据机构编码精确查询
5.需要支持分页查询
create table tbl_org_info
(
ID bigint auto_increment comment 'ID.'
ORG_CODE varchar(50) default '' not null comment '机构编码',
BASE_ORG_CODE varchar(50) default '' not null comment '机构编码(优先于org_code)',
ORG_NAME varchar(50) default '' not null comment '机构名称',
SOURCE_TYPE smallint(4) default 1 not null comment '数据来源,1,2,3'
)
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (1, 'ORG001', '', '机构1', '2');
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (2, 'ORG002', 'ORG001', '机构1-新', '3');
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (3, 'ORG003', '', '机构3', '2');
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (4, 'ORG004', '', '机构4', '3');
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (5, 'ORG005', 'ORG003', '机构3-新', '1');
INSERT INTO `test`.`test_net_org`(`id`, `org_code`, `base_org_code`, `org_name`, `source_type`) VALUES (6, 'ORG006', NULL, '机构6', '2');
插入以上数据,预期结果如下:
ORG001 机构1-新
ORG003 机构3-新
ORG004 机构4
ORG006 机构6
CSDN不允许重复词汇,问题描述其他链接:https://segmentfault.com/q/1010000042511688