I'm going blind here... can't seem to find the error in this SQL:
INSERT INTO sankt_groups_order (
parent_group_id,
child_group_id,
order
) VALUES (?,?,?)
ON DUPLICATE KEY UPDATE
order = ?
;
I am getting this error:
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 'order ) VALUES ('65',NULL,'3') ON DUPLICATE KEY UPDATE order = '3''
Next will this SQL do what I think? I need it to insert the whole row if missing and update order if it exists... I have an index making parent_group_id and child_group_id unique.
order is a reserved word in mysql, you'll have to escape it:
child_group_id,
`order`
^-- ^--- backticks to escape
) VALUES (?,?,?)
and yes, it should do what you think. If there's a unique/primary key violation, you'll only change the order field.
Related
I'm trying to insert data in a table where I can track the first print date(PrintDate) and the latest print date (RePrint) of the specific persons and save the dates where I first printed and the latest print into my database.
My database looks like this
INSERT INTO PrintTable (PrintDate, RePrint)
VALUES
(
'2019-07-25 10:37:46',
'2019-07-25 10:37:49'
)
ON DUPLICATE KEY
UPDATE
PrintDate = '2017-07-25 10:37:46',
RePrint = '2019-07-25 10:37:49'
WHERE MEMB_N = '000002';
This is the error that I got:
Query: INSERT INTO PrintTable (PrintDate, RePrint) VALUES ( '2017-07-25 10:37:46', '2019-07-25 10:37:49' ) ON DUPLICATE KEY UPDATE Prin...
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 'WHERE MEMB_N = '000002'' at line 11
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 'WHERE MEMB_N = 'The specific user number'' at line 11
The ON DUPLICATE KEY does not allow a where clause and it is not needed because MySQL already know which record has to be updated : it is the one that generated the DUPLICATED KEY
The INSERT part of you query is wrong because it implies that the unique constraint is set on {PrintDate, Reprint}. I guess it is actually on MEMB_N
So your query should be
INSERT INTO PrintTable (MEMB_N, PrintDate, RePrint)
VALUES
(
'000002',
'2019-07-25 10:37:46',
'2019-07-25 10:37:49'
)
ON DUPLICATE KEY
UPDATE
PrintDate = '2017-07-25 10:37:46',
RePrint = '2019-07-25 10:37:49';
It means : try to insert a new record for MEMB_N = '000002'. If this record already exists then update PrintDate and RePrint for this record.
I'm trying to use this query but whatever I do I cannot get it to work. I'm still very new to the on duplicate key update syntax, but I can't find anything wrong with it
INSERT INTO product_leverancier (product_id, leverancier_id, prijs)
SELECT i.product_id, i.leverancier_id, i.prijs FROM import_tbl i
ON DUPLICATE KEY UPDATE product_id=VALUES(product_id),
leverancier_id=VALUES(leverancier_id), prijs=VALUES(prijs)
The error I get is this:
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 'UPDATE product_id=VALUES(product_id), leverancier_id=VALUES(leverancier_id), pr' at line 2
Error code 1064.
And whatever I change it's always the same error and error code.
Any idea what the problem is?
Your syntax is a bit off, and I don't believe that VALUES is used when using a SELECT as the source of the insert. Instead, use that source table for the update values:
INSERT INTO product_leverancier (product_id, leverancier_id, prijs)
SELECT i.product_id, i.leverancier_id, i.prijs
FROM import_tbl i
ON DUPLICATE KEY UPDATE
product_id = i.product_id,
leverancier_id = i.leverancier_id,
prijs = i.prijs
Note that the alias i is required when referring to the columns in the source table.
Here is a good reference question which delves deeper into the syntax of ON DUPLICATE KEY UPDATE when it is used with INSERT INTO ... SELECT:
INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE
Have you tried this?
ON DUPLICATE KEY UPDATE
product_leverancier.product_id = i.product_id,
product_leverancier.leverancier_id = i.leverancier_id,
product_leverancier.prijs = i.prijs
I am trying to use IF EXISTS in MySQL but i keep getting syntax errors and I have researched for correct syntax but everything isnt working...
What i need is:
If query exists then UPDATE else INSERT new...
$queryString = "IF EXISTS (SELECT * FROM $ONCALL_TABLE WHERE uid='$contextUser' AND submitid='$submitid' AND submitstatus=3) THEN UPDATE $ONCALL_TABLE SET uid='$contextUser', start_time='$onStr', end_time='$offStr', amount='$amount' ELSE INSERT INTO $ONCALL_TABLE (uid, start_time, end_time, amount) VALUES ('$contextUser','$onStr', '$offStr', '$amount') END IF";
Error message:
Can't perform query: 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 'IF EXISTS (SELECT * FROM timesheet_oncall WHERE uid='admin' AND submitid='136545' at line 1
REPLACE INTO is what you need. http://dev.mysql.com/doc/refman/5.0/en/replace.html
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
In your case
REPLACE INTO
$ONCALL_TABLE (uid, start_time, end_time, amount)
VALUES ('$contextUser','$onStr', '$offStr', '$amount')
WHERE uid='$contextUser';
Assuming uid is a PRIMARY KEY or UNIQUE KEY
NOTE: Since the code in your question contains SQL injection flaws I would recommend you to read this article. http://php.net/manual/en/security.database.sql-injection.php
I have this SQL query:
insert into messages
(message, hash, date_add)
values
('message', 'hash', NOW())
ON DUPLICATE KEY IGNORE
hash is unique, what is wrong with query? i got the 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 'IGNORE' at line 1
According to MySQL doc the syntax should be :
INSERT IGNORE INTO messages (message, hash, date_add)
VALUES('message', 'hash', NOW());
I'm getting that error running on MySQL 5.5.8
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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?