Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:40:15+00:00 2026-06-16T00:40:15+00:00

I’m trying to run a query to select customer audience, but it should select

  • 0

I’m trying to run a query to select customer audience, but it should select the customers who didn’t get an email before. The email tracking comes from another table. This is the original query:

SELECT 
  c.customers_firstname, 
  c.customers_lastname, 
  o.orders_id, 
  o.customers_id, 
  c.customers_email_address
FROM 
  orders o, 
  customers c,  
  order_status s
WHERE 
  o.customers_id = c.customers_id
  AND o.orders_id = s.orders_id
  AND o.orders_status = s.orders_status_id 
ORDER BY 
  o.orders_id ASC

Now, I need to check another table called tracking and see if the customer already exists in that table and if so, skip it.

This is what I’ve tried, but it doesn’t seem to work:

SELECT 
   c.customers_firstname, 
   c.customers_lastname, 
   o.orders_id, 
   o.customers_id, 
   c.customers_email_address
FROM 
   orders o, 
   customers c 
INNER JOIN 
   tracking t 
ON 
   c.customers_id = t.customers_id,  
   order_status s
WHERE 
   o.customers_id = c.customers_id
   AND o.orders_id = s.orders_id
   AND o.orders_status = s.orders_status_id
   AND c.customers_id NOT LIKE t.customers_id
ORDER BY 
   o.orders_id ASC

What am I doing wrong? Or is there any way to do this better?

ADDED: I totally forgot one more important factor – tracking table has “module” column and I need results only from “contact” module. So, in other words, I need to filter out customers who already exist in the tracking table, but only if associated with contact module, not any other module.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:40:16+00:00Added an answer on June 16, 2026 at 12:40 am

    This is equivalent to your original query:

    SELECT c.customers_firstname
         , c.customers_lastname
         , o.orders_id
         , o.customers_id
         , c.customers_email_address
      FROM orders o
      JOIN customers c 
        ON c.customers_id = o.customers_id
      JOIN order_status s 
        ON s.orders_id = o.orders_id
       AND s.orders_status_id = o.orders_status
     ORDER
        BY o.orders_id ASC
    

    Add an anti-join

    To meet your specification, you can use an “anti-join” pattern. We can add this to the query, before the ORDER BY clause:

      LEFT
      JOIN tracking t
        ON t.customers_id = o.customers_id
     WHERE t.customers_id IS NULL   
    

    What that’s going to do is find all matching rows from the tracking table, based on the customers_id. For any rows that the query doesn’t find a matching row(s) in the tracking table, it will generate a dummy row from tracking which consists of all NULL values. (That’s one way of describing what an OUTER JOIN does.)

    The “trick” now is to throw out all the rows that matched. And we do that by checking for a NULL value of customers_id from the tracking table (in the WHERE clause). For a match, that column won’t be NULL. (The equals comparison in the join predicate guarantees us that.) So we know that if we get a NULL value for t.customers_id, that there wasn’t a match.

    So, this query returns the specified result set:

    SELECT c.customers_firstname
         , c.customers_lastname
         , o.orders_id
         , o.customers_id
         , c.customers_email_address
      FROM orders o
      JOIN customers c 
        ON c.customers_id = o.customers_id
      JOIN order_status s 
        ON s.orders_id = o.orders_id
       AND s.orders_status_id = o.orders_status
      LEFT
      JOIN tracking t
        ON t.customers_id = o.customers_id
     WHERE t.customers_id IS NULL   
     ORDER
        BY o.orders_id ASC
    

    Other approaches

    There are other approaches, but the anti-join is frequently the best performer.

    Some other options are a NOT EXISTS predicate and a NOT IN predicate. I can add those, though I expect those solutions will be provided in other answers before I get around to it.


    Starting with that first query (equivalent to the query in your question), we could also use a NOT EXISTS predicate. We’d add this before the ORDER BY clause:

     WHERE NOT EXISTS 
           ( SELECT 1
               FROM tracking t
              WHERE t.customers_id = o.customers_id
           )
    

    To use a NOT IN predicate, again, add this before the ORDER BY clause:

     WHERE o.customers_id NOT IN
           ( SELECT t.customers_id
               FROM tracking t
              WHERE t.customers_id IS NOT NULL
           )
    

    (You may have some guarantee that tracking.customers_id is not null, but in the more general case, it’s important that the subquery NOT return a NULL value, so we include a WHERE clause so that we have that guaranteed.)

    With appropriate indexes, the anti-join pattern usually performs better than either the NOT EXISTS or the NOT IN, but not always.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.