SQL - Select first 10 rows only?

后端 未结 12 605
时光取名叫无心
时光取名叫无心 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:43

    PostgreSQL:

    SELECT ... LIMIT [num] OFFSET [num];
    
    0 讨论(0)
  • 2020-11-28 03:46

    In standard SQL you can use:

    ... FETCH FIRST 10 ROWS ONLY

    This is supported in DB2, PostgreSQL and Oracle 12.1 (and later)

    0 讨论(0)
  • What you're looking for is a LIMIT clause.

    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
       LIMIT 10
    
    0 讨论(0)
  • 2020-11-28 03:48
    SELECT* from <table name> WHERE rownum <= 10;
    
    0 讨论(0)
  • 2020-11-28 03:52

    In SQL server, use:

    select top 10 ...
    

    e.g.

    select top 100 * from myTable
    select top 100 colA, colB from myTable
    

    In MySQL, use:

    select ... order by num desc limit 10
    
    0 讨论(0)
  • 2020-11-28 03:54

    Depends on your RDBMS

    MS SQL Server

    SELECT TOP 10 ...
    

    MySQL

    SELECT ... LIMIT 10
    

    Sybase

    SET ROWCOUNT 10
    SELECT ...
    

    Etc.

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