mysql计算在单个选择查询中按日期分组的多个表

时间:2016-10-26 11:19:14

标签: mysql

我有两个表tbl_pdf_gen_editstbl_gen_pdf_file。我想要在单SELECT个查询中的两个表的每个日期记录数。我尝试了类似的内容:

    SELECT a.added_on,COUNT(a.added_on),COUNT(pe.created_at) 
FROM tbl_pdf_gen_edits a,tbl_gen_pdf_file pe 
GROUP BY a.added_on

返回:

added_on COUNT(a.added_on) COUNT(pe.created_at) 2016-10-06 16:51:50 51 51 2016-10-06 16:52:44 102 102 2016-10-26 17:42:44 51 51 当我在tbl_pdf_gen_edits上执行查询时:

SELECT COUNT(*),added_on FROM `tbl_pdf_gen_edits` GROUP BY added_on

返回:

COUNT(added_on) added_on 1 2016-10-06 16:51:50 2 2016-10-06 16:52:44 1 2016-10-26 17:42:44 还可以查询tbl_gen_pdf_file

SELECT COUNT(*),created_at FROM `tbl_gen_pdf_file` GROUP BY created_at

它返回: COUNT(created_at) created_at 1 2016-09-28 16:33:33 1 2016-09-29 11:06:49 2 2016-09-29 16:50:36 1 2016-09-30 15:07:53 26 2016-10-02 11:06:49 1 2016-10-02 12:30:43 1 2016-10-02 12:30:49

因此,您会在单独的表格中看到单独的SELECT结果,并且合并后的SELECT JOIN查询不会返回相同的计数。是不是有办法将两个查询的单独计数结果合并到单个SELECT查询语句中?

1 个答案:

答案 0 :(得分:-3)

我认为下面的某些事情应该有效,让我们知道它是不是......

选择count(added_rec),count(updated_rec),日期     来自edit_table e,add_table a     其中a.primary_key = b.primary_key     按日期分组

 select Y.add_count, x.edited_data, X.date from (select count(edited_data)  
 edit_count , date form edit_table group by date) full outer join (select 
 count(added_data) add_count , date form edit_table group by date)
 Y on X.date=Y.date