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
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 am very new to Sql, so bear with me. I have run a query the following query:
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
however, by accident I run it twice. I would like to remove the duplicate. How can that be done?
I tried
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
but get an error:
SQL ERROR [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
If your table name include special char of reserved word then you should enclose the table name with backticks:
`table`
But looking to your question, the table name seems:
`adhoc_dt.`table`
In this case, the correct syntax for delete is:
DELETE FROM adhoc_dt.`table`
WHERE id = 53098974 AND name = 'JOHN'
but in this way you delete all the rows with:
id = 53098974 AND name = 'JOHN'
You have to follow steps
truncate table
apply unique constraint on ID
run insert script
I wrote this query but I get the following error:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42000]: Syntax error or access violation: 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 'BEGIN INSERT
INTO forum_topics_track (userid, topic_id, `c' at line 3
I guess it is self explanatory but my goal is to check if the record exists and if it doesn't, to insert it.
IF NOT EXISTS
(SELECT * FROM `forum_topics_track` WHERE `userid` = '{$userid}' AND `topic_id` = '{$topic_id}')
BEGIN
INSERT INTO `forum_topics_track` (`userid`, `topic_id`, `category_id`)
VALUES ('{$topic_id}', '{$category_id}', '{$userid}')
END;
A faster alternative would be to have a UNIQUE INDEX on userid and topic_id.
CREATE UNIQUE INDEX forum_topics_track_ndx ON forum_topics_track(userid, topic_id);
Then you could do
INSERT IGNORE INTO `forum_topics_track` (`userid`, `topic_id`, `category_id`)
VALUES ('{$topic_id}', '{$category_id}', '{$userid}');
which would always succeed (possibly doing nothing if the data already is there).
Or you could look into the ON DUPLICATE KEY UPDATE trick.
This is the wrong way to implement the logic. If you want each user and topic to appear in forum_topics_track one time, then have the database enforce the constraint. This is easy with a unique index or constraint:
create unique index unq_forum_topics_track_user_topic on forum_topics_track(user_id, topic_id);
Then, you can do an insert and ignore or handle the error:
INSERT INTO `forum_topics_track` (`userid`, `topic_id`, `category_id`)
VALUES ('{$topic_id}', '{$category_id}', '{$userid}')
ON DUPLICATE KEY UPDATE userid = VALUES(userid);
No IF is needed in the logic. In fact, using IF just invites problems due to race conditions and doesn't really guarantee anything in the database.
IF NOT EXISTS is a clause that is available to use in MS-SQL but not in MySQL. Please try following query.
INSERT INTO `forum_topics_track` (`userid`, `topic_id`, `category_id`)
SELECT '{$topic_id}', '{$category_id}', '{$userid}'
WHERE NOT EXISTS(
SELECT * FROM `forum_topics_track` WHERE `userid` = '{$userid}' AND `topic_id` = '{$topic_id}'
)
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'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.