 <p>You should implement this in your SQL using a case statement.</p> <p><a href="https://dev.mysql.com/doc/refman/5.7/en/case.html" rel="nofollow">https://dev.mysql.com/doc/refman/5.7/en/case.html</a></p> <p>The above answers using the IF() function are correct, but in my opinion it is better to use the CASE-WHEN so as to match other database engines like Microsoft SQL Server and Oracle.</p>