dpsfay2510 2014-07-29 15:26
浏览 30
已采纳

MySQL加入多个映射表

Haven't been able to crack this one for longer than I care to admit.

I don't reckon this could be that unusual a problem - surely mapping table troubles come a dime a dozen in SQL land - so I'll just ask, in the hope that others may benefit from my ignorance.

I'm trying pull together five tables to display the products that are connected to both a main category and a sub category. What I want to display is the information in the product table.

The tables are as follows:

**products**
product_id (PK) | [other columns with product information]

**categories**
category_id (PK) | category_name

**sub_categories**
sub_category_id (PK) | sub_category_name

**products_and_categories**
pc_rel_id (PK) | pc_product_id (FK - products.product_id) | pc_category_id (FK -    categories.category_id)

**products_and_sub_categories**
psc_rel_id (PK) | psc_product_id (FK - products.product_id) | psc_sub_category_id (FK -    sub_categories.sub_category_id)

My fruitless attempts so far include:

$getSCatProds = new QueryClass();
    $catSubArray['queryCategory'] = 'main category name';
    $catSubArray['querySubCategory'] = 'sub category name';

//PDO fetchAll(PDO::FETCH_ASSOC)

    $resultArray = $getSCatProds->fetchAllQuery("SELECT product_id FROM products
                   INNER JOIN (products_and_categories, products_and_sub_categories)
                       ON (products_and_categories.pc_product_id=products.product_id 
                       AND products_and_sub_categories.psc_product_id=products.product_id) 
                   INNER JOIN (categories, sub_categories)
                       ON (categories.category_id = products_and_categories.pc_category_id 
                       AND sub_categories.sub_category_id = products_and_sub_categories.psc_sub_category_id)
                   WHERE categories.category_name = :queryCategory
                       AND sub_categories.sub_category_name = :querySubCategory", $catSubArray);

    print_r($resultArray); //returns an empty array

and:

//Query method and sent information same as in the above query

$resultArray = $getSCatProds->fetchAllQuery("SELECT product_id FROM products
               INNER JOIN (products_and_categories, products_and_sub_categories, categories, sub_categories)
                   ON (products_and_categories.pc_product_id=products.product_id 
                   AND products_and_sub_categories.psc_product_id=products.product_id) 
               WHERE categories.category_name = :queryCategory
                   AND sub_categories.sub_category_name = :querySubCategory", $catSubArray);

print_r($resultArray); 
//returns a stupendous number of product_id:s, 2030 of them to be exact.
//There are 2075 items in the products table ...

Please, give my poor head a hand with finding the right query.

Thank you.

  • 写回答

1条回答 默认 最新

  • doubaolan2842 2014-07-29 15:38
    关注

    You need to do a separate join with each table.

    SELECT p.product_id
    FROM products AS p
    JOIN products_and_categories AS pc ON p.product_id = pc.pc_product_id
    JOIN categories AS c ON pc.pc_category_id = c.category_id
    JOIN products_and_subcategories AS psc ON p.product_id = psc.psc_product_id
    JOIN sub_categories AS sc ON psc.psc_sub_category_id = sc.sub_category_id
    WHERE c.category_name = :queryCategory
    AND sc.sub_category_name = :querySubCategory
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值