2012-02-27 76 views
0

我想彙總一些我從MySQL數據庫中提取的數據。MySQL - 計數?

查詢我使用的是:

SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`, 
COUNT(distinct IF(sourcelist.hasExtension=1,NAME,NULL)) as Ext, 
COUNT(distinct IF(sourcelist.hasExtension=0,NAME,NULL)) as NoExt, 
COUNT(distinct NAME) as `All` 
FROM sourcelist, main_small 
WHERE sourcelist.SourcePUID = 'My_Variable' AND main_small.NAME = sourcelist.SourceFileName 
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED` ORDER BY `DROID_V` ASC, `SIG_V` ASC, `SPEED` 

,我不知道是否有這個統計結果的方式,所以我可以做一個新的表會告訴我是這樣的:

每不同的PUID,(不同的DROID_V的計數),(不同的Sig_V的計數),(NAME總點擊數的總和)WHERE sourcelist.SourcePUID ='My_Variable'AND main_small.NAME = sourcelist.SourceFileName

如你所見,我真的不擅長SQL!

源表:

CREATE TABLE `t1` (
`DROID_V` int(1) DEFAULT NULL, 
`Sig_V` varchar(7) DEFAULT NULL, 
`SPEED` varchar(4) DEFAULT NULL, 
`ID` varchar(7) DEFAULT NULL, 
`PARENT_ID` varchar(10) DEFAULT NULL, 
`URI` varchar(10) DEFAULT NULL, 
`FILE_PATH` varchar(68) DEFAULT NULL, 
`NAME` varchar(17) DEFAULT NULL, 
`METHOD` varchar(10) DEFAULT NULL, 
`STATUS` varchar(14) DEFAULT NULL, 
`SIZE` int(10) DEFAULT NULL, 
`TYPE` varchar(10) DEFAULT NULL, 
`EXT` varchar(4) DEFAULT NULL, 
`LAST_MODIFIED` varchar(10) DEFAULT NULL, 
`EXTENSION_MISMATCH` varchar(32) DEFAULT NULL, 
`MD5_HASH` varchar(10) DEFAULT NULL, 
`FORMAT_COUNT` varchar(10) DEFAULT NULL, 
`PUID` varchar(15) DEFAULT NULL, 
`MIME_TYPE` varchar(24) DEFAULT NULL, 
`FORMAT_NAME` varchar(10) DEFAULT NULL, 
`FORMAT_VERSION` varchar(10) DEFAULT NULL, 
`INDEX` int(11) NOT NULL AUTO_INCREMENT, 
PRIMARY KEY (`INDEX`) 
) ENGINE=MyISAM AUTO_INCREMENT=960831 DEFAULT CHARSET=utf8 

例如記錄:

5;"v37";"slow";"10266";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/7";"image/tiff";"Tagged Ima";"3";"191977" 
5;"v37";"slow";"10268";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/8";"image/tiff";"Tagged Ima";"4";"191978" 
5;"v37";"slow";"10269";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/9";"image/tiff";"Tagged Ima";"5";"191979" 
5;"v37";"slow";"10270";;"file:";"V1-FL425817.tif";"V1-FL425817.tif";"BINARY_SIG";"MultipleIdenti";"20603284";"FILE";"tif";"2008-11-03";;;;"fmt/10";"image/tiff";"Tagged Ima";"6";"191980" 

回答

1

當然只是像做

Select Count(*) From (Select * From SomeTable) adummynamesoSqlParserDoesntgetupset 

所以把你的查詢括號FROM後,給它一個獨特的名字,你可以把它當作桌子或視圖來對待。

+0

哦!涼!這真的很有用,謝謝!我會有一個探索。 – Jay 2012-02-27 19:15:01