我需要使用数组通过查询来触发动态组。
a=['Branch','Company','Name'];
现在,我需要像这样的查询
"select Branch,Company,Name,count(Name) as count from myTable group by Branch,Company,Name;"
请推荐!!
使用您的初始问题数据:
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);
}
}
});
});
}
谢谢大家,我试过了,终于从下面的代码中得到了我的结果。
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);
}
}
});
});
}
尝试这个:
String quryString = "select Branch,Company,Name,count(Name) as count from myTable group by " ;
for (int i = 0; i < a.length; i++)
quryString =quryString +a[i];
现在使用 quryString 并触发您的查询。