Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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,我遇到了一些问题,无法使此查询按预期工作 我有三个表:products、product\u attributes和attributes 这种关系很明显(一个产品可以有多个属性) 我想要实现的是获得那些具有给定属性列表的产品,但忽略那些仅具有部分所需属性列表的产品。 例如,拥有以下产品和属性: 鞋子1[蓝色,男孩] 鞋子2[蓝色,女孩] 鞋子3[红色,男孩] 鞋子4[红色,女孩] 查询带有[blue,boy]的产品时,只会检索Shoe 1 查询带有[blue]的产品时不会返回任何内容 从现在起

我遇到了一些问题,无法使此查询按预期工作

我有三个表:
products
product\u attributes
attributes

这种关系很明显(一个产品可以有多个属性)

我想要实现的是获得那些具有给定属性列表的产品,但忽略那些仅具有部分所需属性列表的产品。
例如,拥有以下产品和属性:

  • 鞋子1[蓝色,男孩]
  • 鞋子2[蓝色,女孩]
  • 鞋子3[红色,男孩]
  • 鞋子4[红色,女孩]
查询带有[blue,boy]的产品时,只会检索
Shoe 1

查询带有[blue]的产品时不会返回任何内容

从现在起,我一直在处理这个查询:

SELECT p.*, pa.attribute_id
FROM products AS p 
LEFT JOIN product_attributes AS pa ON(pa.product_id=p.id)
WHERE 
pa.attribute_id IN(' . implode(',', $attr_ids) . ')
GROUP BY p.id
HAVING count(pa.attribute_id)=' . count($attr_ids)

当只给出一个属性时,这会失败,因为它将返回任何具有该属性的产品

除了任何其他问题,此查询

  SELECT p.*
       , pa.attribute_id
    FROM products p 
    LEFT 
-- OUTER (this keyword is optional in MySQL)
    JOIN product_attributes pa 
      ON pa.product_id = p.id
   WHERE pa.attribute_id IN('$attr_ids')
   GROUP 
      BY p.id
  HAVING COUNT(*) = $cnt;
。。。逻辑上与…相同

  SELECT p.*
       , pa.attribute_id
    FROM products p 
-- INNER (this keyword is also optional in MySQL)
    JOIN product_attributes pa 
      ON pa.product_id = p.id
   WHERE pa.attribute_id IN('$attr_ids')
   GROUP 
      BY p.id
  HAVING COUNT(pa.attribute_id) = $cnt;

为了维护外部连接的实用性,考虑重写如下…

  SELECT p.*
       , pa.attribute_id
    FROM products p 
    LEFT 
    JOIN product_attributes pa 
      ON pa.product_id = p.id
     AND pa.attribute_id IN('$attr_ids')
   GROUP 
      BY p.id
  HAVING COUNT(pa.attribute_id) = $cnt;
请参见此处

另请参见

另外,WHERE子句将(左)外部联接呈现为内部联接!“草莓”是什么意思?它仍然会给我那些具有列出的任何属性的产品。我需要的产品只有列出的属性。就是这样!非常感谢你!大家好,我遇到了这样一种情况,这种情况仍然不起作用。尝试列出“Shoe 2[blue,violet]”(注意,我的示例数据集中不存在violet),它仍会加载一个结果,即howmuchattr为2,并加载任何具有两个属性的“Shoe 2”,其中一个属性为“blue”。我不理解您的查询。Shoe2在产品属性的两个记录中定义为[蓝色,女孩]。它又是如何定义为“鞋2[蓝色,紫色]?”???请设置sqlfiddle并将其粘贴到此处链接。我很乐意提供帮助。看来SQLFIDLE现在不起作用(构建模式需要很长时间),我将尝试将其发布到其他地方。
  SELECT p.*
       , pa.attribute_id
    FROM products p 
    LEFT 
    JOIN product_attributes pa 
      ON pa.product_id = p.id
     AND pa.attribute_id IN('$attr_ids')
   GROUP 
      BY p.id
  HAVING COUNT(pa.attribute_id) = $cnt;
-- PHP (or any other languaje) parts are hardcoded here!!!!

SELECT p.*, hma.howmuchattr
-- howmuchattr is needed by HAVING clause, 
-- you can omit elsewhere (by surrounding SELECT or by programming languaje)

FROM products AS p 
LEFT JOIN product_attributes AS pa ON pa.product_id = p.id 
LEFT JOIN (
    SELECT product_id, count(*) as howmuchattr
    FROM product_attributes 
    GROUP BY product_id
) as hma on p.id = hma.product_id

WHERE 
pa.attribute_id IN 
(1,3)                    -- this cames from PHP (or any other languaje). Can be (1) for the other case
GROUP BY p.id
HAVING count(*) = howmuchattr;