Mysql set value to empty if specific field is not empty
I created this sql select query:
SELECT subscription_new_projects.banners, subscription_new_projects.banner_link
FROM subscription_new_projects
WHERE be_famous_id =32
AND now( ) < end_date
ORDER BY subscription_new_projects_id DESC
and get this sql result:
Now I want to set the field to banners
blank, If is banner_link
not blank.
I want this result:
My SQL Fiddle: Example
Any idea how to do this?
Thank.
+3
source to share
2 answers
you can use case-when
SELECT
case
when
subscription_new_projects.banner_link <> '' then ''
else subscription_new_projects.banners
end as banners,
subscription_new_projects.banner_link
FROM subscription_new_projects
WHERE be_famous_id =32
AND now( ) < end_date
ORDER BY subscription_new_projects_id DESC
+2
source to share
Use the operator CASE
.
Query
SELECT
CASE WHEN subscription_new_projects.banner_link != ''
THEN NULL
ELSE subscription_new_projects.banners
END AS banners,
subscription_new_projects.banner_link
FROM subscription_new_projects
WHERE be_famous_id =32
AND now( ) < end_date
ORDER BY subscription_new_projects_id DESC;
0
source to share