php - MYSQL SELECT statement Where last Date If Trace = 12 -


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`) ) 

since can't still post images put that, i'm trying take type sporesfungi , amount & idspore sporecount , make inner join, sporecount want latest date data has tracenum = 12 highest value have.

so want data on recent date tracenum reached 12.

this have tried no results yet

select amount, idspore, sporesfungi.type sporecount     inner join sporesfungi on sporecount.idspore = sporesfungi.idspore     date = (         select max(date)             sporecount b             a.idspore = b.idspore)     if(max(b.tracenum) = 12, 1, 0) =1 

this have on table testing.

    insert `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 `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'); 

i hope i'm clear enough, suggestions welcome.

there's 2 step process building query. first, need 1 finds last date spore reached tracenum of 12. since can reach 12 once per day, ignore tracehour.

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

then need join results of query, data sporesfungi table.

select sf.*, sc.amount   sporesfungi sf     inner join sporecount sc       on sf.idspore = sc.idspore     inner join (       select idspore, max(`date`) d         sporecount         tracenum = 12         group idspore     ) q     on sc.date = q.d       , sc.idspore = q.idspore   sc.tracenum = 12  ;           -- check again because  

subquery join result in more 1 row.

demo here

after updates

from comments - seems on different wavelength. seem want find last date spore hit tracenum of 12, , return data, spores, on day. simplifies things.

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

updated fiddle


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -