2011-04-17 77 views
0

我有一个有3个字段(国家,食谱,成分)的数据库。所有这些都是主键。基于多个用户选择的php mysql查询

我有一个网站,用户选择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' "); 

我对编写查询很新,所以我很困惑如何让这些工作。我究竟该如何解决这些问题?任何帮助,将不胜感激。

回答

0

尝试

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

为什么是方的佩伦斯? perens是一个分组操作符或用于指定参数,但在这种情况下,它们不是必需的。 DISTINCT不是一个函数。不知道你的表的结构,我不能帮你进一步

+0

表的结构只是三列(国家,食谱,成分)。所有这些都是主键。所以你可以有多个国家/配方同名,但没有国家/配方/配料可以相同。 – Eric 2011-04-18 00:39:57

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 ;

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

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. 成分

而配方上的成分?比你想要在两个国家展示相同的成分还是?