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

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

Related

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.

Error: Incorrect syntax near the keyword 'RETURN'.SQLState: S0001ErrorCode: 156 when working with Squirrel client

Iam trying to write a PL/SQL function in Squirrel client. when Iam tryin to excute a script in squirrel getting this error. Please help me as Im new to this squirrel client.
Script:
CREATE FUNCTION totalHoliday
RETURN int IS
total int:= 0;
BEGIN
SELECT count(*) into total
FROM dbo.HOLIDAY;
RETURN total;
END;
/
Error: Incorrect syntax near the keyword 'RETURN'.
SQLState: S0001
ErrorCode: 156
Error occured in:
CREATE FUNCTION totalHoliday
RETURN int IS
total int:= 0
I tried by giving AS Instead of IS but did'nt work..
In the SQL tab in the Session Properties dialog change the Statement Separator from ; to /.

Filtering on time on a datetime field using SqlAlchemy + python

I have a LocationEvent model which has a timestamp property which is a DateTime field. I want to write a function which will take a start_time and end_time (which are python time objects) and will return the LocationEvent objects which are between those two times - irrespective of the date.
I tried using the extract function in SQLAlchemy but was not able to succeed. I tried the following -
LocationEvent.query.filter(sqlalchemy.extract('time', LocationEvent.timestamp) >= start_time)
This gives me the following error -
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "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 'time FROM location_event.timestamp) >= '08:00:00'' at line 3") 'SELECT
location_event.id AS location_event_id, location_event.latitude AS location_event_latitude,
location_event.longitude AS location_event_longitude, location_event.type AS
location_event_type, location_event.timestamp AS location_event_timestamp,
location_event.creation_date AS location_event_creation_date \nFROM location_event \nWHERE
EXTRACT(time FROM location_event.timestamp) >= %s' ('08:00:00',)
Any idea on how can I achieve this?
I was able to do this using the following -
from sqlalchemy import Time, cast
LocationEvent.query.filter(cast(LocationEvent.timestamp, Time) >= start_time, cast(LocationEvent.timestamp, Time) <= end_time).all()

Simple error with Database library using codeIgniter

$this->db->select('*');
$this->db->from('product');
$this->db->where('del_in !=',1);
$query=$this->db->get();
return $query;
I am getting sql following error Please help me to solve this...
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 '' at line 3
SELECT * FROM (`product`) WHERE `del_in` != 1 LIMIT 0 ,
Filename: D:\wamp\www\shopcart\system\database\DB_driver.php
Just make sure column name and table name are correct. After that try this code
$this->db->select('*');
$this->db->where('del_in !=',1);
$this->db->from('product');
$query=$this->db->get();
return $query->result();

Simple difficulty with HQL Query

I am having a trouble with executing an HQL query like this:
select new myPackage.view.CoverDocumentReportView(Re.code AS fulCd,
Re.creditPrice AS crtprc,
Re.debitPrice AS dbtprc,
(Re.debitPrice - Re.debitPrice) AS redbtprc,
(Re.creditPrice- Re.creditPrice) AS recrtprc,
(Re.debitPrice-Re.creditPrice) AS rem)
from
(select fullCode as code,
sum(creditPrice) as creditPrice ,
sum(debitPrice) as debitPrice
from DocumentMaster DM,
DocumentAccount DA,
Tree T ,
AccountTree AT,
DocumentDetailed DD
where DM.id = DA.documentMaster and
DA.accountTree = T.id and
DA.accountTree = AT.id and
DD.documentAccount = DA.id
group by DA.accountTree ) As Re
1)
If I execute this like:
SQLQuery crit = (SQLQuery) session
.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li = (ArrayList<CoverDocumentReportView>) crit.list();
ERROR 2012-12-22 14:16:19,838 [http-8080-1] org.hibernate.util.JDBCExceptionReporter : 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 '.datx.web.accounting.view.CoverDocumentReportView(Re.code AS fulCd,
Re.creditP' at line 1
2)
If I execute it with this:
Query query = session.createQuery(sql).setResultTransformer(Transformers.aliasToBean(CoverDocumentReportView.class));
ArrayList<CoverDocumentReportView> li = (ArrayList<CoverDocumentReportView>)query.list();
The error will be:
ERROR 2012-12-22 14:51:46,709 [http-8080-1] org.hibernate.hql.ast.ErrorCounter : line 1:224: unexpected token: (
ERROR 2012-12-22 14:51:46,709 [http-8080-1] org.hibernate.hql.ast.ErrorCounter : line 1:308: unexpected token: sum
What is the problem?
SQL and HQL are two different languages.
HQL doesn't support subqueries in from clauses, so this query can't be an HQL query.
And SQL doesn't know about Java objects, and doesn't have any new() function allowing to create them, so the query is not a valid SQL query either.
Make it a valid SQL query, execute it using createSQLQuery(), then iterate through the results and create instances of your objects from the returned rows. Or use a result transformer as you're doing, which will do that for you. the result transformer will use the aliases you assigned to the returned columns of the SQL query to create beans for you. You don't need any new CoverDocumentReportView() in the query to make that work. Read the javadoc for details.