简体   繁体   中英

MySQL - SELECTING COLUMNS ONLY IF THEY HAVE VALUES

I need to frame a MySQL query in which there is a requirement to pick columns only if it has value (being NOT NULL ) out of the 4 columns given, first_accuracy , second_accuracy , third_accuracy , fourth_accuracy .

Suppose I have only values for first_accuracy , second_accuracy , So the query should be

SELECT first_accuracy, second_accuracy FROM compliance_employees CE;

UPDATE:

Query I applied:

SELECT first_accuracy, second_accuracy, third_accuracy, fourth_accuracy FROM compliance_employees CE;

Result:

在此处输入图片说明

You could see that for 'Third Accuracy' there is no value for any of the rows, So I don't want to select that column at all. Is that possible?

Any solution?

If you just want to make sure that the two columns first_accuracy and second_accuracy are not NULL then use this:

SELECT first_accuracy,
       second_accuracy
FROM compliance_employees CE
WHERE first_accuracy  IS NOT NULL AND
      second_accuracy IS NOT NULL

If you want to make sure that all four columns are not NULL then use this:

SELECT first_accuracy,
       second_accuracy
FROM compliance_employees CE
WHERE first_accuracy  IS NOT NULL AND
      second_accuracy IS NOT NULL AND
      third_accuracy  IS NOT NULL AND
      fourth_accuracy IS NOT NULL

Using 'UNION' ,GROUP_CONCAT() and 'not null' we find count of column which have values. then pass columns to Prepared statment which give you result whhich you want.

        select coalesce (GROUP_CONCAT(col_name),'No_Result') into @ColumnNames
        from (
        select 'first_accuracy' as col_name, count( distinct first_accuracy) as count from compliance_employees
        where first_accuracy  is not null 
        union
        select 'second_accuracy' as col_name, count( distinct second_accuracy) from compliance_employees
        where second_accuracy  is not null
        union
        select 'third_accuracy' as col_name, count( distinct third_accuracy) from compliance_employees
        where third_accuracy  is not null
        union
        select 'fourth_accuracy' as col_name, count( distinct fourth_accuracy) from compliance_employees
        where fourth_accuracy is not null
        )a where count >0\\
        -- select @ColumnNames\\


    SET @sql := CONCAT('SELECT ', @ColumnNames, ' FROM compliance_employees');
    set @sql := REPLACE(@sql,'No_Result','"No Column have values" as No_Result')\\

     PREPARE stmt FROM @sql;
     EXECUTE stmt;          

Check Live demo Here .

Output :

在此处输入图片说明

SELECT 
    first_accuracy, 
    second_accuracy
FROM
    compliance_employees CE
WHERE 
    first_accuracy IS NOT NULL
    AND second_accuracy IS NOT NULL

Did you mean to ask how to select the value out of four fields? If so, I suggest using GREATEST :

SELECT 
    GREATEST(first_accuracy, second_accuracy) as accuracy
FROM
    compliance_employees CE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM