所以我有一张桌子
Table_A
ID Code Arrival date
3 A3 3/1/15
2 A2 1/1/15
1 A3 2/1/15
ID是自动递增,到达日期可以是相同的代码,
所以我想要我的MySQL查询要做的是将所有那些代码分组,其中包含到达日期与日期范围相符并按代码分组,然后只选择最新输入
到目前为止我有什么
Select * from Table_A
Where 'Arrival date' between 1/1/15 and 3/1/15 and group by Code
但不知怎的,这只会返回最早的代码,我怎么能解决这个问题?
答案 0 :(得分:0)
试一试。使用您的条件然后UNION
运行多个查询会更高效。
(SELECT * FROM Table_A WHERE 'Arrival date'
BETWEEN 1/1/15 AND 3/1/15 AND Code = A2
ORDER BY 'Arrival date' DESC LIMIT 1)
UNION ALL
(SELECT * FROM Table_A WHERE 'Arrival date'
BETWEEN 1/1/15 AND 3/1/15 AND Code = A3
ORDER BY 'Arrival date' DESC LIMIT 1)
按日期排序允许我们对从最新到最旧的行(DESC)进行排序,并仅返回最新的行。希望这就是你所需要的。
答案 1 :(得分:0)
我相信这可以帮到你:
SELECT
MAX(id) AS id,
Code,
`Arrival date`
FROM
Table_A
WHERE
`Arrival date`
BETWEEN
'2015-01-01'
AND
'2015-01-03'
GROUP BY
Code