添加/减去以给出结果SQL编码 [英] Adding/subtracting to give result SQL coding

查看:53
本文介绍了添加/减去以给出结果SQL编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我向我解释如何从行中获取SQL中的int值,并生成一个更新字段的Total,而不必手动进行数学运算。或者看一个例子,这样我就能更好地理解MYSQL以及它如何工作





Can someone help explain to me how to take int values in SQL from rows and produce a Total that updates a field without me having to do the math manually. Or an example to look at so I can better understand MYSQL and how it would work


CREATE TABLE [dbo].[Table]
(
	[Id] INT NOT NULL PRIMARY KEY, 
    [Type] VARCHAR(50) NULL, 
    [MUTCD] VARCHAR(50) NULL, 
    [Sign] VARCHAR(50) NULL, 
    [Size] VARCHAR(50) NULL, 
    [Hi_Int] VARCHAR(50) NULL, 
    [Beginning_Inventory] INT NULL, 
    [Purchased_Inventory] INT NULL, 
    [Used_Inventory] INT NULL, 
    [Ending_Inventory] INT NULL
)





以下部分是我需要添加和减去的4个字段。

我需要Purchased_Inventory添加到总计,Used_Inventory从总计中减去。然后将结果发布到Ending_Inventory单元格。



The Section below is the 4 fields I need to add and subtract from.
I need Purchased_Inventory to Add to the total, Used_Inventory to Subtract from the total. Then the result to be posted to the Ending_Inventory cell.

[Beginning_Inventory] INT NULL, 
    [Purchased_Inventory] INT NULL, 
    [Used_Inventory] INT NULL, 
    [Ending_Inventory] INT NULL





Beggining Inventory = 30

已购买库存= 5

Used_Inventory = 15

TOTAL库存=结果(30 + 5-15 = 20)



我基本上需要我的SQL数据库代码才能做到这一点..







如果仍然不清楚请告诉我,我会尝试更好地改写它。 />


我尝试了什么:



我看过如何做的事情这样做,但我只是不确定。这个网站有更好的资源和知识,而且答案更容易理解。谢谢你的时间。



Beggining Inventory= 30
Purchased Inventory= 5
Used_Inventory= 15
TOTAL Inventory= result(30+5-15=20)

I basically need my SQL database code to do this..



If this is still unclear please let me know and I will try and rephrase it better.

What I have tried:

I have looked at stuff on how to do it but I'm just not sure. This website has better resources and knowledge plus the answers are easier to understand. Thanks for your time.

推荐答案

看看你手动完成了什么...

你已经取了<$ c的值$ c> Beginning_Inventory 并添加 Purchased_Inventory 的值,然后减去 Used_Inventory <的值br />


我想知道如果你在SELECT语句中这样做会发生什么......

Look at what you have done manually...
You've taken the value of Beginning_Inventory and added the value of Purchased_Inventory then subtracted the value of Used_Inventory

I wonder what would happen if you did that in a SELECT statement...
select Id, Beginning_Inventory, Purchased_Inventory, Used_Inventory, 
Beginning_Inventory + Purchased_Inventory - Used_Inventory as Total_Inventory
FROM myTable;





有一些MySQL如何在这个链接上工作的例子 - MySQL教程 - MySQL初学者示例 [ ^ ]





如果你想更新最后一列然后语法是



There are some examples of how MySQL works on this link - MySQL Tutorial - MySQL By Examples for Beginners[^]


If you want to update the last column then the syntax is

UPDATE myTable SET Ending_Inventory = Beginning_Inventory + Purchased_Inventory - Used_Inventory;

请注意,这不是一个好习惯 - 只需在需要时计算最终库存而不是存储无关信息

Note that this is not good practice - just calculate the final inventory when you need it rather than storing extraneous information


这篇关于添加/减去以给出结果SQL编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆