[英]Select the second last row in Mysql
我一直在尝试获取mysql上数据的倒数第二行。 但是我的查询仅显示最后一行。
SELECT news.news_title, news.news_details, news.news_author, news_images.filename
FROM news JOIN news_images
ON news.news_title = news_images.news_title
ORDER BY news_id DESC LIMIT 1
使用OFFSET
获取倒数第二个值,例如:
SELECT news.news_title, news.news_details, news.news_author, news_images.filename
FROM news JOIN news_images
ON news.news_title = news_images.news_title
ORDER BY news_id DESC
LIMIT 1 OFFSET 1;
LIMIT number_of_rows OFFSET start_from
如果要最后2行,请使用: LIMIT 2
。
编辑:您的问题非常不清楚,但尝试:
SELECT t.news_title, t.news_details, t.news_author, ni.filename
FROM (SELECT news_title, news_details, news_author
FROM news
ORDER BY news_id DESC
LIMIT 1 OFFSET 1) AS t
JOIN news_images ni
ON t.news_title = ni.news_title
SELECT news.news_title, news.news_details, news.news_author, news_images.filename
FROM news JOIN news_images
ON news.news_title = news_images.news_title
ORDER BY news_id DESC
LIMIT 1 OFFSET 1;
您还应该参考选择数据库表中的第n个最高记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.