MYSQL加入并创建Distinct json

时间:2017-11-14 17:10:03

标签: mysql json

我有以下选择

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/22.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/21.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"},{"quoteID":"8","quoteTitle":"Bryar2","notes":"Long","web_path":"\/surplusAdmin3\/upload\/17.png"},{"quoteID":"11","quoteTitle":"Avram","notes":"Allison","web_path":"\/surplusAdmin3\/upload\/4.jpg"}]

它可以获取我需要的信息。

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path1":"\/surplusAdmin3\/upload\/23.pdf","web_path2":"\/surplusAdmin3\/upload\/22.jpeg","web_path3":"\/surplusAdmin3\/upload\/21.jpeg","web_path4":"\/surplusAdmin3\/upload\/20.jpeg"}]

我现在需要得到我的结果:

var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
    console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+  fileCount);
    if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
  else
    {
        console.log('just add the filename array Row = ' +arrayRow );
        // need to insert to the array arrayRow
        // dataChanged.push('web_path'+fileCount,e["web_path"]);
        toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];

        var field = 'web_path'+fileCount;
        var data = e["web_path"];
        var tempArray = [field,data];
        dataChanged.push(toPush);
        dataChanged.field = data;
        //dataChanged = dataChanged.concat(tempArray);
        fileCount = fileCount +1;
    }
    currentQuoteID = e["quoteID"];
   console.log('stuff '+ JSON.stringify(dataChanged));
});

我不打算重新执行MYSQL语句,我只需要以我需要的格式创建一个新数组。

我有以下代码:

dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image"},"web_path1","/surplusAdmin3/upload/22.jpeg"]

我得到的结果是:

dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image","web_path1","/surplusAdmin3/upload/22.jpeg"}]

我需要的是

select
    min(purchaseDate) filter (where fruitType = 'apple') as apple,
    min(purchaseDate) filter (where fruitType = 'orange') as orange,
    customer
from fruitPurchases
group by customer

但我无法锻炼我需要做的事情。

1 个答案:

答案 0 :(得分:0)

按照以下方式工作:

// loop through and add field 'Type' can all be done in one loop.
    dataReturned.forEach(function(e){
    if (typeof e === "object" ){
    e["Type"] = "other";

   // console.log('stuff '+e['Type'][i] )
}

});

//loop through and workout the file type
    dataReturned.forEach(function(e){
    if (typeof e === "object" ){
    var ext = e['web_path'].substr(e['web_path'].lastIndexOf('.') + 1);
    if ( (ext === 'jpeg' ) || ( ext === 'jpg' )|| ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))

    { e["Type"] = "image"; }
    //console.log(e['web_path']);
   // console.log('stuff '+ JSON.stringify(e))
  }

});
// create a new array formated as needed
var currentQuoteID =0;
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
e["quoteID"] + 'file count = '+  fileCount);
    if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e);     arrayRow = arrayRow + 1; fileCount = 1}
  else
    {
        //console.log('just add the filename array Row = ' +arrayRow );
        // need to insert to the array arrayRow
        // dataChanged.push('web_path'+fileCount,e["web_path"]);
        var field = 'web_path'+fileCount;
        var data = webPath+e["web_path"];
        var tempArray = [field,data];
        dataChanged[arrayRow]['web_path'+fileCount] = data
        fileCount = fileCount +1;
    }
    currentQuoteID = e["quoteID"];
   console.log('stuff '+ JSON.stringify(dataChanged));
});