【问题标题】:Left Join Multiple Tables左连接多个表
【发布时间】:2016-02-19 05:44:27
【问题描述】:

我的mysql数据库结构如下

产品表

+----+--------+-------------+-------+
| id | itemid |    title    | price |
+----+--------+-------------+-------+
|  1 |      1 | Dell XPS 10 |  1000 |
|  2 |      1 | Dell XPS 13 |  1100 |
|  3 |      1 | HP 1        |  1100 |
+----+--------+-------------+-------+

项目表

+----+-------------+
| id | description |
+----+-------------+
|  1 | Laptop      |
|  2 | Monitors    |
+----+-------------+

参数表

+----+--------+------------------+
| id | itemid |   description    |
+----+--------+------------------+
|  1 |      1 | Brand            |
|  2 |      1 | Series           |
|  3 |      1 | Color            |
|  4 |      1 | Operating System |
+----+--------+------------------+

规格表

+----+-----------+-------------+-------------+
| id | productid | parameterid | description |
+----+-----------+-------------+-------------+
|  1 |         1 |           1 | Dell        |
|  2 |         1 |           2 | XPS         |
|  3 |         1 |           3 | Red         |
|  4 |         1 |           4 | Windows 10  |
|  5 |         2 |           1 | Dell        |
|  6 |         2 |           2 | XPS         |
|  7 |         2 |           3 | Black       |
|  8 |         2 |           4 | Windows 7   |
|  9 |         3 |           1 | HP          |
| 10 |         3 |           2 | 1           |
| 11 |         3 |           3 | Black       |
| 12 |         3 |           4 | Windows 10  |
+----+-----------+-------------+-------------+

我正在尝试输出以下结果

 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+
 | id | productid |    title    | price | itemdescription | parameterdescription | specificationdescription |
 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Brand                | Dell                     |
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Series               | XPS                      |
 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+

我得到的不是这个

 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+
 | id | productid |    title    | price | itemdescription | parameterdescription | specificationdescription |
 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Brand                | Dell                     |
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Brand                | XPS                      |
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Brand                | Red                      |
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Brand                | Windows 10                      |
 +----+-----------+-------------+-------+-----------------+----------------------+--------------------------+

我正在使用以下 mysql 查询

SELECT products.title, products.price, items.description, parameters.description, specifications.description
FROM products
LEFT JOIN items ON products.itemid = items.id
LEFT JOIN parameters ON parameters.itemid = items.id
LEFT JOIN specifications ON specifications.productid = products.id
LIMIT 0 , 30

其次,是否可以将所有参数和规格列在同一行中?

 +----+-----------+-------------+-------+-----------------+--------------------------+--------------------------+--------------------------+
 | id | productid |    title    | price | itemdescription | specificationdescription | specificationdescription | specificationdescription |
 +----+-----------+-------------+-------+-----------------+--------------------------+--------------------------+--------------------------+
 |  1 |         1 | Dell XPS 10 |  1000 | Laptop          | Dell                     | XPS                      | Red                      |
 +----+-----------+-------------+-------+-----------------+--------------------------+--------------------------+--------------------------+

如果有人对如何改进数据库结构有任何建议,我愿意接受优化它的想法。谢谢

以下是@Shakir 答案的输出

+-------------+-------+-------------+------------------+-------------+
|    title    | price | description |   description    | description |
+-------------+-------+-------------+------------------+-------------+
| Dell XPS 10 |  1000 | Laptop      | Brand            | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Series           | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Color            | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Operating System | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Screen Size      | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Touchscreen      | Dell        |
| Dell XPS 10 |  1000 | Laptop      | Brand            | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Series           | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Color            | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Operating System | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Screen Size      | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Touchscreen      | XPS         |
| Dell XPS 10 |  1000 | Laptop      | Brand            | Black       |
| Dell XPS 10 |  1000 | Laptop      | Series           | Black       |
| Dell XPS 10 |  1000 | Laptop      | Color            | Black       |
| Dell XPS 10 |  1000 | Laptop      | Operating System | Black       |
| Dell XPS 10 |  1000 | Laptop      | Screen Size      | Black       |
| Dell XPS 10 |  1000 | Laptop      | Touchscreen      | Black       |
| Dell XPS 10 |  1000 | Laptop      | Brand            | Windows 10  |
| Dell XPS 10 |  1000 | Laptop      | Series           | Windows 10  |
| Dell XPS 10 |  1000 | Laptop      | Color            | Windows 10  |
| Dell XPS 10 |  1000 | Laptop      | Operating System | Windows 10  |
| Dell XPS 10 |  1000 | Laptop      | Screen Size      | Windows 10  |
| Dell XPS 10 |  1000 | Laptop      | Touchscreen      | Windows 10  |
+-------------+-------+-------------+------------------+-------------+

【问题讨论】:

  • 检查品牌和系列的参数表中的 itemid 为 1。因此您的结果 id 正确。如果您想要预期的结果,请在系列之前将 2 个项目 id 放入参数表中
  • itemid = 1 因为两者都是 item = Laptop 的参数
  • 您是否在问为什么您在所有 4 行中都获得品牌而不是品牌、颜色、系列和操作系统?是的,这是可能的。查看 group by 和 group_concat
  • @Flyer 是的,我希望它显示品牌颜色系列,而不是一次又一次地重复品牌。让我也看看 group_concat。

标签: php mysql left-join inner-join


【解决方案1】:

试试这个并显示结果

SELECT products.title, products.price, items.description,  
parameters.description, specifications.description
FROM products
INNER JOIN items ON products.itemid = items.id
INNER JOIN parameters ON parameters.itemid = items.id
INNER JOIN specifications ON specifications.productid = products.id
LIMIT 0 , 30

【讨论】:

  • |戴尔 XPS 10 | 1000 |笔记本电脑 |品牌 |戴尔 | |戴尔 XPS 10 | 1000 |笔记本电脑 |系列 |戴尔 | |戴尔 XPS 10 | 1000 |笔记本电脑 |颜色 |戴尔 | |戴尔 XPS 10 | 1000 |笔记本电脑 |操作系统 |戴尔 | |戴尔 XPS 10 | 1000 |笔记本电脑 |品牌 | XPS |
  • 通过编辑您的答案并根据需要提供正确的输出?
【解决方案2】:

试试这个,看看它是否有效。

SELECT 
  products.title, 
  products.price, 
  items.description, 
  (
     SELECT 
        GROUP_CONCAT(spec.description SEPARATOR ' ')
     FROM specifications as spec
     WHERE spec.productid = products.id
     GROUP BY spec.productid
  ) AS `specificationdescription`
FROM products
INNER JOIN items ON products.itemid = items.id
LIMIT 0 , 30

【讨论】:

  • #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 10 行的“WHERE spec.productid = products.id) AS specificationdescription FROM prod”附近使用正确的语法
  • Moving Where before Group By 修复了语法错误。但问题在于,所有规范描述都在一行中的一列中,没有分隔。因此,如果我想对其进行搜索查询,我将无法做到。
  • 您可以通过使用products.id=specifications.productidproducts 表与specifications 表连接起来并包含WHER 来修改上述SQL。
猜你喜欢
  • 2021-09-02
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多