MySQL One to Many relationship exlusions based on criteria

Daniel Greibe

Consider the following.

I have a table with RecipeComponents , which has a one to many relationship to Ingredients.

I want the names of recept_navn from table Recepter which do not include the Raavare champignon.

Recept has a one to many relationship with receptkomponent. One recept has many receptkomponents.

Receptkomponent has a one to many relationship with Raavare. A Raavare is included in multiple receptkomponents.

Initially i thought i would just select and join the three tables and make a where clause for raaavare_navn != Mushroom. Since it is a one to many relationship and the RecipeComponent table contains multiple ingredients, i'll end up deleting the row with mushroom, but the RecipeComponent will still show up in rows with the other ingredients

Here is what i tried to do at first.

SELECT DISTINCT recept.recept_id, recept_navn
FROM recept
JOIN receptkomponent ON recept.recept_id = receptkomponent.recept_id
JOIN raavare ON raavare.raavare_id = receptkomponent.raavare_id
WHERE raavare.raavare_navn != 'champignon'
GROUP BY recept_id

DATA:

Tablename: Raavare
raavare_id    |    raavare_navn    |    leverandoer
___________________________________________________
1             |    dej             |    Wawelka
2             |    tomat           |    Knoor
3             |    tomat           |    Veaubais
4             |    tomat           |    Franz
5             |    ost             |    Ost og Skinke A/S
6             |    skinke          |    Ost og Skinke A/S
7             |    champignon      |    Igloo Frostvarer

Tablename: Receptkomponent

recept_id     |    raavare_id    |    nom_netto    |    tolerance
__________________________________________________________________
1             |    1             |    10.0         |    0.1
1             |    2             |    2.0          |    0.1
1             |    5             |    2.0          |    0.1
2             |    1             |    10.0         |    0.1
2             |    3             |    2.0          |    0.1
2             |    5             |    1.5          |    0.1
2             |    6             |    1.5          |    0.1
3             |    1             |    10.0         |    0.1
3             |    4             |    1.5          |    0.1
3             |    5             |    1.5          |    0.1
3             |    6             |    1.0          |    0.1
3             |    7             |    1.0          |    0.1

Tablename: Recepter

recept_id    |    recept_navn
_____________________________
1            |    margherita
2            |    prosciutto
3            |    capricciosa
Daniel Greibe

I figured out the answer.

First you want to make a query that selects all the recept_names with the raavare 'champignon'. Then you make a query that selects all recept_names where the previous query is not in.

SELECT DISTINCT recept_navn
FROM recept
WHERE recept_navn NOT IN 
(
    SELECT DISTINCT recept_navn
    FROM recept
    JOIN receptkomponent
    ON receptkomponent.recept_id = recept.recept_id
    JOIN raavare 
    ON raavare.raavare_id = receptkomponent.raavare_id
    WHERE raavare_navn = 'champignon'
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Specifying criteria for child table in One To Many Relationship

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

Custom hibernate criteria (one-to-many relationship)

From Dev

Criteria query for unidirectional one-to-many relationship

From Dev

Possible to perform a NOT IN for one-to-many relationship with Hibernate criteria?

From Dev

Grails Criteria dynamic AND conditions for one-to-many relationship

From Dev

Zip Lists together based on many to one relationship

From Dev

MySQL one to many relationship, with shared values

From Dev

create mysql table with one to many relationship

From Dev

MySQL - multiple values in one field or many-to-many relationship?

From Dev

Many to many relationship and MySQL

From Dev

Mysql Many to Many relationship

From Dev

JPA criteria query in a many-to-many relationship

From Dev

In one to many relationship, return distinct rows based on MIN value

From Dev

Hibernate Criteria fetchType JOIN with one-to-many relationship shows different behavior with and without aliases

From Dev

One To Many Relationship In Firebase

From Dev

Doctrine One to Many relationship

From Dev

Seeding one to many relationship

From Dev

Themeing a one to many relationship

From Dev

One to many relationship table

From Dev

Paginate a One to Many Relationship

From Dev

Laravel: one to many relationship

From Dev

Accessing a one to many relationship

From Dev

Displaying one to many relationship

From Dev

Implementing one to many relationship

From Dev

MagicalRecord: one to many relationship

From Dev

Paginate a One to Many Relationship

From Dev

Seeding one to many relationship

From Dev

One to many relationship with NSfetchedresultscontroller

Related Related

  1. 1

    Specifying criteria for child table in One To Many Relationship

  2. 2

    Criteria query for unidirectional one-to-many relationship

  3. 3

    Custom hibernate criteria (one-to-many relationship)

  4. 4

    Criteria query for unidirectional one-to-many relationship

  5. 5

    Possible to perform a NOT IN for one-to-many relationship with Hibernate criteria?

  6. 6

    Grails Criteria dynamic AND conditions for one-to-many relationship

  7. 7

    Zip Lists together based on many to one relationship

  8. 8

    MySQL one to many relationship, with shared values

  9. 9

    create mysql table with one to many relationship

  10. 10

    MySQL - multiple values in one field or many-to-many relationship?

  11. 11

    Many to many relationship and MySQL

  12. 12

    Mysql Many to Many relationship

  13. 13

    JPA criteria query in a many-to-many relationship

  14. 14

    In one to many relationship, return distinct rows based on MIN value

  15. 15

    Hibernate Criteria fetchType JOIN with one-to-many relationship shows different behavior with and without aliases

  16. 16

    One To Many Relationship In Firebase

  17. 17

    Doctrine One to Many relationship

  18. 18

    Seeding one to many relationship

  19. 19

    Themeing a one to many relationship

  20. 20

    One to many relationship table

  21. 21

    Paginate a One to Many Relationship

  22. 22

    Laravel: one to many relationship

  23. 23

    Accessing a one to many relationship

  24. 24

    Displaying one to many relationship

  25. 25

    Implementing one to many relationship

  26. 26

    MagicalRecord: one to many relationship

  27. 27

    Paginate a One to Many Relationship

  28. 28

    Seeding one to many relationship

  29. 29

    One to many relationship with NSfetchedresultscontroller

HotTag

Archive