简体   繁体   English

Mysql Select 第二行

[英]Mysql Select Second Row

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.它选择最新的项目,现在我想 select 倒数第二个项目。

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

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 - 仅选择第二项

LIMIT可以有两个参数:

SELECT ... LIMIT 1, 1

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子句的更多信息。

SELECT * FROM表,其中id>(SELECT id FROM table order by id ASC limit1,1)和id <=(从表中选择max(id))

SELECT *
FROM promotion
WHERE id = 16037
ORDER BY RankID ASC LIMIT 1,1

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

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