嘿,我需要一些有用的提示/建议如何解决我的问题。我有一个包含“注册”表的数据库。此表的日期被格式化为这样:查询月份帮助
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