MySQL - 使用GROUP BY和DESC [英] MySQL - using GROUP BY and DESC
问题描述
在我的SQL查询中,我使用 GROUP BY 和 ORDER BY 子句选择数据。该表在每行中具有相同的数字
,具有不同的时间。所以我想我想应用一个 GROUP BY 子句。
然而,在结果中用数字返回最早的时间,但我需要最近一次。
$ b $ pre $ SELECT * FROM TABLE GROUP BY(numbers)ORDER BY time DESC
该查询看起来应该首先应用 GROUP BY ,然后是 ORDER BY 。 ..但结果似乎不是这样工作的。
有没有办法解决这个问题?
在这种情况下,重新编写查询为:
SELECT * FROM(SELECT * FROM table ORDER BY time DESC)AS t GROUP BY编号;
In my SQL query I am selecting data with GROUP BY and ORDER BY clauses. The table has the same numbers
across multiple rows with different times in each row. So I think I want to apply a GROUP BY clause.
However in the results return the oldest time with the number, but I need the most recent time.
SELECT * FROM TABLE GROUP BY (numbers) ORDER BY time DESC
The query appears as if it should first apply GROUP BY and then ORDER BY... but the results do not appear to work this way.
Is there any way to fix this?
work-around is to re-write the query as:
SELECT * FROM (SELECT * FROM table ORDER BY time DESC) AS t GROUP BY numbers;
这篇关于MySQL - 使用GROUP BY和DESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!