Stopping MySQL query after the first row [closed]

妖精的绣舞 提交于 2020-01-17 08:36:32

问题


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.


回答1:


Try LIMIT 1 at the end of your query.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/15002451/stopping-mysql-query-after-the-first-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!