是否可以通过一个查询获得 JSON 输出?
[{
"name": "Date",
"data": ["2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", "2013-05-01"] //data from grouped from_date column
}, {
"name": "KD",
"data": [4, 5, 6, 2, 5] // arrays from saldo_sprzedazy for KD sales_group
}, {
"name": "SG",
"data": [5, 2, 3, 6, 7] // arrays from saldo_sprzedazy for SG sales_group
}]
我目前的查询:
SELECT
sales_raport_all.from_date,
SUM(sales_raport_all.saldo_sprzedazy),
klienci_ax_all.sales_group,
klienci_ax_all.nazwa
FROM
sales_raport_all
INNER JOIN
klienci_ax_all
ON
sales_raport_all.konto=klienci_ax_all.konto_odbiorcy
WHERE
YEAR(from_date) = YEAR(CURDATE())
GROUP BY
sales_raport_all.from_date
我按 from_date 分组,但我还需要按 sales_group 分组...是否可以在我的 mysql 表上执行此操作?我尝试将数据准备到 Column Highcharts highcharts.com/demo/column-basic
编辑:
好的,也许这会澄清我之前的问题 :)
这是我的 PHP 代码:
$query = mysql_query("SELECT
sales_raport_all.from_date,
sales_raport_all.to_date,
sales_raport_all.konto,
SUM(sales_raport_all.saldo_sprzedazy),
SUM(sales_raport_all.wartosc_kosztowa),
SUM(sales_raport_all.marza),
klienci_ax_all.sales_group,
klienci_ax_all.nazwa
FROM
sales_raport_all
INNER JOIN
klienci_ax_all
ON
sales_raport_all.konto=klienci_ax_all.konto_odbiorcy
WHERE
YEAR(from_date) = YEAR(CURDATE())
GROUP BY
sales_raport_all.from_date");
$category = array();
$category['name'] = 'Data';
while($r = mysql_fetch_array($query)) {
$category['data'][] = $r['from_date'];
}
$querySG = mysql_query("SELECT
sales_raport_all.from_date,
sales_raport_all.to_date,
sales_raport_all.konto,
SUM(sales_raport_all.saldo_sprzedazy),
SUM(sales_raport_all.wartosc_kosztowa),
SUM(sales_raport_all.marza),
klienci_ax_all.sales_group,
klienci_ax_all.nazwa
FROM
sales_raport_all
INNER JOIN
klienci_ax_all
ON
sales_raport_all.konto=klienci_ax_all.konto_odbiorcy
WHERE
klienci_ax_all.sales_group = 'SG'
AND
YEAR(from_date) = YEAR(CURDATE())
GROUP BY
sales_raport_all.from_date");
$series1 = array();
$series1['name'] = 'SG';
while($r = mysql_fetch_array($querySG)) {
$series1['data'][] = intval($r['SUM(sales_raport_all.saldo_sprzedazy)']);
}
....
我的问题是:我必须为每个特定的 sales_group 编写单独的查询还是有更简单的方法?
现在我像这样使用json:
$result = array();
array_push($result,$category);
array_push($result,$series1);
array_push($result,$series2);
array_push($result,$series3);
print json_encode($result);