我有两张桌子:factorsfactor_itemsFIDfactors的主键,外键是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 bylimit子句
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/

10-11 07:01