MYSQL自加入如何工作? [英] How does a MYSQL Self-Join Work?

查看:70
本文介绍了MYSQL自加入如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个有关自我加入"的问题,得到了一个很好的答案.

I recently asked a question about Self-Joins and I got a great answer.

该查询用于查找事件1之后1天的事件2的ID,开始日期和价格.

The query is meant to find the ID, Start Date, and Price of Event2, Following Event1 by 1 Day.

代码工作正常. 但是我不知道如何.

有人可以尽你所能解释吗?查询的不同部分是什么,它们做什么?

Could someone explain as thoroughly as you can- what the different parts of the query are and what they do?

SELECT event2.id, event2.startdate, event2.price
FROM mm_eventlist_dates event1
JOIN mm_eventlist_dates event2 
ON event2.startdate = date_add(event1.enddate, INTERVAL 1 DAY)
WHERE event1.id=$id

非常感谢您的帮助,无论出于什么原因,我都很难解决这个问题.

I really appreciate your help, for whatever reason I'm having a really hard time wrapping my head around this.

推荐答案

我试图理解的方法是在第一块上写出两个列表,分别标记为event1和一个event2.然后在每个列表中列出一些记录(列表将是相同的),现在从下面的描述中的WHERE开始.

The way I'd try to understand this is to write out two lists on piece one labelled event1 and one event2. Then list a few records in each list (the lists will be identical) now start at the WHERE in the description below.

我们正在从两个表中获取数据(确定同一张表使用了两次,但是暂时不要理会)

We're taking data from two tables (OK the same table used twice, but try to ignore that for the moment)

FROM mm_eventlist_dates event1
JOIN mm_eventlist_dates event2 

从下至上阅读其余内容可能会有所帮助.

It probably helps to read the rest from the bottom up.

  WHERE event1.id=$id

因此,我们想要来自event1的具有指定记录ID的记录.大概那是一个记录.现在我们确定事件结束后的第二天.

So we want the record from event1 that has the specified record id. Presumably that's exactly one record. Now we figure out the day after that event ended.

 date_add(event1.enddate, INTERVAL 1 DAY)

现在,告诉我们事件2中的记录,它们需要从该日期开始,

Now that tells us the records from event2, they need to start on that date,

ON event2.startdate = date_add(event1.enddate, INTERVAL 1 DAY)

我们现在确定了两条记录,我们想要什么字段?

We now have two records identified, what fields do we want?

SELECT event2.id, event2.startdate, event2.price

哦,就是我们确定了起始日期的字段.

Oh, just the fields from the one whose start date we figured out.

这篇关于MYSQL自加入如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆