gpt4 book ai didi

mysql - selectRaw 中的原始子查询

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

 BowlerMaster::selectRaw(" 
tour_id, bowler_id, bowler_name, ball_team,
SUM(balls) AS balls,
SUM(overs) AS overs,
DB::raw('SELECT runs, wicket FROM bowler_master WHERE tour_id = ".$request->tour_id." AND bowler_id = ".$request->player_id." ORDER BY wicket DESC, runs LIMIT 1 AS bbi')
")
->where($where_array)
->groupby(['tour_id','bowler_id'])
->get()->first();

在上面的 Eloquent 查询中,我想使用原始查询或任何其他方式来获取原始查询的结果来获取 bbi 的值,但我收到以下错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '::raw('SELECT match_id, runs, wicket FROM bowler_master WHERE tour_id = 1 AND bo' at line 25.

我正在编写此查询以生成以下输出,

{
"status": 200,
"message": "Success",
"bowler": {
"tour_id": 1,
"bowler_id": 21,
"bowler_name": "Kaushal Chauhan",
"ball_team": null,
"balls": "29",
"overs": 4.5,
"bbi": 9/3,
}
}

经过计算,一切正常。我不知道如何获取“bbi”键:值注意:查询结果将被推送到响应数组中,并转换为 json 以获得如上所示的输出。

请指导。

最佳答案

试试这个

BowlerMaster::selectRaw("tour_id, bowler_id, bowler_name, ball_team, SUM(balls) AS balls,SUM(overs) AS overs")
->selectSub("
SELECT runs
FROM bowler_master
WHERE tour_id = ".$request->tour_id."
AND bowler_id = ".$request->player_id."
ORDER BY wicket DESC, runs LIMIT 1
","runs")
->selectSub("
SELECT wicket
FROM bowler_master
WHERE tour_id = ".$request->tour_id."
AND bowler_id = ".$request->player_id."
ORDER BY wicket DESC, runs LIMIT 1
","wicket")
->where($where_array)
->groupby(['tour_id','bowler_id'])
->get()->first();

laravel中select子查询的编写规则如下:

DB::table('tablename')
->select('column name','..')
->selectSub("Your full query","alias name")
->get()

关于mysql - selectRaw 中的原始子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473382/

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