SQL-列相乘时不返回值

发布于 2024-08-12 15:11:22 字数 238 浏览 2 评论 0原文

我的数据库表中有 3 列,我需要将列、itm_count 和 itm_count 相乘。 itm_price 并输出给定​​ id(itm_id) 的总计。

SELECT sum(itm_price * itm_count) as total FROM ods_temporder WHERE itm_id='11'

我尝试使用上面的 sql 查询来执行此操作,但结果为空。这里似乎有什么问题?

I have 3 columns in my db table, I need to multiply column, itm_count & itm_price and output the total for a given id(itm_id).

SELECT sum(itm_price * itm_count) as total FROM ods_temporder WHERE itm_id='11'

I tried to do this using the above sql query, but the result was null. What seems to be the issue here?

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

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

发布评论

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

评论(3

小嗷兮 2024-08-19 15:11:22

这给你带来什么?

SELECT itm_price, itm_count FROM ods_temporder WHERE itm_id='11'

itm_priceitm_count 是否为 NULL?您有 itm_id = 11 的行吗?

What do this give you?

SELECT itm_price, itm_count FROM ods_temporder WHERE itm_id='11'

Is either of itm_price or itm_count NULL? Do you have rows for itm_id = 11?

没有你我更好 2024-08-19 15:11:22

尝试:

SELECT SUM(COALESCE(itm_price, 0) * COALESCE(itm_count, 0)) as total 
  FROM ods_temporder 
 WHERE itm_id='11'

COALESCE 是处理 NULL 值的 ANSI 标准 - 如果 itm_priceitm_count 为 null,则在这种情况下将使用零作为值。否则,要么 itm_id 列中没有值为 11 的行,要么您在错误的列中查找 11 值。

Try:

SELECT SUM(COALESCE(itm_price, 0) * COALESCE(itm_count, 0)) as total 
  FROM ods_temporder 
 WHERE itm_id='11'

COALESCE is an ANSI standard for dealing with NULL values - if itm_price or itm_count is null, zero will be used in this case as the value. Otherwise, either you don't have a row with a value of 11 for the itm_id column, or you're looking for the 11 value in the wrong column.

我很OK 2024-08-19 15:11:22

当itm_id=11时itm_price和itm_count包含什么?
这是 SQL Server 吗?如果是这样,您可以使用 ISNULL 来处理 itm_price 或 itm_count 是否为 null

What does itm_price and itm_count contain when itm_id= 11?
Is this SQL Server? If so you can use ISNULL to handle whether itm_price or itm_count is null

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