Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: insert/update

  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    54
    Thanks
    2
    Thanked 1 Time in 1 Post

    insert/update

    Hi

    I have a simple table as below.

    Code:
    CREATE TABLE `stats` (
      `id` int(11) NOT NULL auto_increment,
      `zones` varchar(100) default NULL,
      `date` date default NULL,
      `hits` int(100) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
    So just storing simple hits counter per zone per day.

    But I just want to increment the hits value for the same day.

    I have tried the MYSQL DUPLICATE KEY UPDATE but this wont work as I may have many zones on different dates so I cant make them unique or dates.

    So the only way I can think is first to do a query to see if a date excists then do a simple if() for insert/update

    Is their a better way of doing such a task as there maybe be many 1000's hits per day.

    Hope this makes sense :-).

    And thanks if you can advise.

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    54
    Thanks
    2
    Thanked 1 Time in 1 Post
    nevemind find a solution.

    Code:
    INSERT INTO stats (zone, date, hits) values ('zone1', 'date1', 1) ON DUPLICATE KEY UPDATE hits = hits + 1;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •