SQL 语法错误 - 连接两个表时更新
SQL Syntax Error - updating while joining two tables
谁能帮我理解为什么我在尝试运行此查询时遇到语法错误?
UPDATE exp_store_orders
SET exp_store_orders.order_status_name = "Digital"
JOIN exp_store_order_items
ON exp_store_orders.id = exp_store_order_items.order_id
JOIN exp_channel_data
ON exp_store_order_items.entry_id = exp_channel_data.entry_id
GROUP BY exp_store_order_items.order_id
HAVING COUNT(CASE exp_channel_data.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(exp_store_order_items.order_id)
这会显示我要更新的订单的 ID,但出于某种原因,上面会返回一个语法错误。
SELECT exp_store_orders.id
FROM exp_store_orders
JOIN exp_store_order_items
ON exp_store_orders.id = exp_store_order_items.order_id
JOIN exp_channel_data
ON exp_store_order_items.entry_id = exp_channel_data.entry_id
GROUP BY exp_store_order_items.order_id
HAVING COUNT(CASE exp_channel_data.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(exp_store_order_items.order_id)
感谢任何帮助,谢谢!
MySQL 中 update
和 join
的正确语法是 join
在 之前 set
。此外,group by
不允许出现在 update
.
中
相反,从子查询中获取列表并使用另一个 join
来设置行。子查询实际上不需要exp_store_orders
,所以可以去掉:
UPDATE exp_store_orders o JOIN
(SELECT oi.order_id
FROM exp_store_order_items oi JOIN
exp_channel_data cd
ON oi.entry_id = cd.entry_id
GROUP BY oi.order_id
HAVING COUNT(CASE cd.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(oi.order_id)
) ou
on o.id = ou.order_id
SET o.order_status_name = 'Digital';
您的语句中缺少 WHERE 子句。
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
谁能帮我理解为什么我在尝试运行此查询时遇到语法错误?
UPDATE exp_store_orders
SET exp_store_orders.order_status_name = "Digital"
JOIN exp_store_order_items
ON exp_store_orders.id = exp_store_order_items.order_id
JOIN exp_channel_data
ON exp_store_order_items.entry_id = exp_channel_data.entry_id
GROUP BY exp_store_order_items.order_id
HAVING COUNT(CASE exp_channel_data.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(exp_store_order_items.order_id)
这会显示我要更新的订单的 ID,但出于某种原因,上面会返回一个语法错误。
SELECT exp_store_orders.id
FROM exp_store_orders
JOIN exp_store_order_items
ON exp_store_orders.id = exp_store_order_items.order_id
JOIN exp_channel_data
ON exp_store_order_items.entry_id = exp_channel_data.entry_id
GROUP BY exp_store_order_items.order_id
HAVING COUNT(CASE exp_channel_data.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(exp_store_order_items.order_id)
感谢任何帮助,谢谢!
MySQL 中 update
和 join
的正确语法是 join
在 之前 set
。此外,group by
不允许出现在 update
.
相反,从子查询中获取列表并使用另一个 join
来设置行。子查询实际上不需要exp_store_orders
,所以可以去掉:
UPDATE exp_store_orders o JOIN
(SELECT oi.order_id
FROM exp_store_order_items oi JOIN
exp_channel_data cd
ON oi.entry_id = cd.entry_id
GROUP BY oi.order_id
HAVING COUNT(CASE cd.field_id_50 WHEN '' THEN null ELSE 1 END) = COUNT(oi.order_id)
) ou
on o.id = ou.order_id
SET o.order_status_name = 'Digital';
您的语句中缺少 WHERE 子句。
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]