我怎样才能在mysql中编写IF语句

Poo*_*oya 5 mysql sql if-statement

我有一张有田地的桌子type.

如何在查询中检查此字段:

如果type=='a',我想检查一下

那么bes= amount,bed=' - '

ELSE bed= amount,bes=' - '

bes和bed在我的表中并不存在但是数量是(int)

这是我的尝试:

SELECT billing.*,
    CASE billing.type WHEN 'A' THEN billing.amount AS bes, '-' AS bed ELSE billing.amount AS bed, '-' AS bes END
    FROM billing
Run Code Online (Sandbox Code Playgroud)

...我的搜索无用

解决方案......

Joh*_*Woo 5

您可以为每一行创建条件,MySQL支持内联 IF语句.

SELECT billing.*,
       IF(billing.type = 'A', billing.amount, '-') AS bes,
       IF(billing.type = 'A', '-', billing.amount) AS bed
FROM   billing
Run Code Online (Sandbox Code Playgroud)