I need to write an SQL query for MySQL so that a row is updated if it exists, but inserted if it does not.
i.e.
If row exists...
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
If it does not exist...
INSERT INTO Table1 VALUES (...)
Can this be done in one query?
i believe you need to reverse your logic in order for it to work:
insert into a table - if it exists (same key) then update it.
this can be achieved by the ON DUPLICATE statement like so:
INSERT INTO Table1 VALUES(...)
ON DUPLICATE KEY UPDATE column=column+1
check the manual here
Use the INSERT... ON DUPLICATE KEY UPDATE syntax.
See the manual
(For searching purposes, btw, this is usually referred to as an "upsert")
Related
I am a complete newbie to MySql so please be gentle.
Is there an equivalent of the RETURNING clause in Oracle or the Inserted'/'Deleted tables in SQL Server in MySQL? What I want to do is the following:
Delete a set of rows from table A
Insert the deleted set of rows into table B.
Please help!
Thanks
Unfortunately, you can't do both insertion and deletion in one query, but you can do it all in one transaction if you are using a transactional store engine (like InnoDB). Moreover, RETURNING is supported by Oracle and PostgreSQL but not by MySQL and therefore you need to write separate delete and insert statements.
Using a transaction however, will guarantee that only the successfully copied data will be deleted from tableA. Consider the following:
begin transaction;
insert into tableB select * from tableA where 'your_condition_here';
delete from tableA where 'your_condition_here';
commit;
Why not insert the rows to be deleted from table A in table B and then delete the rows from table A? you can achieve that like this:
insert into tableB select * from tableA where condition;
and then
delete from tableA where condition.
Hello stackoverflow's friends i need your help with this sql clausule this is the error into mysql:
_mysql_exceptions.ProgrammingError: (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 'WHERE email='Tysaic0344#gmail.com'' at line 1")
and this is my code:
INSERT INTO user (token) VALUES (1) WHERE email='example#email.com'
You cannot insert values into an existing row. You can either update or delete the existing records. In your case, I think you want to update the existing row. You can use UPDATE.
UPDATE user SET token = 1 WHERE email = 'example#email.com';
If you want to add records to the table use INSERT
INSERT INTO user VALUES (1, 'example#email.com');
Here is the link for your reference
https://msdn.microsoft.com/en-us/library/bb243852(v=office.12).aspx
You can't INSERT with a WHERE clause.
If you need to UPDATE the record where you have the email from:
UPDATE user
Set token = 1
WHERE email='example#email.com'
Or INSERT with email
INSERT INTO user (token, email)
VALUES (1, 'example#email.com')
(or without)
INSERT INTO user (token)
VALUES (1)
These kind of errors you MUST be able to fix by yourself, the error even tells you where it went wrong (at the end it says "near 'WHERE...").
Check the docs that dns_nx included (especially https://dev.mysql.com/doc/refman/5.7/en/update.html ) for the correct syntax to do an update.
You cannot INSERT a value into an existing row. The WHERE clause is invalid with INSERT. If you want to update an existing row, then you have to UPDATE the field like this:
UPDATE
user
SET
token = 1
WHERE
email='example#email.com'
Please review the docs about INSERT and UPDATE
https://dev.mysql.com/doc/refman/5.7/en/update.html
https://dev.mysql.com/doc/refman/5.7/en/insert.html
INSERT inserts new rows into a table. The WHERE clause is used to filter existing rows from a table. It doesn't make sense in a INSERT query; that's why the INSERT statement does not contain a WHERE clause.
The WHERE clause is used to filter the rows to fetch from the table (the SELECT statement), the rows to modify (the UPDATE statement) or to remove from the table (the DELETE statement).
Your query looks like you want to modify the data already existing in the table. The UPDATE statement you need looks like this:
UPDATE user SET token = 1 WHERE email = 'example#email.com'
I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
Is there a way to combine an insert, update, and select query into one?
Kind of like in a condition,
if select query, when the condition is true it will go to update and if not it will go to insert.
How can i do this?
No, it's not possible, in the context of a single SQL statement.
It is possible to combine and INSERT and UPDATE action into a single statement. The INSERT ... ON DUPLICATE KEY UPDATE statement will basically attempt to INSERT a row, and if that insert results in a "duplicate key" exception, the UPDATE action will be performed instead.
But in terms of a testing some general condition with a SELECT statement, and then running an INSERT or UPDATE depending on the result of the query, the answer is no, MySQL does not have any single statement syntax to support that.
STORED PROCEDURE is the ANSWER and IF/ELSE
I want to see if can I use mysql to manage a little stock.
table A contain all the movements:
art_code, qty_load, qty_unload, date
table B with the existence:
art_code, total_load, total_unload, available, date
I've created a trigger: (after update on)
INSERT INTO STOCK VALUES(NEW.ART_CODE, TOTAL_LOAD, TOTAL_UNLOAD, TOTAL_LOAD-TOTAL_UNLOAD, NOW());
but after the first correct run it says a row already exists, how could I replace the old row with the new row?
You can use INSERT ... ON DUPLICATE KEY UPDATE command. It will help you to insert and to update existed records using one statement.
INSERT ... ON DUPLICATE KEY UPDATE Syntax