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
Related
I want to update the columns and select the values using single query in MySQL
For example
update table set address='bangalore',updated_count =updated_count+1 where id=1
select * from table where id=1
This is not possible. As #D-Shih mentioned, you cannot do both update and select in the same query. The SQL update convention doesn't allow for return data and the select statement can't write to a table.
Each has its own purpose and can't be written in one statement. They must be executed separately.
i'm using MySQL and i want to check if a record exists and if it exists delete this record.
i try this but it 's not working for me:
SELECT 'Barcelone' AS City, EXISTS(SELECT 1 FROM mytable WHERE City = 'Barcelone') AS 'exists';
THEN
DELETE FROM mytable
WHERE City = 'Barcelone';
Thank you for your help.
The if statement is only allowed in stored procedures, stored functions, and triggers (in MySQL).
If I understand what you want, just do:
DELETE FROM mytable
WHERE City = 'Barcelone';
There is no reason to check for the existence beforehand. Just delete the row. If none exist, no problem. No errors.
I would recommend an index on mytable(city) for performance reasons. If you want to check if the row exists first, that is fine, but it is unnecessary for the delete.
If you mean MySQL is returning an error message (if that's what you mean by "not working for me"), then that's exactly the behavior we would expect.
That SQL syntax is not valid for MySQL.
If you want to delete rows from a table, issue a DELETE statement, e.g.
DELETE FROM mytable WHERE City = 'Barcelone'
If you want to know how many rows were deleted (if the statement doesn't throw an error), immediately follow the DELETE statement (in the same session) with a query:
SELECT ROW_COUNT()
Or the appropriate function in whatever client library you are using.
If the ROW_COUNT() function returns 0, then there were no rows deleted.
There's really no point (in terms of MySQL) in issuing a SELECT to find out if there are rows to be deleted; the DELETE statement itself will figure it out.
If for some reason your use case requires you to check whether there are rows be be deleted, then just run a separate SELECT:
SELECT COUNT(1) FROM mytable WHERE City = 'Barcelone'
I have created a routine which inserts a record in one table, and after that it searches for that id (with a select statement) and updates another table's field with that id.Is this possible? It's one routine so my question is if the statements are executed in a sequential order?
Thanks in advance
You do not need to search for the id. You can use LAST_INSERT_ID() to get the id of the last inserted row.
INSERT INTO tablename
(<columns>)
VALUES
(<columnvalues>);
SELECT LAST_INSERT_ID() INTO somevariable;
Then you can write your UPDATE statement.
UPDATE sometable
SET sometable.col = somevariable
WHERE sometable.something = #something
Statements in a routine are executed in the order they are written.
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")
I'm trying to insert records on multiple mysql tables with similar entities(a normalized table)
I tried to do this but I get an error. I've also seen joins but it seems to work only when retrieving data.
insert into t1(pnum, hospnum) values('117', '656')
insert into t2(TELNUM, HOSPNUM) values('9981235', '676')
If you are executing these statements in a batch, you may need a semicolon to separate/terminate them:
insert into t1(pnum, hospnum) values(117, 656);
insert into t2(TELNUM, HOSPNUM) values(9981235, 676);
I suspect your fields are numbers not strings, try this:
insert into t1(pnum, hospnum) values(117, 656)
insert into t2(TELNUM, HOSPNUM) values(9981235, 676)
No need to use quotes for numbers otherwise you will get an error.
MySQL does not support a statement that inserts into two different tables.
The only option is to use a trigger on t1 that inserts to t2, but of course you don't have access to the telnum value in a trigger; you only have the columns of t1. So this won't work in your situation.
You must execute the two inserts as separate SQL statements.