Update and select using single query in mysql - mysql

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.

Related

Select after update in mysql

Is there a way we can get the updated data from table in same query?
Something like following but in a single query call.
update table set columnA='newA' where columnB='oldB';
select * from table where columnB='oldB';
you can use trigger for manage data after event fired but to execute two un related query i think you can't do

mysql insert ... returning [duplicate]

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.

query to Update another table if first update fails in mysql

I have a .sql file with some update queries.
What i am looking for is when an update query fails, i should be able to execute another update query
e.g. Update table1 set col1="zbc" where id=1;
suppose above query fails as "id=1" was not found.
Then i want to execute below query to do update on another table:
Update table2 set col1="zbc" where id=1;
Since i want this to go in .sql file, I am looking for a single query to achieve this using if/case statement or something similar.
What if just run the second query and check if these rows exist in the first table for example:
Update table1 set col1="zbc" where id=1;
Update table2 set col1="zbc" where id=1
and not exists (select id from table1 where id=1);
or use ROW_COUNT() MySql function and IF. It returns the number of rows changed, deleted, or inserted by the last statement.
Can you use a stored procedure and call that procedure from the .sql file contents? The MySQL manual states that if/then clauses are supported only in "stored programs":
http://dev.mysql.com/doc/refman/5.5/en/if.html

MySQL multiple statement execution order

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.

MySQL - Select and Update in a single command

I need to select a transactionID from a MySQL table and immediately increment it.
SELECT transid FROM idtable;
UPDATE idtable SET transid=transid +1;
I would like to combine the queries but cannot get the correct syntax.
Using a WHERE clause in your UPDATE will have the same effect.
UPDATE table
SET column = column + 1
WHERE column = value
You could use a sub-query style approach, but I have to wonder if there's any need for the initial SELECT. (Can't you just use a WHERE clause on the UPDATE, perhaps involving a multiple table join if so required.)
Take a look at the MySQL UPDATE query syntax for more information.