1

在此处输入图像描述

我希望 sql 查询得到上述结果。结果是 TableA 中的最大 Id,TableB 中的 s_id 具有 Stat=true 即 1。

以下不做我想要的:

select i.category_id,i.image_id,i.image_original,i.image_title,i.photographer 
from images i 
  inner join schedule s 
    on i.scheduleid=s.scheduleid 
    and s.status='live' 
where image_id=(select max(image_id) from images)
4

1 回答 1

1

Use TOP to retrieve only 1 row
Use ORDER BY to control the sorting, so you get the single row you want

SELECT TOP(1) a.id, a.[image], a.s_id, b.stat, b.[desc]
  FROM TableA a
  JOIN TableB b on a.s_id = b.s_id
 WHERE b.stat = 1
ORDER BY A.ID DESC

An SQLFiddle showing this.

于 2012-09-18T20:05:32.780 回答