按制造商,5表格获取所有相关类别

时间:2012-12-10 05:39:41

标签: mysql relational-database

为商店构建报告并尝试按制造商ID获取类别。不是运气做MySQL查询(体验几乎为空)呵呵,但我有这个:

manufacturer-product-product_category-category-category_description

表架构

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

MySQL查询

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

查询结果有很多category_description.name个重复的行(我想继承自我的新手查询)

架构基于产品,其理念是选择制造商,然后获得与其产品相关的所有类别(唯一)。

由于我在构建查询方面缺乏知识而导致库存

已修改:很抱歉,更正了category表格行名称(category.image代替category.name

1 个答案:

答案 0 :(得分:1)

你去(sqlfiddle):

SELECT DISTINCT category_description.name, category.category_id
FROM product
  JOIN product_category ON product_category.product_id = product.product_id
  JOIN category ON category.category_id = product_category.category_id
  JOIN category_description ON category_description.category_id = category.category_id
  WHERE product.manufacturer_id = 1

a)不要链接你不需要的表

b)使用DISTINCT清除由一次不同的数据(即此处的产品)生成的欺骗行为。

c)不要拼写表名:p