使用sql查询在ms access中添加两个差异列中的值

发布于 2024-11-25 22:57:12 字数 377 浏览 1 评论 0原文

嘿,我想知道您知道如何在查询的 SELECT 部分中使用“-”进行减法。那么也可以用“+”来添加吗?我已经尝试过,而不是将这些值加在一起,而是执行此操作 123+0.28=1230.28 这可能与文本格式的数字有什么关系吗?但从我使用“-”开始,我就没有改变过格式,而且这有效。谢谢

我的代码:

INSERT INTO Table( Question, Calculation)

SELECT DISTINCT 'Addition' AS Question,(T2.Calculation + T1.Calculation) AS Calculation

FROM Some_Table T2, Some_Table T1

ORDER BY T2.Question;

Hey I was wondering you know how its possible to use "-" to subtract in the SELECT part of a query. So can you also use "+" to add? I've tried that and instead of adding the values together it does this 123+0.28=1230.28 does that maybe have anything to do with the number being in text format? But I hadn't ever changed format from when i used "-" and that worked . Thanks

my code :

INSERT INTO Table( Question, Calculation)

SELECT DISTINCT 'Addition' AS Question,(T2.Calculation + T1.Calculation) AS Calculation

FROM Some_Table T2, Some_Table T1

ORDER BY T2.Question;

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

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

发布评论

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

评论(4

蔚蓝源自深海 2024-12-02 22:57:12

它可能将 + 解释为 a 和 b 之间的字符串连接。尝试“(a - 0) + (b - 0)”强制解释为数字。

It might be interpreting + as string concatenation between a and b. Try "(a - 0) + (b - 0)" to force interpretation as numbers.

或十年 2024-12-02 22:57:12

如果T2.Calculation和T1.Calculation是文本数据类型,则在相加之前使用Val()函数将它们转换为数字。

(Val(T2.Calculation) + Val(T1.Calculation)) AS Calculation

编辑
当您对两个文本值使用减号运算符时(如 "2" - "1"),Access 会将文本值转换为其等效的数字值(如果可能)。但是,如果任一文本值不代表有效数字,则减号运算符将给出“类型不匹配”错误...如 "2" - "hans"

所示工作方式不同 --- 对于两个文本值,它将尝试连接它们,就像您使用连接运算符 (&) 而不是加法运算符 (+) ... "2" + " 1" 将为您提供“21”作为文本值而不是数字 3。因此,在该特定情况下,"2" + "1" 相当于 "2" & “1”

加法和串联运算符之间的一个重要区别是其中一个值何时为 Null。 "2" + Null 产生 Null。但是 "2" & Null 产生“2”。

If T2.Calculation and T1.Calculation are text data type, use the Val() function to transform them to numbers before addition.

(Val(T2.Calculation) + Val(T1.Calculation)) AS Calculation

Edit:
When you use the minus operator with two text values (as in "2" - "1"), Access will transform the text values to their numerical equivalents, if possible. However, if either of the text values doesn't represent a valid number, the minus operator will give you a "Type mismatch" error ... as in "2" - "hans"

The plus operator works differently --- with two text values, it will attempt to concatenate them, same as if you'd used the concatenation operator (&) instead of the addition operator (+) ... "2" + "1" will give you "21" as a text value rather than the number 3. So, in that specific case, "2" + "1" is equivalent to "2" & "1".

An important distinction between the addition and concatenation operators is when one of the values is Null. "2" + Null yields Null. But "2" & Null yields "2".

温柔戏命师 2024-12-02 22:57:12

是的,您可以使用 '+' 将两个数字相加。

SELECT table1.Field1, table1.Field2, Field1+field2 As SumOfFields
FROM table1;

Field1  Field2  SumOfFields
1       2       3
2       3       5

编辑:
如果您有想要加在一起的字符串,那么您需要将字段转换为数字: - 因为有人指出 CLng 对OP没有帮助。它已更改为 CDbl 以允许小数。

SELECT table1.Field1, table1.Field2, CDbl(Field1)+CDbl(field2) As SumOfFields
FROM table1;

yes, you can use '+' to add two numbers together.

SELECT table1.Field1, table1.Field2, Field1+field2 As SumOfFields
FROM table1;

Field1  Field2  SumOfFields
1       2       3
2       3       5

EDIT:
If you have strings that you want to add together then you need to convert the fields to a number: - since it was pointed out that CLng wouldn't help the OP. It have been changed to CDbl to allow for the decimal.

SELECT table1.Field1, table1.Field2, CDbl(Field1)+CDbl(field2) As SumOfFields
FROM table1;
月亮坠入山谷 2024-12-02 22:57:12

只需在公式前面加上 0+,它就会知道您正在用数字而不是字符串进行计算:

[A]+[B]+[C] 改为 <代码>0+[A]+[B]+[C]

Just precede your formula with 0+ and it will know you're talking in numbers instead of strings:

Instead of [A]+[B]+[C] put 0+[A]+[B]+[C]

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