Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Select Second Row

Tags:

sql

php

mysql

I have a mysql question.


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 --> 
like image 553
Andre Avatar asked Jul 19 '10 10:07

Andre


2 Answers

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

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

like image 50
antyrat Avatar answered Sep 18 '22 03:09

antyrat


LIMIT can take two arguments:

SELECT ... LIMIT 1, 1
like image 36
Alexander Konstantinov Avatar answered Sep 18 '22 03:09

Alexander Konstantinov