0

如果我弄错了一些术语,请原谅我,仍在尝试学习高级 mySql 查询。我正在尝试从电子商务平台导出数据,而我需要的一些数据在查找表中,问题是我拥有的一个查找表可以关联多个值,以及我需要将数据导入的系统需要该列的特定格式。

如果有帮助,我将使用 zen cart 作为我的来源,下面是查询。

Select zp.products_id as id, zpd.products_name as name, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
LEFT JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;

我需要的是一个查找表,它告诉我每个产品属于哪些类别,我知道这将是另一个连接,但我需要它,所以如果多个类别属于一个产品,则将其放在同一列中,并用@符号加入它们,它不是类别的ID,而是路径,所以我需要将类别ID查找到另一个表以获取路径。

例如“category1/subcategory@category2”..

感谢您的任何指导。

编辑:我仍然需要合并结果,因为我得到了我想要的结果的两倍,但是我看到了它所在的每个类别的记录..

Select zp.products_id as id, zpd.products_name as name, zcd.categories_name as categories, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
4

1 回答 1

0

您正在寻找的是group_concat(). 我认为它会是这样的:

Select zp.products_id as id, zpd.products_name as name,
       group_concat(zcd.categories_name separator '@') as categories,
       products_price_w as cost, products_price as price,
       zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
group by zp.products_id
ORDER BY zp.products_id ASC;
于 2013-07-14T15:42:57.547 回答