0

I have the following tables:

agent

+-------------------------------------+ | id | name | desc | comment | |-------------------------------------+ | 1 | agent1 | agent 1 | sss | | 2 | agent2 | agent 2 | eee | |-------------------------------------| 

agent_old

+-------------------------------------+ | id | name | desc | comment | |-------------------------------------+ | 1 | agent1 | agent 1 | sss | | 2 | agent3 | agent 3 | eee | |-------------------------------------| 

auth

+-------------------------------+ | id | login | password | |-------------------------------+ | 1 | agent1 | xxxxxxx | | 2 | agent2 | yyyyyy | |-------------------------------| 

auth_old

+-------------------------------+ | id | login | password | |-------------------------------+ | 1 | oldagent1 | wwwwww | | 2 | oldagent2 | qqqqqq | |-------------------------------| 

I need the resultant tables like this:

agent

+-------------------------------------+ | id | name | desc | comment | |-------------------------------------+ | 1 | agent1 | agent 1 | sss | | 2 | agent2 | agent 2 | eee | |-------------------------------------| 

auth

+-------------------------------+ | id | login | password | |-------------------------------+ | 1 |oldagent1 | wwwwww | | 2 | agent2 | yyyyyy | |-------------------------------| 

This is what I have got but does not run:

update auth a set a.login = oa.login, a.password = oa.password from ( select o.login, o.password from auth_old o join agent na join agent_old ago on ago.id = o.id and na.name = ago.name and na.desc = ago.desc ) oa where a.id = na.id 
2
  • Your WHERE clause should reference oa, not na Commented Jan 28, 2019 at 19:28
  • That looks like implicit join notation with it's comma's replaced with "join"; it really is a good practice to get used to modern explicit join notation. Commented Jan 28, 2019 at 19:46

1 Answer 1

1

In MySQL you could use this syntax but you have not an id in your from ( select ... ) oa. I have added the o.id for this:

 update auth a inner join ( select o.login, o.password , na.id from auth_old o join agent na join agent_old ago on ago.id = o.id and na.name = ago.name and na.desc = ago.desc ) oa on a.id = oa.id set a.login = oa.login, a.password = oa.password 

(And as suggested by Bill Karvin you have a wrong table alias na instead of oa).

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

6 Comments

It should be na.id not oa.id. Not sure how to achieve. id matching should only happen between agent and auth or agent_old and auth_old
@cppcoder .. once you have create the inner join ( ) table_alias the reference for the column in the inner select must be based on the table_alias .. so the ON condition can't be na.id but oa.column_name (oa is your table_alias)
I changed o.id to na.id in the select clause and used it to achieve my need
. have update the answer with na.id .. .. seems that you rarerly ..mark the answer as acpted or rate as usefult .. seems not fair this me ..
Not sure why do you think that way. I have accepted answer for all the questions I ask and has got correct answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.