How to save dates for a specific user - mysql

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.

Related

How to create trigger for updating in mysql

How to Update table1 if the table1 primary key existing in table2. I am using following code but it gives mysql syntax error.
CREATE TRIGGER upd_selectoin
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
IF NEW.customer_sk IN(SELECT quotation_cname FROM quotation) THEN
UPDATE customer s JOIN quotation m
ON m.quotation_cname = s.customer_sk
SET s.grade = 2
WHERE s.customer_sk = NEW.customer_sk;
END IF;
I got the following Error
#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 '' at line 9
I want to update the customer table grade column if the customer_sk existing in quotation table.Please help me

Insert with on duplicate key update gives error 1064

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

mysql if exists giving error wrong syntax?

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

mysql error update or insert failing

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.

Error 1064 in mysql

Why does the following show a mysql 1064 error?
I have a the following table:
daily_note (id, time (timestamp), rate_id, note )
On this table I'm executing the following insert:
INSERT INTO daily_note (note)
VALUES ('this is a note')
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'
Cannot perform insert:
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 rate_id = 37 AND time > '2011-05-22 00:' at line 3'
You can't INSERT using a WHERE statement. If you want to modify existing records based on some condition/criteria, use an UPDATE statement instead of INSERT.
INSERT with WHERE makes no sense.
INSERT creates new rows, whereas WHERE specifies criteria for retrieving/updating existing rows.
Read up about the syntax and meaning of the statement that you're trying to use, before trying to use it.
Code should read:
UPDATE DAILY_NOTE
SET field='this is a not'
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'