2012-03-26 36 views
0

好的,这是我的一个新问题。正如我之前提到的一个问题,我现在使用PtokaX并为其中的某些漫游器编写脚本。我现在需要做的是完全更新我的表格,并交换两个特定列的值。我将所有用户的主要计数存储在一个名为chatstats(当前位于MyISAM中,但考虑将表更改为InnoDB)的MySQL表中。更新完成表,转换列

该表有很多行(近1000),并且几乎每天都在增加。我想要做的是每周(和月;如稍后解释)交换两列的值,并将其中一个列值设置为零。

的创建我的表的语句是这样的:

CREATE TABLE `chatstat` (
`id` BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(25) NOT NULL, 
`totalcount` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0', `thismonth` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0', 
`thisweek` INT(10) UNSIGNED NOT NULL DEFAULT '0', `lastweek` INT(10) UNSIGNED NOT NULL DEFAULT '0', 
`lastmonth` INT(10) UNSIGNED NOT NULL DEFAULT '0', `switched` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0', 
PRIMARY KEY (`id`), UNIQUE INDEX `id` (`id`), UNIQUE INDEX `username` (`username`) 
) COLLATE='utf8_general_ci' ENGINE=MyISAM ROW_FORMAT=DEFAULT; 

现在,列名是不言自明的。我创建了列switched以检查用户的计数是否已在每个星期日互换(通过检查是否为1或0)。每个星期天,我都想改变thisweek的值,lastweek也是这样,只有一次。目前,我的脚本如下(以LUA和PtokaX变量)

function UpdateUserStat(con, user) 
     sNick = ConvertNick(user.sNick) 
     cur = assert(con:execute(string.format([[SELECT * FROM chatstat WHERE username = '%s']], sNick))) 
     row = cur:fetch({}, "a") 
     cur:close() 
     if row then 
       res = assert(con:execute(string.format([[UPDATE chatstat SET totalcount = totalcount + 1 WHERE id='%d' ]], row.id))) 
     else 
       res = assert(con:execute(string.format([[ INSERT INTO chatstat (username, totalcount, thismonth, thisweek) VALUES ('%s', 1, 1, 1) ]], sNick))) 
     end 
     cur = assert(con:execute(string.format([[SELECT * FROM chatstat WHERE username = '%s']], sNick))) 
     row = cur:fetch({}, "a") 
     cur:close() 
     UpdateDateStat(con, row) 
     if os.date("%w") ~= 0 then 
       if row.switched == 1 then 
         res = assert(con:execute(string.format([[UPDATE chatstat SET switched = 0 WHERE id='%d' ]], row.id))) 
       end 
       res = assert(con:execute(string.format([[SELECT * FROM datestat WHERE field='today']]))) 
       tRow = res:fetch({}, "a") 
       if tRow.value ~= os.date("%w") then 
         ChangeWeekDay = assert(con:execute(string.format([[UPDATE datestat SET value='%d' WHERE field='today']], os.date("%w")))) 
       end 
       res:close() 
     end 
end 

功能datestat是只保留聊天记录日志的日期(多少每天等消息)。任何帮助将不胜感激。当前的UpdateUserStat函数对值的更改没有做任何事情(因为我昨天检查了它)。

P.S.如果有其他需要,我可以管理它,我会很乐意提供它。 :)

回答

1
update chatstat set lastweek = thisweek, thisweek = 0, switched = NOW() 
where WEEK(switched) <> WEEK(NOW()) and (DAYOFWEEK(NOW()) = 1); 
+0

但是,每当用户在主聊上放置任何东西时,它会导致在星期日不断更改这两列的值。 – hjpotter92 2012-03-26 12:01:01

+0

所以你可以切换到日期时间字段 – user1027167 2012-03-26 12:14:51

+0

肯定会试试看。谢谢您的帮助。 – hjpotter92 2012-03-26 13:42:44