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());
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 using node to connect to mysql and I need to run an insert and then immediately run a select last_insert_id().
insert into data_temp values (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');
SELECT LAST_INSERT_ID();
In mysql workbench this query works but gives me an error saying:
SELECT LAST_INSERT_ID() )
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 ')' at line 2 0.047 sec
It actually returns the correct id but because it is throwing an error on the last part node is catching the error.
There is an issue in your insert query you just missed to put the name of your columns
Do like this
insert into data_temp (`col1`, `col2`) values (null, '{"test":
{"id":12,"otherdata":"x","otherdata2":"y"}}'); SELECT LAST_INSERT_ID();
In place of col1and col2 put your columns name then it will work for you.
Where is the error in this query?
INSERT INTO chat (`id`,`user`,`message`,`date`)
VALUES (null,'user','test',CURRENT_TIMESTAMP);
This is the error message I receive:
"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 ''id','user','message','date') VALUES (null,'user','test',CURRENT_TIMESTAMP)' at line 1"
if the id is autoincrement then use this instead
INSERT INTO `chat` (`user`,`message`,`date`)
VALUES ('user','test',NOW());
Its difference between single quote ' and backtick `
I'm trying to track the page views by inserting only unique values every 24h.But when I run this query I get a syntax error.
insert ignore into profilepageviews values( '77.777.777.777' , CURRENT_TIMESTAMP, '5') where hitdate NOT LIKE '%2012-06-26%'
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 'where hitdate NOT LIKE '%2012-06-26%'' at line 1
You can not use WHERE clause with INSERT, You might want UPDATE
I am trying to insert data from a csv file into mysql using BigDump.
It stops on line 2, with the error:
"Query: INSERT INTO location VALUES
(1,"O1","","","",0.0000,0.0000,, );
MySQL: 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 ' )' at line 1"
If I run the statement from withing phpmyadmin, it says:
"#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 ' )' at line 1"
What can I do to make the data get into the database?
Thank you.
The commas with no values looks sketchy to me.
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,, );
should probably be
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,NULL,
NULL);
Does your insert statement contain values for every column in the table? If not, you have to name the columns.
For example,
insert into location (col1, col2, col3) values (1, 2, 3);
If you show us the structure of the LOCATION table you can get better answers.