我有两张桌子:factors
和factor_items
。FID
是factors
的主键,外键是factor_项。
有时我需要在factor
表上查看最新的factor_items
行及其对应的行。我怎样才能做到这一点?我知道我应该对这些表进行JOIN
查询,但我对latest row(max FID)
没有任何概念:
SELECT * FROM factors INNER JOIN factor_items ON factors.FID = factor_items.FID;
最佳答案
如果您包含一个WHERE
条件来检查max id like
SELECT * FROM factors
INNER JOIN factor_items
ON factors.FID = factor_items.FID
WHERE factors.FID IN ( select max(FID) from factors );
(或)使用
order by
和limit
子句SELECT * FROM factors
INNER JOIN factor_items
ON factors.FID = factor_items.FID
order by factors.FID desc
limit 100;
关于mysql - MySQL-如何改善此查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26903317/