om komawar om komawar - 4 months ago 9
MySQL Question

AND operator giving wrong output in SQL

I'm firing this query

SELECT *
FROM Orders WHERE (CustomerID <> 51) AND (ShipperID <> 3);


and it is not showing CUSTOMERS WITH 51 with different shipperid

Why?
is their any different view of AND in sql

so what if i want to exclude records with customerid with 51 and shipper id with 3 but show customerid with 51 and shipper excluding 3?

Answer

As you stated it is not showing CUSTOMERS WITH 51 with different shipperid I think you rather mean to say

SELECT * FROM Orders WHERE CustomerID = 51

so what if i want to exclude records with customerid with 51 and shipper id with 3 but show customerid with 51 and shipper excluding 3?

Then change your condition to be

SELECT * FROM Orders 
WHERE (CustomerID <> 51 AND ShipperID <> 3)
OR (CustomerID = 51 AND ShipperID <> 3);
Comments