简体   繁体   English

MySQL不同的表,复制过来,匹配字段,但列行是随机的

[英]Mysql different tables, copy over, match field, but column rows are random

I have two different tables table_usernames and jos_users, I need to match the fields username and users from both tables columns and then if the column row matches i need table-usernames (registered date) column to copy over to the jos-users (registration) column row field. 我有两个不同的表table_usernames和jos_users,我需要匹配两个表列中的用户名和用户字段,然后如果该列行匹配,我需要将表用户名(注册日期)列复制到jos-users(注册)列行字段。 So far the closest thing I have found is 到目前为止,我发现的最接近的东西是

update
  table1 t1
  join table2 t2 on t2.field = t1.field
set
  t1.field1 = t2.matchingfield
where
  t1.whatever = t2.whatever

but that does not look like it would work in the situation I have, just wanted to verify before i destroy my database and find my backup is corrupt... Thank you in advance 但这似乎无法在我遇到的情况下起作用,只是想在销毁数据库并发现备份损坏之前进行验证...预先谢谢

As per your requirement you are having two tables table_usernames and jos_users. 根据您的要求,您有两个表table_usernames和jos_users。

Now First of all i have to find mapping of this two table that you already define username from table_usernames and users from jos_users. 现在,首先我必须找到这两个表的映射,您已经在table_usernames中定义了用户名,并在jos_users中定义了用户。

Now you want to set the ragistered date of table_usernames to registration field of jos_users. 现在,您想将table_usernames的最糟糕的日期设置为jos_users的注册字段。

UPDATE table_usernames t1,
           jos_users t2
    SET t2.registration = t1.registered_date // assign registered_date value to registration column of jos_users table 
    WHERE (t1.username = t2.users);   // Mapping (Common) Columns of both tables

This will update registration coulmn of jos_users table from table_usernames table. 这将从table_usernames表更新jos_users表的注册行。

From your description the statement would be 根据您的描述,声明将是

UPDATE table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users
SET t2.registration = t1.registered_date;

The inner join works already as a filter for matching user names. inner join已作为匹配用户名的过滤器。 For a visual explanation of joins visit this link . 有关连接的直观说明,请访问此链接

If you want to check first, if your statement is correct, either you transform it to a select first, so you can see, which records will be updated 如果要先检查,如果语句正确,则可以先将其转换为选择语句,这样就可以看到哪些记录将被更新。

SELECT * FROM 
table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users;

or, if you are using a storage engine that supports transactions for all tables, like InnoDB, you can do this: 或者,如果您使用的存储引擎支持所有表的事务,例如InnoDB,则可以执行以下操作:

START TRANSACTION;
UPDATE table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users
SET t2.registration = t1.registered_date;

Then you can check with SELECT whatever... (in the same session!) if everything went fine. 然后,您可以使用SELECT whatever...检查SELECT whatever... (在同一会话中!),如果一切顺利。 The data will be written when you do 数据将在您执行时写入

COMMIT;

if you don't want it to be written, you do 如果您不希望它被编写,您可以

ROLLBACK;

Check if your table are using InnoDB as storage engine with 检查您的表是否使用InnoDB作为存储引擎

SHOW CREATE TABLE your_table_name\G

and see in the last line 并在最后一行看到

) ENGINE = InnoDB ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM