我有以下SQL Server查询来更新tblTicket中的列类型:
UPDATE A set type=B.type
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =@tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id
我还想更新表tblTicket中的列出价,但仅限于a.name =" ML":
UPDATE A set bid = B.id
FROM tblTicket A
JOIN (select b.tblTicket_id,f.id
FROM tblP a
JOIN tblA b ON a.allocation_id = b.id
JOIN FIRM f ON f.firm = a.firm
WHERE b.tblTicket_id =@tblTicket_id and a.name = 'ML' Group By b.tblTicket_id,f.id) B
ON A.id=b.tblTicket_id
我可以将第一个查询与上面的第二个查询结合起来吗? 谢谢。
答案 0 :(得分:0)
使用CASE
,如果满足某个条件,您可以更新列,否则您可以使用相同的值“设置”该列。
“最佳做法”提示:避免在嵌套查询中使用相同的别名(甚至从大写切换为小写,反之亦然)。
UPDATE A set type=B.type,
bid = case when a.name = 'ML' then B.id else bid end
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =@tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id