user3498169
user3498169

Reputation:

sql syntax error while using top query

I'm using the top query for my table but facing the error

You have an error in your sql syntax, chekck the manual that corresponds to your mysql server version for the right sytntax to use near '4 * from sitemain order by siteid desc limit 0,30' at line 1

here is the code which i used

SELECT top 4 *
FROM sitemain
ORDER BY siteid DESC

Upvotes: 0

Views: 3292

Answers (4)

StarsSky
StarsSky

Reputation: 6711

With MySQL you need to use the LIMIT command as explained here:

Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).

SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4

Upvotes: 1

user275683
user275683

Reputation:

What you loking for is actually LIMIT Clause,

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

Documentation: https://dev.mysql.com/doc/refman/5.0/en/select.html

SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4

Upvotes: 3

Tomas Pastircak
Tomas Pastircak

Reputation: 2857

You are mixing MySQL and TSQL syntax together. The query obviously is MySQL (from the error message). What you want is

SELECT * FROM sitemain ORDER BY siteid DESC LIMIT 0,4

Upvotes: 4

Pleun
Pleun

Reputation: 8920

SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4

Upvotes: 1

Related Questions