Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a (MySQL) "LIMIT" in SQL Server?

I have a problem when i try to change a query with LIMIT from MYSQL to SQL-Server.

Check that :

SELECT * 
FROM tableEating 
WHERE person = '$identity' 
LIMIT 1;

I tried to change it with some queries but nothing work.

like image 338
bilbaoWon Avatar asked May 03 '12 22:05

bilbaoWon


People also ask

How do you write a limit in SQL?

The SQL LIMIT clause restricts how many rows are returned from a query. The syntax for the LIMIT clause is: SELECT * FROM table LIMIT X;. X represents how many records you want to retrieve. For example, you can use the LIMIT clause to retrieve the top five players on a leaderboard.

How do I limit records in MySQL?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

How do I limit records in SQL Server?

If you don't need to omit any rows, you can use SQL Server's TOP clause to limit the rows returned. It is placed immediately after SELECT. The TOP keyword is followed by integer indicating the number of rows to return. In our example, we ordered by price and then limited the returned rows to 3.


1 Answers

LIMIT does not work in T-SQL.

You have to use TOP instead, like this:

SELECT TOP(1) * FROM tableEating WHERE person='$identity';

I hope that will work for you.

As Aaron says, you also need an ORDER BY if you don't want to get an arbitrary row.

like image 112
ChapMic Avatar answered Oct 19 '22 03:10

ChapMic