我有两个 MySQL 表。 表一:
id name otherid
表 2:
id otherid time
在每个表中,“id”是每一行的唯一标识符(也是主键) “otherids”在表之间对应。 table2 中可能有几行(或 0 行)的 otherid 对应于 table1 中的 otherid。表格
例如, 表 1:
id name otherid
1 bob 24
2 joe 326
表 2:
id otherid time
4 326 2014-03-12 023:38:59
5 24 2013-02-11 22:05:21
6 24 2013-02-10 13:27:58
我希望获取表 1 中的每一行,并根据表 2 中行的最新时间排序。 在我的例子中,这是我要寻找的结果:
id name time
2 joe 2014-03-12 023:38:59
1 bob 2013-02-11 22:05:21
这是因为 Joe 拥有表 2 中的最新时间。
最佳答案
看下面的例子:
SELECT table1.*, max(table2.time)
FROM table1
INNER JOIN table2 ON table1.otherid = table2.otherid
GROUP BY table2.otherid
ORDER BY table2.id
关于MySQL - 按单独表中的值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825309/