Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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
Php MySQL中使用过滤器内部连接多对多表_Php_Mysql - Fatal编程技术网

Php MySQL中使用过滤器内部连接多对多表

Php MySQL中使用过滤器内部连接多对多表,php,mysql,Php,Mysql,表结构…这里只有3个表。。购买、客户、产品 bought table structure : fields (id,product_id,customer_id), customer table structure: (customer_id,name,address) , product table structure: (product_id,name). 产品表中包括产品a和b。从“购买”表中,我希望获得购买产品“a”但未购买产品“b”的客户id 使用mysql或类似工具,您将如何

表结构…这里只有3个表。。购买、客户、产品

bought table structure : fields (id,product_id,customer_id), 
customer table structure: (customer_id,name,address) , 
product table structure: (product_id,name). 
产品表中包括产品a和b。从“购买”表中,我希望获得购买产品“a”但未购买产品“b”的客户id


使用mysql或类似工具,您将如何选择购买了产品“a”但未购买产品“b”的所有客户,而不使用子选择。确保结果中没有客户购买产品“b”。。请帮助我

我不确定,你想按产品名称筛选吗?也许我在你的问题中遗漏了什么,但我认为这应该行得通

select customer_id 
from
bought b
inner join customer c on b.customer_id = b.customer_id
inner join product p on p.product_id = b.product_id
where p.name = 'a'
我想找到购买了产品“a”但没有购买的客户 产品“b”

试试这个:

SELECT * 
FROM Customers c
INNER JOIN
(
     SELECT * 
     FROM bought 
     WHERE product_id = id of 'a'
) ba ON c.CustomerId = ba.CustomerId
LEFT JOIN
(
     SELECT * 
     FROM bought 
     WHERE product_id = id of 'b'
) bb ON c.CustomerId = bb.CustomerId
WHERE bb.CustomerId IS NULL;

这是您的表和联接的整套答案

顾客

产品

购买或交易

插入值

内部加入过滤器->这将是您的$query

SELECT customer.customer_id, customer.name as customer_name, product.name as product_name
FROM customer
INNER JOIN bought ON customer.customer_id = bought.customer_id
INNER JOIN product ON product.product_id = bought.product_id 
WHERE product.name LIKE "apple" AND product.name NOT LIKE "tomato"
ORDER BY customer.name
这将过滤所有购买苹果的客户。您只需更改product.name值即可。如果要更改字段,请更改内部联接的第一行

维恩图说明: 如果你想一想的话,MySQL遵循的是Venn图的设计。它不能与3个对象相交并过滤它们。你试图通过构造另一个圆圈来超越维恩图的设计。见下图

所以,为了解决您在PHP上的问题,我们创建了一个内圈。运行一个循环以获取产品名称上有苹果的所有客户ID。然后再次执行mysql\u fetch数组的循环。并仅输出带有苹果的客户ID


如果你无法通过MySQL解决过滤问题,那么可以通过过滤数据用PHP解决。

你只需编辑上一个问题…………但你没有说你尝试过什么??你在问题中说结果中没有客户购买产品“b”,这是什么意思?如果我正确理解没有客户购买产品“b”,那么您想要的只是购买产品“a”的客户。这可以简单地使用select查询来完成……可能不能使用简单的select查询来完成,因为涉及到3个表。因此,它需要加入。如果您无法通过MySQL解决过滤问题,请使用PHP通过过滤数据来解决。当前查询显示一个名为1.的常见产品,但仅显示产品a…这怎么可能发生?where claues为“=”,名为“1”的产品将不会显示感谢所有..我希望使用子查询进行查询,。。现在我得到了答案。。选择b.customer\u id从购买的b内部连接客户c上选择c.customer\u id=b.customer\u id内部连接产品p上选择p.product\u id=b.product\u id,其中p.name='a'和b.customer\u id不在p.name='a'中…谢谢
CREATE TABLE product (
  product_id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name varchar(50)
) ENGINE = InnoDB;
CREATE TABLE bought (
  id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  product_id int,
  customer_id int,
  FOREIGN KEY (product_id) REFERENCES product(product_id),
  FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
) ENGINE=InnoDB;
INSERT INTO customer (name, address) VALUEs ("George","Kentucky")
INSERT INTO customer (name, address) VALUEs ("Stan","Dublin")
INSERT INTO customer (name, address) VALUEs ("Kazaam","UAE")
INSERT INTO customer (name, address) VALUEs ("Sarah","New York")

INSERT INTO product (name) VALUES ("tomato")
INSERT INTO product (name) VALUES ("apple")

INSERT INTO bought (customer_id, product_id) VALUEs("1","2")
INSERT INTO bought (customer_id, product_id) VALUEs("2","1")
INSERT INTO bought (customer_id, product_id) VALUEs("3","1")
INSERT INTO bought (customer_id, product_id) VALUEs("4","1")
SELECT customer.customer_id, customer.name as customer_name, product.name as product_name
FROM customer
INNER JOIN bought ON customer.customer_id = bought.customer_id
INNER JOIN product ON product.product_id = bought.product_id 
WHERE product.name LIKE "apple" AND product.name NOT LIKE "tomato"
ORDER BY customer.name