how can I write IF statement in mysql

if-statement, mysql, sql

Solution

You can create condition for every row, `MySQL` supports inline `IF` statements.

SELECT billing.*,
       IF(billing.type = 'A', billing.amount, '-') AS bes,
       IF(billing.type = 'A', '-', billing.amount) AS bed
FROM   billing

Problem

I have a table with `type` field. how can I check this field in query: I want to check it if `type` == 'a' then `bes`=`amount`,`bed` = '-' ELSE `bed`=`amount`,`bes` = '-' bes and bed is not really exists in my table but amount is (int) this is my try: ``` SELECT billing.*, CASE billing.type WHEN 'A' THEN billing.amount AS bes, '-' AS bed ELSE billing.amount AS bed, '-' AS bes END FROM billing ``` ... my searches wasn't useful any solution...

Original source