MySQL 和产品交叉销售 (XSell)

标签 mysql sql csv

我正在使用的表:

       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

它返回:

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

我希望能够读取 csv 文件。 本质上是创建某种“交叉联接”来构建所有“反向”交叉销售,然后从给定的 sku 中查找关联的产品 ID,并将它们插入到 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 表中以建立交叉销售和反向交叉销售关系:

     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     |
|------------|-------------|        

感谢您的帮助。

最佳答案

假设您有 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 的值

关于MySQL 和产品交叉销售 (XSell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993682/

相关文章:

Java:从 MySQL 检索记录时结果集为空

php - 如何在 mysql + codeigniter 中按开始时间/结束时间过滤记录

sql - 调度数据库(基本调度+异常(exception))

Python:BeautifulSoup Findall 跳到下一个标签

mysql - 提高sql查询性能

mysql - 为什么这个 MySQL 查询没有正确使用索引?

java - 在 Java 中读取制表符分隔的文件

ruby - 在 CSV 行中写入和读取数组数据

php - PHP 中的 MySQL 查询 - 不正确?

sql - 在 PostgreSql 中获取 "number of referencing and referenced columns for foreign key disagree"