这是基于@itchy 的回答。最佳答案取决于数据库。
一些数据库支持limit
,所以答案是:
SELECT f.id,
f.front,
f.back,
h.statusChangedIdCode,
h.whenCreated,
h.historyIdCode
FROM dpod_site_flashcards AS f
JOIN dpod_site_historyItems AS h ON f.id = h.itemId
WHERE f.id = 216
AND historyIdCode = 'statusChanged'
ORDER BY h.whenCreated
limit 1
SQL Server 和 Sybase 支持top
,导致:
SELECT top 1 f.id,
f.front,
f.back,
h.statusChangedIdCode,
h.whenCreated,
h.historyIdCode
FROM dpod_site_flashcards AS f
JOIN dpod_site_historyItems AS h ON f.id = h.itemId
WHERE f.id = 216
AND historyIdCode = 'statusChanged'
ORDER BY h.whenCreated
Oracle 使用 rownum,但您需要一个子查询:
select * from (SELECT f.id,
f.front,
f.back,
h.statusChangedIdCode,
h.whenCreated,
h.historyIdCode
FROM dpod_site_flashcards AS f
JOIN dpod_site_historyItems AS h ON f.id = h.itemId
WHERE f.id = 216
AND historyIdCode = 'statusChanged'
ORDER BY h.whenCreated
) t
where rownum = 1
DB2 使用 fetch_first:
SELECT f.id,
f.front,
f.back,
h.statusChangedIdCode,
h.whenCreated,
h.historyIdCode
FROM dpod_site_flashcards AS f
JOIN dpod_site_historyItems AS h ON f.id = h.itemId
WHERE f.id = 216
AND historyIdCode = 'statusChanged'
ORDER BY h.whenCreated
fetch first 1 row only