我有以下查询:
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching, COUNT(t3.part) AS AantalOnderdelen
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0 #and t2.part_detailtype_id=825
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
现在,这会导致3000行有t1.exact_matching 0.请参阅下面的select查询输出(只有1行)
shop_id;cat_id;detailtype_id;exact_matching
6;11111;14519;0
现在,我想将exact_matching列更新为1,以便从上面选择查询返回的所有行。我无法弄清楚如何编写更新语句,因为我想确保只有这3000行符合上述选择查询中的having和where条件。
我现在得到了这个:
UPDATE tbldetailspershop SET exact_matching = 1
WHERE (shop_id, cat_id, detailtype_id) IN
(
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
)
答案 0 :(得分:0)
update shopdetails
set exact_matching = 1
where (shop_id, cat_id, detailtype_id, exact_matching) in
(
select t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching from shopdetails t1
left join transcomp t2 on t1.compound_group_id = t2.group_id and t2.part_detailtype_id=825
left join transcompt3 on t1.compound_group_id = t3.group_id
where t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
group by t1.shop_id, t1.cat_id, t1.detailtype_id
having count(t3.part)=1
)