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

  • Home
  • SEARCH
  • 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 882575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:26:54+00:00 2026-05-15T12:26:54+00:00

I have to filter orders which do not have a specific product. It is

  • 0

I have to filter orders which do not have a specific product.

It is simple, but the problem is that every order could have many lines, containing different products.

Here is an example of my data:

ID | Product | Customer      | Quantidy | Date
 1 | Apple   | Alex Sorensen | 3        | 17.4.2009
 2 | Orange  | Alex Sorensen | 1        | 17.4.2009
 3 | Lime    | Alex Sorensen | 4        | 17.4.2009
 4 | Apple   | Fred Jonsson  | 1        | 30.5.2010
 5 | Lime    | Fred Jonsson  | 7        | 30.5.2010
ect...

Lines with the same date and the same customer are for the same order.

How can I find all the orders which do not have (for example) Orange in their order?


My own (not working) MySQL-code:

SELECT o.ID, k.Customer, o.Quantidy, p.Product, o.Date
FROM Products p, Orders o, Customers c
WHERE p.ID = o.ID
AND k.Customer = o.Customer
AND p.Product NOT IN ('Orange')
GROUP BY o.Date
ORDER BY o.ID DESC

The problem is, that even though I don’t want “Alex Sorensen’s” order, because it contains oranges, I get his other lines without the one containing “Orange”.

I need an SQL-code to give me “Fred Jonsson”s and the other orders, that don’t have oranges in the order.

  • 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-05-15T12:26:55+00:00Added an answer on May 15, 2026 at 12:26 pm

    You should be able to use NOT EXISTS:

    SELECT o.ID, c.Customer, o.Quantity, p.Product, o.Date
    FROM Products p, Orders o, Customers c
    WHERE p.product = o.product
    AND c.Customer = o.Customer
    AND NOT EXISTS ( SELECT 1 FROM Orders o2
                     WHERE o2.product = 'Orange'
                     AND o2.customer = o.customer
                     AND o2.date = o.date
                   )
    

    Try to use explicit join syntax by the way, it will make reading your queries easier once you are used to it:

    SELECT o.ID, c.Customer, o.Quantity, p.Product, o.Date
    FROM Orders o
    JOIN Products p ON ( p.product = o.product )
    JOIN Customers c ON ( c.Customer = o.Customer )
    WHERE NOT EXISTS ( SELECT 1 FROM Orders o2
                       WHERE o2.product = 'Orange'
                       AND o2.customer = o.customer
                       AND o2.date = o.date
                     )
    

    Another approach is to use a Left Join and check for NULL:

    SELECT o.ID, c.Customer, o.Quantity, p.Product, o.Date
    FROM Orders o
    JOIN Products p ON ( p.product = o.product )
    JOIN Customers c ON ( c.Customer = o.Customer )
    LEFT JOIN Orders o2 ON (     o2.product = 'Orange'
                             AND o2.customer = o.customer
                             AND o2.date = o.date
                           )
    WHERE o2.Id IS NULL
    

    You will have to try which one performs better.

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

Sidebar

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.