Fetching only First row from mysql

Discussion in 'MySQL' started by seotraining, Nov 10, 2010.

  1. #1
    Hello experts

    I want to fetch only first row from the table using query of mysql. How can I do that? Can anyone advice that?
     
    seotraining, Nov 10, 2010 IP
  2. Fruktkaka

    Fruktkaka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #2
    You need to have a unique ID for every row first. Assuming you have an int field called "id" with auto increment this would show the first row:

    SELECT * FROM table ORDER BY id LIMIT 1;

    If you want to show the last row:
    SELECT * FROM table ORDER BY id DESC LIMIT 1;
     
    Fruktkaka, Nov 10, 2010 IP
  3. teamaguilar

    teamaguilar Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can achieve this using LIMIT keyword. The other variations of LIMIT..
    Use this function to page through your data. If you have a result that returns 26 rows, you could divide it on 3 pages ( giving max 10 rows per page ).
    SELECT * FROM `tablename` LIMIT 0,10; --Page1
    SELECT * FROM `tablename` LIMIT 10,10; --Page2
    SELECT * FROM `tablename` LIMIT 20,-1; --Page3

    -1 refers to till the end
     
    teamaguilar, Nov 23, 2010 IP