Actual Result:
[
{ id: 1, title: 'Test Tournament #$1', d_title: 'Rendering Vue Slots content to an iframe'},
{ id: 1, title: 'Test Tournament #$1', d_title: 'Example' },
{ id: 1, title: 'Test Tournament #$1', d_title: 'Example' },
{ id: 1, title: 'Test Tournament #$1', d_title: 'asdfasdf' },
{ id: 2, title: 'Test Tournament #$2', d_title: 'Rendering Vue Slots content to an iframe'},
{ id: 2, title: 'Test Tournament #$2', d_title: 'Example' },
{ id: 2, title: 'Test Tournament #$2', d_title: 'Example' },
{ id: 2, title: 'Test Tournament #$2', d_title: 'asdfasdf' }
]
Expected Result:
[
{ id: 1, title: 'Test Tournament #$1', detail:
[
{ d_title: 'Rendering Vue Slots content to an iframe'},
{ d_title: 'Example'},
{ d_title: 'Example'},
{ d_title: 'asdfasdf'},
]
},
{ id: 2, title: 'Test Tournament #$2', detail:
[
{ d_title: 'Rendering Vue Slots content to an iframe'},
{ d_title: 'Example'},
{ d_title: 'Example'},
{ d_title: 'asdfasdf'},
]
}
]
如何使用 SQL 查询获取“预期结果”?
在 Laravel 中,我们可以通过使用与表的关系来获取它,但是如何通过纯 SQL 查询来获取它?
最佳答案
你不能直接得到这个结果,你需要手动生成,
1-首先获取tile & id,然后循环它。并运行第二个查询,该查询带来 d_title。这样你就可以得到你想要的输出格式。
2-另一种方法是通过 order by 获取所有需要的数据,然后循环它并制作所需的输出格式。
laravel 也采用第一种方法来做到这一点。现在由你决定你想做什么。
关于mysql - 为一对多关系生成 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605471/