MySQL联接查询仅带参考表的一行

时间:2019-03-21 06:01:26

标签: mysql

这是我的5张桌子。

产品

  • id(PK)
  • 名称

说明

  • id(PK)
  • 身体

价格

  • id(PK)
  • 货币

产品说明

  • id(PK)
  • product_id
  • description_id

产品价格

  • id(PK)
  • product_id
  • price_id

productdescriptionprice是存储实际数据的地方。

product_descriptionproduct_price是参考表。

我的预期查询结果是这样的。

product_id | product_name | description_body | price_currency

当前表格的数据

product
+----+----------------+
| id | name           |
+----+----------------+
|  1 | first product  |
|  2 | second product |
|  3 | third product  |
+----+----------------+
description
+----+------------+
| id | body       |
+----+------------+
|  1 | first desc |
|  2 | second des |
|  3 | third desc |
+----+------------+
price
+----+-------------+
| id | currency    |
+----+-------------+
|  1 | first cur   |
|  2 | second cur2 |
|  3 | third cur   |
+----+-------------+
product_description
+----+------------+----------------+
| id | product_id | description_id |
+----+------------+----------------+
|  1 |          1 |              1 |
|  2 |          2 |              2 |
|  3 |          3 |              3 |
+----+------------+----------------+
product_price
+----+------------+----------+
| id | product_id | price_id |
+----+------------+----------+
|  1 |          1 |        1 |
|  2 |          2 |        2 |
|  3 |          3 |        3 |
+----+------------+----------+

查询

SELECT product.*, description.body, price.currency FROM product
LEFT JOIN product_description ON product.id = product_description.product_id
LEFT JOIN product_price ON product.id = product_price.product_id
LEFT JOIN description ON description.id = product_description.description_id
LEFT JOIN price ON price.id = product_price.price_id

结果

+----+----------------+------------+-------------+
| id | name           | body       | currency    |
+----+----------------+------------+-------------+
|  1 | first product  | first desc | first cur   |
|  2 | second product | second desc| second cur2 |
|  3 | third product  | third desc | third cur   |
+----+----------------+------------+-------------+

那一刻,如果我在product_description中再插入一行包含product_id=1的行并重新查询,它将显示许多行product_id=2

插入product_description个值(4、2、1)后

product_description
+----+------------+----------------+
| id | product_id | description_id |
+----+------------+----------------+
|  1 |          1 |              1 |
|  2 |          2 |              2 |
|  3 |          3 |              3 |
|  4 |          2 |              1 |
+----+------------+----------------+

加入查询后。

+----+----------------+------------+-------------+
| id | name           | body       | currency    |
+----+----------------+------------+-------------+
|  1 | first product  | first desc | first cur   |
|  2 | second product | second desc | second cur2 |
|  2 | second product | first desc | second cur2 |
|  3 | third product  | third desc | third cur   |
+----+----------------+------------+-------------+

您知道由于product=2product_description行的计数为2,因此将结果全部打印出来。

但是我只想最后一行包含product=2的行。

也许限制或区分是有用的,我不知道这是可能的。

在联接查询中可以使用限制还是不重复?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用相关子查询

    SELECT product.*, description.body, price.currency FROM product
    LEFT JOIN 
    ( select * from product_description
      where product_description.id in (select max(id) from product_description b 
      where product_description.product_id=b.product_id)
    ) as x ON product.id = x.product_id
    LEFT JOIN product_price ON product.id = product_price.product_id
    LEFT JOIN description ON description.id = x.description_id
    LEFT JOIN price ON price.id = product_price.price_id