Home Backend Development PHP Tutorial 安插不重复记录~求一mysql语句~

安插不重复记录~求一mysql语句~

Jun 13, 2016 am 10:33 AM
insert name replace select

插入不重复记录~~~求一mysql语句~~~
表结构很简单:

SQL code

1

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->--------------------------------id  |  cat  |  article-------------------------------- 1       abc           1 2       aaa           2 3       ccc           1--------------------------------

Copy after login

其中id是唯一主键,AUTO_INCREMENT,对于插入记录来讲没什么用。

现在要插入一些记录,当新记录的cat和article_id在原先数据中都未出现时才允许插入。比如cat='ddd',article=1和cat='aaa',article=1的记录都允许插入,但是cat="abc",article=1和cat="ccc",article=1的记录都不允许插入,因为已经存在这样的记录了。

请问怎么写mysql语句呢?

网上找的帖子,看得不是很懂,貌似都依赖主键不重复的。

------解决方案--------------------
在 cat 和 article 上做联合唯一索引

插入时用 replace 指令
------解决方案--------------------
你都不看手册的吗?这样如何进步?

CREATE [UNIQUE|FULLTEXT] INDEX index_name
ON tbl_name (col_name[(length)],... )


REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES (expression,...),(...),...
or REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...
or REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name=expression, col_name=expression,...

REPLACE 功能与 INSERT 完全一样,除了如果在表中存在一个老的记录与新记录在一个 UNIQUE 或 PRIMARY KEY 上有相同的值,那么在新记录被插入之前,老的记录将被删除。查看章节 6.4.3 INSERT 句法。 

换句话说,你不可以从一个 REPLACE 中访问老的记录行的值。某些老的 MySQL 版本中,你或许可以这样做,但是这是一个 Bug,现在已被修正了。 

为了能够使用 REPLACE,你必须有对该表的 INSERT 和 DELETE 权限。 

当你使用一个 REPLACE 时,如果新的记录行代替了老的记录行,mysql_affected_rows() 将返回 2。这是因为在新行被插入之前,重复记录行被先删除了。 

这个事实使得判断 REPLACE 是否是添加一条记录还是替换一条记录很容易:检查受影响记录行的值是 1 (添加)还是 2(替换)。 

注意,除非你使用一个 UNIQUE 索引或 PRIMARY KEY ,使用 REPLACE 命令是没有感觉的,因为它会仅仅执行一个 INSERT。

------解决方案--------------------
那你就这样: insert ignore into .............
但是也要在 cat 和 article 上做联合唯一索引
------解决方案--------------------
最笨的办法就是插入前,select一下是否该值已存在。
------解决方案--------------------
直接用select查吧
------解决方案--------------------
repalce into tablename(id,name) values('.$id.','".$name."'); or replace into tablename(id,name) select id,name
------解决方案--------------------
按楼主的想法先select再insert这种方式还不如replace呢
------解决方案--------------------
建议用replace,比较简单高效。
------解决方案--------------------
select 白瞎,又不锁表, 根本不是原子的,白判断。

直接唯一索引插入看结果。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Asynchronous processing method of Select Channels Go concurrent programming using golang Asynchronous processing method of Select Channels Go concurrent programming using golang Sep 28, 2023 pm 05:27 PM

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery How to hide the select element in jquery Aug 15, 2023 pm 01:56 PM

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

What is the difference between insert ignore, insert and replace in mysql What is the difference between insert ignore, insert and replace in mysql May 29, 2023 pm 04:40 PM

The difference between insertignore, insert and replace instructions already exist or not. Example of insert error. Insertintonames(name,age)values("Xiao Ming", 23); insertignore ignores insertignoreintonames(name, age)values("Xiao Ming", 24); replace Replace and insert replaceintonames(name,age)values("Xiao Ming", 25); table requirements: PrimaryKey, or unique index result: the table id will be automatically incremented. Test code creates table

How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Use java's StringBuilder.insert() function to insert a string at the specified position Use java's StringBuilder.insert() function to insert a string at the specified position Jul 24, 2023 pm 09:37 PM

Use java's StringBuilder.insert() function to insert a string at a specified position. StringBuilder is a class in Java used to handle variable strings. It provides a variety of methods to operate strings. The insert() function is used to insert strings at specified positions. One of the common methods of positionally inserting strings. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples. insert()

What are the string search and replace techniques in Python? What are the string search and replace techniques in Python? Oct 20, 2023 am 11:42 AM

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles