php mysql查询基于多个用户选择

时间:2011-04-17 23:40:19

标签: php mysql sql

我有一个包含3个字段的数据库(Country,Recipe,Ingredient)。所有这些都是主键。

我有一个用户选择2个国家/地区的网站,我想显示两个国家/地区之间相同的成分。

所以我写了一个查询:

$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' ");  

我是编写查询的新手,所以我对如何使这些工作感到困惑。我该如何解决这些问题呢?任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:0)

尝试

$result = mysql_query("SELECT DISTINCT (r1.Recipe) FROM recipes r1, recipes r2 WHERE r1.Country = '$temp' ^ r2.Country = '$temp2' ^ r1.Ingredient = r2.Ingredient");

答案 1 :(得分:0)

为什么Recipe in perens? perens是一个分组运算符或用于指定参数,但在这种情况下它们不是必需的。 DISTINCT不是一个功能。不知道你桌子的结构,我无法帮你进一步

答案 2 :(得分:0)

如果您提供了数据库结构的示例,那将会很有帮助。此外,您应首先通过为国家/地区,食谱和成分创建单独的表来优化数据库。就像现在一样,您将重复使用相同的国家/地区和食谱来列出所有成分。

您的表格应如下所示:

国家/地区表

countryId          countryName
    1         United States of America
    2                England

食谱表

recipeId           recipeName       countryId      dateAdded      
   1                Jambalaya           1         2012-10-11
   2             Bangers and Mash       2         2013-11-04

<强>成分

ingredientId       ingredientName
    1                  rice
    2                 potato

食谱和成分地图

riMapId          recipeId          ingredientId
   1                1                    1
   2                2                    2

配方和配方在“配方和配料地图”表中相关联。这恰好将国家与成分分开,并允许您拥有更多与成分或国家无关的“dateAdded”字段。虽然这使得select语句更复杂,但它也使它们更有效和更强大。

选择两个国家的所有成分

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)

mysql结构

--
-- 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 ;

请注意,许多字段都设置为唯一索引。这可以防止重复信息。我会更进一步,添加外键以防止插入引用不存在的项的行。

答案 3 :(得分:0)

你可以尝试克里斯汤普森说或尝试

$result = mysql_query("SELECT DISTINCT (r2.Recipe) FROM recipes r1, recipes r2 WHERE r1.Country = '$temp' ^ r2.Country = '$temp2' ^ r1.Ingredient = r2.Ingredient");

但据我所知,你的桌子有:

  1. 国家
  2. 配方
  3. 成分
  4. 关于配方​​有配料吗?而且你想要在2个国家展示相同的成分或?