Home > Database > Mysql Tutorial > MySQL field processing methods

MySQL field processing methods

小云云
Release: 2018-02-23 10:21:01
Original
1358 people have browsed it

How to deal with the uniqueness of multiple fields?

  • Scenario

There is a table with multiple fields that need to be unique and cannot be repeated, otherwise they will be duplicates Data cannot be inserted
  • Traditional approach

Add unique indexes directly to multiple fields, simple and crude
  • Now how to do it

Add a new field and add a unique index to this field, so you don’t need to add too many unique indexes
insert into news(new_title, new_abstr, new_code) 
values('你是好','我是谁',MD5(CONCAT('你是好','我是谁')))
Copy after login

How to handle it if the data exists, it is updated, if it does not exist, it is inserted?

  • Scenario

There is a table. The records in it cannot have duplicate records. If the record exists, it will be updated. If not If it exists, insert it
  • Traditional approach

Query the select first, if it exists, update it, if it does not exist, update it
  • The current practice

mysql has a special writing method, on DUPLICATE key Update
insert into news(new_title, new_abstr, new_code, update_time, create_time) 
values('你是好','我是谁',MD5(CONCAT('你是好','我是谁')))
on DUPLICATE key Update
update_time=now(), create_time=now()
Copy after login



The above is the detailed content of MySQL field processing methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template