php Mysql选择不含增值税的金额和增值税金额,其中发票总额至少为

ltqd579y  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(120)

有这样的mysql表:

| DocumentNumber | Amount | Account | BuyerName | BuyerVat |
| 123            | 100    | 6110    | Name 01   | null     |
| 123            | 21     | 5727    | Name 01   | null     |
| 456            | 100    | 6112    | Name 02   | null     |
| 456            | 12     | 5732    | Name 02   | null     |
| 789            | 10     | 6112    | Name 02   | null     |
| 789            | 1.2    | 5732    | Name 02   | null     |

希望将SUM(Amount)作为TotalWithoutVat,其中Account 6110或6112的文档总数至少为100,并且BuyerVat为空。还希望将SUM(Amount)作为TotalVat
得到TotalWithoutVat没有什么大问题。

SELECT SUM(`Amount`) AS `TotalWithoutVat` FROM `Table`
WHERE 
`Account` IN("6110", "6112") 
AND `BuyerVat` IS NULL 
HAVING SUM(`Amount`) >= "100"

但是如何获得这些单据的增值税总额,其中不含增值税的总额至少为100,并且BuyerVat为空?不能使用HAVING SUM(金额) >= x,因为x可能为21、12或5(不同的增值税税率)。
目前资源非常消耗的解决方案。首先从mysql得到所有发票号码,其中总没有增值税至少是100和BuyerVat是空的。然后选择总金额发票号码在那些选择。
选择所有发票编号。

SELECT SUM(`Amount`) AS `TotalWithoutVat`, `DocumentNumber` FROM `Table`
WHERE 
`Account` IN("6110", "6112") 
AND `BuyerVat` IS NULL 
GROUP BY `JL`.`DocumentNumber` 
HAVING SUM(`Amount`) >= "100"

然后php foreach现有数组并创建文档编号为$document_numbers[] = trim($one_line['DocumentNumber']);的新数组
接下来是mysql

SELECT SUM(`Amount`) AS `TotalVat` FROM `Table`
WHERE 
`DocumentNumber` IN("123", "456") -- `$document_numbers` 
AND `Account` IN ("5727", "5732")

你知道如何用一个mysql查询得到同样的结果吗?
期望这样的输出:

[0] => Array
    (
        [TotalWithoutVat] => 200.00
        [TotalVat] => 33.00

基于nnichols的思想,创建了这样的代码(目前看来工作正常,但可能会出现一些意想不到的结果;据我所知,从同一个表我创建了好像两个“虚拟”表与必要的列和行。然后“内部mysql”做计算(和值),然后才从mysql得到结果?似乎是很好的解决方案和更少的资源使用。

SELECT `t1`.`TotalWithoutVat`, `t2`.`TotalVat` 

FROM (
SELECT SUM(`Amount`) AS `TotalWithoutVat`, 
`DocumentNumber` -- as i understand this is necessary for joining; without get `Unknown column` error 
FROM `Table`
WHERE `Account` IN (6110, 6112) 
AND `BuyerVat` IS NULL 
-- GROUP BY `DocumentNumber` -- in this case do not need, because i need only one total sum
HAVING SUM(`Amount`) >= 100
) `t1`

INNER JOIN 
( -- otherwise i got incorrect totals...
SELECT SUM(`Amount`) AS `TotalVat`, `DocumentNumber` 
FROM `Table`
WHERE `Account` IN (5727, 5732) 
AND `BuyerVat` IS NULL 
) `t2`

ON `t1`.`DocumentNumber` = `t2`.`DocumentNumber`;
fnx2tebb

fnx2tebb1#

我不确定我是否完全理解您要做的事情,但根据您的两个SELECT查询,我的最佳猜测是-

SELECT `t1`.*, SUM(`t2`.`Amount`) AS `TotalVat`
FROM (
    SELECT SUM(`Amount`) AS `TotalWithoutVat`, `DocumentNumber`
    FROM `Table`
    WHERE `Account` IN (6110, 6112) 
    AND `BuyerVat` IS NULL 
    GROUP BY `DocumentNumber` 
    HAVING SUM(`Amount`) >= 100
) `t1`
INNER JOIN `Table` `t2`
    ON `t1`.`DocumentNumber` = `t2`.`DocumentNumber`
    AND `t2`.`Account` IN (5727, 5732)
GROUP BY `t1`.`DocumentNumber`;

或者在外部查询中不按DocumentNumber分组?

SELECT SUM(`t1`.`TotalWithoutVat`) `TotalWithoutVat`, SUM(`t2`.`Amount`) AS `TotalVat`
FROM (
    SELECT SUM(`Amount`) AS `TotalWithoutVat`, `DocumentNumber`
    FROM `Table`
    WHERE `Account` IN (6110, 6112) 
    AND `BuyerVat` IS NULL 
    GROUP BY `DocumentNumber` 
    HAVING SUM(`Amount`) >= 100
) `t1`
INNER JOIN `Table` `t2`
    ON `t1`.`DocumentNumber` = `t2`.`DocumentNumber`
    AND `t2`.`Account` IN (5727, 5732);

相关问题