我在将需要返回多个值的子查询转换为内部连接时遇到了很多麻烦,因为只有最里面的结果的最新版本应该被返回。我已经尝试搜索和浏览各种答案,但找不到解决方法。下面有点简化,但显示了我想要的,并且根本不起作用:
select
works.id as w_id,works.name as w_name,author,publisher,
(select name as auth from creators where author=id),
(select name as pub from creators where publisher=id),
(select version,
(select pages,uploaded,uri from info where id=workupdates.info)
from workupdates where work=works.id order by date desc limit 1)
from works
(信息通常在修订之间没有变化,但大多数文档至少有一些。)我可以使用多个子查询,每列一个,但这既慢又愚蠢。
天真地,我把它翻译成:
select
works.id as w_id,works.name as w_name,developer,publisher,
(select name as dev from creators where developer=id),
(select name as pub from creators where publisher=id),
version,pages,uploaded,uri
from
works
left join workupdates on workupdates.work = works.id
left join info on info.id=workupdates.info
但显然,当我只想要每件作品的最新信息数据时,这会为每件作品返回多个结果,每个匹配的信息行一个。
我看到一个答案建议开始,from (select max(date) from workupdates where workupdates.work = works.id) wu inner join works
但在我的情况下,这只是给了我一个损坏的查询,其中 DB 声明 works.id 不存在。我已经看到并尝试了很多不同的变体——我不知道这么多类似的查询可以有如此不同的结构——但我还没有运气,我的大脑相当疲惫。