Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 发票表和税务处理的最佳实践建议_Mysql_Sql - Fatal编程技术网

Mysql 发票表和税务处理的最佳实践建议

Mysql 发票表和税务处理的最佳实践建议,mysql,sql,Mysql,Sql,我正在寻找关于多租户应用程序的发票/发票详细信息表格的建议,其中一些租户将注册纳税,而一些不会 invoice\u details表包含以下字段: id description amount_ex_tax tax_amount amount_inc_tax 对于启用税收的租户,详细信息行可能包含: id description amount_ex_tax tax_amount amount_inc_tax 1234 Repairs 1

我正在寻找关于多租户应用程序的
发票/发票详细信息
表格的建议,其中一些租户将注册纳税,而一些不会

invoice\u details
表包含以下字段:

id   description    amount_ex_tax   tax_amount   amount_inc_tax
对于启用税收的租户,详细信息行可能包含:

id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00       20.00          120.00
id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00        0.00            0.00
对于税务禁用的租户,详细信息行可能包含:

id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00       20.00          120.00
id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00        0.00            0.00
这就产生了一个问题,即没有一列要求和以找出发票价值

选项1:按原样保留数据,但必须根据租户类型调整查询以请求正确的列。这使得各种SQL查询更加复杂

选项2:我读到的一个建议是设置
amount\u inc\u tax=amount\u ex\u tax
,然后查询总是对同一列求和以获得发票的值。这简化了SQL。它看起来是这样的:

id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00        0.00          100.00
id   description    amount_ex_tax  tax_amount  amount_inc_tax  total_amount
1234 Repairs               100.00        0.00            0.00        100.00
我认为这个建议混淆了代码本身,因为列名不包含所推断的内容

选项3:另一个建议是创建一个
total列
,以便该列始终可以访问总值,而不管是否纳税

它看起来是这样的:

id   description    amount_ex_tax  tax_amount  amount_inc_tax
1234 Repairs               100.00        0.00          100.00
id   description    amount_ex_tax  tax_amount  amount_inc_tax  total_amount
1234 Repairs               100.00        0.00            0.00        100.00

在这种情况下,哪些最佳做法/建议最适合您?哪种方法可以避免将来出现哪些问题?谢谢你的建议

把每行的总数都删掉怎么样

然后,当您需要总数时,只需使用:

select (amount_ex_tax + tax_amount) as total_amount
. . .
您可以将其包装在视图中,因此
total\u amount
始终是正确的

在一行中包含算术合计几乎没有什么好处。如果有人在插入或更新数据时准确地进行计算,这可能会带来问题。一行内额外空间的成本可能会抵消性能方面非常非常微小的节约


进行计算的一种情况是在列上需要索引。然而,如果你想要一个索引,那么很明显该列应该包含什么。

Awesome suggestion@Gordon Linoff,很有意义,而且还没有看到/想到这一点。也谢谢你的投票。最新的MySQL版本支持虚拟列,也可以对虚拟列进行索引。@PaulSpiegel和任何感兴趣的人。支持见5.7.6,并记录在。