【发布时间】:2014-11-11 14:16:46
【问题描述】:
我想在like expression 中使用Mysql Concat Function。
我想将firstname 和lastname 合并为fullname 并根据fullname 得到匹配的结果。
我在 YII1 中试过这个。以下是我的代码:
$criteria = new CDbCriteria();
$criteria->select = "*";
$criteria->select = 'CONCAT(firstname , "" , lastname) AS fullname';
$criteria->addCondition('fullname LIKE :match');
$criteria->params = array(':match' => $query);
$models = User::model()->findAll($criteria);
下面是生成的错误信息:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fullname' in 'where clause'
(Error 500)
CDbCommand::fetchColumn() failed: SQLSTATE[42S22]: Column not found: 1054
Unknown column 'fullname' in 'where clause'. The SQL statement executed
was: SELECT COUNT(*) FROM `members` `t` WHERE fullname LIKE :match.
提前致谢
【问题讨论】:
-
我不是 Yii 专家,但您需要在条件本身中添加
CONCAT(firstname , "" , lastname),因为别名不能用于 where。 -
$criteria = new CDbCriteria(); $标准->选择='*'; $criteria->addCondition('CONCAT(firstname, lastname) AS fullname LIKE :match');但问题仍然存在
-
没有
$criteria->addCondition('CONCAT(firstname , "" , lastname) LIKE :match');
标签: php mysql activerecord yii yii1.x