是否可以通过一个查询获得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);
答案 0 :(得分:0)
如果您想一次性完成此操作,则必须按sales_group
和from_date
进行分组:
$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,
klienci_ax_all.sales_group
ORDER BY
sales_raport_all.from_date,
klienci_ax_all.sales_group");
然后收集原始数组中的所有可能日期和所有数据。
$raw = array();
$dates = array();
while ($r = mysql_fetch_array($query)) {
$date = $r['from_date'];
if (!in_array($date, $dates)) $dates[] = $date;
$sales_group = $r['sales_group'];
$raw[$sales_group][$date] = intval($r['SUM(sales_raport_all.saldo_sprzedazy)']);
}
最后浏览原始数据,检查sales_group在给定日期是否有相关数据或将其设置为零。
$data = array();
$data[0] = array('name' => "Date", 'data' => $dates);
foreach ($raw as $name => $d) {
$new_data = array('name' => $name, 'data' => array());
foreach ($dates as $date) {
$new_data['data'][] = isset($d[$date]) ? $d[$date] : 0;
}
$data[] = $new_data;
}
最终$data
将具有所需的结构和您需要的所有数据。