gpt4 book ai didi

mysql - cakephp 3.4 mySQL 到 orm 转换

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

我当前的 MYSQL 查询是

SELECT
cl.user_id,
COUNT(q.id) as completed_questions,
(
SELECT
SUM(points)
FROM
completed_levels
WHERE
user_id = cl.user_id
)
as level_point
FROM
completed_levels cl
JOIN
questions q
on q.level_id = cl.level_id
GROUP BY
cl.user_id;

我当前的 ORM 正在遵循我只是无法处理

中的选择部分
$completedLevels = TableRegistry::get('CompletedLevels');
$completedLevels = $completedLevels->find('All');
$completedLevels = $completedLevels
->contain(['Users', 'Levels'])
->select([
'user_name' => 'Users.name',
'count_questions' => 'COUNT(Questions.id)',
'total_pints' => [
'select'=>'SUM(CompletedLevels.points)'
]
])
->join([
'table' => 'Questions',
'conditions' => 'Questions.level_id = CompletedLevels.level_id',
])
->group(['CompletedLevels.user_id'])
->all();

最佳答案

找到答案它的“子查询”

    $completedLevels = TableRegistry::get('CompletedLevels');
TableRegistry::config('cl', ['table' => 'completed_levels']);
$cl = TableRegistry::get('cl');
$cl = $cl->find('all');
$cl ->select($cl->func()->sum('points'));

$completedLevels = $completedLevels->find('All');
$completedLevels = $completedLevels
->contain(['Users', 'Levels'])
->select([
'user_name' => 'Users.name',
'count_questions' => 'COUNT(Questions.id)',
'total_pints' => $cl->where('user_id = CompletedLevels.user_id')
])->join([
'table' => 'Questions',
'conditions' => 'Questions.level_id = CompletedLevels.level_id',
])->group(['CompletedLevels.user_id']);

关于mysql - cakephp 3.4 mySQL 到 orm 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172858/

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