2013-10-25 38 views
0

我目前工作的一些遺留系統和我在一張SQL的書面像這樣來了:請解釋SQL加入

SELECT 
    cel.id, 
    cel.campaign_id, 
    cel.campaign_name, 
    cel.campaign_message_type, 
    cel.responsible_user_id, 
    cel.responsible_user_full_name, 
    cel.person_id, 
    cel.email_template_id, 
    cel.email_message_subject, 
    cel.recipient_list_name, 
    cel.first_name, 
    cel.last_name, 
    cel.email_address, 
    b.message_content_html email_message_content_html, 
    b.message_content_text email_message_content_text, 
    b.course_presentation_id, 
    cp.phone_mobile 
FROM 
    campaigns_email_logs cel 

    LEFT JOIN contacts_people cp ON 
     (cel.person_id = c.id), 
     campaigns_email b 
WHERE 
    cel.campaign_id = b.id and 
    cel.status = 'queued' and 
    cel.datetime_start < now() and 
    cel.datetime_end > now() and 
    b.paused = 'no' 
order by 
    {$campaignMessagesToSendOrdering}; 

請你能解釋一下什麼是LEFT JOIN campaigns_email是幹什麼的?它是否正在對contacts_people進行交叉連接?對於我來說,快速瞭解這一點,任何人都可以在不使用campaigns_email的情況下重寫它。

我知道你可以在from子句中有多個表,但我從來沒有見過這樣的情況。

謝謝。

+0

這裏沒有人會爲你做你的工作,在這裏人們幫助你找到問題的解決方案,你試圖解決。至少檢查Dokumentation befor尋求幫助[加入Doku MySql](http://dev.mysql.com/doc/refman/5.1/de/join.html) –

+0

@winner_joiner在問題中,他問你要做什麼他爲他工作? – jerdiggity

+0

'任何人請重寫它,沒有,campaigns_email.' - 重寫沒有這個連接的查詢意味着不只是從查詢中刪除這個:',campaigns_email b' – AD7six

回答

3

此查詢有一個左連接 CROSS JOIN(使用"comma syntax")。

有關部分語義相同:

FROM campaigns_email_logs cel 
LEFT JOIN contacts_people cp ON (cel.person_id = cp.id) 
CROSS JOIN campaigns_email b 

所以,現在我們有一個CROSS JOIN,讓我們的條件考慮設立:

WHERE2 cel.campaign_id = b.id .. 

繁榮。就在那裏,我們已經把這個CROSS JOIN變成了一個INNER JOIN。重寫:

FROM campaigns_email_logs cel 
LEFT JOIN contacts_people cp ON (cel.person_id = cp.id) 
JOIN campaigns_email b ON (cel.campaign_id = b.id) 
+0

謝謝大家的幫助。難以置信的迴應。我認爲這可能是一個交叉連接 - 我從來沒有像這樣做過。抱歉,錯字「c.id」應該是「cp.id」。我不得不讓它有點可讀性,忘記更新它。 –

0

campaigns_email不是左連接到任何東西。

在你的FROM條款中,你有一個JOINcampaigns_email_logscontacts_people之間。然後你有一個cartessian產品與這個加入的結果和campaigns_email