22

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 --> 
4
  • 1
    @Alexander why not make that an answer.
    – Pekka
    Jul 19, 2010 at 10:18
  • Your question isn't clear. Are you trying to get the latest two rows, or only the second latest row? Jul 19, 2010 at 10:18
  • @Pekka, OK, posted it as an answer. Sometimes I hesitate to post an answer when it is too simple, because anyway somebody will be faster and we'll end with many similar answers. In fact, you can see it right now here. :) Jul 19, 2010 at 10:24
  • How exactly it "doesn't work"? And, by the way, I'd suggest to post your update as a different question (providing more details of course) because it has nothing to do with the "select second row" question (which is already answered). Jul 19, 2010 at 12:25

5 Answers 5

49

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

4
  • It doesn't work, i get this error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
    – Andre
    Jul 19, 2010 at 11:13
  • Yeah i can provide, check my first post ;)
    – Andre
    Jul 19, 2010 at 11:44
  • 1
    It's strange. Code looks good. Have you tried execute query at the phpMyAdmin or something?
    – antyrat
    Jul 19, 2010 at 11:54
  • 1
    I found the problem! Your second query have mistake: write "LIMIT" instead "LIMI".
    – antyrat
    Jul 19, 2010 at 11:55
8

LIMIT can take two arguments:

SELECT ... LIMIT 1, 1
8

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.

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

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

2

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

1
  • Fetch the data from 2nd row to last row
    – saurabh
    Mar 23, 2015 at 9:42
0
SELECT *
FROM promotion
WHERE id = 16037
ORDER BY RankID ASC LIMIT 1,1
1
  • 3
    Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. This is particularly important when answering old questions (this one is over 12 years old) with existing answers. Please edit your answer and explain how it answers the specific question being asked, and how it improves upon what is already here. See How to Answer.
    – Chris
    Sep 17, 2022 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.