标签: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