连接具有多对多关系字段的两个表

时间:2020-07-24 10:29:48

标签: javascript mysql google-apps-script

我正在使用Google Data Studio,并尝试从第三方系统中获取数据。在这种情况下,第三方系统会出现问题,因为它不允许进行SQL连接。因此,我分别使用2个URL来获取数据,并将两个表(customer_order表和Customer_Order_Demand表)连接在一起。但是问题是在一个表中存在一个字段(Order_NO),该字段具有多对多关系。根据我的代码,在连接两个表后,在Order_No列中仅获得第一个值。我想按相应日期显示每个商品的订单数。


  Customer Order Demand Table                                        
+-----------------------------+ 
| id  | item     |    Date    |         
|-----|----------|------------| 
|  1  |  BLU     | 7/10/2020  |  
|  2  |  PM      | 7/20/2020  |   
|  3  |  BLU     | 7/23/2020  |          
+-----------------------------+ 


    Customer Order Table                                        
+---------------------------------------------------------+
| id  |  COD_id         |  Order_NO      |   Date         |
|-----|----------------------------------|----------------|
|  1  |      1          |  #100          |    7/10/2020   |
|  2  |      1          |  #130          |    7/10/2020   |
|  3  |      2          |  #200          |    7/20/2020   |
|  4  |      3          |  #500          |    7/23/2020   | 
+---------------------------------------------------------+

                   Expected  Table                                        
+-----------------------------------------------+
|     |  Item    |    Date    |  Order_NO       |
|-----|----------|------------|-----------------|
|  1  |  BLU     | 7/10/2020  |  #100 /#130     |
|  2  |  PM      | 7/20/2020  |  #200           |
|  3  |  BLU     | 7/23/2020  |  #500           |        
+-----------------------------------------------+



 
 var url=[
    'http://test.smartapps.lk/rest/api/selectQuery?sessionId=' + SessionId + '&maxRows=10&query=select id, Item, Date from Customer_Order_demand &output=json'
  ].join('');
  
  var url2=[
    'http://test.smartapps.lk/rest/api/selectQuery?sessionId=' + SessionId + '&maxRows=10&query=select id,Order_NO from Customer_Order &output=json'
  ].join(''); 
  
  
  var responseID2 = UrlFetchApp.fetch(url);
  var responseID3 = UrlFetchApp.fetch(url2);
   
  var table1 = JSON.parse(responseID2.getContentText());     
  var table2 = JSON.parse(responseID3.getContentText());
   
  var finalArray = [];
  
  for(var i in table1){
      for(var j in table2){

        //Joining two tables 
        if(table1[i][0]==table2[j][0]){

            // converting to json object
             var newObj = {      
             "Item": table1[i][1],
             "Date":table1[i][2],
             "Order_NO": table2[j][1] 
             };
          
         finalArray.push(newObj);
        }
      }
   
  }
  
   return finalArray;
  
  
 

1 个答案:

答案 0 :(得分:1)

  • Reduce第二个表到map

    const table2map = table2.reduce(
      (mp,[codId,ordNo]) => 
          mp.set(codId,
           mp.has(codId)
            ? [...mp.get(codId),ordNo]
            : [ordNo]
          ), new Map
    )
    
  • Map table1到对象数组:

    const finalArr = table1.map(
      ([id,Item,Date]) =>
          ({Item,Date,
          "Order_No": table2map.get(id).join()})
    )