Solved

SQL Join Issue

Posted on 2013-01-14
2
330 Views
Last Modified: 2013-01-14
In MySQL I'm working with a database table called Events I didn't design and can't change which has a field EventTime which is a varchar(8) which has entries like '9:00 AM', '11:00 AM', '3:00 PM'.  The table only has entries for certain times during the day.

I need to create a report for a particular day (in essence a day calendar) which lists all the times in ascending order whether or not they have corresponding records in the Events table, but if there is one it should show up.

Ex:

9:00 AM   Event1
10:00 AM  
11:00 AM  Event2
12:00 PM
1:00 PM
2:00 PM Event3
...
6:00 PM

I have created a table TimeOrder with the various text fields of time with an ID field so that I can order properly:

ID                         TimeValue
1                           9:00 AM
2                           10:00 AM
3                           11:00 AM etc

I'm trying to create a left join statement so that I see all the records in TimeOrder (i.e. the full day with all timeslots) with matching records from the Events table when there are any. Here's my SQL Statement

SELECT e.Date, t.TimeValue, e.EventType, trim(e.FName) + trim(e.LName), e.AttCount
FROM TimeOrder t
Inner JOIN enrolled e on t.timevalue = e.eventtime
WHERE e.Date = '2013-01-01'
ORDER BY t.ID

Open in new window


However my output only has the timeslots with corresponding records in Event (similar to an inner join).

Any thoughts?
0
Comment
Question by:deedub84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38775842
Your query IS using INNER JOIN, try with LEFT JOIN, and you could also try

SELECT e.Date, t.TimeValue, e.EventType, trim(e.FName) + trim(e.LName), e.AttCount
FROM TimeOrder t
left JOIN (select * from enrolled WHERE Date = '2013-01-01') e on t.timevalue = e.eventtime
ORDER BY t.ID
0
 
LVL 1

Author Comment

by:deedub84
ID: 38775915
Yep, I copied the inner join query I was using to compare.

However, your subquery solution did the trick.  Thanks!
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question