我有一张MySQL表,其中包含了姓名、表现和日期时间等列。如何只检索最近的MySQL行?
SELECT *
FROM yourtable
WHERE date_and_time = (SELECT MAX(date_and_time) FROM yourtable)
...
ORDER BY `date and time` DESC
LIMIT 1
日期和时间
列中,这可能是您要查找的内容。SELECT * FROM table ORDER BY date, time LIMIT 1
SELECT name,
perf,
date_and_time
FROM table
ORDER BY date_and_time DESC
LIMIT 1
CREATE TRIGGER mytable_update BEFORE UPDATE ON `mytable`
FOR EACH ROW SET NEW.updated_at = NOW();
ORDER BY `updated_at` DESC LIMIT 1;
此外,时间敏感表通常有一个字段'created_at',当行插入时设置一个不变的时间戳。通常会与触发器连接在一起:
CREATE TRIGGER mytable_create BEFORE INSERT ON `mytable`
FOR EACH ROW SET NEW.created_at = NOW(), NEW.updated_at = NOW();
NEW.created_at = OLD.created_at;
对于 SQL Server:选择最新的一行:select top 1 * from tablename order by date_and_time DESC
对于 MySQL:选择最新的一行:select * from tablename order by date_and_time DESC limit 1