在第一行[关闭]后停止MySQL查询

I have a piece of MySQL code that searches through a quite lengthy mysql database. When done multiple times, it takes a long time, although the only thing I need from the code is to check the existence of at least one entry. Is it possible to instruct the mysql code to stop searching after finding one row? Thanks.

Try LIMIT 1 at the end of your query.

if you are trying to search for just one thing then there must be some primary key associated with it. so in the query you can include somethere in where clause which will bring in only one result. something like this

    select * from students where id='123' 

As primary keys are the unique key so there won't be any duplication and only one search will be done.

SELECT * FROM tablename LIMIT 1 OFFSET 0
  • LIMIT will set the max items you want back
  • OFFSET let's you state the starting point to start your query

Combining LIMIT and OFFSET gives you very precise control of fetching tons of database rows over multiple sessions.

Or use the easier 2-parameter LIMIT function (same as LIMIT, OFFSET):

SELECT * FROM tablename LIMIT 1, 0