MySQL if field equals X show it as Y

问题: I'm pretty new here and to MySQL. I tried googling the question but found only SELECT column FROM table AS t statements, which I believe it is not what I need. I have a c...

问题:

I'm pretty new here and to MySQL. I tried googling the question but found only SELECT column FROM table AS t statements, which I believe it is not what I need.

I have a column named "active" in a table named "products". it's optional values are -1, 0, 1.

I am using SELECT active FROM products. It shows the table as it is but I want to make it show 0 where the value equals -1 without changing the original value.

Can this be done in MySQL?


回答1:

You could use CASE:

SELECT *,CASE WHEN active = -1 THEN 0
               ELSE active
        END active_2
FROM products;

or IF:

SELECT *, IF(active=-1, 0, active) AS active_2
FROM products;
  • 发表于 2018-07-05 15:29
  • 阅读 ( 290 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除