10

Let say we have 4 items in a table:

  1. Michel Jordan
  2. Tom Mark Jordan
  3. Jordan John
  4. Adam Jordan Robert

The search term is "Jordan", how can i get the results sorted alphabetically but with search term match first like this:

  1. Jordan John
  2. Michel Jordan
  3. Tom Jordan Robert
  4. Adam Mark Jordan

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();
3
  • I think you'd have to re-sort in PHP after receiving the results. If you had the name broken up into different fields, you could get it to work.
    – swatkins
    Jan 9, 2013 at 16:27
  • I have the full name in one column. Jan 9, 2013 at 17:49
  • Right, I understand that. Therefore, you have to get the results, then re-sort those results.
    – swatkins
    Jan 9, 2013 at 17:53

3 Answers 3

10

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 |
4
  • Looks great, but how could i use it as CodeIgniter query? And using variable $search_term instead of the word 'Jordan'?? Jan 10, 2013 at 11:10
  • @WaleedAsender I haven't worked on CodeIgniter so sorry for that so you have do it by your own. Jan 10, 2013 at 11:14
  • Unfortunately for some reason its not working for me !! $query = $this->db->query("SELECT name FROM members ORDER BY FIND_IN_SET('$keyword', REPLACE(name, ' ', ',')), name")->result(); Feb 5, 2013 at 14:45
  • you are searching for '$keyword' you need to make sure the veriable is being inserted to the query via this: FIND_IN_SET('" . $keyword . "'... that should work (true with php, im not sure with other languages)
    – Steve P
    Feb 7, 2013 at 12:06
6
+25

You can try this:

$this->db
     ->select('id, fullName')
     ->from('test')
     ->order_by("FIND_IN_SET('Jordan', REPLACE(fullName, ' ', ',')) , fullName");
$query = $this->db->get();
2
  • It keepf throughing this error: A Database Error Occurred Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', ', ')), 'fullName'' at line 3 Feb 7, 2013 at 12:06
  • Please make sure that you haven't used double quotes between orderby clause. Also the fullname is used without quotes
    – Code Prank
    Feb 7, 2013 at 12:29
0

My attempt for you:

$this->db->select('id, name, (SELECT name FROM users WHERE name REGEXP "^'.$search_term.'") as "regexp_match"');
$this->db->from('users');
$this->db->like('name', $search_term);
$this->db->order_by('name', 'asc');
$this->db->order_by("regexp_match", "asc");
$query = $this->db->get();

I know it looks kinda weird, but trust me, you want to use REGEXP. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.