我的SQL数据库中有两个表 一个是公司&另一个是Company_advertisements。 我想更新公司表“adv”的字段
SET Company.adv = 1 if Company_adv.image is not empty
SET Company.adv = 0 if Company_adv.image is empty
Company_adv表的外键是company_id
我尝试使用以下语法,但它没有用。相反,在运行此查询后,Companmy.adv的所有值在此之后变为1
我尝试了这个查询:
UPDATE companies cmp, company_adv cma
SET cmp.adv=1
WHERE cma.company_id=cmp.id AND
cma.image1 IS NOT NULL
请帮助我!!
答案 0 :(得分:0)
如果是这种情况,则需要使用LEFT JOIN
UPDATE companies cmp
LEFT JOIN company_adv cma
ON cmp.id = cma.company_id
SET cmp.adv = IF(cma.image1 IS NULL, 0, 1)
答案 1 :(得分:0)
你可以尝试:
UPDATE companies
SET adv=IF((SELECT image
FROM company_adv
WHERE companies.id=company_adv.company_id) IS NULL,
0,
1);