我的数据看起来像这样:
entities id name 1 Apple 2 Orange 3 Banana
流程将定期运行并为每个实体提供分数.该过程生成数据并将其添加到分数表中,如下所示:
scores id entity_id score date_added 1 1 10 1/2/09 2 2 10 1/2/09 3 1 15 1/3/09 4 2 10 1/03/09 5 1 15 1/4/09 6 2 15 1/4/09 7 3 22 1/4/09
我希望能够选择所有实体以及每个实体的最新记录得分,从而产生如下数据:
entities id name score date_added 1 Apple 15 1/4/09 2 Orange 15 1/4/09 3 Banana 15 1/4/09
我可以使用此查询获取单个实体的数据:
SELECT entities.*, scores.score, scores.date_added FROM entities INNER JOIN scores ON entities.id = scores.entity_id WHERE entities.id = ? ORDER BY scores.date_added DESC LIMIT 1
但我对如何为所有实体选择相同而感到茫然.也许它正盯着我的脸?
非常感谢您抽出宝贵时间.
感谢您的好评.我会给它几天时间来看看首选解决方案是否会起泡然后我会选择答案.
更新:我已经尝试了几个建议的解决方案,我现在面临的主要问题是,如果一个实体还没有生成的分数,它们就不会出现在列表中.
SQL会是什么样的,以确保返回所有实体,即使它们还没有发布任何分数?
更新:答案已选中.感谢大家!
我是这样做的:
SELECT e.*, s1.score, s1.date_added FROM entities e INNER JOIN scores s1 ON (e.id = s1.entity_id) LEFT OUTER JOIN scores s2 ON (e.id = s2.entity_id AND s1.id < s2.id) WHERE s2.id IS NULL;
只是为了添加我的变体:
SELECT e.*, s1.score FROM entities e INNER JOIN score s1 ON e.id = s1.entity_id WHERE NOT EXISTS ( SELECT 1 FROM score s2 WHERE s2.id > s1.id )
方法1
SELECT entities.*, scores.score, scores.date_added FROM entities INNER JOIN scores ON entities.id = scores.entity_id WHERE scores.date_added = (SELECT max(date_added) FROM scores where entity_id = entities.id)