简体   繁体   English

SQL左联接-合并具有相同ID的数组

[英]SQL LEFT JOIN - Combine arrays with the same id

I'm writing a MySQL query (with laravel Query Builder) with multiple LEFT JOIN. 我正在编写一个具有多个LEFT JOIN的MySQL查询(使用laravel Query Builder)。 In the result the array I get looks like: 结果,我得到的数组如下所示:

    array:4 [▼
  0 => {#322 ▼
    +"id": 19
    +"thing_id": 7
    +"title": "new translate"
    +"locale": "en"
    +"thing_sub_cat_id": 19
    +"name": "WINTER"
  }
  1 => {#323 ▼
    +"id": 24
    +"thing_id": 7
    +"title": "new translate"
    +"locale": "en"
    +"thing_sub_cat_id": 24
    +"name": "DIVING"
  }
  2 => {#324 ▼
    +"id": 37
    +"thing_id": 15
    +"title": "thing to do"
    +"locale": "en"
    +"thing_sub_cat_id": 37
    +"name": "LATEST HAPPENINGS"
  }
  3 => {#325 ▼
    +"id": 21
    +"thing_id": 2
    +"title": "Main page"
    +"locale": null
    +"thing_sub_cat_id": null
    +"name": null
  }
]

Query Builder : 查询生成器

DB::table('things_translations')
                            ->leftjoin('things_sub_cat_rel', 'things_sub_cat_rel.thing_id', '=', 'things_translations.thing_id')
                            ->leftjoin('things_sub_cat_translations', 'things_sub_cat_rel.thing_sub_cat_id', '=', 'things_sub_cat_translations.thing_sub_cat_id')
                            ->where('things_translations.locale', '=', 'en')
                            ->get();

The locale column associate with laravel-translatable package. locale列与laravel-translateable软件包关联。 I'm trying to get all rows of database that has locale=en . 我正在尝试获取具有locale=en的数据库的所有行。 I'd use laravel eloquent but I got unknown column locale so I've tried the query builder above. 我会用laravel雄辩,但我unknown column locale所以我尝试了上面的查询生成器。

Associated models : 相关型号

ThingTranslation that has belongsToMany relation with ThingSubCat . ThingTranslation具有与belongsToMany关系ThingSubCat For example let's say that I have 3 tables 1 for articles 1 for categories and 1 for relation that has the articleID and categoryID. 例如,假设我有3个表1,其中表1的类别为1,关系1的表具有articleID和categoryID。

I would like to know the better way to combine arrays that has the same "thing_id". 我想知道更好的方法来合并具有相同“ thing_id”的数组。 For example above I have two arrays with the thing_id: 7. Can I combine these two arrays to one using mysql and for the "name" to have multiple arrays with it's value ? 例如,在上面的示例中,我有两个带有THING_ID的数组:7.我可以使用mysql将这两个数组合并为一个,并且“名称”具有多个具有其值的数组吗?

I think you should use GROUP_CONCAT function in mysql like this : 我认为您应该像这样在mysql中使用GROUP_CONCAT函数:

SELECT tbl.*,
    GROUP_CONCAT(`name` SEPERATOR ',') as `name_combined`
    FROM
(
  SELECT * FROM things_translations
  LEFT JOIN things_sub_cat_rel ON things_sub_cat_rel.thing_id = things_translations.thing_id
  LEFT JOIN things_sub_cat_translations ON things_sub_cat_rel.thing_sub_cat_id = things_sub_cat_translations.thing_sub_cat_id
  WHERE things_translations.locale = 'en'
) tbl
GROUP BY things_id; 

With a query like this you combine name in rows that their thing_id are alike into one row. 有了这样的查询你把name在列,他们的thing_id都是一样成一排。

Syntax Reference : 语法参考:

http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

*If someone has the same problem with laravel he can look at my answers below. *如果有人对laravel有相同的问题,可以在下面查看我的答案。 If someone has similar problem, he can try with the query in the answer of Ramin Omrani. 如果有人有类似问题,他可以尝试在Ramin Omrani的答案中进行查询。 It works perfect. 完美的作品。

I've found the solution with laravel eloquent. 我发现雄辩的雄辩解决方案。 The problem it was because I'm using laravel-translatable package. 问题是因为我使用的是laravel-translateable包。 And the only way to take the rows that has locale = "en" is using the whereHas translations and then with where clause like the code bellow: 取得具有locale =“ en”的行的唯一方法是使用whereHas translations ,然后使用where子句,例如下面的代码:

$getThing = Thing::with('subCategories')->whereHas('translations', function ($query) {
    $query->where('locale', '=', 'en');
})->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM