Jump to content

Simple SQL Join help


benphp

Recommended Posts

I want to fetch the titles from the Courses table, but I only want the Course Title to return once when I join Events.

 

Courses

CID | CourseTitle 

 1  | Course A

 2  | Course B

 3  | Course C

 

Events

EID | CID | EventDate 

1   |  1  | 2016-02-22

2   |  1  | 2016-02-23

3   |  2  | 2016-02-24

4   |  3  | 2016-02-25

5   |  3  | 2016-02-26

 

If I use a JOIN, 

 

SELECT Courses.CourseTitle FROM Courses 

LEFT JOIN Events on (Events.CID = Courses.CID)

 

then I get

Course A

Course A

Course B

Course C

Course C

 

But what I want is

Course A

Course B

Course C

 

Because ultimately, I'm going to select an Event date range, and I want to see just the courses with the event date range.

 

Thanks!

Edited by benphp
Link to comment
Share on other sites

Just because your query will pull the course name for each row doesn't mean you have to display it in the output for each row however.

 

As you process the query results in your code you can detect when the course name has changed and only display it at that point. Alternatively you can have your code group the results by course then output the results with some nested loops.

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.