简体   繁体   English

使用concat列联接表

[英]Joining tables using concat column

So I have two tables I want to join using SQL. 所以我有两个要使用SQL连接的表。

在此处输入图片说明

Since they did not have a common column I used 由于他们没有共同的专栏,所以我使用了

SELECT NEW_ID = CONCAT ('0',table1.ID)

Now that I have the new column with matching data in both tables, how do I join both tables? 现在,我有了两个表中都具有匹配数据的新列,如何连接两个表? Is there any way to use the NEW_ID column as a temporary column so that I do not have to alter table 1? 有什么方法可以将NEW_ID列用作临时列,从而不必更改表1?

In your case, suitable. 根据您的情况,合适。 of course in terms of performance it is not the best solution ( compare fields with diffrent types) 当然,就性能而言,它并不是最佳解决方案(比较具有不同类型的字段)

Select * 
From Table1 As t1 inner join Table2 as t2
     ON t1.ID = CAST(t2.ID AS INT)

You Can Just give the Concrete Expression in the Join.You Don't have to add a new Column For that. 您只需在联接中给出具体表达式即可,而不必为此添加新的列。 Like this 像这样

SELECT
  *
   FROM Table1 A
       INNER JOIN Table2 B
          ON RIGHT('00'+A.Id,2) = RIGHT('00'+B.Id,2)

Different ways, here's one: 不同的方式,这是一种:

 ;WITH CTE AS 
 (
 SELECT NEW_ID = CONCAT ('0',table1.ID) FROM TableA
 )

 SELECT * FROM CTE AS C INNER JOIN TableB AS B ON C.New_ID = B.ID

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

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