Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Php 将mysql中的表与月基合并_Php_Mysql - Fatal编程技术网

Php 将mysql中的表与月基合并

Php 将mysql中的表与月基合并,php,mysql,Php,Mysql,我有4个mysql表,有不同的日期字段,没有公共列。我想得到每个表中的值的总和,并每月将所有值合并到一个表中 样本: 我想要这样的结果: > select monthname(t.the_date ) > , sum(t1.amt) as iamt > , sum(t1.vat) as ivat > , SUM(t2.amount) as amt > , sum(t2.vat) as vat > , sum(t3.p

我有4个mysql表,有不同的日期字段,没有公共列。我想得到每个表中的值的总和,并每月将所有值合并到一个表中 样本:

我想要这样的结果:

>  select monthname(t.the_date )
>     , sum(t1.amt) as iamt
>     , sum(t1.vat) as ivat
>     , SUM(t2.amount) as amt
>     , sum(t2.vat) as vat 
>     , sum(t3.payment) as vamt
>     , sum(t3.vat) as vvat
>     , sum(t4.paid) as camt
>     , sum(t4.vat_amount) as nvat     from(   select indate as the_date   from invoice   union    select edate   from expense_details   union   
> select pdate   from vendor_payment   union    select paid_date   from
> vat ) t   left join invoice t1 on t.the_date  = t1.indate   left join
> expense_details t2 on t.the_date  = t2.edate   left join
> vendor_payment t3 on t.the_date  = t3.pdate   left join vat t4 on
> t.the_date  = t4.paid_date   group by monthname(t.the_date )


您可以使用union从所有4个表中获取不同的日期,使用left join连接表,使用group by连接总和

  select monthname(t.the_date )
    , sum(t1.A_amt)
    , sum(t1.A_vat)
    , sum(t2.B_amt)
    , sum(t2.B_vat) 
    , sum(t3.C_amt)
    , sum(t3.C_vat)
    , sum(t4.D_amt)
    , sum(t4.D_vat)  
  from(
  select A_date as the_date
  from table1
  union 
  select B_date
  from table2
  select D_date
  from table3
  select D_date
  from table4 ) t
  left join table1 t1 on t.the_date  = t1.A_date
  left join table2 t2 on t.the_date  = t2.B_date
  left join table3 t3 on t.the_date  = t3.C_date
  left join table4 t4 on t.the_date  = t4.D_date
  group by monthname(t.the_date )

可以用php数组实现吗?你说“它不工作”是什么意思。。你有错误吗。。错误的结果。。expalin better请添加您尝试过的内容以及遇到困难的代码片段。
  select monthname(t.the_date )
    , sum(t1.A_amt)
    , sum(t1.A_vat)
    , sum(t2.B_amt)
    , sum(t2.B_vat) 
    , sum(t3.C_amt)
    , sum(t3.C_vat)
    , sum(t4.D_amt)
    , sum(t4.D_vat)  
  from(
  select A_date as the_date
  from table1
  union 
  select B_date
  from table2
  select D_date
  from table3
  select D_date
  from table4 ) t
  left join table1 t1 on t.the_date  = t1.A_date
  left join table2 t2 on t.the_date  = t2.B_date
  left join table3 t3 on t.the_date  = t3.C_date
  left join table4 t4 on t.the_date  = t4.D_date
  group by monthname(t.the_date )