基于多用户选择的php-mysql查询
我有一个数据库,有3个领域的国家,配方,成分。它们都是主键 我有一个网站,用户可以选择两个国家,我想显示两个国家之间相同的成分 所以我写了一个查询:基于多用户选择的php-mysql查询,php,mysql,sql,Php,Mysql,Sql,我有一个数据库,有3个领域的国家,配方,成分。它们都是主键 我有一个网站,用户可以选择两个国家,我想显示两个国家之间相同的成分 所以我写了一个查询: $result = mysql_query("SELECT DISTINCT (Recipe) FROM recipes r1, recipes r2 WHERE r1.Country
$result = mysql_query("SELECT DISTINCT (Recipe)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
^ r2.Country = '$temp2'
^ r1.Ingredient = r2.Ingredient");
$result = mysql_query("CREATE VIEW temporary(Inglist) AS (
SELECT DISTINCT (Recipe)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
^ r2.Country = '$temp2'
^ r1.Ingredient = r2.Ingredient)
SELECT COUNT(*)
FROM 'temporary' ");
但这给了我一个错误,即列配方不明确。我该如何解决这个问题
我还想数一数所有不同的食谱。所以我写了一个查询:
$result = mysql_query("SELECT DISTINCT (Recipe)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
^ r2.Country = '$temp2'
^ r1.Ingredient = r2.Ingredient");
$result = mysql_query("CREATE VIEW temporary(Inglist) AS (
SELECT DISTINCT (Recipe)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
^ r2.Country = '$temp2'
^ r1.Ingredient = r2.Ingredient)
SELECT COUNT(*)
FROM 'temporary' ");
我对编写查询非常陌生,所以我不知道如何让它们工作。我该如何着手修复这些?任何帮助都将不胜感激。试试看
$result = mysql_query("SELECT DISTINCT (r1.Recipe) FROM recipes r1, recipes r2 WHERE r1.Country = '$temp' ^ r2.Country = '$temp2' ^ r1.Ingredient = r2.Ingredient");
为什么配方在佩伦斯?Peren是一个分组运算符或用于指定参数,但在本例中不需要它们。DISTINCT不是一个函数。由于不知道您的表的结构,我无法进一步帮助您如果您提供一个数据库结构的示例,将非常有用。此外,您应该首先通过为国家、食谱和配料创建单独的表来优化数据库。正如现在一样,您将重复输入相同的国家和配方,只是为了列出所有成分 您的表应该如下所示: 国家表 配方表 配料 配方和配料图 配料和配方在配方和配料映射表中关联。这将正确地将国家与配料区分开,并允许您有更多与配料或国家无关的字段,如“dateAdded”。这使得select语句更加复杂,同时也使它们更加高效和强大 选择来自两个国家的所有成分 选择来自同两个国家的食谱计数 mysql结构
请注意,许多字段被设置为唯一索引。这样可以防止重复信息。我将更进一步,添加外键以防止插入引用不存在项的行。您可以按照Chris Thompson所说的那样尝试,也可以使用 $result=mysql\u query从配方r1中选择不同的r2.Recipe,配方r2,其中r1.Country='$temp'^r2.Country='$temp2'^r1.component=r2.component 但据我所知,您的桌子有: 国 配方 成分
配方上有什么成分?你想在两个国家或地区展示相同的成分吗 表格的结构只有三列:国家、配方、配料。所有这些都是主键。因此,您可以有多个国家/配方具有相同的名称,但没有任何国家/配方/成分可以相同。
ingredientId ingredientName
1 rice
2 potato
riMapId recipeId ingredientId
1 1 1
2 2 2
SELECT
`ingredients`.`ingredientName` AS 'ingredientName',
`countries`.`countryName` AS 'countryName',
`recipes`.`recipeName` AS 'recipeName',
`recipeIngredientMap`.`riMapId` AS 'riMapId'
FROM
`ingredients`
LEFT JOIN
`recipeIngredientMap` ON `recipeIngredientMap`.`ingredientId` = `ingredients`.`ingredientId`
LEFT JOIN
`recipes` ON `recipes`.`recipeId` = `recipeIngredientMap`.`recipeId`
JOIN
`countries` ON `countries`.`countryId` = `recipes`.`countryId` AND
`countries`.`countryId` IN (1,2)
SELECT
COUNT(`recipeName`) AS 'recipeCount'
FROM
`recipes`
WHERE
`countryId` IN (1,2)
--
-- Database: `food`
--
-- --------------------------------------------------------
--
-- Table structure for table `countries`
--
CREATE TABLE `countries` (
`countryId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`countryName` varchar(255) NOT NULL,
PRIMARY KEY (`countryId`),
UNIQUE KEY `countryName` (`countryName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `ingredients`
--
CREATE TABLE `ingredients` (
`ingredientId` int(11) NOT NULL AUTO_INCREMENT,
`ingredientName` varchar(255) NOT NULL,
PRIMARY KEY (`ingredientId`),
UNIQUE KEY `ingredientName` (`ingredientName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
-- --------------------------------------------------------
--
-- Table structure for table `recipeIngredientMap`
--
CREATE TABLE `recipeIngredientMap` (
`riMapId` int(11) NOT NULL AUTO_INCREMENT,
`recipeId` int(10) unsigned NOT NULL,
`ingredientId` int(10) unsigned NOT NULL,
PRIMARY KEY (`riMapId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
-- --------------------------------------------------------
--
-- Table structure for table `recipes`
--
CREATE TABLE `recipes` (
`recipeId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`recipeName` varchar(255) NOT NULL,
`countryId` int(10) unsigned NOT NULL,
`dateAdded` date NOT NULL,
PRIMARY KEY (`recipeId`),
UNIQUE KEY `recipeName` (`recipeName`),
KEY `countryId` (`countryId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;