Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

James Welch
James Welch
10,363 Points

Two sets of data from the same table

PHP, MySQL.

Hello everyone. I recently got assigned a project for a PHP system.

Basically the project is to allow someone to select a date range. This finds all results between the two dates. I also create a function to find the same date, but the year before so the user can compare both years.

The table does not contain data for days when the shop isn't open, and so the first time this was done, rows from the right were being skipped (the old year), if the rows on the left had a closed day.

I need to be able to show both the selected year, and year before, no matter what days they were open (hopefully filling in the row with 0's or something) because we need to keep all the results in-line with each other.

I don't know what to do, since I'm querying the same table with just a slight change for the BETWEEN part. I haven't used UNION much and not so much with JOINs yet either.

Cheers for your time.

1 Answer

Not really sure how your data is set up without seeing a schema, but it sounds like you need a self-join outer join. Then the dates that don't hava a match would show up as NULL

Here's an article that goes into the details: http://blog.sqlauthority.com/2010/07/08/sql-server-the-self-join-inner-join-and-outer-join/

If you post your schema, or even just give a screen shot, I could give you a better answer.