如何只能插入数据

WBOY
Release: 2016-06-23 14:19:55
Original
1088 people have browsed it

PHP

在php中,往表中连续插入数据的时候已经知道会有重复的值,但是如何做到只向其中插入不重复的值,重复值自动无法插入呢,不采用unique key,primary Key。


insert into('a','b','c') table_name values ('a1','a2','a3'),('a4','a5','a6'),('a8','a7','a9').表中重复值('a4','a5','a6'),如何做到无法插入('a4','a5','a6'),但是可以插入('a1','a2','a3'),(('a8','a7','a9')。

回复讨论(解决方案)

INSERT IGNORE INTO table_name VALUES ('a1','a2','a3'),('a4','a5','a6'),('a8','a7','a9')
Copy after login
Copy after login

INSERT IGNORE INTO table_name VALUES ('a1','a2','a3'),('a4','a5','a6'),('a8','a7','a9')
Copy after login
Copy after login

好方法啊  学习 了 

可以通过程序判断,查询数据库已有数据是否重复,重复就跳过新增操作。

这个是在YII中使用的,其中有个exist()函数,可以自动判断,不过楼上的方法倒是第一次见 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template