简体   繁体   English

MYSQL,多表更新

[英]MYSQL, multiple table update

i have a query that updates values in 6 tables, 我有一个查询来更新6个表中的值,

the 6 tables are all in the same format with a structure like 6个表的格式相同,结构类似

ID   Ref       Date
21    1     26/01/2015
22    2     26/01/2015
23    3     26/01/2015

And there is a table that holds a list of vehicles with a structure like, 还有一张表格,其中包含一系列具有以下结构的车辆:

ID   Name       
1    name     
2    name     
3    name    

So the Ref column in the 6 tables that need updating correspond with the ID of the vehicle name. 因此,需要更新的6个表中的Ref列与车辆名称的ID相对应。

Here is the query i have at to moment, 这是我目前有的查询,

UPDATE transport_tax, transport_tachocalibration, transport_service, transport_rbt, transport_mot, transport_ivc
        SET transport_tax.Date = '$tax',
        transport_tachocalibration.Date = '$tacho',
        transport_service.Date = '$service',
        transport_rbt.Date = '$rbt',
        transport_mot.Date = '$mot',
        transport_ivc.Date = '$ivc',
        WHERE 
        transport_tax.Ref = '$id' AND
        transport_tachocalibration.Ref = '$id' AND
        transport_service.Ref = '$id' AND
        transport_rbt.Ref = '$id' AND
        transport_mot.Ref = '$id' AND
        transport_ivc.Ref = '$id'

this query in in PHP to that's why there are variables there, the 6 values that are to be updated too all equal 1/1/2015, The $id is equal to 1. Currently when this query is run i get this error 此查询在PHP中的原因就是在那里存在变量,要更新的6个值都全部等于1/1/2015,$ id等于1。当前运行此查询时,出现此错误

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE transport_tax.Ref = '1' AND transport_tachocalibr' at line 8 in C:\wamp\www\application\models\Transport_model.php on line 76

Any help would be much appreciated, 任何帮助将非常感激,

Remove the comma before WHERE WHERE之前删除逗号

transport_ivc.Date = '$ivc', WHERE
            here-----------^

First of all remove comma in the line before WHERE and also make your query type safe, if you are running your database in strict mode. 如果在严格模式下运行数据库,则首先删除WHERE之前的行中的逗号,并确保查询类型的安全。 If you got integer or in general number values, you should not add the ticks, eg: 如果您使用整数或一般数字值,则不应添加刻度,例如:

transport_tax.Ref = $id AND
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM