Background
Now I have such a need:
The user table for database a needs to be imported into the account table of database B
User table field: Uid,username,email,password,regdate,salt
Account table field: Id,name,email,password,type,salt
The imported field has only Username,email,password,salt and regdate needs to meet a certain condition
There are several scenarios for writing SQL, mainly distinguishing between insert into and replace into
Situation
The imported data does not exist at all in the table in library B
Direct insert into is good, use the replace into effect
INSERT into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600
Part of the imported data exists
Data part exists in order to distinguish the need to add a unique index to two tables, this unique index (unique) must be the value in the imported field
I added a unique index to the username and name fields in two tables, respectively
Direct replacement
Replace into when the two-table data repeats (must be defined unique) at the time of the operation, the row is deleted (Note that it is an entire row) before the INSERT into
The result is that the Type field in the Account table, if it has a value, is deleted and reinserted, which becomes the default value.
REPLACE into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600
Ignore duplicates
With the Ignore keyword, if you encounter duplicates, do not operate when the forward
INSERT IGNORE into ' B '-' account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Users '
WHERE regdate>1418313600
Partially Updated
than if you only want to update the password and salt fields, you can use on DUPLICATE KEY update
INSERT into ' B '-' u_account ' (name,password,email,salt)
SELECT username,password,email,salt from
' A '. ' Bbs_ Ucenter_members '
WHERE regdate>1418313600 on DUPLICATE KEY UPDATE password=values (PASSWORD), salt=values (salt )
Summarize
Replace into more violent, online also have said prudent use, preferably with INSERT into instead
Questions
The above operation is two databases on the same server, directly a SQL can be done
What would you do if you were to guide two libraries on a different two server?
The current temporary approach:
Import the user table from a library to another server on the B library, and write a SQL fix
or query out the specified data, export SQL, and replace insert into insert ignore in SQL (with limitations)
The above mentioned is the entire content of this article, I hope you can enjoy.