MySQL更新Lefl加入错误#1064

时间:2015-04-14 08:36:41

标签: mysql syntax-error left-join

我有以下查询:

UPDATE `order_tab`
LEFT JOIN `currency_tab`
ON (`order_tab`.`order_actual_loading_date` = `currency_tab`.`currency_date`)
SET `order_tab`.`order_cost_converted` = (`order_tab`.`order_cost_pricelist` * `currency_tab`.`currency_value`);

跑完后我有错误:

  

“LEFT JOIN currency_tab附近的SQL语法错误   ON(order_taborder_actual_loading_date =`order_c'在第2行

MySQL版本:

+---------------+-------------+
| Variable_name | Value       |
+---------------+-------------+
| version       | 3.23.52-log |
+---------------+-------------+

order_tab

+---------------------------------+------------------+------+-----+---------+----------------+
| Field                           | Type             | Null | Key | Default | Extra          |
+---------------------------------+------------------+------+-----+---------+----------------+
| order_id                        | int(10) unsigned |      | PRI | NULL    | auto_increment |
| user_id                         | int(10) unsigned |      |     | 0       |                |
| order_loading_address           | blob             | YES  |     | NULL    |                |
| order_loading_city              | varchar(50)      | YES  |     | NULL    |                |
| order_unloading_address         | blob             | YES  |     | NULL    |                |
| order_unloading_city            | varchar(50)      | YES  |     | NULL    |                |
| order_desc                      | blob             | YES  |     | NULL    |                |
| order_collect_date_start        | date             | YES  |     | NULL    |                |
| order_collect_date_end          | date             | YES  |     | NULL    |                |
| order_delivery_date             | date             | YES  |     | NULL    |                |
| order_cost_pricelist            | float(11,2)      | YES  |     | NULL    |                |
| order_currency_pricelist        | varchar(6)       | YES  |     | NULL    |                |
| order_cost_forwarder            | float(11,2)      | YES  |     | NULL    |                |
| order_currency_forwarder        | varchar(6)       | YES  |     | NULL    |                |
| order_cost_converted            | float(11,2)      | YES  |     | NULL    |                |
| order_currency_converted        | varchar(6)       | YES  |     | NULL    |                |
| order_distance                  | float(11,2)      | YES  |     | NULL    |                |
| order_activation_date           | datetime         | YES  |     | NULL    |                |
| order_state                     | int(2) unsigned  |      |     | 0       |                |
| order_notes                     | blob             | YES  |     | NULL    |                |
| order_date                      | datetime         | YES  |     | NULL    |                |
| order_payer                     | varchar(50)      | YES  |     | NULL    |                |
| order_receiver                  | varchar(50)      | YES  |     | NULL    |                |
| order_forwarder                 | int(10) unsigned | YES  |     | NULL    |                |
| order_forwarder_car_no          | varchar(10)      | YES  |     | NULL    |                |
| order_forwarder_contact_address | blob             | YES  |     | NULL    |                |
| order_forwarder_trailer_no      | varchar(20)      | YES  |     | NULL    |                |
| order_reservation_date          | datetime         | YES  |     | NULL    |                |
| order_assortment                | varchar(11)      | YES  |     | NULL    |                |
| order_weight                    | float(6,3)       | YES  |     | NULL    |                |
| order_group                     | int(2) unsigned  | YES  |     | NULL    |                |
| order_type                      | int(2) unsigned  | YES  |     | NULL    |                |
| order_invoice_no                | varchar(10)      | YES  |     | NULL    |                |
| order_closing_date              | datetime         | YES  |     | NULL    |                |
| order_actual_loading_date       | date             | YES  |     | NULL    |                |
| order_actual_unloading_date     | date             | YES  |     | NULL    |                |
| order_company_offerer           | varchar(11)      |      |     |         |                |
| objversion                      | timestamp(14)    | YES  |     | NULL    |                |
+---------------------------------+------------------+------+-----+---------+----------------+

currency_tab

+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| currency_date  | date       | YES  |     | NULL    |       |
| currency_value | float(6,4) | YES  |     | NULL    |       |
+----------------+------------+------+-----+---------+-------+

如何避免此错误?

1 个答案:

答案 0 :(得分:0)

我认为此代码应该有效:

UPDATE `order_tab`, `currency_tab`
SET `order_tab`.`order_cost_converted` = (`order_tab`.`order_cost_pricelist` * `currency_tab`.`currency_value`)
WHERE (`order_tab`.`order_actual_loading_date` = `currency_tab`.`currency_date`)

或[推荐]

UPDATE `order_tab`
SET `order_tab`.`order_cost_converted` = (`order_tab`.`order_cost_pricelist` * (SELECT `currency_tab`.`currency_value` FROM `currency_tab` 
WHERE `order_tab`.`order_actual_loading_date` = `currency_tab`.`currency_date`))
  

注意:
  使用LEFT JOIN会使currency_tabcurrency_value有时会为NULL