SQL和2个复杂的连接

时间:2011-07-12 18:48:58

标签: mysql sql join

我正在尝试使用MySQL 5.5优化SQL查询,关于2个分离请求。如果可能的话,我想在最后提出一个请求。

我有一个DocumentType表(请参阅本问题末尾的架构)。 例如,我想显示“为Program#4上传的3个DocumentInstance”。

要计算每个程序的文档总数,我有这个查询:

select p.id as programId, count(dt.id) as totalDocPerProgram
from DocumentType dt
join DocumentTypeInProgram dtip on dtip.documentType_id=dt.id
join Program p on dtip.program_id=p.id
group by p.id

要计算用户已经上传的文档数量(按程序分类),以下查询将返回userSession列表以及之前上传的文档数量:

select p2.id as programId, wsc2.id as userSession, count(di2.id) as uploadedDocs
from DocumentType dt2
join DocumentInstance di2 on di2.documentType_id=dt2.id
join WebsiteCase wsc2 on di2.websiteCase_id=wsc2.id
left join Program p2 on p2.id=wsc2.program_id
group by wsc2.id

我的问题: 是否可以为每个程序返回一个单独的查询,totalDocPerProgram以及已经上传了多少文档? 并且可能会有类似的内容:“请为程序#3上传2个以上的文档”吗?

感谢您的帮助,

尼古拉斯

这是数据库SQL定义脚本

CREATE TABLE `DocumentType` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) DEFAULT NULL,
  `lifetime` int(11) DEFAULT NULL,
  `maxDocumentSize` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


CREATE TABLE `Program` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


CREATE TABLE `DataTypeForProgram` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cardinality` int(11) DEFAULT NULL,
  `dataType_id` bigint(20) DEFAULT NULL,
  `program_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FKA6CED6BFFCC6BB96` (`program_id`),
  KEY `FKA6CED6BF17C2EE7E` (`dataType_id`),
  CONSTRAINT `FKA6CED6BF17C2EE7E` FOREIGN KEY (`dataType_id`) REFERENCES `DataType` (`id`),
  CONSTRAINT `FKA6CED6BFFCC6BB96` FOREIGN KEY (`program_id`) REFERENCES `Program` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `WebsiteCase` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `creationDate` datetime DEFAULT NULL,
  `lastUpdate` datetime DEFAULT NULL,
  `program_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_Program` (`program_id`),
  CONSTRAINT `FK_Program` FOREIGN KEY (`program_id`) REFERENCES `Program` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `DocumentInstance` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `url` varchar(255) DEFAULT NULL,
  `documentType_id` bigint(20) DEFAULT NULL,
  `websiteCase_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK8BE8C2F05F1A4ADE` (`documentType_id`),
  KEY `FK8BE8C2F05C57B856` (`websiteCase_id`),
  CONSTRAINT `FK8BE8C2F05C57B856` FOREIGN KEY (`websiteCase_id`) REFERENCES `WebsiteCase` (`id`),
  CONSTRAINT `FK8BE8C2F05F1A4ADE` FOREIGN KEY (`documentType_id`) REFERENCES `DocumentType` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `DocumentTypeInProgram` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cardinality` int(11) DEFAULT NULL,
  `documentType_id` bigint(20) DEFAULT NULL,
  `program_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK190E2A0A5F1A4ADE` (`documentType_id`),
  KEY `FK190E2A0AFCC6BB96` (`program_id`),
  CONSTRAINT `FK190E2A0AFCC6BB96` FOREIGN KEY (`program_id`) REFERENCES `Program` (`id`),
  CONSTRAINT `FK190E2A0A5F1A4ADE` FOREIGN KEY (`documentType_id`) REFERENCES `DocumentType` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = 0;

1 个答案:

答案 0 :(得分:1)

您可以使用连接两个现有查询的查询来执行此操作。像这样:

select a.programId, 
       a.totalDocPerProgram, 
       b.upLoadedDocs, 
       (a.totalDocPerProgram - b.upLoadedDocs) as remainingDocs, 
       b.userSession
from (
    select p.id as programId, 
           count(dt.id) as totalDocPerProgram
    from DocumentType dt
    join DocumentTypeInProgram dtip on dtip.documentType_id=dt.id
    join Program p on dtip.program_id=p.id
    group by p.id) a
join (
    select p2.id as programId, 
           wsc2.id as userSession, 
           count(di2.id) as uploadedDocs
    from DocumentType dt2
    join DocumentInstance di2 on di2.documentType_id=dt2.id
    join WebsiteCase wsc2 on di2.websiteCase_id=wsc2.id
    left join Program p2 on p2.id=wsc2.program_id
    group by p2.id, wsc2.id) b on a.ProgramId = b.ProgramId

我没有调试过这个。但是这种方法很有效,特别是如果你包含一个

 where b.userSession = constant

子句