简体   繁体   English

选择Mysql中的倒数第二行

[英]Select the second last row in Mysql

I've been trying to get the second last row of data on mysql. 我一直在尝试获取mysql上数据的倒数第二行。 But my query only shows the last row. 但是我的查询仅显示最后一行。

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

Use OFFSET to get second last value like: 使用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

If you want 2 last rows use: LIMIT 2 . 如果要最后2行,请使用: LIMIT 2

EDIT: Your question is very unclear but try: 编辑:您的问题非常不清楚,但尝试:

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;

You should also refer Select the nth Highest Record in a Database Table 您还应该参考选择数据库表中的第n个最高记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM