连接三个表后选择

时间:2017-05-05 15:49:49

标签: mysql sql database

我刚刚加入了3张桌子(链接中的图片)。

无论如何选择所有的recept_navn,其中“raavare_navn”不是“champignon”? [这里] [1]是我的代码。

SELECT RECEPT.RECEPT_ID, RECEPT.RECEPT_NAVN, RAAVARE.RAAVARE_NAVN 
FROM RECEPT 
INNER JOIN RECEPTKOMPONENT ON RECEPT.RECEPT_ID = RECEPTKOMPONENT.RECEPT_ID 
INNER JOIN RAAVARE ON RAAVARE.RAAVARE_ID = RECEPTKOMPONENT.RAAVARE_ID 

[1]:

https://scontent.fcph1-1.fna.fbcdn.net/v/t34.0-12/18302492_10211642606955497_1361966660_n.png?oh=39fbf75c8be2bfd5abae35803c7d176a&oe=590E71F3

1 个答案:

答案 0 :(得分:0)

使用

SELECT RECEPT.RECEPT_ID, RECEPT.RECEPT_NAVN, RAAVARE.RAAVARE_NAVN 
FROM RECEPT 
INNER JOIN RECEPTKOMPONENT ON RECEPT.RECEPT_ID = RECEPTKOMPONENT.RECEPT_ID 
INNER JOIN RAAVARE ON RAAVARE.RAAVARE_ID = RECEPTKOMPONENT.RAAVARE_ID 
where 

        RAAVARE.RAAVARE_NAVN 

                             <> "champignon"

如果你需要margherita和prosciutto,你可以使用

  SELECT RECEPT.RECEPT_ID, RECEPT.RECEPT_NAVN, RAAVARE.RAAVARE_NAVN 
  FROM RECEPT 
  INNER JOIN RECEPTKOMPONENT ON RECEPT.RECEPT_ID = RECEPTKOMPONENT.RECEPT_ID 
  INNER JOIN RAAVARE ON RAAVARE.RAAVARE_ID = RECEPTKOMPONENT.RAAVARE_ID 
  where RECEPT.RECEPT_ID not in ( 

      SELECT RECEPT.RECEPT_ID
      FROM RECEPT 
      INNER JOIN RECEPTKOMPONENT ON RECEPT.RECEPT_ID = RECEPTKOMPONENT.RECEPT_ID 
      INNER JOIN RAAVARE ON RAAVARE.RAAVARE_ID = RECEPTKOMPONENT.RAAVARE_ID 
      where RAAVARE.RAAVARE_NAVN = "champignon"
    )