Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORDER BY QUERY BUILDER ISSUE #4395

Closed
mertdogan opened this issue Jan 23, 2016 · 5 comments
Closed

ORDER BY QUERY BUILDER ISSUE #4395

mertdogan opened this issue Jan 23, 2016 · 5 comments

Comments

@mertdogan
Copy link

I hava a test query :
Code:
$this->db->where('1 IS NOT NULL',NULL,FALSE)->WHERE('X>','0')->FROM('X')->ORDER_BY('X','ASC')->count_all_results();

and it is generating error:

Code:
Error Number: 42000/1033

[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

SELECT COUNT(*) AS "numrows" FROM ( SELECT * FROM "X" WHERE 1 IS NOT NULL AND "X" > '0' ORDER BY "X" ASC ) CI_count_all_results

@mertdogan
Copy link
Author

Solution:
Add this line in DB_query_builder.php in system/database file line 1381:
if(!empty($this->qb_orderby)&&empty($this->qb_limit)&&empty($this->qb_offset))$this->qb_orderby=null;

@aanbar
Copy link
Contributor

aanbar commented Jan 23, 2016

Why would you use order_by with count_all_results in the first place?

@mertdogan
Copy link
Author

First of all; i have another problem. I have a script. This script stopes good working when i upgrade from 3.0.2 to 3.0.4 . But in another function; all of database functions seems working normally. When i revert my system folder to the previous function; my script is working as it must be again.

Second one; i check this function because you described there is a bug fix for count_all_results used with order_by clause:
Fixed a bug (#4212) - Query Builder method count_all_results() could fail if an ORDER BY condition is used.

@aanbar
Copy link
Contributor

aanbar commented Jan 23, 2016

Looks like you're right, the latest change converts it to a subquery which is breaking your query.

@mertdogan
Copy link
Author

Yep;

I found its' solution and wrote my 2. message:

Solution:
Add this line in DB_query_builder.php in system/database file line 1381:
if(!empty($this->qb_orderby)&&empty($this->qb_limit)&&empty($this->qb_offset))$this->qb_orderby=null;

@narfbg narfbg added this to the 3.0.5 milestone Jan 25, 2016
narfbg added a commit that referenced this issue Jan 25, 2016
@narfbg narfbg closed this as completed Jan 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants