Results 1 to 5 of 5

Thread: Select TOP in MySQL

  1. #1
    Join Date
    Oct 2002
    Posts
    933

    Select TOP in MySQL

    Any idea what is the equivalent of SELECT TOP .... FROM...
    in MySQL to retrieve a subset of the records.

    Thanks,

    Frank

  2. #2
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    The following should work:

    Code:
    SELECT DISTINCT( `cloumnA` ), COUNT( `columnA` ) FROM `tablename` GROUP BY 1 ORDER BY 2 DESC LIMIT 0,10;
    The nice thing with using LIMIT is that you can use this function to page through your data. If you have a result that returns 23 rows, you could devide it on 3 pages ( giving max 10 rows per page ).

    For page 1:

    Code:
    SELECT * FROM `tablename` LIMIT 0,10;
    For page 2:

    Code:
    SELECT * FROM `tablename` LIMIT 10,10;
    For Page 3:

    Code:
    SELECT * FROM `tablename` LIMIT 20,-1;
    For more info, refer to the MySQL documentation

    Cheers

  3. #3
    Join Date
    Oct 2002
    Posts
    933
    Thanks for the tip. I'll check the doc. The -1 of LIMIT 10,-1 must mean from 10 to the end then ... does all MSSQL has this syntax or just a certain version.

    Frank

  4. #4
    Join Date
    Feb 2003
    Location
    Johannesburg, South Africa
    Posts
    145
    Well, I have used it since earlier 3.x versions, and my current test machine ( 4.0.11a ) shows that it is still working.

    Then again - I see you now write about MSSQL. Remember that this is the MySQL forum - _big_ diff If you really mean MSSQL, you should post your question in the MS SQL forum.

    Of course there will be noe harm if you check out and use MySQL - I am sure the experience will be an enlightenment.

    Cheers

  5. #5
    Join Date
    Oct 2002
    Posts
    933
    OH... I mean MySQL and not MSSQL!!!
    Thanks, This LIMIT feature will solve my problem.

    Thanks,

    Frank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •