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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.