繁体   English   中英

如何检查一列是否具有相同的值?

[英]How to check a column in a row if it has a same value?

我想检查表中的列是否具有相同的值。

ID    School_Name     Student    Status
1        Virginia        Alex         2
2        Virginia        John         2
3       Wonderbow        Devi         1
4     San Antonio       Lucas         1
5     San Antonio       Larsa         2

如果每个School_Name的值为2 ,如何检查列status 输出是这样的:

ID       School_Name    school_stat
 1          Virginia      TRUE      
 2         Wonderbow     FALSE
 3       San Antonio     FALSE

因此,如果每个学校名称中的所有学生的状态均为2,则结果为TRUE。

当前查询:

SELECT *,SUM(case when status > 1 then TRUE else FALSE end) as school_stat from t_school

查询给出了错误的输出。

您可以使用条件聚合:

SELECT
    School_Name,
    IF(COUNT(1) = COUNT(IF(Status = 2, 1, NULL)), 'TRUE', 'FALSE') school_stat
FROM t_school
GROUP BY School_Name

请参阅SQLFiddle中的demo ,如果您想做一些聚合的事情,则非聚合列(如id在select中将毫无意义。

使用group by子句对您的school_name进行分组。

例,

SELECT ID,School_Name,(case when status > 1 then 'TRUE' else 'FALSE' end) as school_stat from temp1
group by School_Name
order by ID asc;

参见演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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