Jump to content

MYSQL UNION AND ADD


tomtimms

Recommended Posts

Ok I have been trying to figure this out for awhile and can't seem to grasp the answer.  I have 2 tables. 

 

Table 1 Has (Date,Amount) and table 2 has (Date,Amount).  I need to join these tables together on the date field.  Table 1 has values of

 

2010-07-03 01:00:00 / $500

2010-07-03 03:00:00 / $200

2010-07-03 04:00:00 / $100

 

Table 2 is just an hour table that has the following.

 

00:00:00 / $0

01:00:00 / $0

02:00:00/ $0

etc.etc.etc

 

I need to union all the tables together so the end result will show.

00:00:00 / $0

01:00:00 / $500

02:00:00 / $0

03:00:00 / $200

04:00:00 / $100

 

I tried doing a left join however I can't get missing vales from table 2.

 

Current Join Code, Maybe a Union?

 

SELECT date_format(rph.date,'%H:00:00') AS date,SUM(rph.amount + rh.amount) AS amount FROM table2 rh LEFT JOIN table 1 rph ON date_format(rh.date,'%H:00:00') = date_format(rph.date,'%H:00:00') WHERE rph.date BETWEEN '2010-07-03 00:00:00' AND '2010-07-03 23:59:59' GROUP BY date_format(rph.date,'%H:00:00') 

 

 

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.