
- 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
Display records with conditions set using if statement in UPDATE statement with MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentMarks int, -> Status varchar(20) -> ); Query OK, 0 rows affected (0.97 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentName,StudentMarks) values('Chris',79); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentName,StudentMarks) values('David',59); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentName,StudentMarks) values('Bob',60); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentName,StudentMarks) values('Mike',45); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+--------------+--------+ | StudentId | StudentName | StudentMarks | Status | +-----------+-------------+--------------+--------+ | 1 | Chris | 79 | NULL | | 2 | David | 59 | NULL | | 3 | Bob | 60 | NULL | | 4 | Mike | 45 | NULL | +-----------+-------------+--------------+--------+ 4 rows in set (0.00 sec)
Following is the query to set conditions while updating −
mysql> update DemoTable -> set Status=if(StudentMarks > 60 ,'PASS','FAIL'); Query OK, 4 rows affected (0.40 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-------------+--------------+--------+ | StudentId | StudentName | StudentMarks | Status | +-----------+-------------+--------------+--------+ | 1 | Chris | 79 | PASS | | 2 | David | 59 | FAIL | | 3 | Bob | 60 | FAIL | | 4 | Mike | 45 | FAIL | +-----------+-------------+--------------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL If statement with multiple conditions?
- Using Update statement with TINYINT in MySQL?
- Update multiple values in a table with MySQL IF Statement
- Set different IDs for records with conditions using a single MySQL query
- MySQL SELECT IF statement with OR?
- Using variables with MySQL prepare statement
- IF ELSE statement in a MySQL Statement?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- Set user-defined variable with table name in MySQL prepare statement?
- In which conditions, MySQL CASE statement return NULL?
- How to implement WHILE LOOP with IF STATEMENT MySQL?
- How to use MySQL CASE statement while using UPDATE Query?
- MySQL CREATE statement with KEY keyword
- C++17 If statement with initializer
- Display records from MySQL stored Procedure with IF…THEN…END IF statements

Advertisements