将连接表从一行更新到另一行

时间:2014-05-19 13:38:12

标签: mysql sql join insert

我加入了一张桌子

SELECT t1.akademik, t1.entity_id, t2.akademik AS akademik_doboru, t2.uid_author 
FROM zakwalifikowani1 t1 
JOIN dobor2 t2 
ON t1.entity_id = t2.uid_author

我加入的表格

    akademik |  entity_id | akademik_doboru |   uid_author |
    Feniks   |     129    |                 |     129      |

如何将此联接表中的值 akademik 复制到表dobor2

我试过这样的事情:

$sql=("SELECT t1.akademik, t1.entity_id, t2.akademik AS akademik_doboru, t2.uid_author 

FROM zakwalifikowani1 t1 JOIN dobor2 t2 ON t1.entity_id = t2.uid_author");

    while ($row = mysql_fetch_array($sql)) {
        $a=$row['akademik'];
        $b=$row['entity_id'];

    mysql_query("INSERT INTO dobor2 ('akademik') VALUES ('$a') WHERE uid_author = '$b'  ");
    }

如果我使用此代码,则不会发生任何事情:(

2 个答案:

答案 0 :(得分:0)

您可以在单个查询中执行此操作。试试这个(没试过):

UPDATE dobor2 t2 
    JOIN zakwalifikowani1 t1 ON t1.entity_id = t2.uid_author 
SET t2.akademik = t1.akademik

答案 1 :(得分:0)

我认为您希望update不是insert。您更新单个行中的列。您在表中插入新行。我认为这就是你要找的东西:

update dobor2 d2 join
       zakwalifikowani1 z
       on z.entity_id = d2.uid_author
    set d2.akademik = z.akademik;