![](/img/trans.png)
[英]MySQL ERROR: 'Unkown column' tablename.colname in 'on clause'
[英]How to get max(colName) value where colName is of Type TimeStamp in mySQL
我有以下表格結構 並在此表中包含以下數據
現在,我想以一種方式進行查詢,以便可以獲取所有列的值+特定game_id的max(currenttime)的值。
我正在運行以下查詢SELECT _id,game_id,inning_id,scoretype,oponentonescore,oponenttwoscore,currenttime,MAX(currenttime)AS最新FROM game_scores
WHERE game_id ='2268'
結果我只得到一行,如下結果 但是我想要所有符合條件的行(即游戲ID 2268)
如何撰寫Query來達到這個目的?
提前致謝
您的MAX()
是一個聚合類型函數,對於多行獲取一個值。
因此,將其刪除並在PHP中使用類似的內容...
$maxTime = 0;
foreach($rows as $row) {
$maxTime = max($maxTime, strtotime($row['currenttime']));
}
...或運行另一個查詢,盡管以上內容就足夠了...
SELECT MAX(`currenttime`) AS `latest`
FROM `game_scores`
WHERE `game_id` = '2268'
格式化為您方便的OMG Ponies 樣式 。
對不起,我還沒有真正測試過,但是像這樣的東西可能會解決問題:
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';
盡管此解決方案在數據庫上比在解決方案上重,但在從數據庫返回查詢后將其找出來的應用程序卻很繁重(如建議的alex)。
該查詢行上的內容可以滿足要求。 未測試。
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.