use sql SELECT nodejs - mysql

I tried to use the select sql option in node js sql plugins.
But I get a error with this syntax :
con.query( 'SELECT token FROM utilisateursinvitation WHERE nomwork = ? AND
Rejoint = ? EXCEPT SELECT token FROM utilisateursinvitation WHERE nomwork
= ? AND Rejoint = ? AND nomdugroupe = ? ',
[nomwork,tatazueyzyu,"nomdugroupe"], function (error, results, fields) {
and the error :
Error: ER_PARSE_ERROR: 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 'EXCEPT SELECT token FROM utilisateursinvitation WHERE nomwork = 'nomdugroupe' ' at line 1
Because I don't know the correct syntax.
Can you help me ? THanks!

EXCEPT is not MySQL syntax.
Instead, you can use NOT IN or NOT EXISTS:
SELECT ui.token
FROM ui.utilisateursinvitation ui
WHERE ui.nomwork = ? AND ui.Rejoint = ? AND
NOT EXISTS (SELECT
FROM utilisateursinvitation ui2
WHERE ui2.token = ui.token AND
ui2.nomwork = ? AND ui2.Rejoint = ? AND
nomdugroupe = ?
);
Or, if you are trying to fine tokens that have nomwork and Rejoint as the input values, but not a particular nomdugroupe:
SELECT ui.token
FROM ui.utilisateursinvitation ui
WHERE ui.nomwork = ? AND ui.Rejoint = ?
GROUP BY ui.token
HAVING SUM(nomdugroupe = ?) > 0;

Related

how to alias(AS) a table in raw SQL with knex.js

I am using knex.js with MySql DB. I have a migration where I have to use raw SQL. My migration looks like this :
import * as Knex from "knex";
const brokersTable = "OriginationAdmin.Brokers";
const brokerIndividualTable = "OriginationAdmin.BrokerIndividuals";
exports.up = async (knex: Knex) => {
await knex.schema.raw(
`UPDATE ${brokersTable}
JOIN(
SELECT CompanyName, MIN(Id) AS Id
FROM ${brokersTable}
GROUP BY CompanyName )
AS x
ON x.CompanyName = ${brokersTable}.CompanyName
AND isDeleted IS NULL
SET isDeleted = CASE WHEN (x.Id <> ${brokersTable}.Id) THEN 1 END`
);
when I run migrate up I get an error message:RequestError: Incorrect syntax near the keyword 'AS'. I know the SQL Query is syntactically correct but why am I getting an error with the aliasing keyword and how do I fix it?
Edit: This most(relevant part) of the error message
migration file "20210822190619_de-duplicate-brokers-and-associate-individuals.ts" failed
migration failed with error: UPDATE OriginationAdmin.Brokers
JOIN (
SELECT CompanyName, MIN(Id) AS Id
FROM OriginationAdmin.Brokers
GROUP BY CompanyName
) AS x
ON x.CompanyName = OriginationAdmin.Brokers.CompanyName
AND isDeleted IS NULL
SET isDeleted = CASE WHEN (x.Id <> OriginationAdmin.Brokers.Id) THEN 1 END
- Incorrect syntax near the keyword 'AS'.
RequestError: Incorrect syntax near the keyword 'AS'.
at handleError (/Users/haroonAzhar/Desktop/FAL/omni-api/node_modules/mssql/lib/tedious/request.js:366:15)
The error message isn't very helpful to what the problem is.

ER_PARSE_ERROR: check the manual that corresponds to your MySQL server version for the right syntax to use near '== false

I want to check a checkbox value true or false and my query like this, but I'm getting an error here. Please help me to fix this issue. It's getting near checkbox value
sql = `
SELECT data
FROM Job
WHERE (? IS NULL OR
JSON_EXTRACT(data, '$.address') LIKE ? OR
JSON_EXTRACT(data, '$.customerJobNumber') = ? OR
JSON_EXTRACT(data, '$.jobId') = ? ) AND
(JSON_EXTRACT(data, '$.jobDate') IS NULL OR JSON_EXTRACT(data, '$.jobDate') <= ?)
AND JSON_EXTRACT(data,'$.checkBoxValue')== false
AND JSON_EXTRACT(data, '$.status') NOT IN (?) `
Just like in the conditions before you should only use one =. Also the value of the JSON_EXTRACT will be a string so you should compare it to a string:
AND JSON_EXTRACT(data, '$.checkBoxValue') = 'false'

MySQL : error in your SQL syntax | LIMIT 0, 25

I dont understand this error, guys please help me. why I am getting this error..
Is there something wrong in my query?
this is the error..
#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 'LIMIT 0, 25' at line 7
and this is my query:
SELECT
equivalent
FROM
tb_student_record
INNER JOIN
tb_student ON tb_student_record.stud_id = tb_student.stud_id
WHERE
tb_student_record.instructor_id = 'INST-20131296'
AND tb_student_record.criteria_id = '1'
AND tb_student_record.class_record_id = '1'
AND (CONCAT(stud_fname, ' ', stud_lname) = 'Jeffrey Oliveras'
AND tb_student_record.term = 'Prelim'
you are missing closing paranthesis at concat function
SELECT equivalent FROM tb_student_record INNER JOIN tb_student ON tb_student_record.stud_id=tb_student.stud_id
WHERE tb_student_record.instructor_id = 'INST-20131296'
AND tb_student_record.criteria_id = '1'
AND tb_student_record.class_record_id = '1'
AND (CONCAT(stud_fname, ' ', stud_lname) = 'Jeffrey Oliveras' )
AND tb_student_record.term = 'Prelim'
It seems that your query have problem at the end:

JDBC update database

i have a form and onClick listener i want to update some data on my database exception says that i have a syntax error but when i execute query at mysql console it works here is my code all variables are checked
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = pquant + ? where pname = ?");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
Error
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'DVD Verdatim' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at buy$1.actionPerformed(buy.java:62)
As the compiler mentioned, there is indeed a Syntax error in your SQL query. Try:
String temp = itemList.getModel().getElementAt(itemList.getSelectedIndex()).toString();
PreparedStatement pt = supplies.con.prepareStatement("update prods set pquant = #0 + 21" + "'variableForPquant'" "where pname = #1" + "'variableForPname'");
pt.setInt(1, Integer.parseInt(empsalary.getText()));
pt.setString(2, temp);
supplies.pst.executeQuery(temp);
You need to use quotes when you are using variables in your SQL query. I believe it is either
"'variableName'" or '"variableName'"
I found my error i declare a variable pt and i execute the query from the statement that is empty pst i change to pt.executeUpdate(); and its ok now

Mysql script update with if statement and case

Why Can't I use or why does it encounters an error. I'm trying to use a script as such as the one below. I'm a newbie when it comes to mysql and I tried creating scripts like this but I get the errr like the below
#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 '1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.statu' at line 4
Here's my script
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount =
CASE WHEN dogdetails.dogcount IS NOT 1 THEN dogs.dogcount = dogcount - dogdetails.total, dogdetails.status = 'Checked'
WHEN dogdetails.dogcount = 1 THEN dogs.pto = pto - dogdetails.total, dogdetails.status = 'Checked'
WHERE dogdetails.id=4
Is there somethng I'm missing or overlooked?
remember that you have to use the keyword 'end' after your case statements and to always return a value. The IS NOT keyword is also incorrect when comparing numbers. Here's a version of your query that should work:
UPDATE dogs
LEFT JOIN dogdetails ON dogs.username = dogdetails.username
SET dogs.dogcount = CASE WHEN dogdetails.dogcount != 1 THEN dogs.dogcount = dogcount-dogdetails.total else dogs.dogcount end,
dogs.pto= case WHEN dogdetails.dogcount = 1 THEN pto - dogdetails.total else pto end,
dogdetails.status = 'Checked'
WHERE dogdetails.id=4