
- 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
How to add a where clause in a MySQL Insert statement?
You need to use UPDATE statement for this.
The syntax is as follows
update yourTableName set yourColumnName1=yourValue1,yourColumnName2=yourValue2,....N where yourCondition;
Let us create a table for our example
mysql> create table addWhereClauseDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(30), -> StudentPassword varchar(40) -> ); Query OK, 0 rows affected (0.45 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('John','John123456'); Query OK, 1 row affected (0.14 sec) mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('Carol','99999'); Query OK, 1 row affected (0.24 sec) mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('Bob','OO7Bob'); Query OK, 1 row affected (0.16 sec) mysql> insert into addWhereClauseDemo(StudentName,StudentPassword) values('David','David321'); Query OK, 1 row affected (0.26 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from addWhereClauseDemo;
The following is the output
+-----------+-------------+-----------------+ | StudentId | StudentName | StudentPassword | +-----------+-------------+-----------------+ | 1 | John | John123456 | | 2 | Carol | 99999 | | 3 | Bob | OO7Bob | | 4 | David | David321 | +-----------+-------------+-----------------+ 4 rows in set (0.00 sec)
Here is the query to add a where clause i.e. updating the records
mysql> update addWhereClauseDemo -> set StudentName='Maxwell',StudentPassword='Maxwell44444' where StudentId=4; Query OK, 1 row affected (0.18 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again.
The query is as follows
mysql> select *from addWhereClauseDemo;
The following is the output
+-----------+-------------+-----------------+ | StudentId | StudentName | StudentPassword | +-----------+-------------+-----------------+ | 1 | John | John123456 | | 2 | Carol | 99999 | | 3 | Bob | OO7Bob | | 4 | Maxwell | Maxwell44444 | +-----------+-------------+-----------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we use WHERE clause with MySQL INSERT INTO command?
- Can we use WHERE clause inside MySQL CASE statement?
- MySQL to Implementing OR operator in a WHERE clause?
- How to use MySQL VIEW with WHERE clause?
- Passing an array to a query using WHERE clause in MySQL?
- How to use MySQL Date functions with WHERE clause?
- How can we use a MySQL subquery with INSERT statement?
- Can we add minutes to VARCHAR datetime records while applying INSERT statement in MySQL?
- How to update multiple rows using single WHERE clause in MySQL?
- Insert multiple data using SET clause in MySQL?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- Is there a need to insert auto_increment column values in MySQL while using INSERT statement?
- Get current year in MySQL WHERE clause?
- Sort by order of values in a MySQL select statement IN clause?
- MySQL automatic string to integer casting in WHERE clause to fetch a specific id

Advertisements