使用内部联接加入2个具有不同名称的表

时间:2016-11-22 04:14:24

标签: sql sql-server-2008

我有2个表首先是

之前

lotname     
--------    
R16A        
R17A        
R17B        
R18A        
R18B        

第二是

之后

lotname     
--------        
R16AA       
R17A        
R17B        
R18AAA      
R18B        

我想加入这两个表,以便像R16A匹配的所有内容都应与R16AA匹配

如果我写这段代码

select * from before
join after
    on before.lotname = after.lotname

O / P

lotname     lotname     
R17A        R17A        
R17B        R17B        
R18B        R18B        

我想要o / p喜欢

Lotname     Lotname     
----------------------------                
R16A        R16AA       
R17A        R17A        
R17B        R17B        
R18A        R18AAA      
R18B        R18B        

3 个答案:

答案 0 :(得分:5)

SELECT *
FROM before
INNER JOIN after
    ON after.lotname LIKE before.lotname + '%'

答案 1 :(得分:0)

[ re.sub(r'(' +  '|'.join(list_1) + ')\d+', r'\1', string) for string in list_2 ] 

答案 2 :(得分:0)

SELECT * FROM before
INNER JOIN after
ON after.lotname LIKE CONCAT(before.lotname,'%')