简体   繁体   English

MYSQL根据日期选择,然后加入

[英]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 ). 我想这样做是JOINevents与表ticketsBYevents_id (事件表的主键,并没有在票表列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. 我想先获取今天的所有事件,然后再基于events_id外键将ticketsJOIN到今天的事件中。

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. 感谢所有帮助。

This is how you can do 这就是你可以做的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM