简体   繁体   English

内连接 MYSQL 查询组合 3 个表

[英]Inner Join MYSQL Query Combining 3 tables

How can I combine 3 tables in a INNER JOIN?如何在 INNER JOIN 中组合 3 个表?

The end result I am after is getting a list of CATEGORIES belonging to a PRODUCT - including the CATEGORY'S PARENT ID CATEGORY value (ie: Sneakers and Nike).我所追求的最终结果是获得属于产品的类别列表 - 包括类别的父 ID 类别值(即:运动鞋和耐克)。

The CATEGORIES table and PRODUCTS table are joined in the PRODUCTS & CATEGORIES table. CATEGORIES 表和 PRODUCTS 表连接在 PRODUCTS & CATEGORIES 表中。 A product can belong to many categories and a category can have many products.一个产品可以属于多个类别,一个类别可以有多个产品。

Here's more-or-less the setup I have in my database...这是我在数据库中或多或少的设置...

CATEGORIES TABLE:类别表:

CAT ID | PARENT ID | CATEGORY
1      | 0         | Sneakers
2      | 1         | Nike
3      | 2         | Jordan

PRODUCTS TABLE:产品表:

PROD ID 
1
2
3

PRODUCTS & CATEGORIES TABLE:产品和类别表:

CAT ID | PROD ID
1      | 0
1      | 1
2      | 3

I am running these queries and I am getting some results, but at the moment I am running 2 separate queries...我正在运行这些查询并得到了一些结果,但目前我正在运行 2 个单独的查询......

$q1 = "SELECT prodid, GROUP_CONCAT(catid SEPARATOR ' // ') as catid FROM products_categories group by prodid order by prodid";
$result1 = $conn->query($q1);

   if ($result1->num_rows > 0) {
      while($prods = $result1->fetch_assoc()) {
         echo "Product Id:" . $prods["prodid"] . " ––> " . "Categories Id:" . $prods["catid"];
      }
   } else {
      echo "0 results";
   }

$q2 =
"  SELECT `ID`.`category` as `IDName`, `LABEL`.`category` as `LabelName`, `LABEL`.`catid` as `LabelId`
   FROM `categories` as ID 
   INNER JOIN `categories` as LABEL
   ON `ID`.`catid` = `LABEL`.`parentid`";
$result2 = $conn->query($q2);

   if ($result2->num_rows > 0) {
      while($prods = $result2->fetch_assoc()) {
            echo "ID# " . $prods["LabelId"] . " is called: ". $prods["LabelName"] . "<br>";
      }
   } else {
      echo "0 results";
   }
$conn->close();

I have tried adding another INNER JOIN with no luck in the results.我尝试添加另一个 INNER JOIN,但结果没有运气。 The end result I am after would be: PROD ID #0 belongs to Sneakers, Nike, Jordan .我追求的最终结果是: PROD ID #0属于运动鞋、耐克、乔丹 Anyone can point me in the right direction?任何人都可以指出我正确的方向吗?

Thank you so much,非常感谢,

Sergio塞尔吉奥


UPDATE - 10/11/16更新 - 10 年 11 月 16 日

The Query:查询:

$q =
"  SELECT PC.productid as productid,  concat_WS('~',C1.category, C2.category, C3.category) as breadcrumb
   FROM xcart_categories as C1

   INNER JOIN xcart_products_categories as PC 
      ON C1.categoryid = PC.categoryid

   LEFT JOIN xcart_categories as C2
      ON C1.categoryid = C2.parentid
      AND C1.parentid = 0

   LEFT JOIN xcart_categories as C3
      ON C2.categoryid = C3.parentid
      WHERE C1.parentid = 0
   ";

The Fetch:获取:

$result = $conn->query($q);

   if ($result->num_rows > 0) {
      while($prods = $result->fetch_assoc()) {
         echo $prods['productid'] . ' Belongs in these categories: ' . $prods['breadcrumb'] . '<br>';
      }
   } else {
      echo "0 results";
   }

This assumes 3 levels of hierarchy no more and a separate join is needed to "put each record on the same line" so they can be combined into a single value result.这假设不再有 3 个层次的层次结构,并且需要一个单独的连接来“将每个记录放在同一行上”,以便它们可以组合成单个值结果。 I thin you were trying to use Group_concat but I can't see how that's going to work as you don't have a way to walk the hierarchy.我想您是在尝试使用 Group_concat,但我看不出这是如何工作的,因为您无法遍历层次结构。

SELECT PC.ProductID,  concat_WS('-',C1.Category, C2.Category, C3.Category) as breadcrumb
FROM categories C1
INNER JOIN ProductsCategories PC 
 on C1.categoryID = PC.CategoryID
LEFT JOIN categories C2
 on c1.CategoryID = C2.ParentID
and C1.parentID = 0
LEFT Join Categories C3
 on C2.CategoryID = C3.ParentID
WHERE C1.ParentID = 0

Working SQL Fiddle example ( this only supports 3 levels deep, but could be altered with added left joins to support a max level but not a undetermined max level..)工作SQL Fiddle示例(这仅支持 3 级深度,但可以通过添加左连接进行更改以支持最大级别但不支持未确定的最大级别..)

I see you're trying to use group concat to bring all the rows for the same product category.productID of 0 to the same line我看到您正在尝试使用 group concat 将同一产品类别的所有行。productID 为 0 到同一行

However as 0 references catID of 1 it would only return "sneakers" on the inner join.但是,当 0 引用 catID 为 1 时,它只会在内部联接上返回“运动鞋”。 You would need to traverse the tree (all of it) somehow, thus the above, or you have to take multiple trips to the db or use some sort of dynamic SQL or method mentioned in link in comments.您需要以某种方式遍历树(所有树),因此如上所述,或者您必须多次访问数据库或使用某种动态 SQL 或注释链接中提到的方法。

This would be fairly simple in SQL Server, Oracle or other Enterprise RDBMS systems, however without recursive queries or engine specific hierarchy queries, this is no easy feat in MySQL on a single trip.这在 SQL Server、Oracle 或其他企业 RDBMS 系统中会相当简单,但是如果没有递归查询或引擎特定的层次结构查询,在 MySQL 中单次完成这件事并非易事。

Maybe I'm missing something so it may help to see the actual expected results for your sample data.也许我遗漏了一些东西,因此查看示例数据的实际预期结果可能会有所帮助。 What is the record set look like that you want back?你想要找回的记录集是什么样的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM