
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Check if MySQL entry exists and if it does, how to overwrite other columns?
For this, use INSERT ON DUPLICATE KEY UPDATE command. Let us first create a table −
mysql> create table DemoTable1891 ( FirstName varchar(20), UNIQUE KEY(FirstName) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1891 values('Chris') on duplicate key update FirstName='Robert'; Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1891 values('David') on duplicate key update FirstName='Robert'; Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1891 values('Chris') on duplicate key update FirstName='Robert'; Query OK, 2 rows affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1891;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | David | | Robert | +-----------+ 2 rows in set (0.00 sec)
- Related Articles
- How to check if a table exists in MySQL and create if it does not already exist?
- Check if table exists in MySQL and display the warning if it exists?
- Check if a user exists in MySQL and drop it?
- How to check if a MySQL database exists?
- Does UPDATE overwrite values if they are identical in MySQL
- How to check if value exists with MySQL SELECT 1?
- How to check if a python module exists without importing it?
- Check if MongoDB database exists?
- How to check if an alert exists using WebDriver?
- How to check if a variable exists in JavaScript?
- How to check if a column exists in Pandas?
- How to check if a file exists in Golang?
- How to check if a file exists in Perl?
- How to check if a specific country code exists in a cell with MySQL?
- How to check if event exists on element in jQuery?

Advertisements