在 SQL 中使用 AND 和 OR 时遇到问题。
这将返回一条记录:
SELECT DISTINCT s.SupplierName, r.ReturnID, r.Reason
FROM tblSupplierProducts sp, tblReturns r,tblSuppliers s
WHERE sp.SupplierID=s.SupplierID
AND sp.ProductID=r.productID
AND r.Reason LIKE 'Unreadable disk'
这也返回一条记录:
SELECT DISTINCT s.SupplierName, r.ReturnID, r.Reason
FROM tblSupplierProducts sp, tblReturns r,tblSuppliers s
WHERE sp.SupplierID=s.SupplierID
AND sp.ProductID=r.productID
AND r.Reason LIKE 'Scratched CD'
但是当我这样做时:
SELECT DISTINCT s.SupplierName, r.ReturnID, r.Reason
FROM tblSupplierProducts sp, tblReturns r,tblSuppliers s
WHERE sp.SupplierID=s.SupplierID
AND sp.ProductID=r.productID
AND r.Reason LIKE 'Unreadable disk'
OR r.Reason LIKE 'Scratched CD'
“不可读的磁盘”结果存在且正确,但“划痕 CD”不存在(对于每个不同的供应商,它具有相同的值)如果我像这样交换它们:
SELECT DISTINCT s.SupplierName, r.ReturnID, r.Reason
FROM tblSupplierProducts sp, tblReturns r,tblSuppliers s
WHERE sp.SupplierID=s.SupplierID
AND sp.ProductID=r.productID
AND r.Reason LIKE 'Scratched CD'
OR r.Reason LIKE 'Unreadable disk'
我的第一个结果是正确的('Scratched CD'),而'Unreadable disk' 为多个供应商返回相同的值。
似乎 AND OR 搞乱了查询,为什么?