Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql选择不在表中的行错误_Mysql - Fatal编程技术网

Mysql选择不在表中的行错误

Mysql选择不在表中的行错误,mysql,Mysql,我有以下模式: product: id , title client: id, name client_has_product:id, id_product, id_client, date. 我想得到一份不属于某个客户的所有产品的清单 我正在运行以下查询: SELECT DISTINCT product.id, product.title, client.id FROM product,client WHERE product.id NOT IN ( SELECT id_product FR

我有以下模式:

product: id , title
client: id, name
client_has_product:id, id_product, id_client, date.
我想得到一份不属于某个客户的所有产品的清单

我正在运行以下查询:

SELECT DISTINCT product.id, product.title, client.id
FROM product,client
WHERE product.id NOT IN (
SELECT id_product FROM client_has_product WHERE id_client = 1)
GROUP BY product.id
问题是,如果client\u has\u product表为空,则查询将不返回任何内容,但如果client\u has\u product表中只有一行,则查询将工作


有人能解释一下我在这里遗漏了什么和/或为什么会这样吗?

这里有问题的一行是

WHERE product.id NOT IN (
SELECT id_product FROM client_has_produc WHERE id_client)
因为当发生错误时,这将始终返回false

WHERE product.id NOT IN ( null )
此外,这种没有交集的表的并集将返回所有行的配对

FROM product,client
即使有两个或多个客户拥有相同的产品,这也会始终产生一种产品

GROUP BY product.id
解决方案是对所有表进行
连接
,然后过滤掉特定的客户机

SELECT DISTINCT product.id, product.title, client.id
FROM product
LEFT JOIN client_has_produc ON product.id = client_has_produc.id_product
LEFT JOIN client ON client.id = client_has_produc.id_client
WHERE client_has_product.id_client <> 1
选择不同的product.id、product.title、client.id
从产品
左连接客户端\u在product.id上有\u produc=客户端\u有\u produc.id\u产品
client.id上的左连接客户端=客户端\u具有\u produc.id\u客户端
其中client_有_product.id_client 1

您应该将NOT IN替换为JOIN,下面是一个示例:

/无疑问/

/联接查询/:-


希望有帮助

你的客户在哪里?查询看起来不完整我修复了它,很抱歉:-)使用join而不是子查询并筛选空值从我从您的问题中了解到,您需要映射的客户端和产品的记录,这意味着,如果客户端中没有记录,那么显然将没有输出,因为没有映射。或者,如果您想要产品结果,即使没有关联的客户机,您也可以在客户机上使用外部联接来获得产品结果,除了where子句中指定的一个特定客户机之外。所以,连接这三个表并使用where子句排除客户机并使其变得简单:)
USE AdventureWorks;
GO
SELECT ProductID
FROM Production.Product
WHERE ProductID
NOT IN (
SELECT ProductID
FROM Production.WorkOrder);
SELECT p.ProductID
FROM Production.Product p
LEFT JOIN Production.WorkOrder w ON p.ProductID = w.ProductID
WHERE w.ProductID IS NULL;