在yii2的$ query中添加beetween选项

时间:2016-03-08 09:00:23

标签: mysql sql sql-server yii yii2

我想在$query

中使用
    $query->where(['and', "SUM(opportunity_conditions.money)>=10", "SUM(opportunity_conditions.money)<=100"]);

这是我的$查询:

   $query = (new \yii\db\Query())->select(['SUM(opportunity_conditions.money),(opportunity_conditions.money)', 'opportunity.id', 'opportunity.logo', 'profile_details.user_id', 'opportunity.name', 'profile_details.co_name', 'opportunity.address', 'opportunity.project_type_id', 'opportunity.state_id'])->from('opportunity ')->innerJoin('profile_details', 'opportunity.user_id=profile_details.user_id')->where('opportunity_conditions.who=2')->innerJoin('opportunity_conditions', 'opportunity.id=opportunity_conditions.opportunity_id')->addGroupBy('opportunity.id')->orderBy('opportunity.id desc');

我需要我的查询结果在哪里:

 SUM(opportunity_conditions.money)>=10 and SUM(opportunity_conditions.money)<=100

这是我的错误:

  

SQLSTATE [HY000]:常规错误:1111无效使用组功能

正在执行的SQL是:

SELECT  SUM(opportunity_conditions.money),
        (opportunity_conditions.money),
        opportunity.id,
        opportunity.logo,
        profile_details.user_id,
        opportunity.name,
        profile_details.co_name,
        opportunity.address,
        opportunity.project_type_id,
        opportunity.state_id
FROM opportunity 
INNER JOIN profile_details 
    ON opportunity.user_id=profile_details.user_id 
INNER JOIN opportunity_conditions 
    ON opportunity.id=opportunity_conditions.opportunity_id 
WHERE 
    ((SUM(opportunity_conditions.id)>=1) AND 
    (SUM(opportunity_conditions.id)<=1)) AND 
    (opportunity.id IN (27, 28)) 
GROUP BY opportunity.id 
ORDER BY opportunity.id DESC

2 个答案:

答案 0 :(得分:2)

尝试将where()替换为having()。然后将opportunity_conditions.who添加到select()列表。

答案 1 :(得分:0)

好的我觉得你可以这样想。

首先检查查询是否正确?我们可以从以下命令中获取查询

var_dump($query->createCommand()->rawSql);

现在检查此查询或在数据库中运行此查询,如果发现错误,则重新创建查询。