Reputation: 33
Please help with following if
statement in MySQL 5.5. I am trying to compare column values to see if they are all equal or different.
SELECT IF(Column1 = Column2 = Column3 = Column4 = Column5, 'SAME', 'Different')
AS ValueStatus
FROM dbs.tabletest
GROUP BY Id
I have also tried the following, however, it only brings one type result as "Different" even when all columns have the same values.
SELECT *,CASE
WHEN Column1 = Column2 = Column3 = Column4 = Column5
Then 'Same'
ELSE 'Different'
END
AS ValueStatus
FROM dbs.tabletest
GROUP BY Id;
Upvotes: 0
Views: 5203
Reputation: 2125
Michael's approch is right. i would use as following in MSSQL. not sure syntex in MySql. select Case WHEN ((column1 = column2) AND (column1 = column3) and (column1 = column4) and (column1 = column5) ) then 'Same' ELSE 'Different' end as 'ValueStatus' FROM tableName
Upvotes: 0
Reputation: 143249
Should be column1 = column2 and column1 = column3 and column1 = column4 and column1 = column5
instead.
What happened to you is probably you compared column1
to column2
, which returned true
, then you compared true
to column3
, which yields false
, compare false
to column4
, which also evaluates to false
, etc.
Upvotes: 3