php Mysql选择第二行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3280155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:13:09  来源:igfitidea点击:

Mysql Select Second Row

phpmysqlsql

提问by Andre

I have a mysql question.

我有一个 mysql 问题。



I have a news section on my website, and I want to display the two latest items. If I do:

我的网站上有一个新闻版块,我想显示两个最新的项目。如果我做:

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1

it selects the latest item, and now I want to select the second to last item.

它选择最新的项目,现在我想选择倒数第二个项目。

Do you guys know how to do it?

大家知道怎么做吗?

/// EDIT

/// 编辑

Now it doesn't work, here's my code: (I have connect included ;) )

现在它不起作用,这是我的代码:(我已包含连接;))

            $select = mysql_query("SELECT * FROM nieuws ORDER BY id DESC LIMIT 1");
            while($row = mysql_fetch_assoc($select)) {
            $datum = $row['time'];
            $titel = $row['title'];
            $bericht = $row['message'];
            ?>
            <div class="entry">

                <span class="blue date"><?php echo "$datum"; ?></span>
                <h3><?php echo "$titel"; ?></h3>
                <p><?php echo "$bericht"; ?></p> <br />
            </div><!-- end of entry --> <?php } ?>
            <?php 
            $select2 = mysql_query("SELECT * FROM nieuws ORDER BY id DESC LIMI 1, 1");
            while($row2 = mysql_fetch_assoc($select2)) {
                $datum = $row2['time'];
                $titel = $row2['title'];
                $bericht = $row2['message'];
                ?>
            <div class="entry">
                <span class="green date"><?php echo "$datum"; ?> </span>
                <h3><?php echo "$titel"; ?></h3>
                <p><?php echo "$bericht"; ?></p>
            </div> <!-- end of entry --> <?php } ?>
        </div><!-- end of news --> 

回答by antyrat

SELECT * FROM nieuws ORDER BY id DESC LIMIT 2- selects last 2 items

SELECT * FROM nieuws ORDER BY id DESC LIMIT 2- 选择最后 2 项

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1- selects only second item

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1- 只选择第二个项目

回答by Alexander Konstantinov

LIMITcan take two arguments:

LIMIT可以采用两个参数:

SELECT ... LIMIT 1, 1

回答by Rich Adams

If you want to display the latest two items, then you can get both at the same time by limiting to 2 instead of 1. This means it's only one database hit to get the information you need.

如果您想显示最新的两项,那么您可以通过限制为 2 而不是 1 来同时获取这两项。这意味着只需点击一次数据库即可获取您需要的信息。

SELECT * FROM nieuws ORDER BY id DESC LIMIT 2

Or if you only want the second row, you can give an offset to the LIMIT, to tell it which row to start from, (Although if you get the first row in one query, then get the second in another, you're doing two database hits to get the data you want, which can affect performance).

或者如果你只想要第二行,你可以给 LIMIT 一个偏移量,告诉它从哪一行开始,(虽然如果你在一个查询中得到第一行,然后在另一个查询中得到第二行,你正在做两次数据库命中以获得您想要的数据,这会影响性能)。

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1

You can find out more information on how to use the LIMIT clause in the MySQL documentation.

您可以在MySQL 文档中找到有关如何使用 LIMIT 子句的更多信息。

回答by saurabh

SELECT * FROM table where id >(SELECT id FROM table order by id ASC limit1,1) and id <=(select max(id) from table)

SELECT * FROM table where id >(SELECT id FROM table order by id ASC limit1,1) and id <=(select max(id) from table)