在下表中,我需要更新列brake_pad,如下所示:
Table ct_maint(simplified).
Primary key is the combined model_id/fuel columns.
model_id fuel brake_pad
1 gas 0
1 diesel 0
2 gas 0
3 diesel 0
4 gas 0
4 diesel 0
5 diesel 0
6 gas 0
6 diesel 0
......
告诉我要更新哪个brake_pad的查询是:
SELECT models.model_id, motors.fuel FROM models
INNER JOIN versions USING(model_id)
INNER JOIN versiontrim USING(version_id)
INNER JOIN motors USING(motor_id)
WHERE trim_id IN(502, 506)
GROUP BY model_id, fuel
此查询提供如下结果集:
model_id fuel
1 diesel
3 diesel
4 gas
4 diesel
6 gas
所以,我想,
UPDATE ct_maint SET brake_pad=1000 WHERE model_id AND fuel IN( SELECT.....)
但是在mysql嗡嗡我之前很久就停止了。
感谢任何帮助。
答案 0 :(得分:2)
尝试此查询:
UPDATE ct_maint SET brake_pad=1000 WHERE (model_id, fuel) IN ( SELECT.....)
答案 1 :(得分:2)
您可以选择使用两种方法:
1)我认为你可以使用元组IN:
UPDATE ct_maint SET brake_pad=1000
WHERE (model_id, fuel) IN
(SELECT models.model_id, motors.fuel FROM models WHERE ... );
2)您可以在更新中使用联接
UPDATE ct_maint SET brake_pad=1000
INNER JOIN (SELECT ...) models_upd
ON ct_maint.model_id = models_upd.model_id
AND ct_maint.fuel = models_upd.fuel;