[英]SQL: combine 2 columns in one table to compare with 1 column in another table
I have 2 tables in MSSQL one is called [DATA] and the other one is called [2020]我在 MSSQL 中有 2 个表,一个称为 [DATA],另一个称为 [2020]
IN [2020] I have user info for example: name , ID, adress, and so on. IN [2020] 我有用户信息,例如:姓名、ID、地址等。 IN [DATA] I have multiple columns that I need to show about the users
在 [DATA] 我有多个列需要显示有关用户的信息
the catch is that I have no common column between those tables, in [2020] I have ID and adress, in [DATA] I have that info in one column called ID-adress问题是我在这些表之间没有共同的列,在 [2020] 中,我有 ID 和地址,在 [DATA] 中,我在名为 ID-adress 的列中有该信息
SO I tried this所以我试过这个
SELECT
A.ID,
A.Nro,
A.name,
A.adress,
B.CGE AS 'GE',
B.CGG as 'GG',
B.CPE AS 'PE'
FROM
[202009] A
LEFT JOIN [DATA] B ON str(A.ID )+'-'+cast(A.adress as varchar) = B.[ID-adress]
But I get NULL on the columns that I want to show on B ¿what I'm doing wrong?但是我想在 B 上显示的列上得到 NULL ¿我做错了什么?
From your rather sparse description, I would guess that the JOIN
condition never evaluates to true.从您相当稀疏的描述中,我猜想
JOIN
条件永远不会评估为真。
Based on the naming of the columns, you may want:根据列的命名,您可能需要:
FROM [202009] A LEFT JOIN
[DATA] B
ON CONCAT(A.ID, '-', A.adress) = B.[ID-adress]
This is speculation based on a reasonable interpretation of the question.这是基于对问题的合理解释的推测。
There are two potential problems with your construct:您的构造有两个潜在的问题:
str()
tends to pad values with spaces. str()
倾向于用空格填充值。varchar
without a length has a default length depending on context that may not be sufficient for what you want to do.varchar
具有默认长度,具体取决于可能不足以满足您想要执行的操作的上下文。以下是调试建议:在 SELECT 中添加表达式( str(A.ID )+'-'+cast(A.adress as varchar)
)以查看结果是否符合您的预期。
Your immediate issue, I believe, is that your string functions str(A.ID )+'-'+cast(A.adress as varchar)
have several problems.我相信,您的直接问题是您的字符串函数
str(A.ID )+'-'+cast(A.adress as varchar)
有几个问题。
I suggest just putting that SQL fragment into a SELECT to see what you get (eg,我建议将该 SQL 片段放入 SELECT 以查看您得到的内容(例如,
SELECT A.ID, A.adress, str(A.ID )+'-'+cast(A.adress as varchar)
FROM [202009] A
to see what results you get.看看你得到了什么结果。
I believe我相信
' 5'
is different from '5'
. ' 5'
与'5'
不同。 Put an LTRIM in front of it eg, ltrim(str(A.ID))
ltrim(str(A.ID))
cast(A.adress as varchar)
needs a length of the varchar eg, cast(A.adress as varchar(100)). cast(A.adress as varchar)
需要cast(A.adress as varchar)
的长度,例如 cast(A.adress as varchar(100))。 If you don't do this, it assumes length 1. To check, run a similar statement as above, but with fixes要检查,请运行与上述类似的语句,但有修复
SELECT A.ID, A.adress, ltrim(str(A.ID))+'-'+cast(A.adress as varchar(100))
FROM [202009] A
However (and I don't like to say this), but this design is ... problematic.然而(我不喜欢这么说),但这种设计是......有问题的。 For example, What happens when a user changes their address?
例如,当用户更改地址时会发生什么? All fields in the data table will be unusable.
数据表中的所有字段都将不可用。
The biggest-bang-for-buck fix (imo) is to have 2 separate fields in the Data table for ID
and address
.最大的解决方案 (imo) 是在 Data 表中为
ID
和address
设置 2 个单独的字段。 If needed, you can still keep your single field as well (ID-address).如果需要,您仍然可以保留您的单个字段(ID 地址)。
As you say, make sure they have common columns.正如你所说,确保它们有共同的列。
Once you have the two fields, you can join them on fields that at least match to get the relevant data rows for that address eg,拥有这两个字段后,您可以将它们加入至少匹配的字段以获取该地址的相关数据行,例如,
SELECT
A.ID,
A.Nro,
A.name,
A.adress,
B.CGE AS 'GE',
B.CGG as 'GG',
B.CPE AS 'PE'
FROM
[202009] A
LEFT JOIN [DATA] B ON A.ID = B.ID AND A.adress = B.adress
And then, if you just want all the data rows for a given user, you can do the same thing but do not join on address (just A.ID = B.ID).然后,如果你只想要给定用户的所有数据行,你可以做同样的事情,但不要加入地址(只是 A.ID = B.ID)。
Note that this won't fix all the issues (eg, when an address in one table is different from the other table because of things like spaces, or 'St' vs 'Street') - but will be a good start.请注意,这不会解决所有问题(例如,当一个表中的地址因空格或“St”与“Street”之类的原因而与另一表不同时) - 但将是一个好的开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.