简体   繁体   English

当一列是文本而另一列是数字时,内部连接两个表

[英]Inner Join two tables when one column is a text and the other is number

I have a program in access that is using some linked ODBC tables.我有一个使用一些链接的 ODBC 表的访问程序。 I had originally had a query that contained the following INNER JOIN:我最初有一个包含以下 INNER JOIN 的查询:

FROM Neptune_FN_Analysis 
INNER JOIN Neptune_prem ON Neptune_FN_Analysis.PremID = Neptune_prem.premice_id 

This worked just fine until the column Neptune_prem.premice_id got changed from a number to a text data type.这工作得很好,直到列 Neptune_prem.premice_id 从数字更改为文本数据类型。 So now I need a way to use an INNER JOIN on two columns when one is a number and the other is a text.所以现在我需要一种方法,当一列是数字而另一列是文本时,可以在两列上使用 INNER JOIN。

Please keep in mind that I am not allowed to simply change the data type in the table that the linked ODBC tables are looking at.请记住,我不能简单地更改链接的 ODBC 表正在查看的表中的数据类型。

If this is impossible or a rediculous amount of code my other logical option would be to make a query to make a local table that I can edit with all of the same data in the Neptune_FN_Analysis table and in the code after that query edit the column I am joining with to type text.如果这是不可能的或可笑的代码量,我的另一个逻辑选项是进行查询以创建一个本地表,我可以使用 Neptune_FN_Analysis 表中的所有相同数据进行编辑,并在该查询之后的代码中编辑列我正在加入以输入文本。 I would prefer to just modify this one SQL query if it is reasonable though.如果合理,我宁愿只修改这个 SQL 查询。

First I'd track down who made the change and find out why.首先,我会追踪谁进行了更改并找出原因。 You may have bigger probklems than just getting numbers to match.您可能遇到的问题不仅仅是让数字匹配。 These changes aren;t made for no reason, if they changed form a numeric filed to atext filed, likely it is becasue they need to put text data into the field and now you won't be able to compare at all if you continue to use numerics.这些更改不是无缘无故的,如果它们从数字字段更改为文本字段,可能是因为他们需要将文本数据放入字段中,现在如果你继续这样做,你将根本无法进行比较使用数字。 Database changes need to consider what else might be affected and this one clearly didn't.数据库更改需要考虑其他可能会受到影响的因素,而这一点显然没有。 Find out who did it and why as the first step.作为第一步,找出是谁做的以及为什么这样做。

If you're talking about turning "500" into 500, check out Val , CDbl , CInt , CCur , CDec , and other conversion functions:如果您正在谈论将“500”变成 500,请查看ValCDblCIntCCurCDec和其他转换函数:

FROM Neptune_FN_Analysis 
INNER JOIN Neptune_prem ON Neptune_FN_Analysis.PremID = CInt(Neptune_prem.premice_id)

Have you tried using CAST('abc' AS varchar(5)) the numeric column in varchar?您是否尝试过在 varchar 中使用CAST('abc' AS varchar(5))数字列?

EDITED已编辑

You should use clng to cast the text as a number...您应该使用clng将文本转换为数字...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM