gpt4 book ai didi

php - Laravel:如何用 Eloquent 方式表达 HAVING COUNT

转载 作者:可可西里 更新时间:2023-11-01 00:02:31 25 4
gpt4 key购买 nike

我如何在 Laravel 中表达这个查询?

SELECT `projects_issues`.* FROM `projects_issues` 

JOIN `projects_issues_tags`
ON `projects_issues_tags`.`issue_id` = `projects_issues`.`id`

JOIN `tags`
ON (`tags`.`id` = `projects_issues_tags`.`tag_id`)

WHERE `project_id` = '1'
AND `tags`.`tag` IN('tagname1','tagname2')

GROUP BY `projects_issues`.`id`
HAVING COUNT(DISTINCT `tags`.`tag`) = 2

ORDER BY `projects_issues`.`updated_at` DESC

现在我的代码是这样的。`

$issues = \Project\Issue::with('tags');

if ($tags || $sort_by != 'updated')
{
$issues = $issues
->join('projects_issues_tags', 'projects_issues_tags.issue_id', '=', 'projects_issues.id')
->join('tags', 'tags.id', '=', 'projects_issues_tags.tag_id');
}

$issues = $issues->where('project_id', '=', Project::current()->id);

if ($tags)
{
$tags_collection = explode(',', $tags);
$tags_amount = count($tags_collection);

$issues = $issues->where_in('tags.tag', $tags_collection);//->get();
}

$issues = $issues
->group_by('projects_issues.id')
->order_by($sort_by_clause, $sort_order);

if($tags && $tags_amount>1){
$issues = $issues->having('count', '=', $tags_amount);
}

$issues = $issues->get(array('projects_issues.*'));

它返回此错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'having clause'

最佳答案

使用havingRaw:

$issues = $issues->havingRaw("COUNT(DISTINCT `tags`.`tag`) = ".$tags_amount);

关于php - Laravel:如何用 Eloquent 方式表达 HAVING COUNT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516371/

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