简体   繁体   中英

Five tables all from first table, join others with max value from 5th table when exists

I have five tables:

person

person_id (pk) 
lname
fname 
address

person_category

persCatID (pk)
category_id
person_id 

member

member_id (pk)
person_id
expires

mailing_person

mailing_id
person_id
flag_mailed

mailing

mailing_id (pk)
mailing_type_id
date_mailed
flag_mailed
date_entry

I want all values from the person table where in person_category the category_id = 1 and in mailing the mailing_type_id = 1, the expires from member when in member the person_id = the person table person_id and IF there exists a mailing_id in mailing_person for a particular person_id in Person, get the date_mailed from mailing .

Mailing_person contains many-to-many, as entries in person can have many different types of entries in mailing_person, I want only the one where the date_entry is the most recent.

This is my query, but I'm not getting any results:

    SELECT distinct p.person_id, fname_mi, lname, 
           company_name, addr1, addr2, city, st, zip, 
           greeting, email, expires, 
           MAX(DATE_FORMAT(date_mailed,'%m/%d/%Y')) as mailingDate
      FROM person p, mailing g
INNER JOIN person_category c ON c.person_id = p.person_id
INNER JOIN member m ON m.person_id = p.person_id
 LEFT JOIN mailing_person i ON i.person_id = p.person_id
 LEFT JOIN mailing g on g.mailing_id = i.mailing_id
     WHERE category_id = 1
           and mailing_type_id = 1

It's not really clear what you are trying to do. Specifically... do you want the max date_mailed of mailing with mailing_type_id=1, if any... or do you want persons that are related with a mailing with mailing_type_id=1, and if so, then the max date_mailed of any mailing they are related to?

Some problems I noticed in your query are:

  • double reference to mailing table, with same alias... ambiguous
  • MAX without a GROUP BY, but with a DISTINCT... that's akward
  • MAX(DATE_FORMAT(...)), why? what type is your date_mailed column?

Here are some queries that may solve your problem, depending on what you want exactly.

The following will return only persons that are related to any mailing with type = 1, and will give you the max date_mailed only of mailings with type = 1. The condition on mailing_type can be in the ON clause or in the WHERE clause. It doesn't matter. But notice that LEFT JOINs are useless, as you are putting a condition on a field on the RIGHT table:

SELECT
  p.person_id,
  p.fname,
  p.lname, 
  p.company_name,
  p.addr1,
  p.addr2,
  p.city,
  p.st,
  p.zip,
  p.greeting,
  p.email,
  m.expires, 
  MAX(m.date_mailed) AS max_date_mailed
FROM person          p
JOIN member          m  ON m.person_id  = p.person_id
JOIN person_category pc ON pc.person_id = p.person_id
JOIN mailing_person  mp ON mp.person_id = p.person_id
JOIN mailing         g  ON g.mailing_id = mp.mailing_id
WHERE pc.category_id = 1 AND g.mailing_type_id = 1
GROUP BY
  p.person_id,
  p.fname,
  p.lname, 
  p.company_name,
  p.addr1,
  p.addr2,
  p.city,
  p.st,
  p.zip,
  p.greeting,
  p.email,
  m.expires

The following query uses a SUBSELECT, but does something different. It returns all persons with category_id = 1, and of those the max date_mailed of any mailing with mailing_type = 1:

SELECT
  p.person_id,
  p.fname,
  p.lname, 
  p.company_name,
  p.addr1,
  p.addr2,
  p.city,
  p.st,
  p.zip,
  p.greeting,
  p.email,
  m.expires, 
  (SELECT MAX(g.date_mailed)
   FROM mailing         g
   JOIN mailing_person  mp ON mp.mailing_id = g.mailing_id
   WHERE mp.person_id = p.person_id AND g.mailing_type=1
  ) AS max_date_mailed
FROM      person          p
JOIN      member          m  ON m.person_id  = p.person_id
JOIN      person_category pc ON pc.person_id = p.person_id
WHERE pc.category_id = 1

There are still other variants you could come up with... it really depends on what you want.

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