Php MYSQL SELECT语句,其中如果Trace=12,则为最后日期 创建表(如果不存在)`sporesfungi` `IDSpore`varchar4字符集latin1 COLLATE latin1\u bin NOT NULL, `名称'varchar25不为空, `输入'varchar10'不为空, 主键“IDSpore”, 键'IDSpore``IDSpore` 如果不存在“孢子计数”,则创建表 `IDSpore`varchar4字符集latin1 COLLATE latin1\u bin NOT NULL, `TraceNum`int2不为空, `TraceHour`int4不为空, `金额'int11不为空, `日期`日期不为空, 唯一键`IDSpore\u 2``IDSpore`、`TraceHour`、`Date`、, 键'IDSpore``IDSpore`

Php MYSQL SELECT语句,其中如果Trace=12,则为最后日期 创建表(如果不存在)`sporesfungi` `IDSpore`varchar4字符集latin1 COLLATE latin1\u bin NOT NULL, `名称'varchar25不为空, `输入'varchar10'不为空, 主键“IDSpore”, 键'IDSpore``IDSpore` 如果不存在“孢子计数”,则创建表 `IDSpore`varchar4字符集latin1 COLLATE latin1\u bin NOT NULL, `TraceNum`int2不为空, `TraceHour`int4不为空, `金额'int11不为空, `日期`日期不为空, 唯一键`IDSpore\u 2``IDSpore`、`TraceHour`、`Date`、, 键'IDSpore``IDSpore`,php,mysql,Php,Mysql,因为我仍然不能发布我所说的图像,所以我尝试从sporesfungi获取类型,从SporesCount获取Amount&IDSpore并进行内部连接,但是从SporesCount,我只需要最新的日期数据,其中TraceNum=12是它的最高值 因此,我想要的是TraceNum达到12的最近日期的所有数据 这是我尝试过的,但还没有结果 选择数量、孢子数、孢子数。从孢子数中键入 在sporcount.IDSpore=sporesfungi.IDSpore上内部连接sporsfungi作为 其中日期=

因为我仍然不能发布我所说的图像,所以我尝试从sporesfungi获取类型,从SporesCount获取Amount&IDSpore并进行内部连接,但是从SporesCount,我只需要最新的日期数据,其中TraceNum=12是它的最高值

因此,我想要的是TraceNum达到12的最近日期的所有数据

这是我尝试过的,但还没有结果

选择数量、孢子数、孢子数。从孢子数中键入 在sporcount.IDSpore=sporesfungi.IDSpore上内部连接sporsfungi作为 其中日期= 选择MAXDate 从孢子计数为b 其中a.IDSpore=b.IDSpore IFMAXb.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');

我希望我说得够清楚,欢迎提出任何建议。

试试这样的建议

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

构建此查询有两个步骤。首先,我们需要一个能找到孢子达到12的最后一个日期的方法。既然你说它每天只能达到12次,我们就忽略了追踪小时

然后我们需要连接到该查询的结果,以便从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 
子查询联接将产生多行

更新后

从评论来看,我们似乎有不同的想法。您似乎想要的是找到任何孢子达到tracenum 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;

您不能发布嵌入的图像,但可以上载到imgur并在帖子中包含链接。如果没有tracenum=12的sporecount,您想要什么?完全没有行,或者sporesfungi数据没有来自另一个表的相应值?tracenum在给定的一天内是否可以多次达到12?在不同的时间?@pala_uuu否,如果没有traceNum=12的数据,那么我想在前一天检查是否有。每天只能达到12次。是否保证每天达到12次?如果第一天已经过了一半,12还没有到达呢?这就是这个查询的重点,我不想显示不完整的Trace=12的数据。好的,修复了它,但我没有得到正确的数据,可能是分组方式?最好给我们显示一些数据和结果。我猜跟踪时间有点不清楚。我会尝试在我的表中添加一些当前数据的图像,结果文本表示会更好。更容易对其运行测试。另一个表的数据如何?好的,我将尝试此方法并返回结果。@alf感谢问题中的数据-我已通过演示更新了此答案。结果如何?好的,请给我至少12行,只有5@alf你为什么会得到12分?您的示例数据只有5行,tracenum=12抱歉,如果我不够清楚,但我想获取最后一个日期12的所有行,即14日,即今天,我想从跟踪1-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;