Jump to content

MYSQL Union 3 Tables


tomtimms

Recommended Posts

Ok I have 3 tables I need to union.  I am having trouble on the third table.  I am getting data within a date range and displaying them by the hour.  My third table is a calendar table that just has the time range: 00:00:00 to 23:00:00.  I want to take the data in table 1 and table 2 and sum them together using a UNION and group them together by the date(HOUR).  I then want to take my calendar table to fill in the hours that were not found in table 1 and table 2.

 

Table 1 has the following.

Date : ID : Money

2010-05-01 01:00:00  | 5 | 23.23

2010-05-01 02:00:00  | 15 | 32.34

2010-05-01 03:00:00  | 5 | 53.55

2010-05-01 04:00:00  | 10 | 11.45

2010-05-01 05:00:00  | 10 | 63.74

 

Table 2 has the following

 

Date : ID : Money

2010-06-01 01:00:00  | 5 | 53.23

2010-06-01 12:00:00  | 15 | 63.34

2010-06-01 15:00:00  | 5 | 111.55

2010-06-01 16:00:00  | 10 | 23.45

2010-06-01 17:00:00  | 10 | 66.74

 

Table 3 (Calendar table)

Date:  ID: Money

00:00:00 | 0 | 0

01:00:00 | 0 | 0

02:00:00 | 0 | 0

etc

etc

 

My query so far

 

SELECT date, SUM(money) FROM table_1 WHERE date BETWEEN (Date 1) AND (DATE2) GROUP BY ID UNION SELECT date, SUM(money) FROM table_1 WHERE date BETWEEN (Date 1) AND (DATE2) GROUP BY ID.

 

How can I add the third table in to find the hours that were not found in this query?  Thanks to anyone who can help out.

Link to comment
https://forums.phpfreaks.com/topic/216482-mysql-union-3-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.