SQL - Select first 10 rows only?

后端 未结 12 606
时光取名叫无心
时光取名叫无心 2020-11-28 03:24

How do I select only the first 10 results of a query?

I would like to display only the first 10 results from the following query:

SELECT a.names,
            


        
相关标签:
12条回答
  • 2020-11-28 03:58

    In MySQL:

    SELECT * FROM `table` LIMIT 0, 10
    
    0 讨论(0)
  • 2020-11-28 03:58
    SELECT  Top(12) Month, Year, Code FROM TempEmp 
    ORDER BY  Year DESC,month DESC
    
    0 讨论(0)
  • 2020-11-28 04:03
    SELECT *  
      FROM (SELECT ROW_NUMBER () OVER (ORDER BY user_id) user_row_no, a.* FROM temp_emp a)  
     WHERE user_row_no > 1 and user_row_no <11  
    

    This worked for me.If i may,i have few useful dbscripts that you can have look at

    Useful Dbscripts

    0 讨论(0)
  • The ANSI SQL answer is FETCH FIRST.

    SELECT a.names,
             COUNT(b.post_title) AS num
        FROM wp_celebnames a
        JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
        WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
    GROUP BY a.names
    ORDER BY num DESC
    FETCH FIRST 10 ROWS ONLY
    

    If you want ties to be included, do FETCH FIRST 10 ROWS WITH TIES instead.

    To skip a specified number of rows, use OFFSET, e.g.

    ...
    ORDER BY num DESC
    OFFSET 20
    FETCH FIRST 10 ROWS ONLY
    

    Will skip the first 20 rows, and then fetch 10 rows.

    Supported by newer versions of Oracle, PostgreSQL, MS SQL Server, Mimer SQL and DB2 etc.

    0 讨论(0)
  • 2020-11-28 04:06

    Firebird:

    SELECT FIRST 10 * FROM MYTABLE
    
    0 讨论(0)
  • 2020-11-28 04:08

    Oracle

    WHERE ROWNUM <= 10  and whatever_else ;
    

    ROWNUM is a magic variable which contains each row's sequence number 1..n.

    0 讨论(0)
提交回复
热议问题