user656925
user656925

Reputation:

How do i only pull the top "x" rows from a table?

I have this query

$query_return = database::query("SELECT * FROM tw ORDER BY time DESC");

that pulls all the rows, however I only want the top x=8 rows.

How do I modify it?

Thanks!

Upvotes: 0

Views: 78

Answers (3)

Haroon
Haroon

Reputation: 1110

If code is for MS-SQL use TOP e.g. SELECT TOP (8) * FROM ....

Upvotes: 0

Barry Brown
Barry Brown

Reputation: 20604

Use LIMIT:

SELECT * FROM tw ORDER BY time DESC LIMIT 8

Upvotes: 3

Arjan
Arjan

Reputation: 9874

Add a limit clause:

$query_return = database::query("SELECT * FROM tw ORDER BY time DESC LIMIT 8");

Upvotes: 3

Related Questions