我不知道该怎么做。
我有一个问题:
SELECT * , (
CASE
WHEN t.video_id IS NOT NULL
THEN pv.status
ELSE 4
END
) AS video_status
FROM `battles` `t`
LEFT JOIN process_videos pv ON ( pv.video_id = t.video_id )
WHERE title IS NOT NULL
AND cat_id IS NOT NULL
AND is_deleted =0
AND is_hidden =0
AND battle_type = "public"
HAVING video_status
IN ( 3, 4 )
ORDER BY `t`.`video_id` DESC
LIMIT 0 , 30
我的查询工作正常,但是当我添加model-> count()函数时,它会将我的查询转换为此。
SELECT COUNT(*) FROM (SELECT *, ( CASE
WHEN t.video_id IS NOT NULL
THEN pv.status
ELSE 4
END
) AS video_status FROM `battles` t LEFT JOIN process_videos pv on (pv.video_id = t.video_id) WHERE t.title IS NOT NULL AND t.cat_id IS NOT NULL AND t.is_deleted=0 AND t.is_hidden=0 and t.battle_type="public" HAVING video_status IN ( 3, 4 )) sq
现在这就产生了问题。我从mysql收到错误: 首先我得到了#12;#1248 - 每个派生表必须有自己的别名"然后在更新查询时我添加了' t'查询中提到的列的别名。现在我正在使用"重复列名称''"
我看到了这样的问题here。但是有没有更好的方法,所以我不需要自己添加别名。
此处添加了更多查询/过滤器,但我在此处添加了基本查询。 可能需要mysql以及yii中的解决方案。我正在使用Yii 1.x
添加更多说明。 在Yii:
$criteria->select = "*, ( CASE
WHEN t.video_id IS NOT NULL
THEN pv.status
ELSE 4
END
) AS video_status";
$criteria->join = "LEFT JOIN process_videos pv on (pv.video_id = t.video_id)";
$criteria->order = $orderBy;
$criteria->alias = 't';
$criteria->condition = "t.title IS NOT NULL AND t.cat_id IS NOT NULL AND t.is_deleted=0 AND t.is_hidden=0 and battle_type='public'".$condition1; // and pv.status=3
$criteria->having = " video_status IN ( 3, 4 )";
if(!$count){
$criteria->limit = 10
$criteria->offset = 0
$model = battles::model()->resetScope()->findAll($criteria);
}
else
{
$model_count = battles::model()->resetScope()->count($criteria);
return $model_count;
}
我的两个条件一个接一个地运行。当我需要战斗的时候。它显示我在mysql中的错误,而没有计数相同的查询正确运行。
修改了我的问题:添加了yii模型查询。 @scaisEdge" video_status IN(3,4)"只使用HAVING子句。如果我在where子句中使用它会显示错误。错误"未知列' video_status'在' where子句'"
答案 0 :(得分:1)
具有聚合功能。我没有在你的第一个选择....案例中看到总结。
以这种方式尝试使用where子句
SELECT * , (
CASE
WHEN t.video_id IS NOT NULL
THEN pv.status
ELSE 4
END
) AS video_status
FROM `battles` `t`
LEFT JOIN process_videos pv ON ( pv.video_id = t.video_id )
WHERE title IS NOT NULL
AND cat_id IS NOT NULL
AND is_deleted =0
AND is_hidden =0
AND battle_type = "public"
AND video_status IN ( 3, 4 )
ORDER BY `t`.`video_id` DESC
LIMIT 0 , 30;