Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql不同的表、复制、匹配字段,但列行是随机的_Mysql - Fatal编程技术网

Mysql不同的表、复制、匹配字段,但列行是随机的

Mysql不同的表、复制、匹配字段,但列行是随机的,mysql,Mysql,我有两个不同的表table_usernames和jos_users,我需要匹配两个表列中的字段username和users,然后如果列行匹配,我需要table usernames registed date列复制到jos users registing column行字段。到目前为止,我发现的最接近的东西是 update table1 t1 join table2 t2 on t2.field = t1.field set t1.field1 = t2.matchingfield w

我有两个不同的表table_usernames和jos_users,我需要匹配两个表列中的字段username和users,然后如果列行匹配,我需要table usernames registed date列复制到jos users registing column行字段。到目前为止,我发现的最接近的东西是

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

但在我现在的情况下,这看起来不起作用,只是想在我销毁数据库并发现备份已损坏之前进行验证。。。提前感谢您

根据您的要求,您有两个表table_用户名和jos_用户

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
现在,首先我必须找到这两个表的映射,您已经从table_usernames定义了username,从jos_users定义了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
现在,您需要将table_用户名的注册日期设置为jos_用户的注册字段

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

这将从table_usernames表更新jos_users表的registration coulmn。

根据您的描述,该语句将是

UPDATE table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users
SET t2.registration = t1.registered_date;
内部联接已经用作匹配用户名的筛选器。有关联接的直观说明,请访问此

如果您想先检查,如果您的语句是正确的,您可以将其转换为先选择,以便查看哪些记录将被更新

SELECT * FROM 
table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users;
或者,如果您使用的存储引擎支持所有表的事务,如InnoDB,则可以执行以下操作:

START TRANSACTION;
UPDATE table_usernames t1
INNER JOIN jos_users t2 ON t1.username = t2.users
SET t2.registration = t1.registered_date;
然后你可以选择任何。。。在同一个环节!如果一切顺利。当您这样做时,数据将被写入

COMMIT;
如果你不想让它被写出来,你就要写

ROLLBACK;
检查您的表是否使用InnoDB作为存储引擎

SHOW CREATE TABLE your_table_name\G
看到最后一行了吗

) ENGINE = InnoDB ...

如果您不使用任何东西来替换实际的列和表名,那么验证它肯定会有所帮助,您不这样认为吗?