【问题标题】:Show a text depending on the value of a column MySQL根据 MySQL 列的值显示文本
【发布时间】:2020-07-08 17:47:18
【问题描述】:

我想知道,如何构建一个查询来显示一个额外的列,而不是显示原始列的真实值,而是显示我想要的文本?

例如,列,在本例中为“inputOutput”,保存值 0 或 1,我想在查询中显示,如果值为 0,我可以看到文本“输出”,如果它为 1 在所有行中显示“输入”。

你可以使用什么功能或结构?

这只是我想展示的列的一个示例:

Select idInventory, date, "text that that replaces 0 or 1 from inputOtput column" 
from inventory;

【问题讨论】:

    标签: php mysql sql select case


    【解决方案1】:

    您可以为此使用case 表达式:

    select 
        idInventory, 
        date, 
        case inputOutput 
            when 0 then 'Output'
            when 1 then 'Input'
            else '???'
        end status
    from inventory;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 2017-03-30
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多