How to write scope where one attribute is less than other? - mysql

I tried this one
scope :opened, lambda {
where("entries_count <= limit")
}
But I get an error:
Mysql2::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'limit)' at line 1: SELECT shifts.* FROM shifts WHERE
(entries_count <= limit)

Are both entries_count and limit active record attributes mapped to columns?
scope :opened, -> { where(:entries_count <= :limit }

You can get this with:
scope :opened, lambda { |limit| where("entries_count <= ?", limit) }
or this way that is in a more updated syntax for lambda:
scope :opened, -> limit { where("entries_count <= ?", limit) }

Related

Laravel 8: query builder escapes the "greater than" operator

I'm getting a MySQL error when trying to run a query through Eloquent.
FlightController.php:
$flightsToFinish = SrteFlight::whereRaw('DATEDIFF(MINUTE, NOW(), disc_time) > 15')->get();
foreach ($flightsToFinish as $flightToFinish)
{
$flightToFinish->status = 1;
$flightToFinish->save();
}
Error:
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation:
1582 Incorrect parameter count in the call to native function 'DATEDIFF' (SQL: select * from
`srte_flights` where DATEDIFF(MINUTE, NOW(), disc_time) > 15) in file
/var/www/vhosts/ar.ivao.aero/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
Am I missing something? Thanks in advance.
DATEDIFF just take 2 argument. (I think you misused SQL server syntax instead of MySQL syntax)
duplicate : Incorrect parameter count in the call to native function 'DATEDIFF'
Sql Server syntax
Mysql syntax

NODEJS MySQL Bindings throws ER_PARSE_ERROR 1064

Given the following:
let sql: any = 'SELECT * FROM test_people ORDER BY :column :direction LIMIT :limit, :offset';
let binds: any = { column: 'name', direction: 'desc', limit: '1', offset: '10' };
let result = await mysql.query(sql, binds);
For whatever reason it throws mysql syntax error, if I replace the bindings and write it hard-coded without the bindings then the query actually works and fetches the result. not sure what is wrong here. help ! :)
BTW, I also tried it with the question marks version, getting same syntax error.
Error output:
...
code: 'ER_PARSE_ERROR',
errno: 1064,
'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \':column :direction LIMIT :limit, :offset\' at line 1',
...
Appreciate any solution,
Only values can be bound. Column names (in ORDER BY), and the :direction cannot be bound. Also FYI table names, database names and other parts of the SQL syntax cannot be bound.

Grails : MySQL server version for the right syntax to use near '))

I am using Grails 2.3.4 and having trouble with named queries.
For example:
def c = Party.createCriteria()
completedSearchList = c.list(max: params.max, offset: params.offset){
if(params.searchText) {
or {
ilike("FirstName", "%${params.searchText}%")
ilike("pDate", "%${params.searchText}%")
"in"("user",users)
}
and {
eq("status","COMP")
}
and {
eq("projectID",session.projectID)
}
}
}
Goal is to bring the search value written in the search field through param value and use Grails criteria to search.
But I am getting the error msg like:
Stacktrace follows:Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) and (this_.status='COMP') and (this_.projectid='bacrt') limit 10' at line 1

SQL syntax Limit 1 error

WordPress database error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 4
for query
SELECT wp_wpsp_templates_fonts.name
FROM wp_wpsp_templates_fonts
WHERE wp_wpsp_templates_fonts.fontID =
LIMIT 1
made by
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/twentyten/wpsp-sales-page.php'), require_once('/plugins/wpsp/wpsp-frontend.php'), TemplatesModel->GetFontByID
Below is the code.
function GetFontByID($fontID) {
global $wpdb;
$results = $wpdb->get_results(" SELECT ".$wpdb->prefix."wpsp_templates_fonts.name
FROM ".$wpdb->prefix."wpsp_templates_fonts
WHERE ".$wpdb->prefix."wpsp_templates_fonts.fontID = ".mysql_real_escape_string($fontID)."
LIMIT 1");
return $results[0]->name;
}
Any ideas on how to fix this issue?
GetFontByID() needs a parameter passed to it. Currently, the param appears blank.

Syntax error of ''?

I can't seem to figure out where this is coming from... MySQL give me a syntax error of an empty quote and gives me a line number that doesn't seem to be wrong at all. Worse still, deleting the loop the line number points to still gives me the same error, just with a different line number.
ERROR 1064 (42000) at line 13: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 39
Talk about unhelpful feedback from MySQL!
The code in question is a stored function, and I ran into this when trying to apply the answer to another question. The updated code is available here.
EDIT: #MarkByers, here's the function reduced as low as I could get it while still triggering the error:
DROP FUNCTION IF EXISTS months_within_range;
DELIMITER //
CREATE FUNCTION months_within_range(starts_at DATE, ends_at DATE, filter_range VARCHAR(255)) RETURNS TINYINT
BEGIN
SET #matches = 1;
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END//
DELIMITER ;
You are missing the END IF
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END IF;
And the reason RETURN IF(#matches >= 1, 1, 0); works is because this is the IF function, which is different from the IF statement