Mysql 选择第二行

2022-08-30 18:48:42

我有一个mysql问题。


我的网站上有一个新闻部分,我想显示两个最新项目。如果我这样做:

SELECT * FROM nieuws ORDER BY id DESC LIMIT 1

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

你们知道该怎么做吗?

编辑

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

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

答案 1

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

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


答案 2

LIMIT可以采用两个参数:

SELECT ... LIMIT 1, 1

推荐