Search code examples
mysqlsqlinner-join

Inner Join with Where condition on both tables


I'm building a calendar and I need some help with my SQL queries.

I have the following tables:

event attendees

attendees is a join table that joins Facebook User ID's to event ID's with a many-many relationship.

I need to run a query that get's all the events where the attendee's UID matches the user's and where the event's month and year field match the month and year input by the user.

I'm not sure how to do it. I know I need an inner join, but beyond that I have no idea what I'm doing.

Edit

So as an example, if I have the following data

Attendee id=1 uid=065786593 isCancelled=0 eventID=3

Attendee id=2 uid=065786593 isCancelled=0 eventID=4

Attendee id=3 uid=065786593 isCancelled=0 eventID=5

Attendee id=4 uid=056836251 isCancelled=0 eventID=6

event id=3 title=Meeting with Manager day=06 month=12 year=2017

event id=4 title=Job Interview day=15 month=12 year=2017

event id=5 title=Celebrate new year day=01 month=01 year=2018

event id=6 title=christmas night out day=22 month=12 year=2017

when I run the query for this month's calendar (12/2017) as user 065786593 I would expect to get events 3 and 4 in the result


Solution

  • select event.id, event.title, event.day
    from attendee
    join event on attendee.eventID=event.id
    where attendee.uid=? and event.year=? and event.month=?