Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if limit was used in mysql? [duplicate]

Tags:

sql

php

mysql

In mysql query, I have something like I select and order, and use where clause, etc..., and finally I have limit 10 for example. Something like this:

Select *
From employee
where fired=0
limit 10

The problem is, how do I know if limit was used and the result was truncated? Basically I want to know easily if there were more results that got cut off from limit. I don't want to do something like

Select (count *)
From employee
where fired=0

subtract

Select (count *)
From employee
where fired=0
limit 10

Is there a simple way for this? Perhaps, like you know how after you run an insert statement, you can get the id using php built in function get_sql_id() (something like that), is there a sql_count_without_limit()?

Thanks

like image 579
omega Avatar asked Dec 25 '22 22:12

omega


1 Answers

You could do

Select *
From employee
where fired=0
limit 10+1

So that:

0-10 values returned: no limit was reached
11 values returned: Limit was reached

And then only use the first 10 values that are returned.

update Raymond's answer is waaaaay better than mine!

like image 83
Peter M Avatar answered Dec 27 '22 18:12

Peter M