最近项目中有个需求,H5页面表:h5和组织的关联关系表= 1:n
如果有多个绑定关系时取关联表id最小的关联关系,要求写出支持按组织名称或页面名称过滤的分页查询SQL
h5页面表 CREATE TABLE `h5page` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `PageName` varchar(255) DEFAULT NULL, `PageUrl` varchar(255) DEFAULT NULL, `CreateDate` datetime DEFAULT NULL COMMENT '建立日期', `LastUpdateDate` datetime DEFAULT NULL COMMENT '最后修改日期', `DisableFlag` varchar(1) NOT NULL DEFAULT '1' COMMENT '是否禁用(0.禁用,1.启用)', PRIMARY KEY (`Id`) ) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=utf8; h5关联组织表 CREATE TABLE `codeh5relation` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `OrganizationId` varchar(40) NOT NULL COMMENT '组织Id', `OrganizationFullName` varchar(255) DEFAULT NULL COMMENT '组织名', `H5PageId` int(20) DEFAULT NULL COMMENT 'h5页面表的id', `CreateDate` datetime DEFAULT NULL COMMENT '建立日期', `LastUpdateDate` datetime DEFAULT NULL COMMENT '最后修改日期' PRIMARY KEY (`Id`) ) ENGINE=InnoDB AUTO_INCREMENT=365 DEFAULT CHARSET=utf8;
查询条数SQL如下
SELECT count(1) FROM h5page a WHERE EXISTS ( SELECT id FROM codeh5relation b WHERE a.Id = b.H5PageId AND b.id = ( SELECT min(c.id) FROM codeh5relation c WHERE b.H5PageId = c.H5PageId) AND b.organizationFullName LIKE concat('江苏华睿投资管理有限公司', '%') ) OR a.pageName LIKE concat('江苏华睿投资管理有限公司', '%')
查询列表数据SQL如下
SELECT a.*, b.organizationFullName FROM h5page a LEFT JOIN codeh5relation b ON a.Id = b.H5PageId AND b.id = ( SELECT min(id) FROM codeh5relation c WHERE b.H5PageId = c.H5PageId ) WHERE ( b.organizationFullName LIKE concat('江苏华睿投资管理有限公司', '%') OR a.pageName LIKE concat('江苏华睿投资管理有限公司', '%') ) ORDER BY a.CreateDate DESC LIMIT 0,10
下面简单分析下sql,红色部分代表去从若干符合条件的数据中取出id最小的一条 。
SELECT
a.*, b.organizationFullName
FROM h5page a
LEFT JOIN codeh5relation b ON a.Id = b.H5PageId
AND b.id = ( SELECT min(c.id) FROM codeh5relation c WHERE b.H5PageId = c.H5PageId )
查询条数的sql用exists减少一次扫描关联表提高了查询效率
SELECT count(1) FROM c h5page a WHERE EXISTS (
SELECT id FROM c codeh5relation b WHERE a.Id = b.H5PageId
AND b.id = ( SELECT min(c.id) FROM codeh5relation c WHERE b.H5PageId = c.H5PageId )
)