使用sql查询在ms access中添加两个差异列中的值
嘿,我想知道您知道如何在查询的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它可能将 + 解释为 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.
如果T2.Calculation和T1.Calculation是文本数据类型,则在相加之前使用Val()函数将它们转换为数字。
编辑:
当您对两个文本值使用减号运算符时(如
"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.
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".是的,您可以使用
'+'
将两个数字相加。编辑:
如果您有想要加在一起的字符串,那么您需要将字段转换为数字: - 因为有人指出 CLng 对OP没有帮助。它已更改为 CDbl 以允许小数。
yes, you can use
'+'
to add two numbers together.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.
只需在公式前面加上
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]
put0+[A]+[B]+[C]