

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a MySQL query to check if field exists and then return the result set?
To check if field exists and then to return the result set, you can use the below syntax −
show columns from yourTableName where field='yourColumnName';
Let us first create a table −
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserFirstName varchar(20), UserLastName varchar(20), UserAge int, UserAddress varchar(200), UserCountryName varchar(20) ); Query OK, 0 rows affected (0.67 sec)
Here is the query to check if field exists and then return the result set −
mysql> show columns from DemoTable where field='UserCountryName';
This will produce the following output −
+-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | UserCountryName | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 1 row in set (0.01 sec)
Here is the case when the column does not exist −
mysql>show columns from DemoTable where field='Marks';
This will produce the following output −
Empty set (0.00 sec)
- Related Questions & Answers
- MySQL query to return a string as a result of IF statement?
- Check if table exists in MySQL and display the warning if it exists?
- Check if a PHP cookie exists and if not set its value
- How to check if a MySQL database exists?
- Set the result of a query to a variable in MySQL?
- Check if a user exists in MySQL and drop it?
- Check if value exists for a field in a MongoDB document?
- Check that Field Exists with MongoDB?
- Check if MySQL entry exists and if it does, how to overwrite other columns?
- How to check if a table exists in MySQL and create if it does not already exist?
- How to check if value exists with MySQL SELECT 1?
- Check if MongoDB database exists?
- Return True if a document exists in MongoDB?
- Check if a file exists in Java
- Check if a File exists in C#
Advertisements