【问题标题】:build dynamic sql insert query to merge multiple tables into single table using mapping table使用映射表构建动态 sql 插入查询以将多个表合并为单个表
【发布时间】:2013-08-13 16:01:01
【问题描述】:

我希望从具有不同结构的多个 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中的affiliate program的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_programsaffiliate_programs.affiliate_network_shortaffiliate_programs.affiliate_program_long 列的串联,并根据表中的映射列为它们构建一个插入查询@ 987654337@向表中插入数据magento_export

任何帮助都会很棒!

【问题讨论】:

    标签: mysql sql insert dynamic-sql


    【解决方案1】:

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

    我会使用一系列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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      相关资源
      最近更新 更多