我有两个桌子。满足条件时,我想更新table1
。这里的条件是检查table 2
中的国家,如果是Mex
,则将比率0.5
乘以price
。
我写了以下代码
UPDATE table1
SET table1.Price = (SELECT *,
CASE table2.Country
WHEN 'CANADA' THEN (1 * table2.price)
WHEN 'MEXICO' THEN (0.5 * table2.price)
ELSE 0
END AS Price_Calc
FROM table2)
FROM table1;
运行此命令会出现以下错误
信息116,级别16,状态1,第12行
只能有一个表达式 未在子查询中引入子查询时在选择列表中指定 存在。
答案 0 :(得分:0)
尝试如下
UPDATE t1
SET t1.table1.Price = (SELECT
CASE t2.Country
WHEN 'CANADA' THEN (1 * t2.price)
WHEN 'MEXICO' THEN (0.5 * t2.price)
ELSE 0
END AS Price_Calc
FROM table2 t2
WHERE t2.Id = t1.Id -- Here it is suggestion to update target
-- column based on relation if exists between
-- ur tables
)
FROM table1 t1;
答案 1 :(得分:0)
假设Table1和Table2通过ID相关联:
UPDATE t1 SET t1.Price = t2.Price
* CASE t2.Country
WHEN 'CANADA' THEN 1.
WHEN 'MEXICO' THEN .5
ELSE 0
END
FROM table1 t1
INNER JOIN table2 t2 ON t2.Id = t1.Id
;