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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程