- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- MySQL query to return a string as a result of IF statement?
- Set the result of a query to a variable in MySQL?
- Check if table exists in MySQL and display the warning if it exists?
- How to check if a MySQL database exists?
- Check if a PHP cookie exists and if not set its value
- Check if a user exists in MySQL and drop it?
- Check if value exists for a field in a MongoDB document?
- Set user variable from result of query in MySQL?
- Check if MySQL entry exists and if it does, how to overwrite other columns?
- Check that Field Exists with MongoDB?
- How to check if value exists with MySQL SELECT 1?
- Write MySQL case statement to set custom messages for student’s result
- How to check if a table exists in MySQL and create if it does not already exist?
- MySQL query to check if a string contains a word?
- Check if a value exists in a column in a MySQL table?

Advertisements