I don't know how to put this.
I have a query:
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
my query is working fine, though when I add model->count() function it turn my query to this.
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
Now this is creating problem. I am getting error from mysql : first i got "#1248 - Every derived table must have its own alias", then on updating query I added 't' alias to the columns which are mentioned in the query. Now I am having "Duplicate column name ''"
I saw such problem here. But can there be a better way, so that I don't need to add alias name myself.
There are more queries /filters added to this one but I have added the basic query here. Possibly need solution in mysql as well as in yii. I am using Yii 1.x
Adding more description. In 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;
}
My both conditions run one by one. When I need to have count of battles. It shows me error in mysql while without count the same query runs correctly.
Modified my question: Added yii model query. @scaisEdge "video_status IN ( 3, 4 ) " is only working using HAVING Clause. It is showing error if I use it with in where clause. Error "Unknown column 'video_status' in 'where clause'"