Check if a field exists and it's not null - mysql

I need to fetch a value from a table with three possibilities: 1) The field exists and it's not null, so it returns the value of the field 2) the field exists but it's null, so it returns is-null-string 3) The field doesn't exists, so it returns not-existing-string
I am trying to run this query but I get this error message #1054 - Unknown column 'm.my_field' in 'field list'
SELECT if (exists(SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'my_table' AND COLUMN_NAME = 'my_field'
), IFNULL(m.my_field, 'is-null-string'), 'not-existing-string'
) AS my_field,
m.*
FROM my_table m
Any idea how can I get it done in mysql 5.6?

What you are looking for is DESCRIBE:
DESCRIBE my_table;
Look at the result to see if your field exists. Note, you don't want this code to be in your application. You need to know your table fields to run queries. You might only use this to generate some boilerplate code.

Related

How can we display the temporary column name (which is not present in the table) ONLY, when using the case statement in SQL?

How does one display the temporary column name (which is not present in the table) ONLY, when using the case statement?
For example:
select my_column_name,
case
when X <=10 then 'A'
when X >=11 AND X <=19 then 'B'
when X >=20 then 'C'
else 'Some error occured'
END AS my_column_name
from table_name
now if I execute this MySql workbench gives this error:
Error Code: 1054. Unknown column 'my_column_name' in 'field list'
this will work if I give column names which are in the table or just give the * but it does not work if I just want the temporary name/alias (I'm not sure what this column is called)

How to alter a db column attribute to `NULL` from `NOT-NULL` state using SQL query?

I want to write a SQL script that alters a column's attribute to null from the not-null state.
Tried:
SELECT COLUMNPROPERTY('prefetching_rules', 'date_delete','AllowsNull') AS 'AllowsNull';
But getting following error:
Error Code: 1054. Unknown column 'COLUMNPROPERTY' in 'field list'
For this you should use alter table (assuming you column is a varchar of 255) just apply modify.
Columns are nullable by default and so just not assign other constrains as NOT NULL
ALTER TABLE mytable MODIFY your_column_name VARCHAR(255);
This query will give you detailed info about all 'not nullable' columns
in all tables in 'your_database_name_here'.
SELECT *
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'your_database_name_here'
AND IS_NULLABLE = 'NO';
Then you have to decide which of them you want to modify.
Just FYI u can't use this query as sub query in ALTER sql because it doesn't accept subqueries

How to check column exist or not before select that column from table

Below is the query I used to select multiple columns from one of my tables.
I get the column names from user to select from the table.
If the user gives a wrong column name it shows Unknown column error. How can I check if that column exists in the table before going to select?
SELECT `address_id`,`address_firstname`,` afserfw`
FROM `patsm_addresstable`
WHERE `address_id`='28'
LIMIT 0, 25
This would give the following error:
#1054 - Unknown column ' afserfw' in 'field list'
This query will give you a list of columns in a particular table with their datatypes.
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'tablename'
You can, when constructing a query against any table, use this resultset to determine whether a given column exists.
But be careful: a web application allowing end-users to give you column names is hard to make secure against intrusion by cybercrooks.

How to check if a column exist inside a table, and preform action based on the result

I use MySQL 5.5.16.
I'm trying to check if a certain column exists in one of my tables. If it is I want to update its type & collation and if it isn't I want to add it to the table.
I've read similar posts Here and Here about IF EXISTS statement. But I'm getting an error with my query:
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID') THEN
ALTER TABLE 'e4s_account_details' CHANGE 'accountID' 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
ELSE
ALTER TABLE 'e4s_account_details' ADD COLUMN 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
END IF;
The error I get is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_acco' at line 1
BTW, If I only query this:
(SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID');
I get no error and the table+column information is returned.
You can't use this syntax directly with select query instead you can make stored procedure or query with information schema table for doing same.

MySQL: ERROR 1054 (42S22): Unknown column in 'where clause'

I'm doing some changes on a WordPress database. I need to replace the URLs in the GUID field on the wp-posts table with the URLs coming from another table called ebdurls. The description of the tables is as follows:
wp_posts: the field type for the two fields I need are:
ID -> bigint(20) unsigned
guid -> varchar(255)
And the table where I have the data I need to export to wp_posts is this:
ebdurls:
title -> varchar(255)
url -> varchar(255)
ebdid -> bigint(20) unsigned
Everything seems correct, but when I apply the next query it gives me an error that I really can't get. I've tried quoting fields, tables, etc... everywhere, but no luck.
mysql> update wp_posts set wp_posts.guid=ebdurls.url where wp_posts.id=ebdurls.ebdid;
ERROR 1054 (42S22): Unknown column 'ebdurls.ebdid' in 'where clause'
Where is the mistake?
You haven't specified what ebdurls is, add a from statement to your query:
UPDATE
wp_posts, ebdurls
SET
wp_posts.guid = ebdurls.url
WHERE
wp_posts.id=ebdurls.ebdid;
edit:
Bill was right, have fixed the format now. Oops.
ebdurls.* has no value. That is what is causing your error. The database has no idea what you are trying to do.
You probably need to use a subquery or add this logic into your application.
Or something like:
UPDATE wp_posts, ebdurls ...