这是我的5张桌子。
产品
说明
价格
产品说明
产品价格
表product
,description
,price
是存储实际数据的地方。
表product_description
和product_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=2
中product_description
行的计数为2,因此将结果全部打印出来。
但是我只想最后一行包含product=2
的行。
也许限制或区分是有用的,我不知道这是可能的。
在联接查询中可以使用限制还是不重复?
答案 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