PhpMyAdmin-在两个表之间共享数据

时间:2016-04-13 15:44:07

标签: mysql

我正在尝试使用table2(所有者)中的id_owner填充table1(video_games)的字段id。 table1的字段possessor包含与table2的字段Name相同的数据。 我正在和PhpMyAdmin一起工作。

我在StackOverflow下找到的尝试:

UPDATE  t 
   SET t.`id_owner` = o.`id`
  FROM `video_games` AS t
         INNER JOIN 
       `owner` AS o 
         ON t.possessor = o.Name

添加了引号:

UPDATE  t 
   SET t.`id_owner` = o.`id`
  FROM `video_games` AS t
         INNER JOIN 
       `owner` AS o 
         ON t.`possessor` = o.`Name`


UPDATE `video_games` 
SET `id_owner` = `i`.`id`, 
FROM (
    SELECT `id` 
    FROM `owner`) `i`
WHERE 
    `i`.`Name` = `video_games`.`possessor`

没有别名

UPDATE
    `video_games`,
    `owner`
SET
    `video_games`.`id_owner` = `owner`.`id`,

WHERE
    `video_games`.`possessor` = `owner`.`Name`;

仍然遇到相同的MySql错误:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'WHERE附近使用正确的语法       第7行的`video_games` .possessor` =`owner` .Name``'

2 个答案:

答案 0 :(得分:0)

您没有在内部SELECT中选择名称。它应该是这样的:

UPDATE video_games 
SET id_owner = i.id
FROM (
  SELECT id, Name
  FROM owner) i
WHERE i.Name = video_games.possessor

答案 1 :(得分:0)

我尝试了你的想法,但结果却出现了同样的错误信息。

进一步的研究引导我https://dev.mysql.com/doc/refman/5.5/en/update.html哪个命题运作良好

UPDATE video_games,owner 
SET video_game.id_owner=owner.id
WHERE video_games.id_possessor=owner.id;