CDbCommand无法执行SQL语句:SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'product_profile.price'

I get this error when I run Product.php that is model file and I get this error

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_profile.price' in 'where clause'. The SQL statement executed was: SELECT COUNT(*) FROM product t INNER JOIN product ON product_profile.product_id = t.product_id WHERE product_profile.price >=0 AND product_profile.price <=100

The code of query is below

if (!empty($_POST['price'])) {
    $price = explode(",", $_POST['price']);
    $criteria = new CDbCriteria;
    $criteria->select = 'product_profile.price,product_profile.product_id';
    $criteria->join.='INNER JOIN product ON product_profile.product_id = t.product_id';
    $criteria->condition = " product_profile.price BETWEEN 1 AND 100";
    // $criteria->condition = 'product_profile.price >=0 AND product_profile.price <=100';
    $criteria->addInCondition("product_profile.price",$price);
}

You are joining product with product table and hence product_profile is not found

The correct sql should be

SELECT COUNT(*) 
FROM product t 
INNER JOIN product_profile  ON product_profile.product_id = t.product_id 
WHERE 
product_profile.price >=0 AND product_profile.price <=100

So the following

$criteria->join.='INNER JOIN product ON product_profile.product_id = t.product_id';

should be

$criteria->join.='INNER JOIN product_profile ON product_profile.product_id = t.product_id';

You need change your SQL syntax via Yii ORM. Your finally query should be like this:

SELECT COUNT(*) FROM product t 
INNER JOIN product_profile
ON product_profile.product_id = t.product_id 
WHERE product_profile.price >=0 AND product_profile.price <=100

It means thant you need to JOIN product_profile table, not product table

And if other columns finded, but one - not. Here is code.

$pages = Yii::app()->db->createCommand()
            ->select('*')
            ->from('sk_subj_img_links')
            ->where('subj_clas=:subj', array(':subj'=>'math-1'))
            ->queryAll();

Then I get error: CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subj_clas' in 'where clause' But if this code

$pages = Yii::app()->db->createCommand()
            ->select('*')
            ->from('sk_subj_img_links')
            ->where('id=:tid', array(':tid'=>’1'))
            ->queryAll();
  • then works write Columns of table sk_subj_img_links: id, link, subj-clas. When find link column – also works.