![](/img/trans.png)
[英](PHP) How to check column A and column B's value that has same ID simultaneously
[英]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.