SELECT card_id from logs
WHERE card_id in ('0001', '0002')
ORDER BY date desc limit 1
此代码首先选择与列表匹配的值,然后选择最后一行。 我怎样才能首先从card_id中选择最后一行,看看它是否与列表中的任何值匹配
答案 0 :(得分:1)
如果在ORDER
之前需要WHERE
,则必须将其嵌套在派生表中:
select *
from
( SELECT card_id
from logs
ORDER BY date desc limit 1 -- find the last row first
) as dt
WHERE card_id in ('0001', '0002') -- and then filter
答案 1 :(得分:0)
这是一种方法,使用子查询从查询中分别选择最后一条记录,查找card_id的特定值:
SELECT l.card_id
FROM logs l
WHERE l.card_id IN ('0001','0002)
AND l.card_id IN (
SELECT TOP 1 card_id FROM logs ORDER BY [Date] DESC)