[英]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
. 我需要构建一个MySQL查询,其中仅当它具有给定的4个列
first_accuracy
, second_accuracy
, third_accuracy
, fourth_accuracy
具有值(不是NOT NULL )时才需要选择列。
Suppose I have only values for first_accuracy , second_accuracy , So the query should be 假设我只有first_accuracy , second_accuracy的值,那么查询应该是
SELECT first_accuracy, second_accuracy FROM compliance_employees CE;
从Compliance_employees CE中选择first_accuracy,second_accuracy。
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: 如果您只想确保两列
first_accuracy
和second_accuracy
不为NULL
使用以下命令:
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: 如果要确保所有四个列都不为
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
Using 'UNION' ,GROUP_CONCAT() and 'not null' we find count of column which have values. 使用'UNION',GROUP_CONCAT()和'not null',我们找到具有值的列数。 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 . 在此处检查Live demo。
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
: 如果是这样,我建议使用
GREATEST
:
SELECT
GREATEST(first_accuracy, second_accuracy) as accuracy
FROM
compliance_employees CE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.