2013-09-24 62 views
0

表:MySQL和產品的交叉銷售,我有工作(XSell)

 products table 
|------------|-------------| 
| product_id | product_sku | 
|------------|-------------| 
|  2345 | G54321 | 
|------------|-------------| 
|  2346 | G64321 | 
|------------|-------------| 

    products_xsell 
|----|------------|-------------| 
| ID | product_id | xsell_id | 
|----|------------|-------------| 
| 3 | 2345 | 2346  | 
|----|------------|-------------| 
| 4 | 2346 | 2345  | 
|----|------------|-------------| 

我收到以下格式的CSV:

SKU, Related_Items 
2345,"2346,2347,2349" 
112,"4840,4841,4844,4890,4891" 

我能夠成功查詢SKU是基於就在XSELL表具有以下數據:

Select 
    products.products_model As 'Displayed Product', 
    products1.products_model As 'Related Item' 
From 
    products_xsell Inner Join 
    products On products_xsell.products_id = products.products_id Inner Join 
    products products1 On products_xsell.xsell_id = products1.products_id 

,並返回:

|-------------------|----------------| 
| Displayed Product | Related Item | 
|-------------------|----------------| 
|  G54321  | G64321  | 
|-------------------|----------------| 
|  G64321  | G54321  | 
|-------------------|----------------| 

我期待能夠閱讀csv文件。 本質上創建一個'交叉連接'的各種建立所有'反向'交叉銷售,然後查找相關的產品ID從給定的SKU和插入到products_xsell表。

例如,如果該文件有SKU的一行:

2345,"2346,2347,2349" 

我想打造出以下幾點:

2345,2346 
2345,2347 
2345,2349 
2346,2345 
2346,2347 
2346,2349 
2347,2345 
2347,2346 
2347,2349 
2349,2345 
2349,2346 
2349,2347 

從以上的SKU獲取相關產品的ID:

54321,64321 
54321,49245 
54321,99499 
64321,54321 
64321,49245 
64321,99499 
49245,54321 
49245,64321 
49245,99499 
99499,54321 
99499,49245 
99499,64321 

並將其插入到products_xsell表建立交叉銷售和反向交叉銷售relationsh ips:

 products_xsell 
|------------|-------------| 
| product_id | xsell_id | 
|------------|-------------| 
|  54321 | 64321  | 
|------------|-------------| 
|  54321 | 49245  | 
|------------|-------------| 
|  54321 | 99499  | 
|------------|-------------| 
|  64321 | 54321  | 
|------------|-------------| 
|  64321 | 49245  | 
|------------|-------------| 
|  64321 | 99499  | 
|------------|-------------| 
|  49245 | 54321  | 
|------------|-------------| 
|  49245 | 64321  | 
|------------|-------------| 
|  49245 | 99499  | 
|------------|-------------| 
|  99499 | 54321  | 
|------------|-------------| 
|  99499 | 64321  | 
|------------|-------------| 
|  99499 | 49245  | 
|------------|-------------|   

任何幫助表示讚賞。

+0

這聽起來像你有你想要做一個清晰的思路。你爲實現這個目標做了什麼?你在哪裏遇到問題? –

+0

我認爲這可以用輔助數字表很容易地完成。在這裏閱讀:http://dba.stackexchange.com/questions/11506/why-are-numbers-tables-invaluable – cha

+0

@MikeBrant我能讀取的CSV只是罰款和分配他們的變量,但我堅持在在SQL中構建sku數組。特別是如果一個有3個相關產品,另一個有8個。if空邏輯和陣列構建是我現在正在困難的時刻。 – Limitless

回答

0

假設你有Numbers表格和CSV表稱爲CSV,查詢select這樣輕鬆創建:

select c.sku, n.n from csv c INNER JOIN Numbers n ON 
INSTR(CONCAT(',', c.Related_Items, ','), CONCAT(',', CAST(n.n AS CHAR), ',')) > 0 
UNION ALL 
select n.n, c.sku from csv c INNER JOIN Numbers n ON 
INSTR(CONCAT(',', c.Related_Items, ','), CONCAT(',', CAST(n.n AS CHAR), ',')) > 0 

但是,你需要的是填充了值從1到最大SKU輔助號碼錶可能在您的系統中。

檢查這個SQL Fiddle與填充了從2345的值的數字爲例,2349