Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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将数据从一个表移动到另一个表,匹配ID';s_Mysql_Sql - Fatal编程技术网

MySQL将数据从一个表移动到另一个表,匹配ID';s

MySQL将数据从一个表移动到另一个表,匹配ID';s,mysql,sql,Mysql,Sql,我有(a.o.)两个MySQL表,其中(a.o.)有以下列: tweets: ------------------------------------- id text from_user_id from_user ------------------------------------- 1 Cool tweet! 13295354 tradeny 2 Tweeeeeeeet 43232544 bolleke 3 Yet another 13295354

我有(a.o.)两个MySQL表,其中(a.o.)有以下列:

tweets:
-------------------------------------
id text        from_user_id from_user
-------------------------------------
1  Cool tweet! 13295354     tradeny
2  Tweeeeeeeet 43232544     bolleke
3  Yet another 13295354     tradeny
4  Something.. 53546443     janusz4

users:
-------------------------------------
id from_user num_tweets from_user_id
-------------------------------------
1  tradeny   2235
2  bolleke   432
3  janusz4   5354
现在我想规范化tweets表,将tweets.from_user替换为与users.id匹配的整数。其次,我想填写相应的users.from_user_id,最后,我想删除tweets.from_user_id,以便最终结果如下所示:

tweets:
------------------------
id text        from_user
------------------------
1  Cool tweet!     1
2  Tweeeeeeeet     2
3  Yet another     1
4  Something..     3

users:
-------------------------------------
id from_user num_tweets from_user_id
-------------------------------------
1  tradeny   2235       13295354
2  bolleke   432        43232544
3  janusz4   5354       53546443
我的问题是,是否有人能帮助我对此形成适当的查询。我只走了这么远:

UPDATE tweets SET from_user =
  (SELECT id FROM users WHERE from_user = tweets.from_user)
WHERE...

UPDATE users SET from_user_id =
  (SELECT from_user_id FROM tweets WHERE from_user = tweets.from_user)
WHERE...

ALTER TABLE tweets DROP from_user_id

任何帮助都将不胜感激:-)

我认为这样的方法应该有效:

UPDATE FROM tweets t 
LEFT JOIN users u ON u.from_user = t.from_user
SET t.from_user = u.id

UPDATE FROM users u
LEFT JOIN tweets t ON t.from_user = u.from_user
SET u.from_user_id = t.from_user_id

ALTER TABLE tweets DROP COLUMN from_user_id
示例工作:

推特表

用户表

##可能的MySQL查询## 未测试

UPDATE users, tweets
SET users.from_user_id = tweets.from_user_id
WHERE users.from_user = tweets.from_user;

UPDATE tweets, users
SET tweets.from_user = users.id
WHERE tweets.from_user = users.from_user;

ALTER TABLE tweets DROP COLUMN from_user_id;
ALTER TABLE tweets CHANGE COLUMN from_user from_user int;

“更新tweets SET tweets.from_user=users.uid from tweets LEFT JOIN users ON tweets.from_user=users.screen_name”作为“更新from tweets LEFT JOIN users ON tweets.from_user=users.uid from_name SET tweets.from_user=users.uid”返回错误:“#1064-您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解可在“FROM”[…]附近使用的正确语法。我已经检查过了,但我相信这两个查询的语法都正确,“tweets”不是保留词……有什么想法吗?另外,感谢您的示例工作;还没有看到此堆栈溢出功能:-)您总是像这样命名表名吗#名称“”(使用散列)我目前没有mysql访问权限,当我有权限时,我将检查这些mysql查询,#name是temporary table@data.stackexchange您需要创建临时表来尝试查询从mysql引用添加可能的mysql查询需要测试
UPDATE users, tweets
SET users.from_user_id = tweets.from_user_id
WHERE users.from_user = tweets.from_user;

UPDATE tweets, users
SET tweets.from_user = users.id
WHERE tweets.from_user = users.from_user;

ALTER TABLE tweets DROP COLUMN from_user_id;
ALTER TABLE tweets CHANGE COLUMN from_user from_user int;