我想检查表中的列是否具有相同的值。
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 来自 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
参见 demo在 SQLFiddle 中,如果您想做一些聚合操作,像 id
这样的非聚合列在 select 中将毫无意义。
关于php - 如果它具有相同的值,如何检查一行中的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45025048/