如何在yii中编写此查询?

时间:2015-11-17 00:23:47

标签: php sql database yii

 SELECT business.id,business.business_name,address.city FROM business INNER JOIN address ON business.id=address.business_id WHERE business.business_name like '%monal%' and address.city='islamabad' 

monal和islamabad是来自表格的价值。 当我用变量替换islamabad时,它给了我错误。 我在yii中的查询。

$user = Yii::app()->db->createCommand()
    ->select('business.id,business_name,business.image,business.business_description,address.city')
    ->from('business')
    ->join('address', 'business.id=address.business_id')
    //->where(array('like', 'business.business_name', '%'.$name.'%'))   
    ->where(array('and', 'address.city=$city', array('like', 'business.business_name', '%'.$name.'%')))
    ->queryALL();



 CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Islamabad' in 'where clause'. The SQL statement executed was: SELECT `business`.`id`, `business_name`, `business`.`image`, `business`.`business_description`, `address`.`city`
FROM `business`
JOIN `address` ON business.id=address.business_id
WHERE (address.city=Islamabad) AND (`business`.`business_name` LIKE '%nan%') 

1 个答案:

答案 0 :(得分:3)

尝试将此作为您的查询

$results = Yii::app()->db->createCommand()
    ->select('b.id, b.business_name, a.city')
    ->from('business b')
    ->join('address a', 'b.id = a.business_id')
    ->where('b.business_name LIKE :businessName AND a.city = :city', array(
        ':businessName' => '%' . $businessNameVariable . '%',
        ':city' => $cityVariable,
    ))
    ->queryAll();