英文:
SQL ERROR - on Updating Column Value in First Table Based on Other Two Tables
问题
我有3个表格,分别是OrderPayment、OrderSummaryInfo和OrderLossEntity。
OrderPayment表格有一个叫做OrdertypeCD的列,而OrderLossEntity表格有一个叫做OrderType的列。
这3个表格之间没有直接的关联。
OrderSummary表格有一个主键叫做orderevententity。这个表格是OrderPayment和OrderLossEntity两个表格的主表。
OrderSummaryinfo表格有一个叫做orderNumber的列,而OrderPayment表格也有一个叫做OrderNumber的列。这个OrderNumber是OrderPayment表格的主键,并且它参考了OrderSummaryinfo表格:
OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
OrderSummaryinfo.orderevententity是OrderLosseventEntity.id的主键,其中OrderLosseventEntity表格的主键参考了OrderSummaryinfo.orderevententity:
OrderSummaryinfo.orderevententity = OrderLossEntity.id
我的需求是,OrderTypeCD有3个可能的值:"Cash"、"Gift"和"rewards"。
OrderType列有2个可能的值:"Direct"和"Indirect"。
无论何时OrderTypeCD的值为"Rewards",我应该将其更改为新值"CashBack",对于那些OrderType为"Indirect"的订单。
我正在尝试一个查询,但是我得到了一个错误:
UPDATE Orderpayment op
JOIN OrderSummaryInfo osi ON op.orderNumber = osi.OrderNumber
JOIN OrderLossEntity ole ON ole.id = osi.orderevententity
SET op.ordertypeCd = 'CashBck'
WHERE op.orderTypeCD = 'Rewards'
AND ole.OrdeType = 'Indirect';
我得到了一个错误:
SQL State: 42601
请帮助我纠正这个查询。
英文:
I have 3 Tables OrderPayment, OrderSummaryInfo and OrderLossEntity.
OrderPayment table has a column OrdertypeCD, OrderLossEntity table has a column OrderType.
There is no direct relation between all the 3 tables.
Ordersummary has a primary key orderevententity. This table is the master table for both the tables (OrderPayment and OrderLossEntity).
OrderSummaryinfo table has a column called orderNumber and OrderPayment table also has OrderNumber. This OrderNumber is the primary key in OrderPayment table and is referring to OrderSummaryinfo:
OrderSummaryInfo.OrderNumber = OrderPayment.OrderNumber
OrderSummaryinfo.orderevententity is the primary key for OrderLosseventEntity.id --> where is the primary key in OrderLosseventEntity table referring to OrderSummaryinfo.orderevententity:
OrderSummaryinfo.orderevententity = OrderLossEntity.id
My requirement is OrderTypeCD has 3 possible values "Cash", "Gift" and "rewards".
OrderType column has 2 possible values - "Direct" and "Indirect".
Wherever OrderTypeCD has value "Rewards", I should change it to a new value "CashBack" for those orders which have OrderType="Indirect".
I am trying a query but I get an error
UPDATE Orderpayment op
JOIN OrderSummaryInfo osi ON op.orderNumber = osi.OrderNumber
JOIN OrderLossEntity ole ON ole.id = osi.orderevententity
SET op.ordertypeCd = 'CashBck'
WHERE op.orderTypeCD = 'Rewards'
AND ole.OrdeType = 'Indirect';
I get an error
> SQL State : 42601
Please help me correct this query.
答案1
得分: 0
Your first error is the sequence of clauses. The set clause must immediately follow the update and you cannot use table alias in the set.
Secondly, the from portion must follow standard SQL format, so update table_name set column_name join will also give a syntax error. To accomplish the necessary join you need a "sub select."
Taking this and the above together along with your other stated requirements you get:
update OrderPayment op
set OrderTypeCd = 'CashBck'
from (select osi.OrderNumber
from OrderSummaryInfo osi
join OrderLossEntity ole
on ole.id = osi.OrderEventEntity
where ole.OrderType = 'Indirect'
) jOrder
where op.OrderNumber = jOrder.OrderNumber
and op.OrderTypeCd = 'Rewards';
NOTE: Not tested, no test data supplied.
英文:
Your first error is the sequence of clauses. The set clause must immediately follow the update and you cannot use table alias in the set (see documentation). So you need:
update OrderPayment op
set OrderTypeCd = 'CashBck' ...
Secondly, the from portion must follow standard SQL format, so update table_name set column_name join will also give a syntax. To accomplish the necessary join you need a "sub select". Taking this and the above together along with your other stated requirements you get:
update OrderPayment op
set OrderTypeCd = 'CashBck'
from (select osi.OrderNumber
from OrderSummaryInfo osi
join OrderLossEntity ole
on ole.id = osi.OrderEventEntity
where ole.OrderType = 'Indirect'
) jOrder
where op.OrderNumber = jOrder.OrderNumber
and op.OrderTypeCd = 'Rewards';
NOTE: Not tested, no test data supplied.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论