n个嵌套的内连接

时间:2013-01-30 09:08:30

标签: mysql sql sql-server join inner-join

我有一个sql语句,可以正确地从一个表中获取数据。但是,我需要从名为table_n的n个表中以相同的方式获取。所有这些表都包含projiddocidrevnr的3字段主键。我还需要将n作为docType返回,以区分表格。结果将按projid和/或docid排序。

我尝试在PHP中对来自不同查询的所有输出进行排序,但速度太慢(在3MB数据库上至少几秒钟)。我确信MySQL / MSSQL可以更快地完成它。

这是我目前的查询:

 SELECT a.*  FROM   `table_1` a  
        INNER JOIN (SELECT docid,  
                           Max(revnr) max_val  
                    FROM   `table_1`  
                    WHERE  ( projid = something )  
                    GROUP  BY docid) b  
                ON a.docid = b.docid  
                   AND a.revnr = b.max_val  ORDER  BY docid DESC  

我当前的查询会为每个revnrdocid获取最高projid的行。

我正在开发MySQL,但我也需要它来处理MSSQL。一般的SQL解决方案会很棒。

谢谢!

编辑:我目前拥有的表格的表格模式:

CREATE TABLE IF NOT EXISTS `table_1` (
  `projid` int(11) NOT NULL,
  `docid` int(11) NOT NULL,
  `revnr` int(11) NOT NULL,
  `revname` varchar(64) NOT NULL,
  `signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sign` int(11) NOT NULL,
  `ritningsnr` varchar(128) NOT NULL,
  `moment` varchar(256) NOT NULL,
  `omrade` varchar(256) NOT NULL,
  `start` datetime NOT NULL,
  `stop` datetime NOT NULL,
  `extTodo` int(11) NOT NULL,
  PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='egenkontroll';



CREATE TABLE IF NOT EXISTS `table_2` (
  `projid` int(11) NOT NULL,
  `docid` int(11) NOT NULL,
  `revnr` int(11) NOT NULL,
  `revname` varchar(64) NOT NULL,
  `signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sign` int(11) NOT NULL,
  `extWater` int(11) NOT NULL,
  `extRisk` int(11) NOT NULL,
  `extSystem` int(11) NOT NULL,
  `extHelp` int(11) NOT NULL,
  `extProvtryck` int(11) NOT NULL,
  `extDoc` int(11) NOT NULL,
  `extEgenkontroll` int(11) NOT NULL COMMENT 'exttabell',
  `extOther` int(11) NOT NULL,
  `extMontorer` int(11) NOT NULL,
  PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='arbetsberedning';


CREATE TABLE IF NOT EXISTS `table_3` (
  `projid` int(11) NOT NULL,
  `docid` int(11) NOT NULL,
  `revnr` int(11) NOT NULL,
  `revname` varchar(64) NOT NULL,
  `adress` varchar(256) NOT NULL,
  `pipesMark` tinyint(1) NOT NULL,
  `pipesKulvert` tinyint(1) NOT NULL,
  `pipesBasement` tinyint(1) NOT NULL,
  `pipesVaning` tinyint(1) NOT NULL,
  `pipesIngjutna` tinyint(1) NOT NULL,
  `ledningTappvatten` tinyint(1) NOT NULL,
  `ledningVarmevatten` tinyint(1) NOT NULL,
  `ledningHetvatten` tinyint(1) NOT NULL,
  `ledningKylaPrim` tinyint(1) NOT NULL,
  `ledningKylaSek` tinyint(1) NOT NULL,
  `ledningGas` tinyint(1) NOT NULL,
  `ledningLuft` tinyint(1) NOT NULL,
  `ledningAvlopp` tinyint(1) NOT NULL,
  `ledningOther` varchar(512) NOT NULL,
  `materialGjutjarn` tinyint(1) NOT NULL,
  `materialSteel` tinyint(1) NOT NULL,
  `materialKoppar` tinyint(1) NOT NULL,
  `materialPlast` tinyint(1) NOT NULL,
  `materialRostfritt` tinyint(1) NOT NULL,
  `materialOther` varchar(512) NOT NULL,
  `omfattningLength` int(11) NOT NULL COMMENT 'meter',
  `omfattningDimension` varchar(16) NOT NULL,
  `omfattningRitningnr` varchar(128) NOT NULL,
  `doneWithPump` tinyint(1) NOT NULL,
  `doneWithVattenledning` tinyint(1) NOT NULL,
  `doneWithKompressor` tinyint(1) NOT NULL,
  `doneWithTathetsprovare` tinyint(1) NOT NULL,
  `tryckmedieVatten` tinyint(1) NOT NULL,
  `tryckmedieLuft` tinyint(1) NOT NULL,
  `tryckmedieOther` varchar(128) NOT NULL,
  `manometerDiameter` int(11) NOT NULL COMMENT 'mm',
  `manometerGradering` int(11) NOT NULL COMMENT 'kPa',
  `manometerReadPressure` int(11) NOT NULL,
  `manometerTid` int(11) NOT NULL COMMENT 'sekunder',
  `testedOn` datetime NOT NULL,
  `testedBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
  `comments` varchar(1024) NOT NULL,
  `commentsBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
  `signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sign` int(11) NOT NULL,
  PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

我需要的字段是projiddocidrevnrrevnamesignedOnsign并且存在于所有当前和未来的表格。

0 个答案:

没有答案