MySQL - 只有在它们有价值的情况下选择列

时间:2017-01-06 04:30:17

标签: mysql

我需要构建一个MySQL查询框架,在该查询中,只有在给定的4列first_accuracy,{second_accuracy中有值( NOT NULL )时才需要选择列。 {1}},third_accuracyfourth_accuracy

假设我只有 first_accuracy second_accuracy 的值,那么查询应该是

  

SELECT first_accuracy,second_accuracy FROM compliance_employees CE;

更新

我申请的查询:

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

结果:

enter image description here

您可以看到“第三准确度”'任何行都没有价值,所以我根本不想选择该列。这可能吗?

任何解决方案?

4 个答案:

答案 0 :(得分:2)

如果您只是想确保first_accuracysecond_accuracy两列不是NULL,请使用此列:

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

如果您想确保所有四列不是NULL,请使用此列:

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

答案 1 :(得分:2)

使用' UNION' ,GROUP_CONCAT()和' not null'我们找到具有值的列数。 然后将列传递给Prepared statment,它会为您提供所需的结果。

new Notification("Title")

检查实时演示Here

  

输出:

enter image description here

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您是否想要询问如何从四个字段中选择值?如果是这样,我建议使用input

GREATEST