Search code examples
mysqlrelational

mysql n to m query


I have following table structure

table domains

 ID     | name        |
________________________
   1    | example.com |
________________________
   2    | example.net |
________________________
   3    | example.org |

table products

 ID     | group        |
________________________
   1    | furniture    |
________________________
   2    | electronics  |
________________________
   3    | toys         |

table transit

 Domain.ID     |Produkt.ID |
_____________________________
   1           |    3      |
_____________________________
   1           |    2      |
_____________________________
   3           |    1      | 

as variable I have the domain name and in function of this I would like to get all the related products to the domain.

example:domain =>example.com should give me back the properties of product table with ID's 2 and 3


Solution

  • Select domains.ID, name,group
    from Transit Inner Join Domains ON
    Transit.domainId = Domains.Id
    INNER JOIN products  ON
    Transit.productId = products.Id
    Where domains.Name= ?