ERROR 1064 (42000) - MySQL error in INSERT ... SELECT query - mysql

As part of a MySQL trigger I'm writing, I've got an INSERT ... SELECT query that is returning :
ERROR 1064 (42000) at line 7: 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 'pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled) S' at line 33
INSERT INTO product_pricing pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled)
SELECT cc, `pp1`.`pricing_id`, `pp1`.`pid`, `pp1`.`title`, `pp1`.`startdate`, `pp1`.`enddate`, `pp1`.`priority`, `pp1`.`enabled`
FROM product_pricing pp1
WHERE pp1.pp_id = NEW.pp_id
ON DUPLICATE KEY UPDATE pp2.pp_id=(SELECT newppid := pp2.pp_id);
I'm not sure if it's the cc part? That's a declared variable in the trigger but it should work given that you should be able to do a SELECT 'hello', t.col1 FROM table t
Any suggestions as to what the error is greatly received.

The INSERT syntax doesn't allow for aliases.
INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

Remove that pp2 from the INSERT query

Related

Python mysql Connector Error when tying to write to database only when not In Table

I am trying to write some data to a database but only when its not already in the table. Im using the Variables data , data1 ,timestamp, and name.My Python MySql Connector looks like this
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s)WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'name')""", (timestamp ,data ,data1 ,name))
I get the Following Error
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s)WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'name')""", (timestamp ,data ,data1 ,name))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor.py", line 559, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection.py", line 494, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection.py", line 396, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'n' at line 1
I dont get whats wrong with the Syntax
I also dont want to update an Existing entry just to skip them
SAMPLE DATA:
timestamp=2019-11-20 00:00:00
name =testpc
data= 18.14
data1= 58.53
I am Splitting strings like this
2019-11-18 00:00:00 testpc data 11.58 data1 29.20
MySQL Insert Syntax doesn't support where clause in it. May be you can try
INSERT ON DUPLICATE KEY UPDATE.
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE Date = 'timestamp' """, (timestamp ,data ,data1 ,name))
https://www.mysqltutorial.org/mysql-insert-or-update-on-duplicate-key-update/

SQL nested queries and subqueries

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#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 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

syntax error in ON UPDATE DUPLICATE KEY?

I was trying to use the ON UPDATE DUPLICATE KEY clause for the first time, following this link
SQL - IF EXISTS UPDATE ELSE INSERT INTO
and I'm getting an error in my sql syntax:
SQLSTATE[42000]: Syntax error or access violation: 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 ''AJAY KUMAR')'
at line 2 The SQL being executed was: INSERT INTO fee_acc_balance
(guardian_name, account_no, paid_amount, due, days_overdue,
total_fees, updated_on) VALUES ('AJAY KUMAR', '10', 0, 12550, 0,
12550, '2017-02-10 21:28:05') ON DUPLICATE KEY UPDATE guardian_name =
VALUES ('AJAY KUMAR') Error Info: Array ( [0] => 42000 [1] => 1064 [2]
=> You have an error..
The unique key in my case is account_no, and this is my sql :
INSERT INTO fee_acc_balance (guardian_name, account_no, paid_amount, due, days_overdue, total_fees, updated_on)
VALUES ('$father_name', '$account->account_no', $payments, $sum, 0, $sum,'$now')
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Where does the error lie?
You cannot specify an absolute value in ON DUPLICATE KEY UPDATE:
ON DUPLICATE KEY UPDATE guardian_name = VALUES ('$father_name')
Try with
ON DUPLICATE KEY UPDATE guardian_name = VALUES(guardian_name)
Notice that the right part of the assignment is the new field coming in from VALUES, and the left side is the extant record; "UPDATE a = VALUES(a)" means "put the a from VALUES into the record", not "leave everything as it is".
Also, you may want to write variables in curly brackets:
...VALUES ('{$father_name}', '{$account->account_no}', {$payments}, {$sum}, 0, {$sum}, '{$now}')
or even better use PREPAREd statement with PDO:
$stmt->prepare("INSERT... VALUES(?, ?, ?, ?, 0, ?, ?)");
$stmt->execute([
$father_name,
$account->account_no,
$payments,
$sum,
$sum,
$now
]);
and, better still, bound parameters.
Otherwise, strange things might happen if the guardian name is Ajay Al'Kumar (note the quote mark) or a string value is passed instead of an integer one.

Can't create a MySQL View

I run this SQL code to create a view but this doesn't work :
Create VIEW as
select
c.ID_CAPTEUR as ID_CAPTEUR,
d.ID_CAPTURE as ID_CAPTURE,
d.VALEUR_CAPTURE as valeur,
d.DATE_CAPTURE,
t.INTITULE_TYPE_CAPTURE as TYPE_CAPTURE,
z.INTITULE_ZONE as zone,
r.INTITULE_REGION as region
from CAPTEUR c, CAPTURE d, TYPECAPTURE t, ZONE z, REGION r
where c.ID_CAPTEUR=d.ID_CAPTEUR
and d.ID_TYPE_CAPTURE=t.ID_TYPE_CAPTURE
and c.ID_ZONE = z.ID_ZONE
and z.ID_REGION = r.ID_REGION
I get this 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 'as select c.ID_CAPTEUR as ID_CAPTEUR , d.ID_CAPTURE as ID_CAPTURE , d.VALEUR_CAP' at line 1
This is the syntax of the CREATE VIEW statement:
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
As you can see, your query is missing the view_name parameter (parameters between [ and ] are optionnal, which is not the case of view_name).

create mysql views with UNION operator on query

I am trying to create sql view with a UNION operator on it. I tried executing the sql first before embedding it to the view and the result is success. But when I try it to embed on creating a view it returned this error
"Error Code : 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 'UNION
SELECT p.product_media_id AS media_id, p.product_title AS title, p.product' at line 8
"
I also assure that all columns have the same data type.
SQL view:
CREATE
/*[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]*/
VIEW `mydbname`.`s_views`
AS
(SELECT c.media_id AS media_id,
c.title AS title,
c.title_slug AS slug,
c.content_one AS description,
c.type AS cat
FROM content_content c
WHERE c.type = 'news'
OR c.type='travel_genius'
AND c.media_id IS NOT NULL
AND c.status = 1
UNION
SELECT p.product_media_id AS media_id,
p.product_title AS title,
p.product_title_slug AS slug,
p.product_description AS description,
'product' AS cat
FROM p_views p
WHERE p.product_media_id IS NOT NULL);
It is a bug in MySQL. It is the parenthesis that is triggering it:-
http://bugs.mysql.com/bug.php?id=21614