简体   繁体   English

为什么mysql查询返回错误结果?

[英]Why mysql query is returning wrong Results?

I am attempting to find the latest log entries in my MySQL database. 我试图在我的MySQL数据库中找到最新的日志条目。 I cooked up this query late last night but upon testing today it does not seem to be returning the correct data. 我昨晚做了这个查询,但今天测试后似乎没有返回正确的数据。

SELECT MAX(id), hostName, email, info, time 
FROM log
WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
GROUP BY hostName 
ORDER BY `time` DESC

The query runs just fine, but none of the other fields seem to match the id column. 查询运行得很好,但其他字段似乎都没有匹配id列。 It grabs the max id number, but the hostName, email, and info do not match the id, and they are not the latest. 它抓取最大ID号,但hostName,email和info与id不匹配,并且它们不是最新的。 I've spent a few hours spinning my wheels on this (I'm a MySQL noob and just doing this for fun) so I'm pretty confused now.. 我已经花了几个小时在我的车轮上旋转(我是一个MySQL菜鸟,只是为了好玩而这样做)所以我现在很困惑..

Thanks for any help! 谢谢你的帮助!

EDIT: Wow thanks for all the responses! 编辑:哇谢谢所有的回复! Sorry for the confusion, I should have said "I want the latest log per hostname!" 对不起,我应该说“我想要每个主机名最新的日志!” <- That is specifically what I need. < - 这是我特别需要的。

You said: 你说:

I am attempting to find the latest log entries in my MySQL database 我试图在我的MySQL数据库中找到最新的日志条目

There is no need to group nor use a subquery. 无需分组或使用子查询。 Just try this: 试试这个:

SELECT id, hostName, email, info, time FROM log
WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
ORDER BY `time` DESC

Now you said: 现在你说:

I want the latest log per hostname! 我想要每个主机名的最新日志!

Then the solution should be this: 然后解决方案应该是这样的:

SELECT l1.id, l1.hostName, l1.email, l1.info, l1.time FROM log l1
LEFT JOIN log l2 on l1.hostName = l2.hostName AND l1.time < l2.time
WHERE l2.time IS NULL
ORDER BY l1.time DESC

If you want the full row that corresponds to the max id for each distinct hostName , 如果您想要与每个不同hostName的max id对应的完整行,

SELECT id, hostName, email, info, time
FROM log
WHERE id IN 
    (SELECT MAX(id) FROM log WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
     GROUP BY hostName)
ORDER BY `time` DESC

Another way to do the same thing is to GROUP BY and then JOIN to the log table: 另一种做同样事情的方法是GROUP BY然后JOIN到日志表:

If "latest" means the maximum id , use a (hostname, time, id) index and this: 如果“latest”表示最大id ,请使用(hostname, time, id)索引,并且:

SELECT log.id
FROM 
        log
    JOIN 
        ( SELECT MAX(id) AS id
          FROM log
          WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
          GROUP BY hostName
        ) AS grp
      ON grp.id = log.id ;

UPDATE : You may alo want to try this one. 更新 :您可能还想尝试这个。 It just might be more efficient (depending on the hostname distribution) - and would benefit from a index: (hostname, id, time) index: 它可能更有效(取决于主机名分布) - 并将受益于索引: (hostname, id, time)索引:

SELECT log.id
FROM 
        log
    JOIN 
        ( SELECT MAX(id) AS id
          FROM log
          GROUP BY hostName
        ) AS grp
      ON grp.id = log.id 
WHERE log.time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) ;

If "latest" means the maximum time (eg if time is insertion_time and is not changed later), use a (hostname, time) index and this: 如果“最新”表示最time (例如,如果timeinsertion_time且以后未更改),请使用(hostname, time)索引,并且:

SELECT log.*
FROM 
        log
    JOIN 
        ( SELECT hostname, MAX(time) AS max_time
          FROM log
          WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
          GROUP BY hostname
        ) AS grp
      ON  grp.hostname = log.hostname
      AND grp.max_time = log.time ;

And finally, since everyone forgot to answer the "Why mysql query is returning wrong Results?" 最后,因为每个人都忘了回答“为什么mysql查询返回错误的结果?” question, here is another question with some useful answers: Why does MySQL add a feature that conflicts with SQL standards? 问题,这是另一个有一些有用答案的问题: 为什么MySQL会添加一个与SQL标准冲突的功能?

This is a famous one: 这是一个着名的:

SELECT *
FROM log
WHERE log.id IN (
    SELECT MAX(id)
    FROM log
    WHERE time >= DATE_SUB(NOW(), INTERVAL 1 HOUR) 
    GROUP BY hostName) T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM