数据库本身就是用自己的食谱(食谱)和食材(RecipeIngredient)储存鸡尾酒。每个用户(用户)都有自己的“食品室”(UserIngredients),在这里他们可以存储他们在家里的食材。此查询现在应该向他们展示他们可以混合的鸡尾酒
我有以下查询:
SELECT u.User_Name, r.Recipe_Name
FROM User u
INNER JOIN UserIngredient ui ON u.User_ID = ui.User_ID
INNER JOIN RecipeIngredient ri ON ui.Ingredient_ID = ri.Ingredient_ID
INNER JOIN Ingredient i ON ri.Ingredient_ID = i.Ingredient_ID
INNER JOIN Recipe r ON ri.Recipe_ID = r.Recipe_ID
WHERE u.User_Session = 'DgRkQztkvUhotfSf53l7ciiI8rOhKtuvoPqCTvdlBXWTn9cYxz'
并且想知道是否可以为每个配方获得一个“r.Recipe_Name”而不是每个配料一个。
我的tablelayout如下:
CREATE TABLE User
(
User_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
User_Pass TEXT NOT NULL,
User_Name TEXT NOT NULL,
User_Surname TEXT NOT NULL,
User_Nickname TEXT,
User_EMail TEXT,
User_Session VARCHAR(50) UNIQUE,
User_Admin BOOLEAN
);
CREATE TABLE Recipe
(
Recipe_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Recipe_Name TEXT NOT NULL,
Recipe_Clicks INT,
Recipe_Description TEXT
);
CREATE TABLE Ingredient
(
Ingredient_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Ingredient_Name VARCHAR(255) UNIQUE,
Ingredient_Description TEXT
);
CREATE TABLE RecipeIngredient
(
RecipeIngredient_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
RecipeIngredient_Amount DECIMAL(8,2) NOT NULL,
MeasuringUnit_ID INT NOT NULL,
Recipe_ID INT NOT NULL,
Ingredient_ID INT NOT NULL,
FOREIGN KEY (MeasuringUnit_ID) REFERENCES MeasuringUnit(MeasuringUnit_ID),
FOREIGN KEY (Recipe_ID) REFERENCES Recipe(Recipe_ID),
FOREIGN KEY (Ingredient_ID) REFERENCES Ingredient(Ingredient_ID)
);
CREATE TABLE UserIngredient
(
UserIngredient_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Ingredient_ID INT NOT NULL,
User_ID INT NOT NULL,
FOREIGN KEY(Ingredient_ID) REFERENCES Ingredient(Ingredient_ID),
FOREIGN KEY(User_ID) REFERENCES User(User_ID)
);
答案 0 :(得分:1)
要使用此数据库获得所需结果,请尝试
SELECT DISTINCT u.User_Name, r.Recipe_Name
FROM User u
INNER JOIN UserIngredient ui ON u.User_ID = ui.User_ID
INNER JOIN RecipeIngredient ri ON ui.Ingredient_ID = ri.Ingredient_ID
INNER JOIN Ingredient i ON ri.Ingredient_ID = i.Ingredient_ID
INNER JOIN Recipe r ON ri.Recipe_ID = r.Recipe_ID
WHERE u.User_Session = 'DgRkQztkvUhotfSf53l7ciiI8rOhKtuvoPqCTvdlBXWTn9cYxz'
我的猜测是用户创建了Recipies,为什么不将User_ID添加到Receipe?
答案 1 :(得分:1)
你可以使用不同的
SELECT DISTINCT u.User_Name, r.Recipe_Name
FROM User u
INNER JOIN UserIngredient ui ON u.User_ID = ui.User_ID
INNER JOIN RecipeIngredient ri ON ui.Ingredient_ID = ri.Ingredient_ID
INNER JOIN Ingredient i ON ri.Ingredient_ID = i.Ingredient_ID
INNER JOIN Recipe r ON ri.Recipe_ID = r.Recipe_ID
WHERE u.User_Session = 'DgRkQztkvUhotfSf53l7ciiI8rOhKtuvoPqCTvdlBXWTn9cYxz'
答案 2 :(得分:1)
尝试
SELECT u.User_Name, MAX(r.Recipe_Name)
FROM User u
INNER JOIN UserIngredient ui ON u.User_ID = ui.User_ID
INNER JOIN RecipeIngredient ri ON ui.Ingredient_ID = ri.Ingredient_ID
INNER JOIN Ingredient i ON ri.Ingredient_ID = i.Ingredient_ID
INNER JOIN Recipe r ON ri.Recipe_ID = r.Recipe_ID
WHERE u.User_Session = 'DgRkQztkvUhotfSf53l7ciiI8rOhKtuvoPqCTvdlBXWTn9cYxz'
GROUP BY u.User_Name, r.Recipe_Name
不确定这一点,但听起来多个成分将具有相同的配方,因此只需选择最大值,这将返回唯一的配方名称,如果按用户名+配方名称分组,它可能会为您提供所需。