如何在一行中从三个不同的表中添加三个数字mysql phpmyadmin

时间:2019-04-03 02:06:07

标签: mysql phpmyadmin

我正在帮助一个朋友进行查询,这些需求显示了这些表格中每个表格的百分比:当月的购买,费用和销售交易。

所以我需要显示这个:


销售百分比-采购百分比-费用百分比

  10%                   50%                   40%

问题之一是这三个表没有任何类型的关系(外键)。

要使这些成为可能,很明显,我必须获得每一项的总数并将其加起来以具有百分比基数。

那么,如何将所有内容添加到SELECT子句中?

问候。

1 个答案:

答案 0 :(得分:0)

好了,经过了很多思考,我终于明白了。

所以这是最终答案,希望它可以有用:

select 
sum(total) as total_transacciones,
(select count(id_venta) from venta where fecha__venta between            '2019-01-01' and '2019-04-02') as total_ventas,
(select round((count(id_venta) / sum(total) * 100),2) from venta         where fecha__venta between '2019-01-01' and '2019-04-02') as  porcentaje_ventas,
(select count(id_compra) from compra where fecha_compra between '2019-01-01' and '2019-04-02') as total_compras,
(select round((count(id_compra) / sum(total) * 100),2) from compra where fecha_compra between '2019-01-01' and '2019-04-02') as porcentaje_compras,
(select count(id_gastos) from gastos where fecha_cr between '2019-01-01' and '2019-04-02') as total_gastos,
(select round((count(id_gastos) / sum(total) * 100),2) from gastos where fecha_cr between '2019-01-01' and '2019-04-02') as  porcentaje_gastos
from
(
select count(id_venta) as total from venta
UNION ALL
select count(id_compra) as total from compra
UNION ALL
select count(id_gastos) as total from gastos
) t


从结果中捕获:
https://i.stack.imgur.com/XdkxP.png

问候。