Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to check if field exists and then return the result set

Tags:

mysql

I dont know how can i explain it, may be this example garbage query can help.

SELECT if_exists(Fieldname) * FROM my table name

I am trying to select all the rows of the database only if Fieldname field is present.

Can you help me out

like image 251
mrN Avatar asked Mar 27 '11 10:03

mrN


People also ask

How do you check if a data exists in SQL query?

The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.

How do you check if a column exists in SQL?

Checking Existence of the Column: For checking the existence we need to use the COL_LENGTH() function. COL_LENGTH() function returns the defined length of a column in bytes. This function can be used with the IF ELSE condition to check if the column exists or not.

How do I check if a record exists on a table?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

How do you check if data is present in database?

To check whether a particular value exists in the database, you simply have to run just a regular SELECT query, fetch a row and see whether anything has been fetched. Here we are selecting a row matching our criteria, then fetching it and then checking whether anything has been selected or not.


1 Answers

SHOW columns from `yourtable` where field='yourfield'

You will get a empty rows, if you dont have that field in that table.

like image 136
user692653 Avatar answered Sep 21 '22 23:09

user692653