简体   繁体   English

3 表左外连接不适用于子查询

[英]3 table left outer join doesn't work with subquery

A brief description... I have 4 tables.简要说明...我有 4 张桌子。 "contacts" (a list of each person, unique IDs), contact_phones (multiple telephone numbers for each contact, joins on contact_id), and contact_communication (each time we've spoken to this contact, joins on contact_id), and schools (a list of schools, joins schools.school_id = contacts.contact_id). “联系人”(每个人的列表,唯一 ID)、contact_phones(每个联系人的多个电话号码,加入 contact_id)和contact_communication(每次我们与此联系人交谈时,加入 contact_id)和学校(a学校列表,加入schools.school_id = contacts.contact_id)。

What I need: I need to look up an individual school.我需要什么:我需要查找一所单独的学校。 For that school, I need a list of each person that goes there, their main telephone number, and the last communication we had with them (if any).对于那所学校,我需要一份去那里的每个人的名单,他们的主要电话号码,以及我们与他们的最后一次联系(如果有的话)。

The problem is, if we have had NO communication with them, they don't show up in the list.问题是,如果我们没有与他们沟通,他们就不会出现在列表中。 If I take out the "AND" statement in the "WHERE" clause, then I get more than one communication record.如果我去掉“WHERE”子句中的“AND”语句,那么我得到的通信记录不止一个。 I only want the latest communication record, but I want all the contacts.我只想要最新的通讯记录,但我想要所有的联系人。 Some contacts don't have a communication record though.但有些联系人没有通讯记录。

This is my query:这是我的查询:

SELECT c.id,
            c.f_name,
            c.l_name,
            c.address1,
            c.address2,
            c.city,
            c.state,
            c.zip,
            c.tel,
            c.school_id,
            c.email,
                ct.tel,
                cc.date,
                cc.reason,
                cc.result,
                cc.caller
                FROM contacts AS c
                LEFT OUTER JOIN contact_phones AS ct ON c.id = ct.contact_id
                LEFT OUTER JOIN contact_communication AS cc ON c.id = cc.contact_id 
            WHERE school_id = '$schoolId' AND 
                cc.id IN (SELECT MAX(id) FROM contact_communication WHERE contact_id = c.id)
                ORDER BY cc.date DESC

The problem is, this query gives me only the latest communication (which is all I want) but won't list contacts that have no communication.问题是,这个查询只给我最新的通信(这就是我想要的),但不会列出没有通信的联系人。

I've been at this for 3 days.我已经在这呆了3天。 Any tips?有小费吗?

Thanks!!谢谢!!

(PS: I'll edit and give more info if needed.) (PS:如果需要,我会编辑并提供更多信息。)

EDIT The answer (thank you, ysrb) is changing my where clause:编辑答案(谢谢,ysrb)正在改变我的 where 子句:

SELECT c.id,
            c.f_name,
            c.l_name,
            c.address1,
            c.address2,
            c.city,
            c.state,
            c.zip,
            c.tel,
            c.school_id,
            c.email,
                ct.tel,
                cc.date,
                cc.reason,
                cc.result,
                cc.caller
                FROM contacts AS c
                LEFT OUTER JOIN contact_phones AS ct ON c.id = ct.contact_id
                LEFT OUTER JOIN contact_communication AS cc ON c.id = cc.contact_id 
            WHERE school_id = '$schoolId' AND 
                (cc.id IN (SELECT MAX(id) FROM contact_communication WHERE contact_id = c.id) OR cc.id IS NULL) 
                ORDER BY cc.date DESC

Try:尝试:

SELECT c.id,
            c.f_name,
            c.l_name,
            c.address1,
            c.address2,
            c.city,
            c.state,
            c.zip,
            c.tel,
            c.school_id,
            c.email,
                ct.tel,
                cc.date,
                cc.reason,
                cc.result,
                cc.caller
                FROM contacts AS c
                LEFT OUTER JOIN contact_phones AS ct ON c.id = ct.contact_id
                LEFT OUTER JOIN contact_communication AS cc ON c.id = cc.contact_id 
            WHERE school_id = '$schoolId' AND 
                (cc.id = (SELECT MAX(id) FROM contact_communication WHERE contact_id = c.id) OR cc.ID IS NULL)
            ORDER BY cc.date DESC

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

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