[英]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.