Question

currently I am in the process of structuring a database for a site I am creating. However, I have come across a problem. I want to log the amount of times a user has logged in each day, and then be able to keep track of that info over large periods of time such as a 8 months, a year, 2 years, etc.

The only way I can think of right now, is to just have a column for each day of the year/automatically create a column each day. This idea however, just seems plain stupid to me. I'm sure there has to be a better way to do this, I just can't think of one.

Any suggestions?

Thanks, Rob

Was it helpful?

Solution

Create a separate table where you store user_id, and datetime the user logs in.

Averytime the user logs in, you insert a new record on this table.

Example:

CREATE TABLE user_activity (
         userid varchar(50),
         log_in_datetime datetime
       );

OTHER TIPS

Here is a login table I use for one of my sites. The datetime can either be logged as a datetime or as a timestamp. If you use datetime make sure to consider the timezone of your mysql server.

There is plenty of stuff to track. Then you can just query it later. Each of these column names should be self explanatory with a google search.

CREATE TABLE `t_login` (
    `id_login` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `id_user` INT(10) UNSIGNED NOT NULL DEFAULT '0',
    `id_visit` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'fk to t_visit',
    `id_org` INT(10) UNSIGNED NOT NULL DEFAULT '0',
    `when_attempt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `uname_attempt` VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'attempted username' COLLATE 'latin1_swedish_ci',
    `valid_uname` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'valid username',
    `valid_uname_pword` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'valid username and valid password together',
    `pw_hash_attempt` BINARY(32) NOT NULL DEFAULT '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0',
    `remote_ip` CHAR(20) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
    `user_agent` VARCHAR(2000) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
    PRIMARY KEY (`id_login`),
    INDEX `when_attempt` (`when_attempt`),
    INDEX `rempte_ip` (`remote_ip`),
    INDEX `valid_user` (`valid_uname`),
    INDEX `valid_password` (`valid_uname_pword`),
    INDEX `username` (`uname_attempt`),
    INDEX `id_ten` (`id_org`),
    INDEX `id_user` (`id_user`),
    INDEX `id_visit` (`id_visit`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=429;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top