我的表格report_business_likes
包含相关字段:
id, facebook_id, created_at, some_info
-- ----------- ----------- ----------
1 123456789 '2013-12-23' blabla
我还有一个名为businesses
的表,其结构如下:
id, fb_id, data
-- ----- ----
33 123456789 xxx
我想在表report_business_likes
中使用facebook_id
替换id
字段businesses
。
在我的情况下,结果应该是:
id, facebook_id, created_at, some_info
-- ----------- ----------- ----------
1 33 '2013-12-23' blabla
如您所见,我将123456789
替换为33
。
我怎样才能做到这一点?
我试过了:
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
请帮忙,
答案 0 :(得分:4)
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'
OR
UPDATE RBL
SET RBL.facebook_id = B.id
FROM report_business_likes RBL INNER JOIN businesses B
ON RBL.facebook_id = B.facebook_id
答案 1 :(得分:2)
UPDATE report_business_likes r JOIN businesses b ON r.facebook_id=b.fb_id
SET r.facebook_id=b.id
答案 2 :(得分:1)
您正在尝试混合语法。 update
的正确语法是:
update `Your table`
set `Field in yourTable` = `value or expression`
where `Conditions to filter the data in your table`
update
子句的from
语法中没有位置。
当然,如果你想用子查询的结果更新你的字段,你可以(参见M.Ali的回答)。
请查看reference manual。
答案 3 :(得分:1)
您可以尝试以下方法:
Update set sb.facebook_id=b.id from
report_business _likes rb join business b on r.facebook_id=b.fb_id