Skip to content
Advertisement

Select from two tables with group by date

I have two tables:

Table t1:

So from this table I want to get count field Id for each day.

For this I use next query:

It’s work good.

So next table is t2.

To select data by date from this table I use next query:

It’s also work good. So my purpose is merge these two queries into one SELECT that next I can write in php one tables with count of Id by date from table t1 and with count of count field from table t2 by date.

When I try next query:

It’s not work. What I do wrong?

Advertisement

Answer

First you should UNION these results and then group by days and select aggregate fields. Also you can JOIN these queries but it can be a problem if some days miss in one of two tables:

SQL fiddle demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement