
- 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
Return true or false in a MySQL select if another field contains a string?
To return TRUE or FALSE if another field contains a string, use IF(). Let us first create a table
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (1.28 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | David | Miller | | Adam | Smith | +-----------+----------+ 3 rows in set (0.00 sec)
Following is the query to return true or false in a MySQL select if another field contains a string. Here, we are checking for string “Miller” −
Mysql> select if(LastName=’Miller’,’Yes','No') AS LastName,FirstName from DemoTable;
This will produce the following output −
+----------+-----------+ | LastName | FirstName | +----------+-----------+ | No | Chris | | Yes | David | | No | Adam | +----------+-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Select a field and if it's null, select another with MySQL?
- Check if a field contains a string in MongoDB?
- Only update the MySQL field if the field contains null or 0?
- Check if string contains another string in Swift
- Check if a string contains numbers in MySQL?
- How do I determine if a String contains another String in Java?
- SELECT where row value contains string in MySQL?
- Concat a field in MySQL SELECT?
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- Can we select field name in MySQL that contains an asterisk?
- How to check if a String contains another String in a case insensitive manner in Java?
- MySQL query to check if a string contains a word?
- Return TRUE if the first string starts with a specific second string JavaScript
- Yeast is a fungus. True or False.
- Select MongoDB documents where a field either does not exist, is null, or is false?

Advertisements