动态分组查询

时间:2013-11-08 05:58:23

标签: mysql database sqlite

我需要使用数组通过查询触发动态组。

a=['Branch','Company','Name'];

现在,我需要像

这样的查询
"select Branch,Company,Name,count(Name) as count from myTable group by Branch,Company,Name;"

请建议!!

3 个答案:

答案 0 :(得分:0)

试试这个:

String quryString =“选择分支,公司,名称,计数(名称)作为来自myTable组的计数”;

for (int i = 0; i < a.length; i++)
    quryString =quryString +a[i];

现在使用quryString并激活您的查询。

答案 1 :(得分:0)

谢谢你们, 我尝试了,最后从以下代码得到了我的结果。

jsonString=[{"field":"Branch"},{"field":"Company"},{"field":"Name"}]

function addCount(jsonString){
    var a=[];
    var str="";
    $.each(jsonString,function (i,result){
        a.push(result.field);
        if(i == jsonString.length-1){
            str += result.field; 
        } else{
             str += result.field+",";
        }
    });
          db.transaction(function (tx) {
            var q=(a.length-1);

            var query="select "+str+",count("+a[q]+") as count from documentProperty GROUP BY "+str+";";        console.log(query);
            tx.executeSql(query, [], function (tx, result) {
                if (result != null && result.rows != null) {
                    for (var i = 0; i < result.rows.length; i++) {
                        var row = result.rows.item(i);
                        console.log(row);
                    }
                }
            });
         });

} 

答案 2 :(得分:0)

使用您的初始问题数据:

a=['Branch','Company','Name'];

function addCount(a) {
    db.transaction(function (tx) {
        str=a.join(", ");
        tx.executeSql("select "+str+",count(*) as count from documentProperty GROUP BY "+str+";", [], function (tx, result) {
            if (result != null && result.rows != null) {
                for (var i = 0; i < result.rows.length; i++) {
                    var row = result.rows.item(i);
                    console.log(row);
                }
            }
        });
    });
}