2016-02-26 67 views
2

我有兩個表創建一個SELECT:MySQL的 - 如何更新後LEFT OUTER JOIN

SELECT p.products_id, p.gender, f.feature_set_id, f.products_id 
FROM products_item_codes AS p 
LEFT OUTER JOIN feature_set_to_products AS f ON p.products_id = f.products_id 

products_id gender feature_set_id products_íd 
     1235 Damen    7  1235 
     1236 Damen    7  1236 
     1237     NULL  NULL 
     1238 Herren   NULL  NULL 
     1239 Herren   NULL  NULL 
     1240 Damen    7  1240 

我怎樣才能更新feature_set_id和products_id?

  • 性別= '赫倫',feature_set_id = '6'
  • 性別= '達門',feature_set_id = '7'

的NULL-數據集犯規存在,我將創建它們。

+0

你不能在同一時間做出最後幾天與不同的價值觀與condiction – bhrached

+0

相同的兩個領域難道是更新或插入? – Shadow

+0

我認爲是一個插入 –

回答

0

我會用一個insert ... select ...語句來填滿feature_set_to_products表。 case語句可以根據性別來處理要插入的相應feature_set_id。

insert into feature_set_to_products (feature_set_id, products_id) 
select case when p.gender='Damen' then 7 when p.gender='Herren' then 6 else null end, p.products_id 
from products_item_codes p 
left join feature_set_to_products AS f ON p.products_id = f.products_id 
where p.gender in ('Damen', 'Herren') and f.products_id is null 
0

作爲@Shadow評論,您必須INSERT,因爲行不存在。

然而,由於要一次因爲genderproducts_item_codes,而feature_set_idfeature_set_to_products設置在2個表中的值,你將不得不使用至少 2個插入查詢。 您應該使用事務,或在應用層面做插入(即PHP,Perl或任何你使用的語言)

+0

我只會在'feature_set_to_products'表中設置新值。 –