如何在MySql中基于其他表更新字段值?

斯纳格斯

我有report_business_likes与相关字段

id, facebook_id, created_at,  some_info
--  -----------  -----------  ----------
1    123456789   '2013-12-23'   blabla

我还有其他表,其名称businesses如下结构:

id, fb_id,    data
--  -----     ----
33  123456789  xxx

我想from table替换report_business_likes字段facebook_ididbusinesses

就我而言,结果应为:

id, facebook_id, created_at,  some_info
--  -----------  -----------  ----------
1    33          '2013-12-23'   blabla

如您所见,我被替换12345678933

我该如何实现?

我试过了:

UPDATE `report_business_likes` SET facebook_id = BZ.id from 
  (select id from `businesses` where fb_id = 123456789 ) as BZ, 
   where  facebook_id = 123456789 AND date(created_at) = '2013-12-23';

但是得到语法错误:

[SQL] /*SELECT * FROM `report_business_likes` where facebook_id = 123456789 AND date(created_at) = '2013-12-23';*/

UPDATE `report_business_likes` SET facebook_id = BZ from 
(select id from `businesses` where fb_id = 123456789) as BZ, 
where  facebook_id = 123456789AND date(created_at) = '2013-12-23';
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from 
(select id from `businesses` where fb_id = 123456789) as BZ,' at line 3

请帮忙,

马里
UPDATE report_business_likes 
    SET facebook_id =   (select id 
                        from businesses  
                        where facebook_id = 123456789 )
 WHERE facebook_id = 123456789 AND date(created_at) = '2013-12-23'

或者

UPDATE RBL
SET RBL.facebook_id = B.id
FROM report_business_likes RBL INNER JOIN businesses B 
ON RBL.facebook_id = B.facebook_id

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

发现重复项时,如何在其他表中插入忽略的MySQL行并更新字段?

如何使用具有多个连接的其他表中的值更新字段?

如何使用触发器和其他表中的数据更新字段?

PostgreSQL 在同表中的其他字段更新时触发更新字段

如何在mongodb中全局更新字段值

如何在Go模板中更新字段值

如何在Ecto迁移中动态更新字段值?

仅在其他表中找到特定值时才更新字段

如何基于其他3个表更新MySQL表

OrientDB:如何使用其他顶点的数据更新字段

如何在SQL查询中比较日期和日期数组并在MySQL中更新字段值?

如何使用 MySQL 中其他两个表的值总和更新表

使用 mySQL 中的其他表更新值

如何基于不同模型中其他字段的值设置Django模型字段的值

每次提取文档时,将基于其他字段的新字段添加到文档中

如何在基于一个列的其他表中查找具有相同值的表名

从mysql中另一个表更新表值,其中更新字段是外键

如何在MySQL中根据其他列自动更新表的列?

如果数据与其他匹配,如何在表中更新数据,然后插入mysql

如何在mongodb查询中查找和更新字段的最小值?

MySQL查询只有在其他字段存在时才更新字段?

如何在Cassandra表中添加默认值“ whatever”的新字段?

我如何在mysql表中做一个值echo作为其他东西?

模型-基于其他字段中的值的字段

如何基于其他字段值使值空

用mySQL中的其他表选择结果更新表中的字段

根据正在更改的其他字段更新字段

如何在MongoDB中普遍更新字段?

如何在Yii中禁用更新字段