jacurtis
jacurtis

Reputation: 806

MYSQL Select based on date, then join

I have two tables, one table has a list events with various details of an event. I have a second table with tickets and specific ticket information (seat number, section number, price, etc) that link to the event.

Obviously an event may have several tickets.

What I would like to do is JOIN the events table with the tickets table BY the events_id (the primary key of the events table and there is a column in the tickets table for events_id). But I need to take it one step further. Currently I can do this to join the events and the tickets.

SELECT * FROM events e INNER JOIN tickets t ON e.id = t.events_id;

This works. But I need to take it one step further. I don't want to join all of the events. I only want the events for today and then join those events.

I have been able to successfully get todays events (without joining) with this:

SELECT * FROM events WHERE `date` = CURDATE();

But I want to be able to combine these two queries into one query. I want to first grab all of today's events, and then I want to JOIN the tickets table to todays events based on the events_id foreign key.

I have tried and tried, many different things without any success. This can not be the first time someone has needed to do something like this. All help is appreciated.

Upvotes: 0

Views: 717

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

This is how you can do

SELECT * FROM events e 
INNER JOIN tickets t ON e.id = t.events_id
where e.`date` = CURDATE();

Upvotes: 3

Related Questions