Let say we have 4 items in a table:
The search term is "Jordan", how can i get the results sorted alphabetically but with search term match first like this:
Im using this code, but not getting what i want:
$this->db->select('id, name');
$this->db->from('users');
$this->db->like('name', $search_term);
$this->db->order_by('name', 'asc');
$query = $this->db->get();
Try this:
SELECT id, fullName
FROM test
ORDER BY FIND_IN_SET('Jordan', REPLACE(fullName, ' ', ',')), fullName;
Check this link SQL FIDDLE DEMO
OUTPUT
| ID | FULLNAME |
|----|-------------------|
| 1 | Jordan John |
| 2 | Michel Jordan |
| 4 | Tom Jordan Robert |
| 3 | Adam Mark Jordan |
You can try this:
$this->db
->select('id, fullName')
->from('test')
->order_by("FIND_IN_SET('Jordan', REPLACE(fullName, ' ', ',')) , fullName");
$query = $this->db->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With