3

I have a sql query below:

SELECT 
    md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther
FROM
    marketing_details md
WHERE
    md.id = 14588

From the 7 columns in the above select statement only one column will have a value and rest will be null. Is it possible to select just that one column value that is not null using some sort of sql statement ?

user4676307
  • 409
  • 8
  • 22
  • Possible duplicate of [MySQL SELECT only not null values](http://stackoverflow.com/questions/5285448/mysql-select-only-not-null-values) – Martin Oct 12 '16 at 15:24

1 Answers1

7

Use the coalesce() function to return the 1st non-null value from a list of parameters:

SELECT 
    coalesce(md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther) as non_null_value
FROM
    marketing_details md
WHERE
    md.id = 14588

However, it will not be able to tell you which column the value came from.

UPDATE

If you really want to use sql to retrieve the name of the field that has the non null value, then you can do that with the following monstrous sql statement below. What it does it concatenates each field value from a record into a single string, where the values are separated by comma. NULL values are converted to empty string. Then using find_in_set() function it finds the position of the only non null value within the above string. Then using the elt() function it returns the name of the field from the list of field name literals based on the position returned by find_in_set().

SELECT
    md.id, 
    coalesce(md.refereeInternetSearch,
    md.refereeCompanyColleague,
    md.refereeIndustryPeer,
    md.refereeIndustryEvent,
    md.refereeIndustryPublication,
    md.refereeMarketingEmail,
    md.refereeOther) as non_null_value,
    elt(find_in_set(coalesce(md.refereeInternetSearch,
                                 md.refereeCompanyColleague,
                                 md.refereeIndustryPeer,
                                 md.refereeIndustryEvent,
                                 md.refereeIndustryPublication,
                                 md.refereeMarketingEmail,
                                 md.refereeOther),
                        concat(coalesce(md.refereeInternetSearch,''),',',
                               coalesce(md.refereeCompanyColleague,''),',',
                               coalesce(md.refereeIndustryPeer,''),',',
                               coalesce(md.refereeIndustryEvent,''),',',
                               coalesce(md.refereeIndustryPublication,''),',',
                               coalesce(md.refereeMarketingEmail,''),',',
                               coalesce(md.refereeOther,'')
                              ) 
                       ),'refereeInternetSearch',
                         'refereeCompanyColleague',
                         'refereeIndustryPeer',
                         'refereeIndustryEvent',
                         'refereeIndustryPublication',
                         'refereeMarketingEmail',
                         'refereeOther'
      ) as field_name 
FROM
    marketing_details md
WHERE
    md.id = 14588

Huh, I hope I got all the parentheses right!

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Thank you mate that does exactly what I needed however is there a way of knowing which column the value came from ? – user4676307 Oct 12 '16 at 15:36
  • Well, I did give you a solution, but I think you might fare better if you do this from a proper programming language, not from sql. – Shadow Oct 12 '16 at 16:16