如何使用条件更新一个表与来自单独表的数据

时间:2012-07-20 21:09:48

标签: sql ms-access sql-update

这个查询可能非常荒谬,或者我只是有一个小错误。我不太确定。但基本上我正在尝试使用Access中另一个表中的数据更新表。给我提出问题的列是设置为“是/否”类型的列,我尝试更新它的数据设置为“文本”类型,其值为字符串“是”或“否”。

所以我对这个查询的想法,如果它甚至可能,则将“Text”字段粗略地转换为1或0,具体取决于它们的Yes或No值。无论如何,足够的背景,这是查询:

UPDATE Group_Pricing SET 
Contract_Type = (SELECT Contract_Type FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID), 
Hybrid_Retail =  
(SELECT CASE (SELECT RetailSpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
RetailSpread = 1
CASE "No"
RetailSpread = 0), 
Hybrid_Mail =  
(SELECT CASE (SELECT MailSpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
MailSpread = 1
CASE "No"
MailSpread = 0),
Hybrid_Specialty =  
(SELECT CASE (SELECT SpecialtySpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
SpecialtySpread = 1
CASE "No"
SpecialtySpread = 0),
Hybrid_Rebates =  
(SELECT CASE (SELECT Rebates FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
Rebates = 1
CASE "No"
Rebates = 0)
WHERE EXISTS (SELECT 1 FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID);

非常感谢任何帮助。如果这是不可能的,那就告诉我,我是个白痴。谢谢!

1 个答案:

答案 0 :(得分:3)

你不能在MS Access中拥有CASE,你可以拥有IIf。

UPDATE Group_Pricing SET 
Contract_Type = 
     (SELECT Contract_Type 
      FROM ContractTypesFinal 
      WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID), 
Hybrid_Retail =  
IIf (SELECT RetailSpread 
     FROM ContractTypesFinal 
     WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)="Yes",1,0)

<...>

请注意,MS Access中的YesNo为-1和0

然而,仅仅说:

会容易得多
UPDATE table1 SET AYesNo=(AText="Yes")

修改

UPDATE Group_Pricing 
INNER JOIN ContractTypesFinal 
ON Group_Pricing.GroupID = ContractTypesFinal.FirstGroup 
SET Hybrid_Retail = (RetailSpread="Yes")

(RetailSpread="Yes")将评估为True(-1)