如何仅选择查询的前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
答案 0 :(得分:137)
在SQL Server中,使用:
select top 10 ...
e.g。
select top 100 * from myTable
select top 100 colA, colB from myTable
在MySQL中,使用:
select ... order by num desc limit 10
答案 1 :(得分:34)
取决于您的RDBMS
MS SQL Server
SELECT TOP 10 ...
的MySQL
SELECT ... LIMIT 10
的Sybase
SET ROWCOUNT 10
SELECT ...
等
答案 2 :(得分:17)
在MySQL中:
SELECT * FROM `table` LIMIT 0, 10
答案 3 :(得分:16)
DB2
... FETCH FIRST 10 ROWS ONLY
答案 4 :(得分:16)
的Oracle
WHERE ROWNUM <= 10 and whatever_else ;
ROWNUM
是一个神奇的变量,它包含每一行的序列号1 .. n 。
答案 5 :(得分:14)
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,PostgreSQL,MS SQL Server,Mimer SQL和DB2等的支持。
答案 6 :(得分:2)
您正在寻找的是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
答案 7 :(得分:2)
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
答案 8 :(得分:1)
火鸟:
SELECT FIRST 10 * FROM MYTABLE
答案 9 :(得分:0)
<div class="action-bar">
<a href="" class="btn btn-primary">
<i class="fa fa-icon"></i>
Button label
</a>
<a href="" class="btn btn-primary">
<i class="fa fa-icon"></i>
Button label
</a>
</div>
答案 10 :(得分:0)
SELECT Top(12) Month, Year, Code FROM TempEmp
ORDER BY Year DESC,month DESC
答案 11 :(得分:0)
PostgreSQL:
SELECT ... LIMIT [num] OFFSET [num];