2015-05-14 29 views
2
CREATE TABLE IF NOT EXISTS `sporesfungi` (
    `IDSpore` varchar(4) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL, 
    `Name` varchar(25) NOT NULL, 
    `Type` varchar(10) NOT NULL, 
    PRIMARY KEY (`IDSpore`), 
    KEY `IDSpore` (`IDSpore`) 
) 

CREATE TABLE IF NOT EXISTS `sporecount` (
    `IDSpore` varchar(4) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL, 
    `TraceNum` int(2) NOT NULL, 
    `TraceHour` int(4) NOT NULL, 
    `Amount` int(11) NOT NULL, 
    `Date` date NOT NULL, 
    UNIQUE KEY `IDSpore_2` (`IDSpore`,`TraceHour`,`Date`), 
    KEY `IDSpore` (`IDSpore`) 
) 

既然不能仍然张贴我把,我想要做的就是从sporecount采取从sporesfungiTypeAmount & IDSpore并作出图像内部加入,但从sporecount我只想要最新的日期数据,它有TraceNum = 12这是它的最高值。MYSQL的SELECT语句如果最后日期如果跟踪= 12

所以我想在最近的日期在TraceNum达到12

的所有数据这是我已经试过,但没有结果尚未

SELECT Amount, IDSpore, sporesfungi.Type FROM sporecount 
    INNER JOIN sporesfungi ON sporecount.IDSpore = sporesfungi.IDSpore AS a 
    WHERE Date = (
     SELECT MAX(Date) 
      FROM sporecount AS b 
      WHERE a.IDSpore = b.IDSpore) 
    IF(MAX(b.TraceNum) = 12, 1, 0) =1 

这是我目前对我的桌子进行测试。

INSERT INTO `sporecount` (`IDSpore`, `TraceNum`, `TraceHour`, `Amount`, `Date`) VALUES 
('C', 10, 1400, 1, '2015-05-14'), 
('e', 4, 200, 1, '2015-05-14'), 
('d', 8, 1800, 1, '2015-05-14'), 
('r', 5, 0, 1, '2015-05-14'), 
('t', 6, 2200, 1, '2015-05-14'), 
('q', 2, 600, 1, '2015-05-14'), 
('w', 3, 400, 1, '2015-05-14'), 
('a', 1, 800, 1, '2015-05-14'), 
('2', 12, 1000, 1, '2015-05-14'), 
('y', 7, 2000, 1, '2015-05-14'), 
('1', 9, 1600, 1, '2015-05-14'), 
('A', 11, 1200, 1, '2015-05-14'), 
('x', 9, 1630, 1, '2015-05-11'), 
('z', 9, 1630, 1, '2015-05-11'), 
('s', 9, 1630, 1, '2015-05-11'), 
('s', 11, 1230, 1, '2015-05-11'), 
('s', 1, 830, 2, '2015-05-11'), 
('s', 2, 630, 1, '2015-05-11'), 
('r', 10, 1430, 1, '2015-05-11'), 
('d', 9, 1630, 1, '2015-05-11'), 
('q', 6, 2230, 1, '2015-05-11'), 
('q', 10, 1430, 2, '2015-05-11'), 
('s', 8, 1830, 1, '2015-05-11'), 
('t', 10, 1430, 1, '2015-05-11'), 
('x', 11, 1230, 1, '2015-05-11'), 
('x', 12, 1030, 2, '2015-05-11'), 
('w', 6, 2230, 1, '2015-05-11'), 
('w', 10, 1430, 1, '2015-05-11'), 
('w', 11, 1230, 2, '2015-05-11'), 
('x', 6, 2230, 2, '2015-05-11'), 
('w', 2, 630, 1, '2015-05-11'), 
('w', 5, 30, 1, '2015-05-11'), 
('u', 7, 2030, 1, '2015-05-11'), 
('y', 7, 2030, 1, '2015-05-11'), 
('t', 7, 2030, 2, '2015-05-11'), 
('v', 12, 1030, 2, '2015-05-11'), 
('q', 5, 30, 2, '2015-05-11'), 
('h', 7, 2030, 1, '2015-05-11'), 
('a', 11, 1230, 1, '2015-05-11'), 
('a', 12, 1030, 1, '2015-05-11'), 
('a', 1, 830, 1, '2015-05-11'), 
('a', 2, 630, 2, '2015-05-11'), 
('T', 3, 430, 1, '2015-05-11'), 
('S', 3, 430, 1, '2015-05-11'), 
('R', 3, 430, 1, '2015-05-11'), 
('D', 3, 430, 1, '2015-05-11'), 
('1', 4, 230, 1, '2015-05-11'), 
('C', 3, 430, 1, '2015-05-11'), 
('4', 4, 230, 1, '2015-05-11'), 
('3', 4, 230, 2, '2015-05-11'), 
('a', 10, 1430, 1, '2015-05-11'), 
('a', 8, 1830, 1, '2015-05-11'), 
('h', 1, 830, 1, '2015-05-11'), 
('g', 1, 830, 2, '2015-05-11'), 
('e', 9, 1630, 1, '2015-05-11'), 
('e', 10, 1430, 1, '2015-05-11'), 
('e', 2, 630, 1, '2015-05-11'), 
('e', 5, 30, 1, '2015-05-11'), 
('d', 6, 2230, 1, '2015-05-11'), 
('d', 8, 1830, 1, '2015-05-11'), 
('d', 2, 630, 1, '2015-05-11'), 
('c', 8, 1830, 1, '2015-05-11'), 
('c', 12, 1030, 1, '2015-05-11'), 
('a', 6, 2230, 1, '2015-05-11'), 
('2', 4, 230, 1, '2015-05-11'); 

INSERT INTO `sporesfungi` (`IDSpore`, `Name`, `Type`) VALUES 
('', '', ''), 
('0', 'Basidiosporas', 'Fungus'), 
('1', 'Ascosporas', 'Fungus'), 
('2', 'Penicillum/Aspergillus', 'Fungus'), 
('3', 'BasidiosporasPleurotus', 'Fungus'), 
('4', 'Other', 'Fungus'), 
('5', 'Cladospori', 'Fungus'), 
('6', 'Coprinus/A', 'Fungus'), 
('7', 'Basidio Tr', 'Fungus'), 
('8', 'Ganoderma', 'Fungus'), 
('9', 'Cecropia', 'Fungus'), 
('A', 'Delitschia', 'Fungus'), 
('C', 'Rumex', 'Trees'), 
('D', 'Smut/Myxomycete', 'Fungus'), 
('E', 'Spegazzinia', 'Fungus'), 
('F', 'Sporomiella (A)', 'Fungus'), 
('G', 'Torula', 'Fungus'), 
('H', 'Pollen', 'Grass'), 
('I', 'Arthrinium', 'Fungus'), 
('J', 'Arbol', 'Trees'), 
('K', 'Arthrocarpus', 'Trees'), 
('L', 'Casuarina/Myrica', 'Trees'), 
('O', 'Xylariae', 'Fungus'), 
('P', 'Botrytis', 'Fungus'), 
('Q', 'Spondylocladiella', 'Fungus'), 
('R', 'Tetraploa', 'Fungus'), 
('S', 'Diatrypaceae', 'Fungus'), 
('T', 'Ulocladium', 'Fungus'), 
('U', 'Amphisphaeria (A)', 'Fungus'), 
('W', 'Stemphylium', 'Fungus'), 
('X', 'Mimosa', 'Trees'), 
('Y', 'Agrocybe-type', 'Fungus'), 
('Z', 'Mangle', 'Trees'), 
('a', 'Fusarium', 'Fungus'), 
('b', 'Pleospora', 'Fungus'), 
('c', 'Leptosphaerulina', 'Fungus'), 
('d', 'Helocomyces', 'Fungus'), 
('e', 'Periconia', 'Fungus'), 
('f', 'Acrodictys', 'Fungus'), 
('g', 'Alternaria', 'Fungus'), 
('h', 'Bipolaris', 'Fungus'), 
('i', 'Poaceae', 'Grass'), 
('j', 'Cerebella', 'Fungus'), 
('k', 'Dreshlera/Helmitosporum', 'Fungus'), 
('l', 'Epicoccum', 'Fungus'), 
('m', 'Rusts Puccinia', 'Fungus'), 
('n', 'Nigrospora', 'Fungus'), 
('o', 'Hifas Fragmentos', 'Fungus'), 
('p', 'Curvularia', 'Fungus'), 
('q', 'Ustilago', 'Fungus'), 
('r', 'Helicoma', 'Fungus'), 
('s', 'Helicomina', 'Fungus'), 
('t', 'Leptosphaeria-Like', 'Fungus'), 
('u', 'Chaetomiun', 'Fungus'), 
('v', 'Pithomyces', 'Fungus'), 
('w', 'Cercospora', 'Fungus'), 
('x', 'Exserohilum', 'Fungus'), 
('y', 'Tetrapyrgos', 'Fungus'), 
('z', 'Erysiphe/Oidium', 'Fungus'); 

我希望我很清楚,欢迎任何建议。

+0

您不能嵌入图像,但你可以上传到imgur,包括在帖子的链接。 –

+0

如果没有spcencount with tracenum = 12,你想要什么?根本没有行,或者是孢子真菌数据,其他表中没有相应的值?在特定的日子里可以达到12次以上吗?在不同的时间? –

+0

@pala_ no,如果没有traceNum = 12的数据,那么我想检查前一天是否有任何数据。它每天只能达到12次。 – Alf

回答

1

有两个步骤建立这个查询。首先,我们需要一个发现孢子达到tracenum为12的最后日期。既然你说它每天只能达到12次,我们忽略了tracehour

select idspore, max(`date`) 
    from sporecount 
    where tracenum = 12 
    group by idspore 

然后,我们需要连接到该查询的结果,以从sporesfungi表中获取数据。

select sf.*, sc.amount 
    from sporesfungi sf 
    inner join sporecount sc 
     on sf.idspore = sc.idspore 
    inner join (
     select idspore, max(`date`) d 
     from sporecount 
     where tracenum = 12 
     group by idspore 
    ) q 
    on sc.date = q.d 
     and sc.idspore = q.idspore 
    where sc.tracenum = 12 ;   -- we do this check again because the 

子查询连接将导致多行。

demo here

更新

从意见后 - 看来我们是在不同的波长。你似乎想要的是找到ANY孢子达到12的最后一个日期,然后在当天返回所有孢子的所有数据。这实际上简化了事情。

select sf.*, sc.* 
    from sporesfungi sf 
    inner join sporecount sc 
     on sf.idspore = sc.idspore 
    inner join (
     select max(`date`) d 
     from sporecount 
     where tracenum = 12 
    ) q 
    on sc.date = q.d; 

Updated Fiddle

+0

好吧,我会试试这个,并得到我得到的结果。 – Alf

+0

@alf感谢问题中的数据 - 我已经用演示更新了这个答案。结果如何显示? –

+0

好,是给我至少12行,只有5 – Alf

0

尝试这样的事情

SELECT sc.Amount, sc.IDSpore, sf.Type 
FROM sporecount sc INNER JOIN 
    (SELECT IDSpore, MAX(Date) AS last_date 
    FROM sporecount 
    WHERE TraceNum= 12 
    GROUP BY IDSpore 
) last_sc 
ON last_sc.IDSpore=sc.IDSpore 
    AND last_sc.last_date = sc.Date 
INNER JOIN sporefungi sf 
ON sf.IDSpore = sc.IDSpore 
+0

好吧,修复它,但我没有得到正确的数据,也许是GROUP BY? – Alf

+0

更好地向我们展示一些数据和结果。我猜测对于追踪时间有些不清楚。 – Tim3880

+0

我尝试在我的表格中添加一些当前数据的图像,并且结果 – Alf