I want to query a javascript array object using SQL(to perform grouping on data).
var data = [{ dep: 'A', qt: 10, price: 5},
{ dep: 'A', qt: 5, price: 2.30 },
{ dep: 'B', qt: 3, price: 2.20 },
{ dep: 'C', qt: 1, price: 4 },
{ dep: 'C', qt: 4, price: 10 }];
var res = alasql('SELECT dep, SUM(qt) AS qt, SUM(qt*price) AS amt, AGGR(amt/qt) AS price FROM ? GROUP BY dep',[data]);
document.getElementById("res").textContent = JSON.stringify(res);
But in my original scenario, I have column names with spaces as:
var data = [{ "dep name": 'A', qt: 10, price: 5},
{ "dep name": 'A', qt: 5, price: 2.30 },
{ "dep name": 'B', qt: 3, price: 2.20 },
{ "dep name": 'C', qt: 1, price: 4 },
{ "dep name": 'C', qt: 4, price: 10 }];
How to modify the above SQL query so that column name with spaces can be used. Any help would be greatly appreciated.
You can use backticks (`) to escape MySQL column names. This allows you to:
SELECT A as `dep name` ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句