带有两个表的Mysql UPDATE查询

时间:2012-12-27 02:06:49

标签: mysql

我想运行此查询:

UPDATE up SET up.pts = uc.checkin_worth WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test@gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'

但是,我收到错误,因为我不知道如何在UPDATE查询中组合两个表(uc和up)。

任何人都可以帮我解决这个问题吗?

谢谢,

2 个答案:

答案 0 :(得分:2)

只需使用普通的JOIN语法:

UPDATE up JOIN uc ON uc.email = up.email AND uc.company_id = up.company_id
SET up.pts = uc.checkin_worth
WHERE uc.email = 'test@gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'

您还可以使用旧的逗号语法,它与原始查询更相似:

UPDATE uc, up
SET up.pts = uc.checkin_worth
WHERE uc.email = up.email
AND uc.company_id = up.company_id
AND uc.email = 'test@gmail.com'
AND uc.company_id = '4'
AND uc.qrcode = 'j'

答案 1 :(得分:1)

试试这个:

UPDATE up, uc 
SET up.pts = uc.checkin_worth, 
WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test@gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'