1

我希望从具有不同结构的多个 mysql 表中获取数据,并将其插入到一个包含来自源的不同列/数据的主表中,

我创建了一个包含以下数据的列名映射表:

表:affiliate_datafeed_magento_mapping

+----+-------------+----------------+
| id | datafeed    | magento        |
+----+-------------+----------------+
|  1 | SKU         | sku            |
|  1 | Name        | name           |
|  1 | Description | description    |
|  1 | Url         | affiliate_link |
|  1 | Price       | price          |
|  1 | Brand       | brand          |
|  1 | ModelNumber | model          |
|  2 | SKU         | sku            |
|  2 | Name        | name           |
|  2 | Description | description    |
|  2 | Url         | affiliate_link |
|  2 | Price       | price          |
|  2 | Currency    | affiliate_cur  |
+----+-------------+----------------+

id对应表中联属计划的idaffiliate_programs

datafeed将源表中的列名与表中对应的id对应起来affiliate_program

magento对应表中的列名接收数据

表:affiliate_programs

+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
| id | affiliate_network | affiliate_network_short | affiliate_program      | affiliate_program_long | affiliate_program_short | affiliate_program_datafeed_url                                                                                                                                                        | affiliate_program_datafeed_csv_filename | affiliate_program_datafeed_last_get | order |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
|  1 | CommissionFactory | cf_                     | Lifestyle Clotheslines | lifestyleclotheslines  | lcl                     | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPuB5NPigbec7prnjLCX5MHygO$A5sTlhOHb8t3nmufzta@k5uyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_lifestyleclotheslines.csv            | 2013-08-12 22:23:02                 |  NULL |
|  2 | CommissionFactory | cf_                     | MacArthur Baskets      | macarthurbaskets       | mcb                     | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPSA4dbg17SY7svvjeSX5Jf1iO@b5JXshOfY@ovjzeKj4PGivuyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_macarthurbaskets.csv                 | 2013-08-12 22:23:02                 |  NULL |
|  3 | ClixGalore        | cg_                     | Boutique Wineries      | boutiquewineries       | btw                     | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=9522&AfID=264058                                                                                                              | cg_boutiquewineries.csv                 | 2013-08-12 22:23:02                 |  NULL |
|  4 | ClixGalore        | cg_                     | Brewtopia              | brewtopia              | bwt                     | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4817&AfID=264058                                                                                                              | cg_brewtopia.csv                        | 2013-08-12 22:23:02                 |  NULL |
|  5 | ClixGalore        | cg_                     | MacArthur Baskets      | macarthurbaskets       | mcb2                    | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4909&AfID=264058                                                                                                              | cg_macarthurbaskets.csv                 | 2013-08-12 22:23:02                 |  NULL |
|  6 | ClixGalore        | cg_                     | Winemakers Choice      | winemakerschoice       | wmc                     | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4282&AfID=264058                                                                                                              | cg_winemakerschoice.csv                 | 2013-08-12 22:23:02                 |  NULL |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+

id对应id表中相同affiliate_datafeed_magento_mapping

源表的连接affiliate_programs.affiliate_network_shortaffiliate_programs.affiliate_program_long组成源表的名称

表:cf_lifestyleclotheslines

+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
| id | DateCreated         | DateModified        | SKU  | Name                                                 | Category                                                                       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | Url                                                    | OriginalUrl                                                                                       | Image                                                             | Image50                                                                 | Image100                                                                  | Image120                                                                  | Image200                                                                  | Image300                                                                  | Image400                                                                  | Price        | Brand       | ModelNumber |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
|  1 | 2012-10-19 10:52:50 | 2013-06-20 02:07:37 | 30   | Airaus Ceiling Mounted Clothes Airer                 | Home & Garden > Household Supplies > Laundry Supplies > Drying Racks & Hangers | Watch the Product Video            Just arrived from Europe!  Lifestyle Clotheslines is now stocking the latest ceiling mounted clothes airers from Europe.  Ceiling mounted clothes airers are the perfect alternative for those with limited space situations or who need the benefit of an indoor clothesline or airer.  Boasting 6 individually adjustable drying rods which are made of steel not plastic, these indoor airers can be used in a range of situations and are ideally suited for the disabled as they can easily be lowered or raised to the height of the user.  An indoor airer can also help extend the life of your clothes by reducing the exposure to harsh UV rays and they can also be a big power saver for your home when used to dry your washing instead of a tumble dryer.  Discover today how one of our new ceiling mounted clothes airers can save you money and take all the hard work and fuss out of drying your washing.           //           Customer Video Reviews    &nbsp... | https://track.commissionfactory.com.au/p/10604/1718691 | http://www.lifestyleclotheslines.com.au/airaus-ceiling-mounted-clothes-airer/                     | http://content.commissionfactory.com.au/Products/7228/1718691.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@50x50.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@100x100.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@120x120.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@200x200.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@300x300.jpg | http://content.commissionfactory.com.au/Products/7228/1718691@400x400.jpg | 99.0000 AUD  |             | AIRAUS      |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+

因此,使用提供的数据,我希望遍历所有表(串联affiliate_programs.affiliate_network_shortaffiliate_programs.affiliate_program_long列,affiliate_programs并根据表中的映射列为它们构建一个插入查询,affiliate_datafeed_magento_mapping以将数据插入表中magento_export

任何帮助都会很棒!

4

1 回答 1

0

蒂姆,我认为你根本不需要映射表

我会使用一系列UNION查询。每个来源一个查询。在每个查询的 SELECT 子句中,您的目标表中的每个列都应该有一个 1:1 列,包括映射该NULL目标列不适用于该源的值。(这是映射真正发生的地方)

然后,您的 INSERT 语句是所有 UNIONED 查询到目标表的直接映射。

伪代码

INSERT INTO Destination(Col1, Col2, Col3, Col4)
SELECT  T1.Field1 as Col1
        ,NULL as Col2
        ,T1.Field7 as Col3
        ,T1.Field2 as Col4
FROM    FirstSource T1
UNION
SELECT  T2.Field1 as Col1
        ,Some Calulation as Col2
        ,T2.Field3 as Col3
        ,NULL as Col4
FROM    FirstSource T1
于 2013-08-13T17:33:09.560 回答