2011-04-17 75 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. 成分

而配方上的成分?比你想要在兩個國家展示相同的成分還是?