我有一个有点复杂的查询加入了很多表,我有一个"列"在结果中是其他列的串联。现在我需要获取此连接的结果并在查找表中查找此值,并返回关联的正确字符串,如果存在匹配值,则替换原始字符串。
编辑:我创建了一个SQL小提琴帮助。 http://sqlfiddle.com/#!2/209437/1到目前为止,这是我的查询:
SELECT prices.part_no AS sku, CONCAT_WS(' | ', GROUP_CONCAT(DISTINCT cat.`category` SEPARATOR ' | '), GROUP_CONCAT(DISTINCT CONCAT_WS(' > ', c.`section_code`, c.`lev1_subcategory_name`, c.`lev2_subcategory_name`, c.`lev3_subcategory_name`) SEPARATOR ' | ')) AS category
FROM parts_file_prices AS prices
LEFT JOIN parts_file_content AS c on prices.`part_no` = c.`part_no` AND prices.`batch_id` = c.`batch_id`
LEFT JOIN parts_file_categories AS cat on prices.`part_no` = cat.`part_no`
WHERE prices.`batch_id` = 1 AND (c.`catalog_part_description` IS NOT NULL AND c.`catalog_part_description` != '')
GROUP BY sku
ORDER BY prices.`part_no` asc LIMIT 5;
结果如下:
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
| sku | category |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
| 11-2000 | Best Selling > Test | Best Selling > Test 2 | Best Selling > Test > Test Sub | 2 > Books & CD's > Technical Books | 2 > Technical Books |
| 11-3000 | 2 > Books & CD's > Technical Books | 2 > Technical Books |
| 11-8006 | 2 > Books & CD's |
| 11-8033 | 2 > Books & CD's > Log/Record Books | 2 > Log/Record Books |
| 11-8034 | 2 > Books & CD's > Log/Record Books | 2 > Log/Record Books |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
我从未问过SQL问题,所以如果需要更多信息,请说明。 我需要查找这个连接值" 2>书籍和CD>>技术书籍"在我的表parts_file_category_overrides中,并将该表中的替换值返回到我的结果行。
我的结局结果需要如下所示:
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
| sku | category |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
| 11-2000 | Best Selling > Test | Best Selling > Test 2 | Best Selling > Test > Test Sub | Inside Living > Books |
| 11-3000 | Inside Living > Books |
| 11-8006 | Inside Living > Books |
| 11-8033 | Inside Living > Books |
| 11-8034 | Inside Living > Books |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------+
以下是包含一些数据的表格:
mysql> SELECT * FROM parts_file_prices LIMIT 5;
+----+----------+---------+--------------------------+--------------+-----------------+--------------+
| id | batch_id | part_no | description | dealer_price | manufacturer_no | upc |
+----+----------+---------+--------------------------+--------------+-----------------+--------------+
| 1 | 1 | 11-2000 | TRAILER/FIFTH WHEEL BOOK | 4.66 | A02-2000 | 019079200020 |
| 2 | 1 | 11-3000 | MOTOR HOME BOOK | 4.66 | A02-3000 | 019079200037 |
| 3 | 1 | 11-8006 | "MOM'S CAMPER COOKING" | 6.65 | MOMS | 799530378986 |
| 4 | 1 | 11-8033 | CAMPER'S DAILY LOG | 9.54 | NULL | 831196001522 |
| 5 | 1 | 11-8034 | DAILY LOG REFILL | 4.86 | NULL | 831196001546 |
+----+----------+---------+--------------------------+--------------+-----------------+--------------+
mysql> SELECT * FROM parts_file_content LIMIT 5;
+----+----------+---------+--------------+-----------------------+-----------------------+-----------------------+-----------------+--------------+----------------------------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------+--------+-------+--------+--------------------------+----------------------------------+----------------+--------------+----------------+
| id | batch_id | part_no | section_code | lev1_subcategory_name | lev2_subcategory_name | lev3_subcategory_name | group_name | mfg_part_num | catalog_part_description | weight | text_body | 250_image_location | length | width | height | manufacturer | logo_location | b2ccategory | subcategory1 | subcategory2 |
+----+----------+---------+--------------+-----------------------+-----------------------+-----------------------+-----------------+--------------+----------------------------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------+--------+-------+--------+--------------------------+----------------------------------+----------------+--------------+----------------+
| 1 | 1 | 11-2000 | 2 | Books & CD's | Technical Books | NULL | RVing Made Easy | A02-2000 | RVing Made Easy, Trailers/Fifth Wheels | 0.190 | Written by a long-time RVer who shares his experience, covers important operating and safety features. Hints follow each section listing do's and don'ts. 48 pages. Priced each. | 112/250/112000_silo.jpg | 0.25 | 5.50 | 8.38 | VALTERRA PRODUCTS, INC. | Null | Inside Your RV | BOOKS | RV MAINTENANCE |
| 2 | 1 | 11-2000 | 2 | Books & CD's | Technical Books | NULL | RVing Made Easy | A02-2000 | RVing Made Easy, Trailers/Fifth Wheels | 0.190 | Written by a long-time RVer who shares his experience, covers important operating and safety features. Hints follow each section listing do's and don'ts. 48 pages. Priced each. | 112/250/112000_silo.jpg | 0.25 | 5.50 | 8.38 | VALTERRA PRODUCTS, INC. | 009050/weblogo/valterra_silo.jpg | Inside Your RV | BOOKS | RV MAINTENANCE |
| 3 | 1 | 11-2000 | 2 | Technical Books | NULL | NULL | RVing Made Easy | A02-2000 | RVing Made Easy, Trailers/Fifth Wheels | 0.190 | Written by a long-time RVer who shares his experience, covers important operating and safety features. Hints follow each section listing do's and don'ts. 48 pages. Priced each. | 112/250/112000_silo.jpg | 0.25 | 5.50 | 8.38 | VALTERRA PRODUCTS, INC. | Null | Inside Your RV | BOOKS | RV MAINTENANCE |
| 4 | 1 | 11-2000 | 2 | Technical Books | NULL | NULL | RVing Made Easy | A02-2000 | RVing Made Easy, Trailers/Fifth Wheels | 0.190 | Written by a long-time RVer who shares his experience, covers important operating and safety features. Hints follow each section listing do's and don'ts. 48 pages. Priced each. | 112/250/112000_silo.jpg | 0.25 | 5.50 | 8.38 | VALTERRA PRODUCTS, INC. | 009050/weblogo/valterra_silo.jpg | Inside Your RV | BOOKS | RV MAINTENANCE |
| 5 | 1 | 11-3000 | 2 | Books & CD's | Technical Books | NULL | RVing Made Easy | A02-3000 | RVing Made Easy, Motorhomes | 1.000 | Written by a long-time RVer who shares his experience, covers important operating and safety features. Hints follow each section listing do's and don'ts. 48 pages. Priced each. | 113/250/113000_silo.jpg | 0.13 | 5.50 | 8.50 | VALTERRA PRODUCTS, INC. | Null | Inside Your RV | BOOKS | RV MAINTENANCE |
+----+----------+---------+--------------+-----------------------+-----------------------+-----------------------+-----------------+--------------+----------------------------------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------+--------+-------+--------+--------------------------+----------------------------------+----------------+--------------+----------------+
mysql> SELECT * FROM parts_file_categories LIMIT 5;
+----+---------+--------------------------------+
| id | part_no | category |
+----+---------+--------------------------------+
| 1 | 11-2000 | Best Selling > Test |
| 2 | 11-2000 | Best Selling > Test 2 |
| 3 | 11-2000 | Best Selling > Test |
| 4 | 11-2000 | Best Selling > Test > Test Sub |
+----+---------+--------------------------------+
mysql> SELECT * FROM parts_file_category_overrides LIMIT 5;
+----+-------------------------------------+-----------------------+
| id | search | replace |
+----+-------------------------------------+-----------------------+
| 1 | 2 > Books & CD's > Technical Books | Inside Living > Books |
| 2 | 2 > Technical Books | Inside Living > Books |
| 3 | 2 > Books & CD's > Log/Record Books | Inside Living > Books |
| 4 | 2 > Log/Record Books | Inside Living > Books |
| 5 | 2 > Books & CD's | Inside Living > Books |
+----+-------------------------------------+-----------------------+
答案 0 :(得分:0)
你可以使用子查询并尝试这样的事情:
select sku, case when o.replace is not null then o.replace else category end as category
FROM (
SELECT prices.part_no AS sku, CONCAT_WS(' | ', GROUP_CONCAT(DISTINCT cat.`category` SEPARATOR ' | '), GROUP_CONCAT(DISTINCT CONCAT_WS(' > ', c.`section_code`, c.`lev1_subcategory_name`, c.`lev2_subcategory_name`, c.`lev3_subcategory_name`) SEPARATOR ' | ')) AS category
FROM parts_file_prices AS prices
LEFT JOIN parts_file_content AS c on prices.`part_no` = c.`part_no` AND prices.`batch_id` = c.`batch_id`
LEFT JOIN parts_file_categories AS cat on prices.`part_no` = cat.`part_no`
WHERE prices.`batch_id` = 1 AND (c.`catalog_part_description` IS NOT NULL AND c.`catalog_part_description` != '')
GROUP BY sku
ORDER BY prices.`part_no` asc)
as test LEFT JOIN parts_file_category_overrides AS o on o.`search` = category COLLATE utf8_unicode_ci;
结果:
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
|SKU | CATEGORY
|11-2000 | Best Selling > Test | Best Selling > Test 2 | Best Selling > Test > Test Sub | 2 > Books & CD's > Technical Books | 2 > Technical Books
|11-3000 | Inside Living > Books
+-----------------------------------------------------------------------------------------------------------------------------------------------------+
如果 parts_file_category_overrides 中的外键不是从很多列中删除的,那么你可能会容易得多。