我有一个名为sample的表,它有3列。
id customer default_address
1 1 0
2 1 0
3 1 0
4 1 0
5 2 0
6 2 0
7 3 0
8 3 0
9 2 0
10 2 0
这里我想按客户分组并更新第一个默认地址应为1,并为每个客户全部为0。怎么做mysql查询? 感谢
答案 0 :(得分:1)
UPDATE tb a
LEFT JOIN
(
SELECT customer, MIN(id) id
FROM tb
GROUP BY customer
) b ON a.customer = b.customer AND
a.ID = b.ID
SET a.default_address = b.ID IS NOT NULL
答案 1 :(得分:0)
UPDATE sample
LEFT JOIN
(SELECT MIN(id) id,customer
FROM sample
GROUP BY customer) t ON sample.customer=t.customer
SET default_address="1"
WHERE sample.id=t.id;
这应该有用。
答案 2 :(得分:0)
Update Table1
INNER JOIN (Select Min(`id`) as MinID,`customer`
From Table1 Group by `customer`) t2
SET Table1.`default_address` = 1
WHERE Table1.`id` = t2.MinID;
<强> SAMPLE FIDDLE 强>