【发布时间】:2021-02-03 06:17:34
【问题描述】:
我有三张桌子。 items、authors 和 author_item,将 author_id 与 item_id 配对。
我在加入时使用第三个表成功地从项目和作者中选择了我需要的所有内容。
我遇到的问题是有时有两三个作者与一个项目一起使用,我想将所有匹配的作者附加到一个项目行。
$publications = \DB::table('items')
->select('items.pub_number', 'items.title', 'authors.first_name', 'authors.last_name', 'items.published_date', 'items.updated_at')
->leftjoin('author_item', 'items.id', '=', 'author_item.item_id')
->leftjoin('authors', 'author_item.author_id', '=', 'authors.id')
->where('items.is_product', false); ```
对于一个可见的表示,我将在下面演示。
我愿意
| Pub # | Title | Authors | Published | Revision |
| ------| ------| ----------- | 1989-09-03 | 2020-10-05|
| 138 | beledo|Jaeden Pouros | 1989-09-03 | 2020-10-05|
| 138 | beledo|Jennifer Lexy | 1989-09-03 | 2020-10-05|
| 145 | argade|Alex Johnson | 1989-09-03 | 2020-10-05|
成为
| Pub # | Title | Authors | Published | Revision |
| ------| ------| --------------------------- | 1989-09-03 | 2020-10-05|
| 138 | beledo|Jaeden Pouros, Jennifer Lexy | 1989-09-03 | 2020-10-05|
| 145 | argade|Alex Johnson | 1989-09-03 | 2020-10-05|
这正在被查询到 Yajra 数据表中,所以如果我能够通过更改我的查询来完成此操作,那么这将变得不那么复杂。
【问题讨论】:
标签: php mysql laravel yajra-datatable