Customer does own none of these 2 products

Hello,

In addition to my previous question (which received a great answer), I now need to select all users that own NONE of the selected productIDs

Products: Table products
ID
Name

Order Table orders
ID
clientID
address etc.

Order Lines (products in order) orders_lines
orderID
productID

Clients table clients
ID
Name
Etc.

Now let’s say I want to select the clients that have not ordered BOTH these 2 products:
2: Motor
735: Pasta
What query do I use to select all clients that have NONE of these product in the orders_lines table?

Help is much appreciated! (again) :smile:

(Yes, I do need ANY of these products as well, but I’m not to bothered to setup another topic after this one, I hope you guys are still willing to help out :smile:

I suspect it would’ve been better/easier to ask all three questions in a single topic, since it’s dealing with the exact same data set, but I could be wrong.

same as @oddz’s solution to your other query, except this time with COUNT(*)=0

I’m testing this now…

And the query when they do not own either one of the 2 products?

I’m aware of it and this might be a good idea, I was expecting totally different answers for both questions though…

@r937 this doesn’t seem to be working.

it just selects all clientsthat own any of those products but are cancelled out by count(*)=0, so instead of selecting all client that are not fetched by that query it selects no clients. It should do the exact opposite…

oops, i forgot to mention it has to be a LEFT OUTER JOIN

try this –

SELECT o.clientID FROM orders AS o LEFT OUTER JOIN orders_lines AS i ON i.orderID = o.ID AND i.productID IN (1,23,163) GROUP BY o.clientID HAVING COUNT(i.productID) = 0

this still doesn’t work, this part of your query assumes it would select all clients, but it doesn’t, it just selects the one that actually ordered these:

It should select all clients and cancel out the ones that ordered one of the selected IDs

[quote=“Sergeant, post:8, topic:193483, full:true”]
this still doesn’t work[/quote]
i bet it actually does, but you changed it somehow

you sure you used AND and not WHERE?

you sure you included the HAVING?

You’re right. Because you said it was the same as the previous topic answer I left in WHERE, now it gives the result I’m looking for :smile:

In addition to that, I’m looking for the way to select all clients that do not own AT LEAST ONE of the selected productIDs, so they are allowed to have the Motor, but when they do not own the flowers, they still get selected.

Could you please confirm if this is indeed the correct way to select all clients that do not own at least one of the productIDs? Help is much appreciated :slight_smile:

SELECT o.clientID 
FROM orders AS o
LEFT OUTER
JOIN orders_lines AS i
ON i.orderID = o.ID
AND i.productID IN (1,23,163)
  GROUP BY o.clientID
HAVING COUNT(i.productID) < 3

[quote=“Sergeant, post:11, topic:193483, full:true”]
Could you please confirm if this is indeed the correct way to select all clients that do not own at least one of the productIDs? [/quote]
what happened when you tested it? ™ © ®

1 Like

well i think it gave me the right results, but since things get so complicated in the actual real life software I’m just looking for a confirmation from a theoretical standpoint…

[quote=“Sergeant, post:14, topic:193483, full:true”]
well i think it gave me the right results[/quote]
those results probably were not right

you’re getting “owned fewer than 3”

that’s not the same as “do not own at least one”

the query for the latter was given earlier in this thread in post #7

If they do not own all three, than they own at least not one of them, I actually think that:
fewer than 3 == not all
not all != none (answer 7)
not all == does not own at least one of the productIDs

Answer 7 gives me all clients that own NONE of the selected products.
I’m now looking for clients that do not own all of the selected products. Less than 3 (=amount of productIDs in the query) is actually the correct statement for this I believe. Correct me if i’m wrong.

Maybe i should have created another topic to avoid the confusion (asking 2 things).

well, i can sort of see your interpretation now, but it’s still awkwardly expressed

“do not own at least one” means do not own any

“do not own all three” means might own 1 or 2

your sql is fine but your english is ambiguous

Omg, you’re right, my English IS ambigous!

Thank you for the feedback, this topic is now fully answered :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.