简体   繁体   English

SQL:将一个表中的 2 列与另一表中的 1 列进行比较

[英]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:您的构造有两个潜在的问题:

  1. str() tends to pad values with spaces. str()倾向于用空格填充值。
  2. 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我相信

  • The ID is numeric (int, probably). ID 是数字(可能是整数)。 str(A.ID) will usually put leading spaces before it, and when comparing strings, ' 5' is different from '5' . str(A.ID) 通常会在它前面放置前导空格,并且在比较字符串时, ' 5''5'不同。 Put an LTRIM in front of it eg, ltrim(str(A.ID))在它前面放一个 LTRIM,例如, 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.如果不这样做,则假定长度为 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 表中为IDaddress设置 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.

相关问题 将一个表的连接列与另一个表的一列组合成 JAVA object - combine joined columns of a table with one column of another table into JAVA object SQL将一个表的多条记录合并成另一表的多列 - SQL combine multiple records of one table into multiple columns of another table 如何将两个表列合并到一个表中与SQL中的所选列 - how to combine two table columns into one table with selected column in SQL 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? SQL 比较两列的查询,其中一列等于另一个表中的列,而第二列不等于 t2 中的第二列 - SQL Query to compare two columns with one column equal to a column in another table and second column is not equal to the second column from t2 SQL:通过联接将一个表中的列链接到另一表中的不同列 - SQL: link a column in one table to different columns in another table with joining sql两个列的一个表引用另一个表中的同一列 - sql two columns of one table reference same column in another table 将SQL表列名称与另一个表列数据进行比较以查找缺少的列 - Compare a SQL table column names with another table column data for missing columns 在SQL中将表的两列合并为一列 - Combine two columns of table into one in SQL Oracle SQL 合并一张表中的列 - Oracle SQL combine columns from one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM