2015-10-06 75 views
0

我一直在嘗試獲取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 
+1

'LIMIT 1 OFFSET 1'獲得倒數第二 – lad2025

+1

SELECT * FROM MY_TABLE ORDER BY news_id DESC LIMIT 2,1 –

+0

對不起,但它仍然得到最後一行 –

回答

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 
+0

@ JoshuaC.Tuonan查看更新的答案 – lad2025

0
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; 

您還應參考Select the nth Highest Record in a Database Table

相關問題