我一直在创建sql查询,如下所示:
select * from (select DISTINCT * from (select po.tGroup_id,pp.tGroup_id as GroupID from tPhos_Line_Operator PO
LEFT join tPhos_Line_Parameter PP
on PO.tGroup_id = PP.tGroup_id) A) Ac
left JOIN
(SELECT top 1 tGroup_id FROM tGROUP_LOG order by id desc) B
on Ac.tGroup_id = B.tGroup_id
我期待看到如下图所示的记录:
但我一直得到这些记录:
我尝试通过放置 tGroup_id = 29 在左连接中进行硬编码,并且它可以正常工作,因为我得到了我想要的确切记录。请参阅第一张图片。
select * from (select DISTINCT * from (select po.tGroup_id,pp.tGroup_id as GroupID from tPhos_Line_Operator PO
LEFT join tPhos_Line_Parameter PP
on PO.tGroup_id = PP.tGroup_id) A) Ac
left JOIN
(SELECT top 1 tGroup_id FROM tGROUP_LOG where tGroup_id = 29 order by id desc
) B
on Ac.tGroup_id = B.tGroup_id
我不想硬编码。 有人能告诉我在哪里错过了或做错了吗?
提前致谢。
答案 0 :(得分:0)
我找到了一种方法,而不是使用top 1,我可以使用max。
SELECT max(id) as TESTID,tGroup_id FROM tGROUP_LOG
group by tGroup_id
我认为这解决了我的问题。感谢