如何在关系 SQL 查询中使用 NOT
How to use NOT in relational SQL query
当我这样 select table 时:select count(*) from products
是 returning 12900 结果。
我有一个关系查询 return 多个 table 关系结果如下:
SELECT category.name,
manifacturer.name,
supplier.name,
product.name
FROM products as product,
suppliers as supplier,
manifacturers as manifacturer,
categories as category
WHERE product.sid=supplier.id AND
product.manid = manifacturer.id AND
product.catid = category.id
本次查询returns 12873 结果,
所以我找不到哪个数据不匹配。我怎样才能找到这些丢失的数据?我使用 NOT 查询但没有 return 任何结果。
首先,您应该学会使用正确、明确的 join
语法:
SELECT category.name, manifacturer.name, supplier.name, product.name
FROM products as product join
suppliers as supplier
on product.sid = supplier.id join
manifacturers as manifacturer
on product.manid = manifacturer.id join
categories as category
on product.catid = category.id;
然后,如果您想要不匹配项,请切换到 left join
并在 where
子句中查找不匹配项:
SELECT category.name, manifacturer.name, supplier.name, product.name
FROM products as product left join
suppliers as supplier
on product.sid = supplier.id left join
manifacturers as manifacturer
on product.manid = manifacturer.id left join
categories as category
on product.catid = category.id
WHERE supplier.id IS NULL OR manifacturer.id IS NULL or category.id IS NULL;
以下查询 returns 其产品不在您的查询中的记录:
SELECT *
FROM Products P
WHERE (P.sid,P.mainid,P.catid) NOT IN (
SELECT DISTINCT product.sid
,product.mainid
,product.catid
FROM products AS product
,suppliers AS supplier
,manifacturers AS manifacturer
,categories AS category
WHERE product.sid = supplier.id
AND product.manid = manifacturer.id
AND product.catid = category.id
)
您可以看到不匹配的记录与 FULL OUTER JOIN 是这样的:
SELECT prod.id, rel.id FROM (
SELECT category.name,
manifacturer.name,
supplier.name,
product.name
FROM products as product,
suppliers as supplier,
manifacturers as manifacturer,
categories as category
WHERE product.sid=supplier.id AND
product.manid = manifacturer.id AND
product.catid = category.id
) as rel
FULL OUTER JOIN products as prod
ON rel.id = prod.id
因此您可以在列表中看到 null id 而不是 null id。
除了我上面关于别名的评论之外,您还应该使用 ANSI-92 中引入的 "newer" 连接语法。请注意这里用于同一事物的代码少了多少。您编写代码的方式所有连接都是内部连接,因为您想要 return 行没有匹配我将它们更改为左连接。
SELECT c.name,
m.name,
s.name,
p.name
FROM products p
left join suppliers s on p.sid = s.id
left join manifacturers m on p.manid = m.id
left join categories c on p.catid = c.id
当我这样 select table 时:select count(*) from products
是 returning 12900 结果。
我有一个关系查询 return 多个 table 关系结果如下:
SELECT category.name,
manifacturer.name,
supplier.name,
product.name
FROM products as product,
suppliers as supplier,
manifacturers as manifacturer,
categories as category
WHERE product.sid=supplier.id AND
product.manid = manifacturer.id AND
product.catid = category.id
本次查询returns 12873 结果,
所以我找不到哪个数据不匹配。我怎样才能找到这些丢失的数据?我使用 NOT 查询但没有 return 任何结果。
首先,您应该学会使用正确、明确的 join
语法:
SELECT category.name, manifacturer.name, supplier.name, product.name
FROM products as product join
suppliers as supplier
on product.sid = supplier.id join
manifacturers as manifacturer
on product.manid = manifacturer.id join
categories as category
on product.catid = category.id;
然后,如果您想要不匹配项,请切换到 left join
并在 where
子句中查找不匹配项:
SELECT category.name, manifacturer.name, supplier.name, product.name
FROM products as product left join
suppliers as supplier
on product.sid = supplier.id left join
manifacturers as manifacturer
on product.manid = manifacturer.id left join
categories as category
on product.catid = category.id
WHERE supplier.id IS NULL OR manifacturer.id IS NULL or category.id IS NULL;
以下查询 returns 其产品不在您的查询中的记录:
SELECT *
FROM Products P
WHERE (P.sid,P.mainid,P.catid) NOT IN (
SELECT DISTINCT product.sid
,product.mainid
,product.catid
FROM products AS product
,suppliers AS supplier
,manifacturers AS manifacturer
,categories AS category
WHERE product.sid = supplier.id
AND product.manid = manifacturer.id
AND product.catid = category.id
)
您可以看到不匹配的记录与 FULL OUTER JOIN 是这样的:
SELECT prod.id, rel.id FROM (
SELECT category.name,
manifacturer.name,
supplier.name,
product.name
FROM products as product,
suppliers as supplier,
manifacturers as manifacturer,
categories as category
WHERE product.sid=supplier.id AND
product.manid = manifacturer.id AND
product.catid = category.id
) as rel
FULL OUTER JOIN products as prod
ON rel.id = prod.id
因此您可以在列表中看到 null id 而不是 null id。
除了我上面关于别名的评论之外,您还应该使用 ANSI-92 中引入的 "newer" 连接语法。请注意这里用于同一事物的代码少了多少。您编写代码的方式所有连接都是内部连接,因为您想要 return 行没有匹配我将它们更改为左连接。
SELECT c.name,
m.name,
s.name,
p.name
FROM products p
left join suppliers s on p.sid = s.id
left join manifacturers m on p.manid = m.id
left join categories c on p.catid = c.id