# 请教一条关联查询的语句。

0

lhyterry | 初学一级 | 园豆：102

0

select e.*,f.* from Email e inner join
(select el.ID,
FKID=(select top 1 ID from Feedback where Email_Id=el.Id order by feedbackdate desc)
from Email as el) et on e.Id = et.Id inner join Feedback f on f.ID=et.FKID

Yu | 专家六级 |园豆：12950 | 2014-07-11 09:49

select e.*,f.* from Questions e inner join
(select el.ID,
FKID=(select top 1 ID from Answers where QuestionId=el.Id order by CreateDate desc)
from Questions as el) et on e.Id = et.Id inner join Answers f on f.ID=et.FKID

Yu | 园豆：12950 (专家六级) | 2014-07-11 10:00

@Yu: 我觉得快行了，只是查出的问题和答案不对应，是否是排序的问题。

lhyterry | 园豆：102 (初学一级) | 2014-07-11 10:15

@lhyterry:肯定对应的

Yu | 园豆：12950 (专家六级) | 2014-07-11 10:19

@Yu:

Yu | 园豆：12950 (专家六级) | 2014-07-11 10:22

@Yu: 不好意思，看错了，谢谢你啊，真的出来了。

lhyterry | 园豆：102 (初学一级) | 2014-07-11 10:26

@lhyterry: 你好 Yu 能说明下这个实现的原理吗。

lhyterry | 园豆：102 (初学一级) | 2014-07-11 10:33

0

select
q.Description as Title,
Answer=(select top 1 Description from Answers where QuestionId=q.Id order by Id  desc)
from Questions as q

Alex_QY1987 | 园豆：1888 (小虾三级) | 2014-07-11 09:24

@lhyterry: 那你用下面这个吧，根据你的字段再稍做一下调整，主要是用到 over(partition by  这个东西

select aa.*,bb.Description as 问题 from
(
select * from
(
select ROW_NUMBER() over(partition by QuestionId order by createTime desc) as row,
QuestionId,Description
) a where row=1
) aa join Questions bb on aa.QuestionId=bb.Id

@Alex_QY1987: 我们用的sql2000数据库...

0
```SELECT *
FROM   (
SELECT *,
rownum = ROW_NUMBER() OVER(PARTITION BY t1.Id, ORDER BY t2.CreateDate DESC)
FROM   Questions t1
LEFT JOIN Answers t2
ON  t1.Id = t2.QuestionId
) t
WHERE  t.rownum = 1```

您需要登录以后才能回答，未注册用户请先注册