|
2009,12,09, Wednesday
自己メモ備忘録です。
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で関数作っておけば引数渡すだけで一瞬で終わっちゃいます☆ |
|
2009,12,02, Wednesday
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; |

