Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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,我有三张表,产品,成分和成分 我需要找到所有产品,其中该产品含有相关成分。 它还需要在百分比列上有一个匹配值 一种产品存在两种相关成分 +-----+------------+---------------+------------+ | id | product_id | ingredient_id | percentage | +-----+------------+---------------+------------+ | 1 | 1 |

我有三张表,
产品
成分
成分

我需要找到所有
产品
,其中该产品含有相关成分。 它还需要在
百分比
列上有一个匹配值

一种产品存在两种相关成分

+-----+------------+---------------+------------+
| id  | product_id | ingredient_id | percentage |
+-----+------------+---------------+------------+
|   1 |          1 |            1 |         50  |
|   2 |          1 |            2 |         50  |
+------------------+--------------+-------------+
要检索的SQL:

SELECT
      products.id
    FROM
      products,
      ingredient_product
    WHERE
        ingredient_product.product_id = products.id
    AND
        (ingredient_product.ingredient_id = 1 AND ingredient_product.percentage = 50)
    AND
        (ingredient_product.ingredient_id = 2 AND ingredient_product.percentage = 50)
但这将返回一个空结果<代码>空集(0.00秒)

产品:

+-------------------+-----------------------+------+-----+---------+----------------+
| Field             | Type                  | Null | Key | Default | Extra          |
+-------------------+-----------------------+------+-----+---------+----------------+
| id                | int(10) unsigned      | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)          | NO   |     | NULL    |                |
| short_description | text                  | YES  |     | NULL    |                |
| long_description  | text                  | YES  |     | NULL    |                |
+-------------------+-----------------------+------+-----+---------+----------------+
成分:

+-------------------+-----------------------+------+-----+---------+----------------+
| Field             | Type                  | Null | Key | Default | Extra          |
+-------------------+-----------------------+------+-----+---------+----------------+
| id                | int(10) unsigned      | NO   | PRI | NULL    | auto_increment |
| short_name        | varchar(50)           | NO   |     | NULL    |                |
| thumbnail         | varchar(255)          | NO   |     | NULL    |                |
+-------------------+-----------------------+------+-----+---------+----------------+
产品成分

+---------------+---------------------+------+-----+---------+----------------+
| Field         | Type                | Null | Key | Default | Extra          |
+---------------+---------------------+------+-----+---------+----------------+
| id            | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| product_id    | int(10) unsigned    | NO   | MUL | NULL    |                |
| ingredient_id | int(10) unsigned    | NO   | MUL | NULL    |                |
| percentage    | tinyint(3) unsigned | NO   |     | NULL    |                |
+---------------+---------------------+------+-----+---------+----------------+

假设id是多余的,并且您在(产品id、成分id)上有一个完全可用的自然密钥


假设id是多余的,并且您在(产品id、成分id)上有一个完全可用的自然密钥


根据草莓回答:

SELECT product_id 
FROM ingredient_product 
WHERE ingredient_id IN (1,2) AND percentage = 50
GROUP BY product_id 
HAVING COUNT(*) = 2;
但是,如果您需要单独的百分比,这不起作用,但您可以这样做:

SELECT product_id FROM
    (SELECT product_id 
    FROM ingredient_product 
    WHERE ingredient_id = 1 AND percentage = 50
    UNION ALL
    SELECT product_id 
    FROM ingredient_product 
    WHERE ingredient_id = 2 AND percentage = 50) AS tmp
GROUP BY product_id 
HAVING COUNT(*) = 2;
基本上,您为每个需求添加了一个
联合
(由id和百分比组合组成),然后将
HAVING
条件增加到需求数量中

注意:由于在这种情况下要求是互斥的,
UNION-ALL
UNION
更快,并且会给出相同的结果

您可以使用
来代替子查询中的
联合
,但我们发现MySQL似乎更喜欢这种格式。然而,这可能会随着版本的变化而变化。为了完整性起见,这里还提供了该解决方案:

SELECT product_id 
FROM ingredient_product 
WHERE (ingredient_id = 1 AND percentage = 50) OR (ingredient_id = 2 AND percentage = 50)
GROUP BY product_id 
HAVING COUNT(*) = 2;

根据草莓回答:

SELECT product_id 
FROM ingredient_product 
WHERE ingredient_id IN (1,2) AND percentage = 50
GROUP BY product_id 
HAVING COUNT(*) = 2;
但是,如果您需要单独的百分比,这不起作用,但您可以这样做:

SELECT product_id FROM
    (SELECT product_id 
    FROM ingredient_product 
    WHERE ingredient_id = 1 AND percentage = 50
    UNION ALL
    SELECT product_id 
    FROM ingredient_product 
    WHERE ingredient_id = 2 AND percentage = 50) AS tmp
GROUP BY product_id 
HAVING COUNT(*) = 2;
基本上,您为每个需求添加了一个
联合
(由id和百分比组合组成),然后将
HAVING
条件增加到需求数量中

注意:由于在这种情况下要求是互斥的,
UNION-ALL
UNION
更快,并且会给出相同的结果

您可以使用
来代替子查询中的
联合
,但我们发现MySQL似乎更喜欢这种格式。然而,这可能会随着版本的变化而变化。为了完整性起见,这里还提供了该解决方案:

SELECT product_id 
FROM ingredient_product 
WHERE (ingredient_id = 1 AND percentage = 50) OR (ingredient_id = 2 AND percentage = 50)
GROUP BY product_id 
HAVING COUNT(*) = 2;

和子句不能同时为真,请在(1,2)@mitkosoft中使用component_product.component_id。如果您有解决问题的SQL,请将其作为答复。这样我可以接受。想要的输出是什么?AND子句不能同时为真,请在(1,2)@mitkosoft中使用component_product.component_id。如果您有解决问题的SQL,请将其作为答复。这样我可以接受。期望的输出是什么?与透视表上的百分比列不匹配。如果我的问题不清楚,请告诉我哪一位,我将尝试澄清。@charliepryn我知道。看看你是否能把这一点弄清楚。@Stawberry我不确定这是不是最好的主意,就像其他人有同样的问题并回顾了这个问题一样,这里只有部分答案。这与透视表上的百分比列不匹配。如果我的问题不清楚,请告诉我哪一点,我会尝试澄清。@CharliePryn我知道。看看你是否能把这一点弄清楚。@Stawberry我不确定这是不是最好的主意,就像其他人有同样的问题并回顾了这个问题一样,只有部分答案在这里。