嘿,我需要一些有用的提示/建議如何解決我的問題。我有一個包含「註冊」表的數據庫。此表的日期被格式化爲這樣:查詢月份幫助
2010-04-03 00:00:00
現在假設我有10個記錄在這個數據庫:
2010-04-03 00:00:00
2010-01-01 00:00:00
2010-06-22 00:00:00
2010-02-08 00:00:00
2010-02-05 00:00:00
2010-03-08 00:00:00
2010-09-29 00:00:00
2010-11-16 00:00:00
2010-04-09 00:00:00
2010-05-21 00:00:00
,我想獲得各月累計寄存器...所以下面的例子以上:
Jan = 1
Feb = 2
Mar = 1
Apr = 2
May = 1
Jun = 1
Jul = 0
Aug = 0
Sep = 1
Oct = 0
Nov = 1
Dec = 0
現在,我怎麼使用查詢來做到這一點,但不是必須使用的查詢,如:
WHERE left(date, 7) = '2010-01'
並繼續這樣做12次?我想這是一個查詢電話,只是把它放置幾個月探訪成數組,像這樣:
do until EOF
theMonthArray[0] = "total for jan"
theMonthArray[1] = "total for feb"
theMonthArray[2] = "total for mar"
theMonthArray[3] = "total for apr"
...etc
loop
我只是不能想辦法做到這一點除了我張貼用的例子12查詢稱爲每個月一個。
這是我現在的查詢。再次,這隻填充一個月,我試圖一次填充所有12個月。
SELECT count(idNumber) as numVisits, theAccount, signUpDate, theActive
from userinfo
WHERE theActive = 'YES'
AND idNumber = '0203'
AND theAccount = 'SUB'
AND left(signUpDate, 7) = '2010-04'
GROUP BY idNumber
ORDER BY numVisits;
上面這個例子查詢輸出這樣的:
numVisits | theAccount | signUpDate | theActive
2 SUB 2010-04-16 00:00:00 YES
哪個是正確的,因爲我已經在四月份內2條記錄。
但同樣,我試圖做所有12個月一次(在單個查詢),因此相比於做12個不同的查詢的時候,不爲多少稅數據庫服務器...
UPDATE 我希望做類似的東西沿着這些線路:
if NOT rst.EOF
if left(rst("signUpDate"), 7) = "2010-01" then
theMonthArray[0] = rst("numVisits")
end if
if left(rst("signUpDate"), 7) = "2010-02" then
theMonthArray[1] = rst("numVisits")
end if
etc etc....
end if
任何幫助將是巨大的! :)
大衛
你爲什麼要在文本字段中存儲日期? – SLaks 2010-04-14 20:55:09
@SLaks:他可能會將其作爲日期進行存儲。 MySQL允許在日期列上使用像'LEFT(signupDate,7)'這樣的字符串操作。 – 2010-04-14 21:09:43
我將它們存儲爲日期時間。數據庫上的格式爲2010-01-01 00:00:00。 – StealthRT 2010-04-14 21:26:27