2012-12-07 16 views
0

加入我有以下表格:SQL要求:得到共同的圖案列從兩個表

表1:

col: 
20kasi 
30kasi 
40eswar 
00eswar 

表2:

col: 
00kasi 
05kasi 
06kasi 
03eswar 
44eswar 

我想加入這兩個由自己通過忽略數字的名稱,以便我可以獲得其他列。

select {something from each tables} 
    from table1, table2 
where t1.col1 = t2.col2 

它應該顯示:

col    {other colums from one of tables after join} 

kasi   ----   ----- \n 
eswar   ----   ----- 

這是不是有正確的語法。但我想要這樣。有什麼建議麼??

+0

你很可能忘了「名稱別名」你的表1和2(如果它是像你說的上述) – Najzero

回答

0

如果所有字符串恰好有兩位數字,你可以這樣做:

SELECT * 
FROM table1 inner join table2 
    on SUBSTRING(table1.col, 3)=SUBSTRING(table2.col, 3) 

如果位數可以改變,或者如果他們在不同的崗位,因爲MySQL不支持正則表達式,你可以使用的東西像這樣:

SELECT * 
FROM table1 inner join table2 
    on 
     Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(table1.col,'9',''),'8',''),'7',''),'6',''),'5',''),'4',''),'3',''),'2',''),'1',''),'0','') = 
     Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(table2.col,'9',''),'8',''),'7',''),'6',''),'5',''),'4',''),'3',''),'2',''),'1',''),'0','') 

(它看起來並不很漂亮,但它應該工作)

0

你總是前兩個字符爲數字?如果是,那麼這將工作

select {something from each tables} 
    from table1 t1, table2 t2 
where substr(t1.col1,3,length(t1.col1)) = substr(t2.col2,3,length(t2.col2)) 
相關問題