mysql - 将表连接在一起Mysql 3次

标签 mysql sql join

我有 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/

相关文章:

mysql - 有*.chm格式的MySQL手册吗?

sql - 体面的可视化 SQL 查询生成器

mysql - 加入 3 个表并选择计数

mysql语法连接两个字段

python - 为什么在 2012 年 python 中的 pandas 合并比 R 中的 data.table 合并更快?

MySQL 检索 friend 的 friend 结构和性能

php - "annotations"在Doctrine中有什么优点?

仅mysql转储表

java - 找不到合适的驱动程序发现 odbc、db2 错误

php - SQL 查询执行时间急剧增加