How to update multiple tables by single mysql query? - mysql

I have two tables tb1 & tb2
I have to update a common column of both tables, i.e user_level
I have a common criteria for both tables like username.
So I want to update like this:
UPDATE tb1, tb2 SET user_level=1 WHERE username="Mr.X"
But somehow it is not working.
What would be the correct mysql query for this?

Try this:
UPDATE table_1 tb1,
table_2 tb2,
table_3 tb3
SET tb1.field2 = "some value",
tb2.field3 = "some other value",
tb3.field4 = "some another value"
WHERE tb1.field1 = tb2.field1
AND tb1.field1 = tb3.field1
AND tb1.field1 = "value"
I tested the code on MSAccess and SQL SERVER 2008

Your problem is solved,just follow this what I have done-
create table tb1(user_level int);
create table tb2(user_level int,username varchar(20));
insert into tb1 values(2);
insert into tb2 values(2,'Mr.X');
I have this two tables like this where user_level is common,now according to you I tried to update the user_level column in both table using one query on a common criteria for both table i.e. username.
I tried to update the value of user_level column from 2 to 3 in both tables where the username is 'Mr.X' using a single query,so I tried the following query and it perfectly worked..
update tb1 inner join tb2
on tb1.user_level=tb2.user_level
set tb1.user_level=3,
tb2.user_level=3
where tb2.username='Mr.X' ;

Try this:
update db1 inner join db2 on db1.username = db2.username
set db1.user_level = 1,
db2.user_level = 1
where db1.username = 'a';
See it here on fiddle: http://sqlfiddle.com/#!2/ba34ac/1

The correct query is that you have to specify the full table and row/column you are trying to update in the two tables and indeed database if you are updating across databases too.
typical example:
UPDATE tb1, tb2 SET tb1.username = '$username', tb2.location ='New York'WHERE tb1.userid = '$id' AND tb2.logid = '$logid'

We can update it without join like this
UPDATE table1 T1, table2 T2
SET T1.status = 1 ,T2.status = 1
WHERE T1.id = T2.tab1_id and T1.id = 1
We can update it with join like this
UPDATE table1
INNER join table2 on table1.id=table2.tab1_id
SET table1.status=3,table2.status=3
WHERE table1.id=1

Related

MySQL Conditional Update in same table

I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;

update variable from multiple tables in one row

I have two databases, and I need to UPDATE variable 'birthday' from one row to another checking 'name' (from db1 to db2).
The problem is, that 'name' variable in second db is located in another table (table2).
It is possible to do this without changing DB structure using 'id' variable from db2 to synchronizing?
UPDATE `db1`.`table1`, `db2`.`table2` SET `db2`.`table1`.`birthday` = `db1`.`table1`.`birthday` WHERE `db2`.`table1`.`name` ...
Scheme:
db1-> table1 -> name,birthday
db2-> table1 -> name,id
table2 -> birthday,id
UPDATE `db.1`.table1 AS t11
JOIN `db.2`.table1 AS t21 ON t11.name = t21.name
JOIN `db.2`.table2 AS t22 ON t21.id = t22.id
SET t11.birthday = t22.birthday

Do I need to use an insert or inner join?

I am working with MySQL version 5. I have two tables structured as shown below.
Table1
From To Time TravelTime
Table2
From To Time CongestionTime
I would like to achieve the following output.
If From, To and Time are equal in both the tables, then assign TravelTime = CongestionTime
Table2 contains only a subset of the From | To | Time combinations available in Table1.
From is mysql reserved word. If you don't want to escape it, change column names "From" to "TimeFrom".
UPDATE table1,table2
SET table1.TravelTime=table2.CongestionTime
WHERE table1.From = table2.From
AND table1.To = table2.To
Update Table1 Set Table1.TravelTime = Table2.CongestionTime
FROM Table2
WHERE Table1.From = Table2.From
AND Table1.To = Table2.To
AND Table1.Time = Table2.Time
update table1
set traveltime = congestiontime
from table1
inner join table2 on table1.from = table2.from
and table1.to = table2.to
and table1.time = table2.time

How to update certain column in a table based on duplication of other columns?

UPDATE tb1 SET percentage = 80
WHERE EXISTS (SELECT ip FROM tb1 WHERE tb1.ip IN (SELECT ip FROM tb2))
The above Mysql query is written to update percentage based on duplication of ip columns into tb1 and tb2. But it doesn't work. Mysql says that I cant make tb1 as target! How to resolve this issue?
Join of both should work.
Try below :
UPDATE tb1 join tb2 on tb1.ip=tb2.ip SET tb1.percentage = 80

Issue with the TOP 1 query

Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1