[英]mysql query to insert the value of a column with respect to another column in same table
[英]Insert into a column using group by value of another column of the same table
id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
以下查詢“按名稱從臨時組中選擇count(name);” 給我:
count(name)
--------
1
3
我希望將此結果更新為“計數”列。 確切地說,我希望我的桌子看起來像:
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
您可以使用COUNT / GROUP BY獲取這些值。 您可以執行UPDATE語句,該語句將表與子查詢連接:
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.