简体   繁体   English

PHP / Mysql在现有表中插入多行以输出csv

[英]PHP/Mysql Inserting multiple rows in existing table for csv output

I am creating a table in a specific format from my existing mysql database so I can export it as a csv file to my woocommerce website. 我正在从现有的mysql数据库中以特定格式创建表,因此可以将其作为csv文件导出到我的woocommerce网站。

I have no problem creating a row for each product (simple product with no variation), however I am having problems with variable products (requiring several rows each). 我没有为每个产品(没有变化的简单产品)创建一行的问题,但是我有可变产品的问题(每个产品都需要几行)。

The picture below shows an example of 3 products (3 different ParentSKU) in the table 下图显示了表格中3种产品(3种不同的ParentSKU)的示例

表

What I need to do is to create a new row for each ParentSKU with the common information of each variation. 我需要做的是为每个ParentSKU创建一个新行,其中包含每个版本的通用信息。 ie 1 extra row for 14615, 1 extra row for 14622 and 1 extra row for 14624. 即1个额外的行用于14615,1个额外的行用于14622和1个额外的行用于14624。

I have about 1000 rows that need to be inserted like this. 我大约需要插入1000行。

Below is how I insert my rows in the table in the first place: 下面是我如何首先在表中插入行的方法:

$Query2 = "INSERT INTO Website ( `ParentSKU`,`SKU`,`Brand`,`ProductName`,`Images`)

SELECT  ID,ID,Brand,Style,MainLink

FROM    StockAccount,Stock_Items

WHERE   StockAccount.ID=Stock_Items.ID_Product AND
    Stock_Items.Name='' AND
    Stock_Items.Returned='' AND
    StockAccount.LAXS='Yes' AND
    StockAccount.MainLink!=''

GROUP BY Size,MainLink

ORDER BY `ID`
";

I know how to insert rows one at a time but could not find anything about my particular issue. 我知道如何一次插入一行,但是找不到关于我特定问题的任何信息。

Use SELECT DISTINCT to get a single row for each ParentSKU , using the ParentSKU also as the SKU in the new row. 使用SELECT DISTINCT可以为每个ParentSKU获得一行,同时将ParentSKU用作新行中的SKU

INSERT INTO WebSite (ParentSKU, SKU, Brand, ProductName, Images)
SELECT DISTINCT ParentSKU, ParentSKU, Brand, ProductName, Images
FROM WebSite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM