-1

是否可以通过一个查询获得 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);
4

1 回答 1

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将具有所需的结构和您需要的所有数据。

于 2013-10-13T06:41:16.113 回答