MySQL增删改
插入数据
插入数据方式有:插入完整的行、插入行的一部分、插入多行、插入某些查询结果
-- 插入完整的行
mysql> INSERT INTO user VALUES
-> (1,'GWX','e10adc3949ba59abbe56e057f20f883e','3232@QQ.COM','15898541258','男','1991-11-24',1555751064),
-> (2,'snn','e10adc3949ba59abbe56e057f20f883e','321132@QQ.COM','15898541258','女','1993-11-24',1555751061);
-- 插入行的一部分
mysql> INSERT INTO user(username,userpass,email) VALUES
-> ('heh','e10adc3949ba59abbe56e057f20f883e','3232@qq.com'),
-> ('eh','e10adc3949ba59abbe56e057f20f883e','31232@qq.com');
Query OK, 2 rows affected (0.01 sec)
-- 插入查询结果
mysql> INSERT INTO user(username,userpass)
-> SELECT 'james','e10adc3949ba59abbe56e057f20f883e';
Query OK, 1 row affected (0.00 sec)
更新及删除数据
注:不要省略where语句,不然会影响所有行,后果不堪设想
mysql> UPDATE user SET username='hello' WHERE id=3;
Query OK, 1 row affected (0.01 sec)
mysql> DELETE FROM user WHERE id=4;
Query OK, 1 row affected (0.00 sec)
-- UPDATE 结合JOIN
UPDATE user JOIN
(SELECT user_id,COUNT(*) n FROM apply
WHERE
end_time<$time -- 工作已经结束
AND apply_status=31 -- 报名了该工作
AND clk_num=0 -- 打卡数为0
GROUP BY user_id)
AS b ON `user`.user_id=b.user_id SET rating=rating+n*{$num} ;
该sql意思是 找到那些已报名用户但在工作结束前都没有打卡的用户,扣除他们的信誉分(按他们未正常上班的次数来扣除)