SQL error while using LIMIT
I am a beginner with SQL and I'm following the tutorial at W3 Schools SQL Exercises. I开发者_开发问答 tried to run the following query but I am getting error:
SELECT * FROM Customers LIMIT 5, 5
ERROR:
Syntax error in FROM clause.
Your problem is that you are using MySql syntax instead of SqlServer syntax. You should use:
SELECT TOP 5 * FROM Customers
If you're following the example in the link you provided...
We will use the Customers table in the Northwind database:
Northwind is a sample database in MS SQL products. And MS SQL does not have support using LIMIT
queries. So that website is probably using some version of that RDBMS.
Try running:
SELECT TOP 5 * FROM customers;
This gets you the first 5 records in the result set (unordered). It does not skip any records, though, as your LIMIT 5, 5
clause would have.
You can see that they use ADODB, so you should use TOP clause:
SELECT TOP 5 * FROM Customers
I suspect you need to use offset
:
select * from Customers limit 5 offset 5;
Not all RDBMSs support the limit m,n
syntax (I believe MySQL does, but PostgreSQL doesn't). Also, you'll probably want to use an order by
clause to help ensure that you get consistent results with your query (because databases, in general, don't care about the order of rows or columns unless you specify):
select * from Customers order by col limit 5 offset 5;
I had the same problem. You can use rownum
:
select * from Customers where rownum < 6;
精彩评论