[英]Combine results for SQL query
我有点紧。 我正在使用WHMCS并构建自定义报告,但是我的一个查询遇到了麻烦。 以下是概述:
我有2张桌子; tblinvoices包含发票的小计,税额,总计等,以及tblinvoiceitems包含发票上显示的各个行项目。 我想运行一个查询,该查询返回我能够执行的所有单个订单项及其价格。 我在执行“ GROUP BY”并将结果按发票编号分组时遇到了问题,然后它仅返回每个发票的第一行项目。 我想按发票号对它们进行分组,因此报告中只有1行。 这是我的查询:
$query = "SELECT date_format(tblinvoices.datepaid,'%m-%d-%Y') AS datepaid,
tblinvoices.userid,
tblinvoices.id,
tblinvoices.subtotal,
tblinvoices.credit,
tblinvoices.tax,
tblinvoices.tax2,
tblinvoices.total,
tblinvoices.taxrate,
tblinvoices.taxrate2,
tblinvoiceitems.description,
tblinvoiceitems.amount,
tblinvoices.status,
FROM tblinvoices
INNER JOIN tblinvoiceitems ON tblinvoices.id = tblinvoiceitems.invoiceid
GROUP BY tblinvoices.id";
$result = mysql_query($query);
# Math Operations
$statement = array();
$count = 0;
if ($result !== false) {
while ($data = mysql_fetch_object($result)) {
$invoiceid = $data->id;
$datepaid = $data->datepaid;
$description = $data->description;
$item_amount = $data->item_amount;
$subtotal = $data->subtotal;
$credit = $data->credit;
$tax = $data->tax;
$tax2 = $data->tax2;
$total = $data->total;
if ($export != true) {
$client_link = '<a href=clientssummary.php?userid='.$data->userid.'>'.$data->userid;
$invoice_link = '<a href=invoices.php?action=edit&id='.$data->id.'>'.$data->id;
}
else {
$client_link = $data->userid;
$invoice_link = $data->id;
}
if (strpos($description, 'Setup') !== false) {
$setup = $item_amount;
}
else {
$setup = 0;
}
if (strpos($description, 'Addon') !== false) {
$addon = $item_amount;
}
else {
$addon = 0;
}
if (strpos($description, 'Tax Guide: No => Yes') !== false) {
$taxguide = $item_amount;
}
else {
$taxguide = 0;
}
if (strpos($description, 'Reading Rack Bundle') !== false) {
$reading = $item_amount;
}
else {
$reading = 0;
}
if (strpos($description, 'Toolkit Bundle') !== false) {
$toolkit = $item_amount;
}
else {
$toolkit = 0;
}
$hosting = $subtotal - $setup - $addon - $taxguide - $reading - $toolkit;
$statement[$invoiceid."_".$count] = array($datepaid,$client_link,$promo,$dtn,$company,$state,$invoice_link,$setup,$addon,$taxguide,$reading,$toolkit,$hosting,$subtotal,$credit,$tax+$tax2,$total);
$count++;
}
}
foreach ($headings AS $k=>$v) {
$reportdata["tableheadings"][] = $v;
}
//ksort($statement);
foreach ($statement AS $invoiceid=>$entry) {
$reportdata["tablevalues"][] = array(
$entry[0], // datepaid
$entry[1], // clientid
$entry[2], // promocode
$entry[3], // dtn
$entry[4], // companyname
$entry[5], // state
$entry[6], // invoiceid
formatCurrency($entry[7]), // setup
formatCurrency($entry[8]), // addon
formatCurrency($entry[9]), // taxguide
formatCurrency($entry[10]), // reading
formatCurrency($entry[11]), // toolkit
formatCurrency($entry[12]), // hosting
formatCurrency($entry[13]), // subtotal
formatCurrency($entry[14]), // credit
formatCurrency($entry[15]), // tax
formatCurrency($entry[16]) // total
);
}
mysql_free_result($result);
如果有帮助,我将很乐意提供任何其他信息/代码。 我虽然这可能是一个更一般的类型问题...谢谢!
因此,请尝试按invoice.id和invoice.userid分组,然后再按invoice.subtotal分组。
如果您以后决定聚合任何内容,可能会遇到一些问题,但是应该这样做。
使用该结果,您描述的结果集将如下所示:
userid | id | subtotal | credit | tax | status | desc | amount
______________________________________________________________
1 | 1 | 20 | 0 | .7 | 1 | item1| 10
1 | 1 | 20 | 0 | .7 | 1 | item2| 10
1 | 2 | 30 | 0 | 2.1 | 1 | item3| 15
1 | 2 | 30 | 0 | 2.1 | 1 | item3| 15
1 | 3 | 10 | 0 | .7 | 0 | item1| 10
1 | 4 | 1 | 0 | .07 | 0 | item4| 1
这是你想要的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.