我有 4 张 table 。它们是客户、元素、图像和规范。目标是将这 4 个表连接在一起。现在我正在像这样组合项目和图像:
SELECT item_id, item_name, item_price,images_id, GROUP_CONCAT(images_fullname) AS images, images_itemid
FROM items
LEFT OUTER JOIN images ON item_id=images_itemid
但是加入第三张表规范的最佳方式是什么? specs包含:spec_id, spec_itemid, spec_name, spec_value(specs是具体item的规范)。我想最好的做法是不要再次复制 Left Outer Joine?
我需要的布局如下:
Item name 1
image1, image2, image 3 (is working now with concat)
spec_name, spec_value
spec_name, spec_value
spec_name, spec_value
Item name 2
image1, image2, image 3 (is working now with concat)
spec_name, spec_value
spec_name, spec_value
spec_name, spec_value
最佳答案
仅当连接的表可以为空时才需要外连接。目前尚不清楚这里是否属于这种情况,但请记住这一点。
SELECT
various_columns
FROM
items
LEFT JOIN images ON item_id = images_itemid
LEFT JOIN specs ON item_id = spec_itemid
添加另一个连接没有错;您仍在加入关键专栏(我假设)。
关于mysql - 将表连接在一起Mysql 3次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887191/