I want to use Mysql Concat Function
in like expression
.
I want to merge firstname
and lastname
as fullname
and get matched results according to the fullname
.
I have tried this in YII1. Below is my code:
$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);
Below is the generated error message:
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.
Thanks in advance
If you don't need the fullname afterwards, you can just use the CONCAT method in the WHERE clause:
However, if you do want to keep the fullname in the SELECT clause, you can only use this alias in the HAVING clause:
Please note that your User model should have a fullname attribute in this case, otherwise you won't be able to access the fullname field.