2015-01-21 65 views
0

表1具有(與行作爲)等的PostgreSQL ---通過用表進行比較在表1中添加新的列2

date   product  comment 
12.01.2014 1201/sm/pb  yes 
13.01.2014 1202/sa/pa  no 
14.01.2014 1215/ja/pc  yes 

表2具有列(與行作爲)等

certificate name 
1201   pencil 
1202   pen 
1215   parker 

我想在表中添加一列(名)1

date   product  comment name 
12.01.2014 1201/sm/pb  yes  pencil 
13.01.2014 1202/sa/pa  no  pen 
14.01.2014 1215/ja/pc  yes  parker 

有一個人請告訴我如何我可以加入哪些行應滿足一列條件(product.table1 = certificate.table2 ==>在表1名)

感謝ü

回答

2

你需要連接表上product列的前綴。

select t1.date, t1.product, t1.comment, t2.name 
from table_1 t1 
    joint table_2 t2 on left(t1.product, strpos(t1.product,'/') - 1) = t2.certificate; 
+0

非常感謝....它在表1的工作 – Chanti 2015-01-21 12:34:03

+0

我有20,000個警報對象(行)......上述操作後,我能夠只提取18500對象。 – Chanti 2015-01-21 12:50:55

+0

可以請你告訴我任何命令如何在上述操作後找到丟失的對象(行) – Chanti 2015-01-21 12:51:51