请原谅我,如果我的错误,仍然试图学习高级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;
我需要的是一个查找表,告诉我哪些类别属于每个产品,我知道它将成为另一个连接,但我需要它,所以如果超过1个类别属于产品将其放入相同的列,并用@符号连接它们,它不是类别的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;
答案 0 :(得分: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;