gpt4 book ai didi

mysql - Laravel 查询生成器中的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 18:25:51 26 4
gpt4 key购买 nike

我有这个 SQL 查询,目前我不知道如何使用 larval 查询构建器构建它

SELECT h1.Name, h2.Torvorlage from 
(SELECT Name, Player_ID FROM `Spieler` WHERE Vereins_ID = 1) h1
left JOIN
(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) h2
on h1.Player_ID = h2.Player_ID

谁能给我一个如何构建这个的例子吗?

最佳答案

试试这个。

DB::table('Spieler')->where('Vereins_ID','=',1)
->join('SpielerStats','SpielerStats.Player_ID','=','Spieler.Player_ID','left')
->select('SpielerStats.Torvorlage','Spieler.Name')
->join('Spielplan','SpielerStats.Spielplan_ID','=','Spielplan.Spielplan_ID','left')
->where('Spielplan.Spielplan_ID','=',1)
->where('Spielplan.Spieltag','=',2)->get();

或者

$d = DB::raw('(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) as h2')


DB::table('Spieler')
->select('Spieler.Name','h2.Torvorlage')
->leftJoin($d, 'h2.Player_ID', '=', 'Spieler.Player_ID')
->where('Vereins_ID','=',1)
->get();

关于mysql - Laravel 查询生成器中的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com