创建数据库、创建表等:
1 //php中按天创建表 2 $sql = "create database if not exists ".$db_name; 3 $date_time_array = getdate(time()); 4 $sql = "create table if not exists A_".$date_time_array['year'].'_'.$date_time_array['month']. 5 '_'.$date_time_array['day']."( 6 `id` int(8) NOT NULL AUTO_INCREMENT, 7 `usrid` varchar(20) NOT NULL, 8 `date` date NOT NULL, 9 PRIMARY KEY(`id`), 10 UNIQUE KEY(`usrid`, `date`) 11 )ENGINE=InnoDB DEFAULT CHARSET=utf-8";
数据库中,记录不存在时插入,当记录存在时更新:
1 insert into Table_A(userid, date, data) values('XXX', CURDATE(), 100) on duplicate key update `data`=`data`+100;
前提是,userid、date、data必须有一个或者多个为UNIQUE KEY;