MySQL和产品交叉销售(XSell)
我正在使用的表:MySQL和产品交叉销售(XSell),mysql,sql,csv,Mysql,Sql,Csv,我正在使用的表: products table |------------|-------------| | product_id | product_sku | |------------|-------------| | 2345 | G54321 | |------------|-------------| | 2346 | G64321 | |------------|-------------| products_x
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"
我能够根据XSELL表中的数据通过以下方式成功查询SKU:
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
2345,2346
2345,2347
2345,2349
2346,2345
2346,2347
2346,2349
2347,2345
2347,2346
2347,2349
2349,2345
2349,2346
2349,2347
它返回:
|-------------------|----------------|
| Displayed Product | Related Item |
|-------------------|----------------|
| G54321 | G64321 |
|-------------------|----------------|
| G64321 | G54321 |
|-------------------|----------------|
我希望能够在csv文件中读取。
本质上,创建一个“交叉连接”排序,以构建所有“反向”交叉销售,然后从给定sku中查找关联的产品id,并将其插入products_xsell表中
例如,如果文件中有一行sku:
2345,"2346,2347,2349"
我想提出以下几点:
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
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
并将其插入Productsxsell表中,以建立交叉销售和反向交叉销售关系:
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 |
|------------|-------------|
非常感谢您的帮助。假设您有数字表,并且csv表被称为csv,则可以像下面这样轻松创建查询选择:
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
但是您需要helpernumbers表,该表中填充了从1到系统中可能的最大SKU的值
以2345到2349之间的值填充的数字为例,检查此示例。听起来您对要执行的操作有了明确的想法。您为实现这一目标做了哪些工作?你在哪里遇到了问题?我认为这可以通过一个helpernumbers表轻松完成。阅读这里:@MikeBrant我能够很好地读入csv并为它们分配变量,但我在SQL中构建sku数组时遇到了困难。特别是如果一个有3个相关产品,另一个有8个。if-null逻辑和数组构建是我现在很难理解的地方。@cha我必须反复阅读关于数字表的文章,看看我是否能理解这些概念。