Jump to content

should I use LIMIT = 1


piano0011

Recommended Posts

Hey guys!

This is a strange issue but I have been trying to implement a forum into my website and I know that i will always get problems when trying to follow someone's else but anyway, he has included the use of limit=1. For some reason, my other codes suddenly won't work and when I mean won't work, I mean that it will display multiple times like a loop unless I also use limit=1. Is this usually the case? Should I always use limit=1? to make sure that only 1 record is displayed?

Link to comment
Share on other sites

Do you even know what you are doing?   The 'limit' clause is used to do just that - limit the number of results one wants returned by the query engine. 

 

SO?  What is it YOU want?  Do you want only one record or do you want all the records that match your query question?  And why do you think that we have any idea?

Link to comment
Share on other sites

Every SELECT query returns a result SET.

That set can be:

  • Empty Set (no rows)
  • 1..n rows

There are many ways to construct a query so that you know in advance it will return exactly one row.  COUNT(*) queries for example.  Another case you know you will get 0 or 1 row is when you query a table by primary key as the only WHERE clause criteria.  You know in advance you will get either 1 row or no rows if the primary key can not be matched.

All LIMIT does, is constrain a result SET that it has multiple rows, so that you are provided a SUBSET of the full SET that would otherwise have been returned.

I don't see LIMIT 1 used all that much, but you frequently will see LIMIT used to facilitate pagination of a large result set as in 

LIMIT offset, count;

If your pages are assumed to display let's say 10 entries, then:

LIMIT 0, 10;

would allow your client to fetch the first 10 entries.  By keeping track of the 'offset' in your navigation you can paginate large result sets fairly simply, so long as you have determined the full size of the result set initially using a count or alternatively the ability of the client library api to tell you the number of rows in the full result set.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.