superuseroi
superuseroi

Reputation: 1348

MySQL select data across 4 tables (multiple conditions)

Thanks to another user I was finally able to collect some data using this query:

SELECT r.form, s.value as email
FROM subrecords s join
     records r
     on s.record = r.id AND r.name = 'question-form'
WHERE s.title = 'email'
GROUP BY s.value, r.form

details on the tables involved in the above query are found at Finding duplicates in MYSQL table where data is in multiple tables (multiple conditions needed)

With the above query I get the list of emails that submitted a specific form.

I would need now to find out which of those email addresses is subscribed to a specific mailing list, Using the "s.value" of the above query which lists email addresses

I first need to find out the subscriber.subid which identifies each unique subscriber and their email address, which is where I would join the result from the query above

table -> subscriber schema

subid | email

Then select from the following table WHERE listid = '33'

table -> listsub schema

listid | subid | subdate | unsubdate | status

Thank you so much everyone for the incredible help!

Upvotes: 1

Views: 285

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Here is a way by doing more joins:

SELECT r.form, s.value as email,
       (case when max(l.listid is not null) then 'YES' else 'NO' end) as InList33
FROM subrecords s join
     records r
     on s.record = r.id AND r.name = 'question-form' left outer join
     subscriber_schema ss
     on ss.email = s.value left outer join
     listsub l
     on ss.subid = l.subid and
        l.listid = '33'
WHERE s.title = 'email'
GROUP BY s.value, r.form;

Upvotes: 1

Related Questions