3

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?

8
  • 1
    When you say database you really mean database or table? Commented Nov 26, 2013 at 4:15
  • No, I said it mistakenly. Those would be tables. I updated my question Commented Nov 26, 2013 at 4:19
  • It is showing: Column 'user_level' in field list is ambiguous Commented Nov 26, 2013 at 4:27
  • I want to use Like this: UPDATE tb1,tb2 SET tb1.user_level=1, tb2.user_level=1 WHERE username="Mr.X" Is it right query? Commented Nov 26, 2013 at 4:35
  • but username has to come from either tb1 or tb2 and you probably want another condition. Try WHERE tb1.username = "Mr.X" AND tb2.username = "Mr.X" Commented Nov 26, 2013 at 4:37

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Try to format your code better in the future. I took care of it this time. Also try to add an explanation, a code block alone might not help anybody who is looking for an answer to the question in the future.
This works perfectly. I have a tendency to use AS, e.g. table_1 AS tb1, etc. This also works with MySQL.
5

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' ;

Comments

3

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

3 Comments

I don't want to use inner join in this case
update db1,db2 set db1.user_level = 1, db2.user_level = 1 where db1.username = 'a' and db1.username = db2.username;
Tin Tran are you sure your code is ok? I am asking because a little mistake can cause serious security issue of my site while implementing the code. If it is ok and correct I think it is the best and simplified code.
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'

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.