sql用null或文本替换零0值

发布于 2025-01-21 00:40:11 字数 688 浏览 2 评论 0原文

我有一张产品表
某些 ProductPrice 值为 0(类型为十进制(10,0))

ID   | Productname |      ProductPrice                    |
+----+-------------+--------------------------------------+
|  1 | ShirtXS     |      299                             |
|  2 | TrousersM   |        0                             |

ProductPrice 为 DECIMAL(10,0)。
如何编写 SQL 查询来将 ProductPrice 值 0 转换/替换为 NULL 或文本 N/A?
期望的输出:

ID   | Productname |      ProductPrice                    |
+----+-------------+--------------------------------------+
|  1 | ShirtXS     |      299                             |
|  2 | TrousersM   |       NULL or N/A                    |

I have a table Products
Some ProductPrice values are 0 (type is decimal(10,0))

ID   | Productname |      ProductPrice                    |
+----+-------------+--------------------------------------+
|  1 | ShirtXS     |      299                             |
|  2 | TrousersM   |        0                             |

ProductPrice is DECIMAL(10,0).
How to write SQL query to convert/replace ProductPrice value 0 with NULL or text N/A?
Desired output:

ID   | Productname |      ProductPrice                    |
+----+-------------+--------------------------------------+
|  1 | ShirtXS     |      299                             |
|  2 | TrousersM   |       NULL or N/A                    |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2025-01-28 00:40:11

我不确定您是否正在寻找更新选择。如果您正在寻找选择可以从mysql使用nullif()以替换0null。语法如下:

SELECT *,NULLIF(<column_name>,0) as <variable_name> from <table_name>;

I'm not sure if you are looking to UPDATE or SELECT. If you are looking for SELECT you can use NULLIF() from MySQL to replace 0 with NULL. The syntax is below:

SELECT *,NULLIF(<column_name>,0) as <variable_name> from <table_name>;
野の 2025-01-28 00:40:11

为此使用 case 表达式:

SELECT Productname ,
       CASE WHEN ProductPrice = 0 THEN NULL
            ELSE ProductPrice
       END as ProductPrice
  FROM table

Use case expression for this:

SELECT Productname ,
       CASE WHEN ProductPrice = 0 THEN NULL
            ELSE ProductPrice
       END as ProductPrice
  FROM table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文