如何连接表,查询sql

时间:2017-10-23 10:11:51

标签: sql oracle

我有以下配方数据库表及其数据。

我如何找到食谱的总数,每种成分的类别数量?我使用了很多加入方法,但我无法进行我想要的查询。

我需要as out

成分id,我们可以找到多少配方中的这种成分,我们可以在多少类别中找到这种成分。

这是我的尝试 我的尝试的问题是,如果我有一个配方在一个食谱和两个类别 它将在结果中显示该成分在2个配方中,2个类别

SELECT  
  I.idIng,COUNT(CI.idcat)AS "CAT FOR ING" , COUNT(RI.idRecipe )AS "RECETTE 
  FOR 
  ING" 
FROM
  INGREDIENT I
LEFT JOIN   
  Ingredient_Recipe RI  ON I.idIng = ri.idIng
RIGHT JOIN  
  Ingredient_Catigory CI ON I.idIng = CI.idIng
GROUP BY 
  I.idIng
ORDER BY
  I.idIng;

以下是一些测试数据:

在这里,我创建了我的类别,它与Ingredient有许多关系。

-- creating table cat category 
CREATE TABLE category (
idCat INT NOT NULL PRIMARY KEY,
nomCat INT NOT NULL
);

在这里,我创建了我的食谱表,它与Ingredient有很多很多关系。

-- creating table cat Recipe
CREATE TABLE Recipe(
idRecipe INT NOT NULL PRIMARY KEY,
nameRecipe VARCHAR2(30) NOT NULL
);

这是我的成分表,它将与食谱,类别链接。

-- creating table Ingredient
CREATE TABLE Ingredient(
idIng INT NOT NULL PRIMARY KEY ,
nameIng VARCHAR2(30) NOT NULL
);

这是成分之间的中间表,因为关系是多对多的。

-- creating table Ingredient_category
CREATE TABLE Ingredient_category (
idIng INT NOT NULL,
idCat INT NOT NULL,
CONSTRAINT idIng_FK FOREIGN KEY (idIng) REFERENCES Ingredient(idIng),
CONSTRAINT idCat_FK FOREIGN KEY (idCat) REFERENCES category(idCat)
);

这是Ingredient,Recipe之间的中间表,因为关系是多对多的。

-- creating table Ingredient_Recipe
CREATE TABLE Ingredient_Recipe(
idIng INT NOT NULL,
idRecipe INT NOT NULL,
CONSTRAINT idIngRecipe_FK FOREIGN KEY (idIng)    REFERENCES  
Ingredient(idIng),
CONSTRAINT idRecipe_FK FOREIGN KEY    (idRecipe) REFERENCES Recipe(idRecipe) 
);

这里我们插入数据进行测试。

-- insert data into  Recipe 
INSERT INTO Recipe VALUES(1,'SOUP');
INSERT INTO Recipe VALUES(2,'FRIED');
INSERT INTO Recipe VALUES(3,'BURGER');

-- insert data into  category 
INSERT INTO category VALUES(1,'VEGES');
INSERT INTO category VALUES(2,'DAIRY');
INSERT INTO category VALUES(3,'MEAT');
INSERT INTO category VALUES(4,'ANIMAL PRODUCT');

-- insert data into  Ingredient 
INSERT INTO Ingredient VALUES (1,'Eggs');
INSERT INTO Ingredient VALUES (2,'milk');
INSERT INTO Ingredient VALUES (3,'Beef');
INSERT INTO Ingredient VALUES (4,'chess');

-- insert data into  Ingredient_Catigory
INSERT INTO Ingredient_Catigory VALUES(1,4);
INSERT INTO Ingredient_Catigory VALUES(2,2);
INSERT INTO Ingredient_Catigory VALUES(2,4);
INSERT INTO Ingredient_Catigory VALUES(3,3);
INSERT INTO Ingredient_Catigory VALUES(3,4);
INSERT INTO Ingredient_Catigory VALUES(4,2);
INSERT INTO Ingredient_Catigory VALUES(4,4);


-- insert data into Ingredient_Recip
INSERT INTO Ingredient_Recipe VALUES (1,2);
INSERT INTO Ingredient_Recipe VALUES (1,3);
INSERT INTO Ingredient_Recipe VALUES (2,1);
INSERT INTO Ingredient_Recipe VALUES (3,3);
INSERT INTO Ingredient_Recipe VALUES (3,2);

2 个答案:

答案 0 :(得分:1)

我认为你正在寻找COUNT(DISTINCT value),你几乎就在那里。试试这样的事情;

SELECT  
  I.idIng,
  COUNT(DISTINCT CI.idCat)AS [Categories], 
  COUNT(DISTINCT RI.idRecipe)AS [Recipes] 
FROM #INGREDIENT I
LEFT JOIN #Ingredient_Recipe RI  ON I.idIng = ri.idIng
LEFT JOIN #Ingredient_Category CI ON I.idIng = CI.idIng
GROUP BY 
  I.idIng
ORDER BY
  I.idIng

结果看起来像这样;

idIng   Categories  Recipes
1       1           2
2       2           1
3       2           2
4       2           0

请注意,我认为示例数据中的拼写错误但我已经在我的测试系统上更正了(我已经使用了#TempTables)。我已将您的RIGHT JOIN更改为LEFT JOIN(作为备注,我从未见过需要在生产代码中使用RIGHT JOIN,请尽量避免使用它们)。

编辑:我刚刚注意到这是一个Oracle问题,上面的查询只在SQL Server上进行了测试,尽管粗略地看一下文档显示语法应该是对Oracle来说也一样。

答案 1 :(得分:-1)

select * from Ingredient_Recipe a join Ingredient_category b on a.idIng=b.idIng join Ingredient c on a.idIng=c.idIng join Recipe d on a.idRecipe=d.idRecipe join category e on b.idCat=e.idCat

试试这个