按月显示 table 中的结果

Display the result in table By month

我正在尝试计算我的 intax 和 outTax 总和。我有正确的查询,如果我运行它会显示正确的结果。但是我无法按需要显示它。

这是我的代码。

  $sqlOut = "SELECT sales_invoice.invoice_id, MONTHNAME(sales_invoice.date_invoiced) AS month, sales_invoice_line_items.invoice_id, sales_invoice_line_items.tax, SUM(sales_invoice_line_items.tax_amount) AS totaltax, taxes.tax_id, taxes.rate, taxes.name AS Tname FROM sales_invoice INNER JOIN sales_invoice_line_items ON sales_invoice.invoice_id=sales_invoice_line_items.invoice_id INNER JOIN taxes ON sales_invoice_line_items.tax=taxes.tax_id WHERE sales_invoice_line_items.tax=".$tax." GROUP BY sales_invoice.date_invoiced";

$sqlIn = "SELECT purchase_invoice.invoice_id, MONTHNAME(purchase_invoice.date_invoiced) AS month, purchase_invoice_line_items.invoice_id, purchase_invoice_line_items.tax, SUM(purchase_invoice_line_items.tax_amount) AS totaltax, taxes.tax_id, taxes.rate, taxes.name AS Tname FROM purchase_invoice INNER JOIN purchase_invoice_line_items ON purchase_invoice.invoice_id=purchase_invoice_line_items.invoice_id INNER JOIN taxes ON purchase_invoice_line_items.tax=taxes.tax_id WHERE purchase_invoice_line_items.tax=".$tax." GROUP BY purchase_invoice.date_invoiced";

$ResOut = mysql_query($sqlOut) or die(mysql_error());

$ResIn = mysql_query($sqlIn) or die(mysql_error());

}

我要这样显示

<td>Months</td><td>Out Tax</td><td>In Tax</td><td>Difference(OutTax-InTax)</td>

我的输出格式必须是

<table><tr>
<td>Months</td><td>Out Tax</td><td>In Tax</td><td>Difference(OutTax-InTax)</td></tr>
<tr><td>Jan</td>   <td>3456</td>    <td>2311</td>   <td>1145</td></tr>
<tr><td>March</td> <td>4123</td>    <td>3125</td>   <td>2978</td></tr>
</table>

和table结构是 purchase_invoicetable

CREATE TABLE IF NOT EXISTS `purchase_invoice` (
  `invoice_id` int(50) NOT NULL AUTO_INCREMENT,
  `order_id` int(50) NOT NULL,
  `date_invoiced` date NOT NULL,
  `status` varchar(10) NOT NULL,
  PRIMARY KEY (`invoice_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

purchase_invoice_line_items

CREATE TABLE IF NOT EXISTS `purchase_invoice_line_items` (
  `id` int(50) NOT NULL AUTO_INCREMENT,
  `invoice_id` int(50) NOT NULL,
  `tax` int(10) NOT NULL,
  `discount` int(10) NOT NULL,
  `freight` int(20) NOT NULL,
  `sub_total` double NOT NULL,
  `tax_amount` double NOT NULL,
  `reason` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

有人可以帮我解决这个问题吗

试试这个:

SELECT A.month_invoiced AS 'Months', A.totaltax AS 'Out Tax', 
         B.totaltax AS 'In Tax', (A.totaltax - B.totaltax) AS 'Difference(OutTax-InTax)'
FROM (SELECT MONTHNAME(si.date_invoiced) AS month_invoiced, 
             EXTRACT(YEAR_MONTH FROM si.date_invoiced) AS YM_Invoced, 
             SUM(sil.tax_amount) AS totaltax
      FROM sales_invoice si 
      INNER JOIN sales_invoice_line_items  sil ON si.invoice_id=sil.invoice_id 
      INNER JOIN taxes t ON sil.tax=t.tax_id 
      WHERE sil.tax=".$tax." 
      GROUP BY YM_Invoced
     ) AS A 
INNER JOIN (SELECT MONTHNAME(pi.date_invoiced) AS month_invoiced, 
                   EXTRACT(YEAR_MONTH FROM pi.date_invoiced) AS YM_Invoced, 
                   SUM(pil.tax_amount) AS totaltax
            FROM purchase_invoice PI 
            INNER JOIN purchase_invoice_line_items pil ON pi.invoice_id=pil.invoice_id 
            INNER JOIN taxes t ON pil.tax=t.tax_id 
            WHERE pil.tax=".$tax." 
            GROUP BY YM_Invoced
          ) AS B ON A.YM_Invoced = B.YM_Invoced;

::编辑::

SELECT A.month_invoiced, 
         MAX(CASE WHEN A.taxType = 'Out' THEN A.totaltax ELSE 0 END) AS 'Out Tax', 
         MAX(CASE WHEN A.taxType = 'In' THEN A.totaltax ELSE 0 END) AS 'In Tax', 
        (IFNULL(MAX(CASE WHEN A.taxType = 'Out' THEN A.totaltax ELSE 0 END), 0) - 
         IFNULL(MAX(CASE WHEN A.taxType = 'In' THEN A.totaltax ELSE 0 END), 0)
        ) AS 'Difference(OutTax-InTax)' 
FROM (SELECT MONTHNAME(si.date_invoiced) AS month_invoiced, 
                 EXTRACT(YEAR_MONTH FROM si.date_invoiced) AS YM_Invoced, 
                 SUM(sil.tax_amount) AS totaltax, 
                 'Out' AS taxType
        FROM sales_invoice si 
        INNER JOIN sales_invoice_line_items  sil ON si.invoice_id=sil.invoice_id 
        INNER JOIN taxes t ON sil.tax=t.tax_id 
        WHERE sil.tax=".$tax." 
        GROUP BY YM_Invoced
        UNION 
        SELECT MONTHNAME(pi.date_invoiced) AS month_invoiced, 
                 EXTRACT(YEAR_MONTH FROM pi.date_invoiced) AS YM_Invoced, 
                 SUM(pil.tax_amount) AS totaltax, 
                 'IN' AS taxType
        FROM purchase_invoice PI 
        INNER JOIN purchase_invoice_line_items pil ON pi.invoice_id=pil.invoice_id 
        INNER JOIN taxes t ON pil.tax=t.tax_id 
        WHERE pil.tax=".$tax." 
        GROUP BY YM_Invoced
     ) AS A 
GROUP BY A.YM_Invoced