通过迭代为Union设置mysql变量

时间:2017-06-20 22:14:13

标签: mysql variables

我有一个产品表,我需要以特定格式上传。产品由类似的base_name组成。我创建了这个UNION查询,以按照需要的方式组织行。

SELECT 'Product' as 'Item Type', '' as 'Product ID', base_name as 'Product Name', 'P' as 'Product_Type', mfid as 'Product Code/SKU', manufacturer as 'Brand Name', imap_price as 'Price' from 3d_cart_worksheet where base_name='Jasmine Pendant' group by base_name
UNION
SELECT 'SKU' as 'Item Type', '' as 'Product ID', concat(concat('[S]Finish=', finish), ',',concat('[S]Lamp Type=', lamp_type)) as 'Product Name', '' as 'Product_Type', mfid as 'Product Code/SKU', '' as 'Brand Name', '' as 'Price' from 3d_cart_worksheet where base_name='Jasmine Pendant'
UNION
SELECT 'RULE' as 'Item Type', '' as 'Product ID', concat(concat('[S]Finish=', finish), ',',concat('[S]Lamp Type=', lamp_type)) as 'Product Name', '' as 'Product_Type', mfid as 'Product Code/SKU', '' as 'Brand Name', concat('[FIXED]',imap_price) as 'Price' from 3d_cart_worksheet where base_name='Jasmine Pendant';

这完美无缺。我无法弄清楚如何在整个产品列表中运行它。此代码仅适用于一个。如何迭代以便在遇到新的base_name时运行此联合集?我使用变量吗?如何将变量值更改为下一个新产品名称?是否每个都可以应用?

谢谢

0 个答案:

没有答案