?
Solved

mysql join where second table condition does not exist

Posted on 2009-05-16
5
Medium Priority
?
326 Views
Last Modified: 2013-12-12
I have a table contact with key i_contact
I have a table schedule with a field i_contact
I need all contact.i_contact ids that meet certain criteria AND where they do not have a record in schedule with certain criteria.  There may be records in schedule, but I need those that do not have certain characteristics.

For example,
SELECT c.i_contact, c.i_broker, c.fname, c.lname, c.email, c.hphone, c.cphone, c.state, c.referral_source, c.fico, c.amort, c.profile_completed, c.paid, c.created, c.is_customer, c.deleted from contact
INNER JOIN primary_home as p on p.i_contact=c.i_contact AND p.isLiquid=1
LEFT OUTER JOIN schedule as s on s.i_contact=c.i_contact
AND (s.task Like '%meeting%' or s.task Like '%go to%' or s.task Like '%goto%' or s.task Like '%online%')
WHERE c.deleted=0
This gives me (I think) the data on contacts who's primary_home field isLiquid=1 and they have a record in schedule like the parameters listed above.  What I need is 2 things:
1.  am I right with the first query
2.  how do I reverse that and have it give me only those contacts who'se primary_home field isLiquid=1 who may or may not have records in schedule, but who do NOT have any record in schedule where the task is like (meeting, online, goto, go to)
0
Comment
Question by:thewebwench
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:racek
ID: 24404223

SELECT c.i_contact, c.i_broker, c.fname, c.lname, c.email, c.hphone, c.cphone, c.state, c.referral_source, c.fico, c.amort, c.profile_completed, c.paid, c.created, c.is_customer, c.deleted 
from contact c
INNER JOIN primary_home as p on p.i_contact=c.i_contact AND p.isLiquid=1
LEFT OUTER JOIN schedule as s on s.i_contact=c.i_contact
AND (s.task Like '%meeting%' or s.task Like '%go to%' or s.task Like '%goto%' or s.task Like '%online%')
WHERE c.deleted=0 
and s.i_contact IS NULL

Open in new window

0
 
LVL 14

Accepted Solution

by:
racek earned 1600 total points
ID: 24404231

SELECT c.i_contact, c.i_broker, c.fname, c.lname, c.email, c.hphone, c.cphone, c.state, c.referral_source, c.fico, c.amort, c.profile_completed, c.paid, c.created, c.is_customer, c.deleted 
from contact c
INNER JOIN primary_home as p on p.i_contact=c.i_contact AND p.isLiquid=1
LEFT OUTER JOIN schedule as s on s.i_contact=c.i_contact
WHERE c.deleted=0 
AND NOT (s.task Like '%meeting%' or s.task Like '%go to%' or s.task Like '%goto%' or s.task Like '%online%')

Open in new window

0
 

Author Comment

by:thewebwench
ID: 24404276
what's the difference between the two?  I think the first one is more right.  I get about expected results (not exact, but about) where with the second one I get hundreds more results -- I get all contacts with rows in schedule where those rows don't equal "online, meeting, etc."

I should be getting 45 results (unless my first query is not right)
I'm getting 42 with your query
0
 

Author Comment

by:thewebwench
ID: 24404293
never mind, I've gotten it to match; THANKS
0
 

Author Comment

by:thewebwench
ID: 24404297
I accidentally marked the wrong solution; it should have been the first one.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question