我有一個名稱,屬性a
和b
,然後cluster
的MySQL表。如何在MySQL表中添加具有計數值的列?
+--------+-----------+----------+---------+
| names | a | b | cluster |
+--------+-----------+----------+---------+
| Johny | 225630096 | 447 | 3 |
| Johny | 225630118 | 491 | 4 |
| Johny | 225630206 | 667 | 5 |
| Johny | 225630480 | 1215 | 6 |
| Johny | 225630677 | 1609 | 7 |
| Johny | 225631010 | 2275 | 8 |
| Manny | 154247076 | 6235 | 1 |
| Manny | 154247079 | 6241 | 1 |
| Manny | 154247083 | 6249 | 1 |
| Manny | 154247084 | 6251 | 1 |
| Manny | 154247087 | 6257 | 1 |
| Manny | 154247090 | 6263 | 1 |
| Manny | 154247091 | 6265 | 1 |
| Manny | 154247093 | 6269 | 1 |
| Manny | 154247097 | 6277 | 1 |
| Manny | 154247098 | 6279 | 1 |
| Manny | 154247099 | 6281 | 1 |
| Manny | 154247100 | 6283 | 1 |
| Manny | 154247555 | 7193 | 2 |
| Manny | 154247629 | 7341 | 3 |
| Manny | 154247630 | 7343 | 3 |
| Manny | 154247633 | 7349 | 3 |
| Manny | 154247634 | 7351 | 3 |
| Douges | 146340582 | 6811 | 1 |
我想添加基於names
和cluster
列count
列。例如,輸出將包含每個名稱內的簇數。例如,輸出將是這個樣子:
+--------+-----------+----------+---------+---------+
| names | a | b | cluster |count |
+--------+-----------+----------+---------+---------+
| Johny | 225630096 | 447 | 3 | 1 |
| Johny | 225630118 | 491 | 4 | 2 |
| Johny | 225630206 | 667 | 5 | 3 |
| Johny | 225630480 | 1215 | 6 | 4 |
| Johny | 225630677 | 1609 | 7 | 5 |
| Johny | 225631010 | 2275 | 8 | 6 |
| Manny | 154247076 | 6235 | 1 | 1 |
| Manny | 154247079 | 6241 | 1 | 1 |
| Manny | 154247083 | 6249 | 1 | 1 |
| Manny | 154247084 | 6251 | 1 | 1 |
| Manny | 154247087 | 6257 | 1 | 1 |
| Manny | 154247090 | 6263 | 1 | 1 |
| Manny | 154247091 | 6265 | 1 | 1 |
| Manny | 154247093 | 6269 | 1 | 1 |
我曾嘗試使用COUNT
功能,但是,它似乎計算集羣的數量來代替。
你對[count()](https://dev.mysql.com/doc/refman/5.1/en/counting-rows.html)有RTFM嗎?你想要的東西不能直接用mysql來完成,但可以使用select中的局部變量僞裝。 'select if(@prev_name <> names,@count:= 1,@count:= @ count + 1)AS count ...' –
你認爲使用python做這件事可能更有意義嗎? – msakya
它肯定會使查詢更容易維護。 –