SQL – 只select前10行?
如何只select查询的前10个结果?
我只想显示以下查询的前10个结果:
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
在SQL服务器中,使用:
select top 10 ...
例如
select top 100 * from myTable select top 100 colA, colB from myTable
在MySQL中,使用:
select ... order by num desc limit 10
取决于你的RDBMS
MS SQL Server
SELECT TOP 10 ...
MySQL的
SELECT ... LIMIT 10
SYBASE
SET ROWCOUNT 10 SELECT ...
等等。
神谕
WHERE ROWNUM <= 10 and whatever_else ;
ROWNUM
是一个魔术variables,它包含了每一行的序号1 .. n 。
DB2
... FETCH FIRST 10 ROWS ONLY
在MySQL中:
SELECT * FROM `table` LIMIT 0, 10
ANSI SQL答案是第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
如果你想要包含关系,取而代之的是取FETCH FIRST 10 ROWS WITH TIES
。
要跳过指定数量的行,请使用OFFSET
,例如
... ORDER BY num DESC OFFSET 20 FETCH FIRST 10 ROWS ONLY
将跳过前20行,然后取10行。
新版本的Oracle,MS SQL Server和Mimer SQL等支持
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
这对我有用。如果我可以,我有几个有用的dbscripts,你可以看看
有用的dbscripts
你要找的是一个LIMIT子句。
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
火鸟:
SELECT FIRST 10 * FROM MYTABLE
SELECT* from <table name> WHERE rownum <= 10;