简体   繁体   中英

Zend DB Concatenate two columns

Hello I have a small function which I use for searching for a users details (its an ajax search) . At the moment the function I use is this:

public function findAllUsersLike($like)
{
    $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)->setIntegrityCheck(false);
    $select->where('users_table.username LIKE ?', '%'.strip_tags($like).'%')
           ->orWhere('users_data.first_name LIKE ?', '%'.strip_tags($like).'%')
           ->orWhere('users_data.last_name LIKE ?', '%'.strip_tags($like).'%')
           ->join('users_data', 'users_table.id = user_id', array('first_name', 'last_name'));
    return $this->fetchAll($select);
}

As you can see, if I were to write a users first_name OR last_name OR username I can find their details.

What I want to be able to do is fine tune the search a little more, so if I were to write first_name last_name it would fine tune the results.

I have found a couple of questions which look like they would work (see here) , if I were getting the data from one table, but I'm not sure how to incorporate this with using a join but sticking with the Zend syntax rather than using a plain old SQL statement?

OK so the answer was a lot simpler than I had thought, I've been messing all morning trying to find possibly an over complex solution, asked the question and gone away and it dawned on my to try:

->orWhere('CONCAT(users_data.first_name, " ", users_data.last_name) LIKE ?', '%'.strip_tags($like).'%')

This works for me, I'm not sure its the correct method, but it works so I will leave it here in case it helps anyone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM