我有一个问题,我花了几个小时寻找解决方案。但直到现在,我还没有找到任何东西。问题是我有以下查询:
$sql = "SELECT
MONTH( i.invoicedate ) AS month,
ROUND(SUM(t.transactions * ( t.selling_price - t.buyin ) ),0) AS margin
FROM invoice i, project p, transactions t, users u
WHERE
t.project = p.proj_id AND
t.invoice = f.inv_id AND
p.proj_owner = u.usr_id AND
YEAR( i.invoice_date ) = '2012' AND
u.usr_login = '" . $current_user->first_name . "'
GROUP BY MONTH( i.invoice_date )
UNION
SELECT DISTINCT
MONTH(p.invoicedate) AS month,
SUM( ROUND( p.quantity * p.listprice - IF( p.discount_percent > 0,
p.quantity * p.listprice * p.discount_percent / 100, 0 ) - IF( i.discount_amount > 0, p.discount_amount, 0 ), 0 ) ) - SUM( ROUND( p.quantity * purchaseprice, 0 ) ) AS margin
FROM productrel p
LEFT JOIN invoice f ON i.id=f.invoiceid
LEFT JOIN entity e ON e.id=f.invoiceid
LEFT JOIN salesorder s ON f.salesorderid = s.salesorderid
LEFT JOIN entity t ON t.id=s.salesorderid
LEFT JOIN users d ON t.ownerid=d.id
WHERE
e.deleted=0 AND
t.ownerid <> 15 AND
d.id = " . $current_user->id . " AND
YEAR(f.invoicedate)='2012'
GROUP BY MONTH(f.invoicedate)";
$result = $adb->pquery($sql, array());
while ($row = $adb->fetch_array($result)) {
$margin .= $row['margin'].', ';
}
使用此查询,输出如下所示:
-------------------------------
| Month | Margin |
|-----------------------------
| 5 | 3824 |
| 11 | 24344 |
| 12 | 45664 |
| | |
主要问题是我想显示这个用户所有月份的数据。即使它是空的。更糟糕的是,许多单元格没有显示出来,正如您在示例中看到的那样。所以我想要的是这样的:
-------------------------------
| Month | Margin |
|-----------------------------
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
| 5 | 3824 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 0 |
| 10 | 0 |
| 11 | 24344 |
| 12 | 45664 |
| | |
像这样格式化,如果我用 PHP 打印它:
0, 0, 0, 0, 3824, 0, 0, 0, 0, 0, 24344, 45664
最后一点:不可能用这些数据制作一个额外的表格,因为我无权这样做。