作者akillerbear (我是歹人雄大)
看板Database
標題[問題] 關於group by的問題
時間Mon Apr 27 16:21:55 2009
小弟目前是用mysql 5
我有多個資料表如下
200001 .200002 ......200012, 200101..........
每個資料表內容大概是
id sex local
-----------------------
12 F 台北市大安區
13 M 嘉義縣布袋鎮
14 M 基隆市中正區
. . .
. . .
. . .
-----------------------------
我想統計各月份中 不同地區 不同性別的人數
所以我下了
selcet local,count(*)
from 200001
where sex='F'
group by local;
這樣只能統計一個月
所以我就用 union 把12個表連起來
selcet local,count(*)
from 200001
where sex='F'
group by local;
union
selcet local,count(*)
from 200002
where sex='F'
group by local;
.
.
.
union
selcet local,count(*)
from 200012
where sex='F'
group by local;
------------------------------
不過因為有區域會顯得很雜(鄉鎮區)
小弟我想只要縣市就好
也就是 台北市大安區 *1
台北市信義區 *1
就只分成 台北市 *2 這樣一區
不知道有沒有辦法只group by 前面三個字呢?
感謝^^
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.71.94.30
推 bobju:可參考substring函式的用法. 04/27 16:27
→ akillerbear:乾蝦~~ 04/27 17:03
推 adrianshum:我還是第一次看到這種 table design,一個月一個table?! 04/27 17:04
推 asklove:如果資料量非常龐大就必須考慮一個月一個資料表檔案 04/27 20:19
推 asklove:試假設一個月的資料量就到達6G,經年累月的結果會怎樣呢? 04/27 20:20
推 asklove:結果就是該資料表膨脹到數十百G,每次開關檔都冒著危險 04/27 20:20
推 asklove:以上是以假設資料庫儲存方式為檔案的前提下 04/27 20:21
推 asklove:另外備份及還原資料時 一個大檔與多個小檔 我會想選後者 04/27 20:23
推 adrianshum:解決資料量可用其他方法,拆table只會令entity關係混亂 04/28 02:58
推 asklove:願聞其詳....另外 硬體無法新增或換更好的 04/28 06:57
推 chrisQQ:._./ 我也想知道有什麼其他方法!! 04/28 17:47
推 eggeggss:用table partition.... 04/29 01:39
推 eggeggss:MSSQL 2005有提供partition function,可以參考一下 04/29 23:54
推 chrisQQ:mysql 5.1 好像也有提供 partition 04/30 07:33