PHP MySQL选择数组中的数组

PHP MySQL选择数组中的数组,php,mysql,arrays,Php,Mysql,Arrays,我的数据库中有一个字段是数组,即:id_contracents=“1,8,3,9,5,2,7,4” 我想比较一个也是数组的变量,如果字段ID中存在任何变量ID,则给出一个结果 我试图检查产品的成分是否包含变量中的任何成分。例如,如果有人对坚果过敏,我想选择所有含有任何坚果的产品。因此,我查询我的配料,以获得名称中包含“nut”一词的任何配料的id。然后我需要得到含有任何成分ID的产品 这就是我所拥有的 $alg = $_POST['alg']; mysql_select_db($databas

我的数据库中有一个字段是数组,即:id_contracents=“1,8,3,9,5,2,7,4”

我想比较一个也是数组的变量,如果字段ID中存在任何变量ID,则给出一个结果

我试图检查产品的成分是否包含变量中的任何成分。例如,如果有人对坚果过敏,我想选择所有含有任何坚果的产品。因此,我查询我的配料,以获得名称中包含“nut”一词的任何配料的id。然后我需要得到含有任何成分ID的产品

这就是我所拥有的

$alg = $_POST['alg'];

mysql_select_db($database, $Products);
$query_availIngredients = "SELECT * FROM ingredients WHERE ingredient LIKE '%$alg%' ";
$availIngredients = mysql_query($query_availIngredients, $Products) or die(mysql_error());
$row_availIngredients = mysql_fetch_assoc($availIngredients);

$ingarray = array(); 

    do{
         $ingarray[] = $row_availIngredients['id'];

    } while ($row_availIngredients = mysql_fetch_assoc($availIngredients));

$alg = implode (',', $ingarray);


mysql_select_db($database, $Products);
$query_Products = "SELECT *FROM products  WHERE 
 id_ingredients LIKE '%$alg%' " ;
$Products = mysql_query($query_Products, $Products) or die(mysql_error());
$row_Products = mysql_fetch_assoc($Products);

提前感谢您的帮助。

在单个数据库字段中放置一个ID数组并不是一个好主意,这样设计数据库会使关系数据库失去一些功能

我将创建第三个表,而不是在产品的表中存储成分ID数组

示例表架构如下所示:

Products id productName productDescription
SELECT id FROM Ingredients WHERE ingredientName LIKE '%nut%'
SELECT
  Products.productName
FROM
  Products
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Products = Products.id
  LEFT JOIN Ingredients ON Ingredients.id = ProductIngredients.id_Ingredients
WHERE
  Ingredients.ingredientName LIKE '%nut%'
SELECT
  Ingredients.ingredientName
FROM
  Ingredients
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Ingredients = Ingredients.id
WHERE
  ProductIngredients.id_Products = 1
以及含有“坚果”一词成分的产品列表,如下所示:

Products id productName productDescription
SELECT id FROM Ingredients WHERE ingredientName LIKE '%nut%'
SELECT
  Products.productName
FROM
  Products
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Products = Products.id
  LEFT JOIN Ingredients ON Ingredients.id = ProductIngredients.id_Ingredients
WHERE
  Ingredients.ingredientName LIKE '%nut%'
SELECT
  Ingredients.ingredientName
FROM
  Ingredients
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Ingredients = Ingredients.id
WHERE
  ProductIngredients.id_Products = 1
您可以获得如下产品的成分列表:

Products id productName productDescription
SELECT id FROM Ingredients WHERE ingredientName LIKE '%nut%'
SELECT
  Products.productName
FROM
  Products
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Products = Products.id
  LEFT JOIN Ingredients ON Ingredients.id = ProductIngredients.id_Ingredients
WHERE
  Ingredients.ingredientName LIKE '%nut%'
SELECT
  Ingredients.ingredientName
FROM
  Ingredients
  LEFT JOIN ProductIngredients ON ProductIngredients.id_Ingredients = Ingredients.id
WHERE
  ProductIngredients.id_Products = 1
这将为您提供如下列表:

  • 花生酱
  • 果冻
  • 面包
编辑
这就是所谓的多对多关系。

看起来很棒。我会尝试一下。感谢您为这个答案所做的一切努力!请参考这个精彩的答案!