■CALENDAR■
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   
<<前月 2009年12月 次月>>
■LOGIN■
現在のモード: ゲストモード
USER ID:
PASSWORD:
■NEW ENTRIES■
■RECENT COMMENTS■
■RECENT TRACKBACK■
■CATEGORIES■
■ARCHIVES■
■LINK■
■PROFILE■
■POWERED BY■
BLOGN(ぶろぐん)
BLOGNPLUS(ぶろぐん+)
■OTHER■
 

MySQLの重複レコード削除
自己メモ備忘録です。

select文ならdistinct keyで重複を省いて結果を返してくれますが、レコードその物を消したいときどうするか考えてみました。

1.まずは現在のテーブルのレコード数を算出
mysql> select count(*) from table_now;
+----------+
| count(*) |
+----------+
| 348748 |
+----------+
1 row in set (0.00 sec)

2.distinct tel で電話番号のユニーク数を確認
mysql> select count(distinct tel) from table_now;
+---------------------+
| count(distinct tel) |
+---------------------+
| 238354 |
+---------------------+
1 row in set (3.15 sec)


3.telとname(念の為)でグループをかけたものをtemp_tableに格納
mysql> CREATE TABLE temp_table as SELECT * FROM table_now GROUP BY tel,name;
Query OK, 238354 rows affected (18.05 sec)
Records: 238354 Duplicates: 0 Warnings: 0


4.temp_tableのレコード数を確認
mysql> select count(*) from temp_table;
+----------+
| count(*) |
+----------+
| 238354 |
+----------+
1 row in set (0.00 sec)

5.ユニーク数と合致したので、table_nowを削除
mysql> DROP TABLE table_now;
Query OK, 0 rows affected (0.18 sec)

6.temp_tableをtable_nowにalter table
mysql> ALTER TABLE temp_table RENAME TO table_now;
Query OK, 0 rows affected (0.00 sec)

7.一応確認
mysql> select count(*) from table_now;
+----------+
| count(*) |
+----------+
| 238354 |
+----------+
1 row in set (0.00 sec)

これで完了です。

PHPで関数作っておけば引数渡すだけで一瞬で終わっちゃいます☆

| Linux::PHP + MySQL | 11:01 AM | comments (74) | trackback (0) |
MySQL 5.xで月別時間帯集計
datetime型の集計で月別とか時間別とかMySQLだけで集計できないかな??って思ってテスト
うまくできたんで自己メモハート

select substring(datetime,6,2) as month,substring(datetime,12,2) as hour,concat(format(count(*),0),"件") as cnt from t_record where datetime >= '2009-06-01 00:00:00' and datetime <= '2009-11-30 23:59:59' group by month,hour;

| Linux::PHP + MySQL | 04:00 PM | comments (0) | trackback (0) |
PAGE TOP ↑