构建动态sql insert查询,使用映射表将多个表合并到单个表中

时间:2013-08-13 16:01:02

标签: mysql sql insert dynamic-sql

我希望从多个具有不同结构的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对应表affiliate_programs

中联盟计划的ID

datafeed将源表中的列名与表affiliate_program中的相应ID对应

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_longaffiliate_programs列的串联,并为它们构建插入查询,所有这些都基于映射表affiliate_datafeed_magento_mapping中的列以将数据插入表magento_export

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

添 我认为你不需要映射表

我会使用一系列UNION次查询。每个源一个查询。在每个查询的SELECT子句中,目标表中的每个coulmn都应该有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
相关问题