如何从数据库中使用当前值减去旧值?

发布于 2025-02-09 09:30:09 字数 658 浏览 1 评论 0原文

我的问题是我有2张桌子。假设表旧表当前。这两个表都有列数量old.quantity是旧值,current.quantity是当前值。

表旧:

id  pro.no  Quantity
1    123        3

表当前:

id  pro.no  Quantity
1    123       2

sql:

SELECT A.`Quantity`, B.`Quantity`
FROM Table Current A 
LEFT JOIN Table Old B ON B.`pro.no` = A.`pro.no`
WHERE A.`id` = '1' 

所以​​,我想减去两个值以获取1的新值,以便在pro.no中的用户键textbox,它将向他们展示它们的新值Quantity is 1

我正在为后端使用vb.net,但我仍然对此语言使用。

my question is I have 2 tables. let say Table Old and Table Current. Both tables have columns Quantity. Old.Quantity is the old values, and Current.Quantity is the current values.

Table Old :

id  pro.no  Quantity
1    123        3

Table Current :

id  pro.no  Quantity
1    123       2

SQL :

SELECT A.`Quantity`, B.`Quantity`
FROM Table Current A 
LEFT JOIN Table Old B ON B.`pro.no` = A.`pro.no`
WHERE A.`id` = '1' 

So, I want to subtract both values to get the new values which is 1 So that, when user key in the pro.no into the textbox, it'll show them they new value for Quantity is 1.

I am using VB.NET for the backend, and I'm still new for this language too.

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

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

发布评论

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

评论(1

姐不稀罕 2025-02-16 09:30:10

您可以简单地减去这两个列,但是由于这是一个左联接,您需要照顾零值:

SELECT CASE
      WHEN o.quantity IS NULL THEN c.quantity
      ELSE c.quantity - o.quantity
    END
  FROM current c LEFT JOIN old o ON ... WHERE ... 

或替代

SELECT c.quantity - CASE WHEN o.quantity IS NULL THEN 0 ELSE o.quantity END
  FROM current c LEFT JOIN old o ON ... WHERE ... 

You can simply subtract both columns, however as this is a left join you'll need to care for the null values:

SELECT CASE
      WHEN o.quantity IS NULL THEN c.quantity
      ELSE c.quantity - o.quantity
    END
  FROM current c LEFT JOIN old o ON ... WHERE ... 

or alternatively

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