CodeIgniter Forums
Sql Query Like by Multiple columns - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Sql Query Like by Multiple columns (/showthread.php?tid=84811)



Sql Query Like by Multiple columns - UchihaSV - 11-11-2022

Hello, i use SQL QB and it's fine, but i need use more flexible - need find data by one string but in multiple concat columns (first_name, last_name, patronymic).
For example i need find string "Mark Suckerberg" in first_name + last_name columns (without OR, AND) - need generate sql part as "where (first_name + last_name) like '%string%'", but SQL QB like, orLike generated extra escaping and it's not work correctly:
PHP Code:
...
if (isset(
$_GET['search'])) {
  $this->userModel->like('phone'$_GET['search']);
  $this->userModel->orLike('email'$_GET['search']);
  $this->userModel->orLike('(last_name + first_name + patronymic)'$_GET['search']);
}
$this->data->users $this->userModel->orderBy('id','desc')->withDeleted()->paginate(25); 

Any suggestion?


RE: Sql Query Like by Multiple columns - ikesela - 11-12-2022

Code:
$this->userModel->like('CONCAT(last_name,first_name)',$_GET['search']);



RE: Sql Query Like by Multiple columns - UchihaSV - 11-12-2022

(11-12-2022, 12:30 AM)ikesela Wrote:
Code:
$this->userModel->like('CONCAT(last_name,first_name)',$_GET['search']);

Tnx, and if i need space between columns i can use - CONCAT(last_name," ",first_name) or need use something else?


RE: Sql Query Like by Multiple columns - ikesela - 11-12-2022

(11-12-2022, 06:04 AM)UchihaSV Wrote:
(11-12-2022, 12:30 AM)ikesela Wrote:
Code:
$this->userModel->like('CONCAT(last_name,first_name)',$_GET['search']);

Tnx, and if i need space between columns i can use - CONCAT(last_name," ",first_name) or need use something else?

Yes -> can refer this: https://www.mysqltutorial.org/sql-concat-in-mysql.aspx


RE: Sql Query Like by Multiple columns - UchihaSV - 11-12-2022

(11-12-2022, 08:05 AM)ikesela Wrote: Yes -> can refer this: https://www.mysqltutorial.org/sql-concat-in-mysql.aspx
Ok, then i can use CONCAT_WS like implode.