简体   繁体   中英

3 table left outer join doesn't work with subquery

A brief description... I have 4 tables. "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).

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. 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. Any tips?

Thanks!!

(PS: I'll edit and give more info if needed.)

EDIT The answer (thank you, ysrb) is changing my where clause:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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