SQL version: 5.5.24-log - MySQL Community Server (GPL)
PHP v5.3.13
Here’s my code:
public $calculated_height; // this is for the calculated field
public function search()
{
$criteria=new CDbCriteria;
$criteria->select='height as calculated_height';
$criteria->condition='calculated_height > 50';
$mtnresult = Mtn::model()->find($criteria);
}
this runs
SELECT height as calculated_height FROM stagingIR2013t WHERE calculated_height > 50 LIMIT 1
and gives this error
#1054 - Unknown column ‘calculated_height’ in ‘where clause’
I get the same error if I run this SQL in phpmyadmin
it’s because the SQL is calculated from right to left and calculated_height is evaluated in the where clause.
Because of this the aliasing of height to calculated_height has not yet occurred.
in other posts on this forum I read that i need to have public $calculated_height; in my model.
I have done that.
Can someone give some more examples of how I could change this so that the calculated_height is already there when I do the select
Yes, I agree this will work. However, I need to use the alias. The example here is a simplified version of what I need to do. In my actual production code, I will be using a stored function on MySQL so that is why I have to use calculated_height.
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘>calculated_height > 50 LIMIT 1’ at line 1. The SQL statement executed was: SELECT * FROM stagingIR2013t WHERE $mtnresult->calculated_height > 50 LIMIT 1
If I put $mtnresult in quotes in the find statement it will be evaluated as a literal so that’s not going to work in an SQL statement.
Thank you for your help but I’m going to take a slightly different approach.
I was doing a $criteria->compare on a calculated field and it was giving an error so instead of compare with an alias which doesn’t exist, I am doing the compare against the actual SQL function call. I have it almost working now. I think this is a cleaner approach for me.