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:

enter image description here

Now I want to set the field to banners

blank, If is banner_link

not blank.

I want this result:

enter image description here

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


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;

      

Demo screenshot

0


source







All Articles