I'm almost certain that the issue with my SQL code is something very minor, but I've been attempting to get this query to run correctly for hours to no avail. I have a table that has 3 columns: Maker, Model, and Type. I'm attempting to write a query that outputs the "Makers" that produce "PCs" or "Laptops", but NOT "Printers". I'll post my code below.
SELECT DISTINCT Product.maker
FROM Product
WHERE (((Product.type)="pc" Or (Product.type)="laptop")) AND ((Product.type)<>"printer");
This is a much smaller, modified table similar to what I'm working with:
maker model type
A 1001 pc
A 1002 pc
C 1007 pc
D 1008 pc
E 2003 laptop
A 2004 laptop
E 3003 printer
D 3005 printer
H 3006 printer
Any assistance would be greatly appreciated. Thank you!
you can use NOT EXISTS for this
SELECT DISTINCT P.maker
FROM Product P
WHERE (P.type="pc") Or (P.type="laptop")
AND NOT EXISTS
( SELECT 1 FROM Product P1
WHERE P.maker = P1.maker
and P1.type ="printer"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With