简体   繁体   English

如何在MySQL中获取colName为TimeStamp类型的max(colName)值

[英]How to get max(colName) value where colName is of Type TimeStamp in mySQL

I am having following table structure 我有以下表格结构 在此处输入图片说明 and having the following Data in this table 并在此表中包含以下数据 在此处输入图片说明 Now I want to query in such a way so that I can have values of all the columns + the value of max(currenttime) for a particular game_id. 现在,我想以一种方式进行查询,以便可以获取所有列的值+特定game_id的max(currenttime)的值。

i am running the following query SELECT _id, game_id, inning_id, scoretype, oponentonescore, oponenttwoscore, currenttime, MAX( currenttime ) AS latest FROM game_scores WHERE game_id = '2268' 我正在运行以下查询SELECT _id,game_id,inning_id,scoretype,oponentonescore,oponenttwoscore,currenttime,MAX(currenttime)AS最新FROM game_scores WHERE game_id ='2268'

and as a result i am getting only one row as in the following result 结果我只得到一行,如下结果 在此处输入图片说明 But i want want all rows matching the criteria(ie game id 2268) 但是我想要所有符合条件的行(即游戏ID 2268)

How to compose Query to achieve this? 如何撰写Query来达到这个目的?

Thanks in advance 提前致谢

Your MAX() is an aggregate type function and gets one value for multiple rows. 您的MAX()是一个聚合类型函数,对于多行获取一个值。

So remove it and use something like this with PHP... 因此,将其删除并在PHP中使用类似的内容...

$maxTime = 0;

foreach($rows as $row) {
   $maxTime = max($maxTime, strtotime($row['currenttime']));
}

...or run another query, though the above should be sufficient... ...或运行另一个查询,尽管以上内容就足够了...

SELECT MAX(`currenttime`) AS `latest`
  FROM `game_scores`
 WHERE `game_id` = '2268'

Formatted in your convenience OMG Ponies style . 格式化为您方便的OMG Ponies 样式

i'm sorry, i haven't actually tested this, but maybe something like this would do the trick: 对不起,我还没有真正测试过,但是像这样的东西可能会解决问题:

SELECT
    _id,
    game_id,
    inning_id,
    scoretype,
    oponentonescore,
    oponenttwoscore,
    currenttime,
    latest
FROM
    game_scores
JOIN (
    SELECT
        MAX(currenttime) AS latest
    FROM
        game_scores
    WHERE
        game_id = '2268'
) AS latest_calculation
WHERE game_id = '2268';

although this solution is heavier on the database than it is heavy on the application to figure it out after the query is back from the database (like alex suggested). 尽管此解决方案在数据库上比在解决方案上重,但在从数据库返回查询后将其找出来的应用程序却很繁重(如建议的alex)。

Something on the lines of this query would do the required. 该查询行上的内容可以满足要求。 NOT TESTED. 未测试。

 SELECT _id, game_id, inning_id, scoretype, oponentonescore, oponenttwoscore,
    currenttime,maxtime from game_scores OUTER join (SELECT _id,MAX(currenttime)
    as maxtime from game_scores) as t on t._id = game_scores._id  where game_id = '2268';

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

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