You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: [QueryBuilder] if set limit to 0, it will return all data. Perhaps it should return 0 data. #8278
changed the title [-]Bug: Query limit, if set limit => 0 , it will return all data. Perhaps it should return 0 data.[/-][+]Bug: [QueryBuilder] if set limit to 0, it will return all data. Perhaps it should return 0 data.[/+]on Nov 30, 2023
"LIMIT 0 quickly returns an empty set. This can be useful for checking the validity of a query. It can also be employed to obtain the types of the result columns within applications that use a MySQL API that makes result set metadata available."
Do you have a use case for limit 0 with Query Builder?
LIMIT is not in SQL standards. So we don't need to implement like MySQL's.
But since "limit 1" returns 1 row, it is logical that "limit 0" would return 0 rows.
However, the implementation will be more complicated than it is now.
Activity
kenjis commentedon Nov 30, 2023
Thank you for reporting. This is very interesting.
As I read it, I expect
limit(0, 0)producesLIMIT 0, 0.In reality, however, no
LIMITclause is generated.lonnieezell commentedon Nov 30, 2023
What is the practical purpose of doing a query where you don't want any results? I'm not sure that I can think of a reason to do that.
Think of it as "there's 0 limit on the number of results I'd like returned."
kenjis commentedon Nov 30, 2023
This behavior has not changed since the beginning of 4.0.
CodeIgniter4/system/Database/BuilderTrait.php
Lines 2400 to 2404 in 4d2746f
kenjis commentedon Nov 30, 2023
In CI3, the following code generates
SELECT * FROM "job" LIMIT 0(sqlite3):https://github.com/bcit-ci/CodeIgniter/blob/63d037565dd782795021dcbd3b1ca6f8c8a509e4/system/database/DB_query_builder.php#L2440-L2444
[-]Bug: Query limit, if set limit => 0 , it will return all data. Perhaps it should return 0 data.[/-][+]Bug: [QueryBuilder] if set limit to 0, it will return all data. Perhaps it should return 0 data.[/+]kenjis commentedon Nov 30, 2023
I don't know if this is a bug or not.
Since no one has reported it before, there are probably few use cases where 0 is specified.
RikhzanAmir commentedon Nov 30, 2023
I am writing this query in MariaDB: "SELECT * FROM payment limit 0 offset 0" , and it shows no result.
So on Ci3, and Ci4 I am expecting $builder->limit(0,0) or $builder->limit($set, $offset) do the same.
Maybe, it is not a bug, some query do different depending on database we use.
https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
"LIMIT 0 quickly returns an empty set. This can be useful for checking the validity of a query. It can also be employed to obtain the types of the result columns within applications that use a MySQL API that makes result set metadata available."
kenjis commentedon Nov 30, 2023
Do you have a use case for limit 0 with Query Builder?
LIMITis not in SQL standards. So we don't need to implement like MySQL's.But since "limit 1" returns 1 row, it is logical that "limit 0" would return 0 rows.
However, the implementation will be more complicated than it is now.
RikhzanAmir commentedon Nov 30, 2023
If that so, maybe do not change and stick to it. Because on my side it can be fix by adding 1 line of code.
kenjis commentedon Nov 30, 2023
kenjis commentedon Dec 1, 2023
I sent PR #8280 to fix this.
kenjis commentedon Dec 20, 2023
You can fix this behavior in v4.5.0. See #8280