gpt4 book ai didi

php - Yii1 : How to use concat function in mysq like query

转载 作者:行者123 更新时间:2023-11-29 02:23:46 24 4
gpt4 key购买 nike

我想在 like 表达式 中使用 Mysql Concat 函数
我想将firstnamelastname合并为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.

提前致谢

最佳答案

如果以后不需要全名,可以在WHERE子句中使用CONCAT方法:

$criteria = new CDbCriteria();
$criteria->addCondition('CONCAT(userId , " " , username) LIKE :match');
$criteria->params = array(':match' => $query);
$models = User::model()->findAll($criteria);

但是,如果您确实想在 SELECT 子句中保留全名,则只能在 HAVING 子句中使用此别名:

$criteria = new CDbCriteria();
$criteria->select = '*, CONCAT(userId , " " , username) AS fullname';
$criteria->having = 'fullname LIKE :match';
$criteria->params = array(':match' => $query);
$models = User::model()->findAll($criteria);

请注意,在这种情况下,您的用户模型应该具有全名属性,否则您将无法访问全名字段。

关于php - Yii1 : How to use concat function in mysq like query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26866940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com