码迷,mamicode.com
首页 > 其他好文 > 详细

当有相同数据时不操作,没有时插入新数据

时间:2017-11-27 16:41:54      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:exists   time   code   _id   直接   没有   values   create   use   

为防止多次插入数据,要先判断,但是如果使用:

1、如果直接这样写:

INSERT INTO `diagnose`
(`user_id`,`is_done`,`create_time`) VALUES ( 1,0,now())
WHERE NOT EXISTS(SELECT 1 FROM `diagnose` WHERE `user_id`= 1 AND` is_done`=0)

会报错,因为VALUES之后不能再用where判断;

2、这样写才对

INSERT INTO `diagnoses`
(`user_id`,`is_done`,`create_time`)
SELECT 1,0,now()
from `diagnoses`
where not exists(
select 1
from `diagnoses`
where `user_id` = 1
and `is_done` = 0
) LIMIT 1

 

 

当有相同数据时不操作,没有时插入新数据

标签:exists   time   code   _id   直接   没有   values   create   use   

原文地址:http://www.cnblogs.com/chaoyong/p/7904451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!