title: Get all related categories by manufacturer, 5 tables
body: <p>Building report for a store and trying to get categories by manufacturer id. Not luck doing MySQL queries (experience almost null hehe) but I have this:</p>
  
  <p><img src="https://i.stack.imgur.com/JuU41.png" alt="manufacturer-product-product_category-category-category_description"></p>
  
  <h2>Table schema</h2>
  
  <pre><code>CREATE SCHEMA IF NOT EXISTS `table` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
  USE `table` ;
  
  -- -----------------------------------------------------
  -- Table `table`.`category_description`
  -- -----------------------------------------------------
  CREATE  TABLE IF NOT EXISTS `table`.`category_description` (
    `category_id` INT(11) NOT NULL AUTO_INCREMENT ,
    `name` VARCHAR(45) NOT NULL ,
    PRIMARY KEY (`category_id`) )
  ENGINE = InnoDB;
  
  
  -- -----------------------------------------------------
  -- Table `table`.`category`
  -- -----------------------------------------------------
  CREATE  TABLE IF NOT EXISTS `table`.`category` (
    `category_id` INT(11) NOT NULL AUTO_INCREMENT ,
    `image` VARCHAR(255) NULL ,
    PRIMARY KEY (`category_id`) )
  ENGINE = InnoDB;
  
  
  -- -----------------------------------------------------
  -- Table `table`.`manufacturer`
  -- -----------------------------------------------------
  CREATE  TABLE IF NOT EXISTS `table`.`manufacturer` (
    `manufacturer_id` INT(11) NOT NULL AUTO_INCREMENT ,
    `name` VARCHAR(64) NOT NULL DEFAULT '' ,
    PRIMARY KEY (`manufacturer_id`) )
  ENGINE = InnoDB;
  
  
  -- -----------------------------------------------------
  -- Table `table`.`product`
  -- -----------------------------------------------------
  CREATE  TABLE IF NOT EXISTS `table`.`product` (
    `product_id` INT(11) NOT NULL AUTO_INCREMENT ,
    `manufacturer_id` INT(11) NOT NULL ,
    `name` VARCHAR(45) NULL ,
    PRIMARY KEY (`product_id`) ,
    INDEX `product_manufacturer` (`manufacturer_id` ASC) )
  ENGINE = InnoDB;
  
  
  -- -----------------------------------------------------
  -- Table `table`.`product_category`
  -- -----------------------------------------------------
  CREATE  TABLE IF NOT EXISTS `table`.`product_category` (
    `product_id` INT(11) NOT NULL ,
    `category_id` INT(11) NOT NULL ,
    PRIMARY KEY (`product_id`, `category_id`) ,
    INDEX `product_to_category_product_id` (`product_id` ASC) ,
    INDEX `product_to_category_category_id` (`category_id` ASC) )
  ENGINE = InnoDB;
  
  -- Sorry if example data is not provided
  </code></pre>
  
  <h2>MySQL query</h2>
  
  <pre><code>SELECT category_description.name, category.category_id
  FROM manufacturer
    JOIN product ON product.manufacturer_id = manufacturer.manufacturer_id
    JOIN product_to_category ON product_to_category.product_id = product.product_id
    JOIN category ON category.category_id = product_to_category.category_id
    JOIN category_description ON category_description.category_id = category.category_id
    WHERE manufacturer.manufacturer_id = 7
  </code></pre>
  
  <p>The query result has many <code>category_description.name</code> repeated rows (inherited from my newbie-query I guess)</p>
  
  <p>Schema is based on products and the idea is when a manufacturer is selected then all categories related to its products are obtained (unique).</p>
  
  <p>I'm stock due to my lack of knowledge in building queries</p>
  
  <p><strong>Edited</strong>: So sorry, corrected <code>category</code> table row name (<code>category.image</code> instead <code>category.name</code>) </p>