我有 3 个表,分别是 product、category 和 product_category。如何在我的管理面板的表格中显示具有多个类别的产品?
product table category table product_category table
+---+------------+ +---+------------+ +-----------+------------+
|id | name | |id | name | |product_id | category_id|
+---+------------+ +---+------------+ +-----------+------------+
| 1 | Cake | | 1 | Sweets | | 1 | 1 |
| 2 | Chocolate | | 2 | Dessert | | 1 | 2 |
+---+------------+ +---+------------+ | 2 | 1 |
| 2 | 2 |
+-----------+------------+
我尝试创建一个“链接表”,但是当我尝试获取数据时,它给了我多行。
$query = mysqli_query($con, "SELECT * FROM product, category, product_category WHERE product.product_id=product_category.product_id AND product_category.category_id=category.category_id");
它给了我这个输出:
+-------------+------------+
|product name | category |
+-------------+------------+
| Cake | Sweets |
| Cake | Dessert |
| and so on... |
+-------------+------------+
但我希望它看起来像这样:
+-------------+--------------------+
|product name | category |
+-------------+--------------------+
| Cake | Sweets,Dessert |
| and so on... |
+-------------+--------------------+