0

How to create smart search query depends on column-name?

I have a table like this:

+----+----------+-------+-------+
| id |   name   | code1 | code2 |
+----+----------+-------+-------+
| 1  |  jack    | 1     | 1     |
| 2  |  peter   | 1     | 2     |
| 3  |  abel    | 1     | 3     |
| 4  |  brent   | 2     | 1     |
| 5  |  arnold  | 2     | 2     |
| 6  |  chester | 3     | 1     |
| 7  |  morgan  | 3     | 2     |
| 8  |  olin    | 3     | 3     |
| 9  |  marvin  | 3     | 4     |
| 10 |  gavin   | 3     | 5     |
| 11 |  harley  | 4     | 1     |
+----+----------+-------+-------+

Well, I have 3 keywords: name, code1, code2

My question is about the order of results depends on those keywords. There is some examples:

Example1:

$q = 'name brent'; // it is what user writes for search in the input
                   // actually I meant of smart is detecting this automatically:
                   // keyword: 'name', value: 'brent'

I want this output:

+----+----------+-------+-------+
| 4  |  brent   | 2     | 1     |
|                               |
| /* the order of other colunms |
|    does not matter */         |
|                               |
+----+----------+-------+-------+

Example2:

$q = 'code1 3';

I want this output:

+----+----------+-------+-------+
| 6  |  chester | 3     | 1     |
| 7  |  morgan  | 3     | 2     |
| 8  |  olin    | 3     | 3     |
| 9  |  marvin  | 3     | 4     |
| 10 |  gavin   | 3     | 5     |
|                               |
| /* the order of other colunms |
|    does not matter */         |
|                               |
+----+----------+-------+-------+

Example3:

$q = 'code1 3 code2 2';

I want this output:

+----+----------+-------+-------+
| 7  |  morgan  | 3     | 2     |
|                               |
| /* the order of other colunms |
|    does not matter */         |
|                               |
+----+----------+-------+-------+

Example4: If there is all three keywords in input, code1 and code2 have higher priority.

$q = 'name jack code1 3 code2 1';

I want this output:

+----+----------+-------+-------+
| 6  |  chester | 3     | 1     |    // code1 3 code2 1
| 1  |  jack    | 1     | 1     |    // name jack
|                               |
| /* the order of other colunms |
|    does not matter */         |
|                               |
+----+----------+-------+-------+

Example5: If the value of keyword (name jack => keyword: name, value: jack) does not exist, it means: The keyword and its value will not be considered, Just like it does not exist in the input.

$q = 'code1 2 code2 5';

I want this output:

+----+----------+-------+-------+
| 4  |  brent   | 2     | 1     |
| 5  |  arnold  | 2     | 2     |
|                               |
| /* the order of other colunms |
|    does not matter */         |
|                               |
+----+----------+-------+-------+

For start, I think I have to separate the numbers of string, something like this:

preg_match_all('!\d+!u', $q, $matches);

And then use them in my query,something like this:

... IN (" .implode(',', $matches[0]) . ") ...

Or I don't know, is there any good solution? Actually what I need is a magic ordering ...!

7
  • I think you're confusing 'rows' and 'columns'
    – Strawberry
    Nov 18, 2015 at 17:16
  • Actually I want to know, how can I define some priority for columns? and then order them by those priority ....
    – stack
    Nov 18, 2015 at 17:19
  • 1
    ordery by x = 'foo' DESC - or see FIELD()
    – Strawberry
    Nov 18, 2015 at 17:21
  • 1
    Relevant: stackoverflow.com/questions/1250156/… Here is a Fiddle using something along the lines of that answer: sqlfiddle.com/#!9/9514c6/4
    – AdamMc331
    Nov 18, 2015 at 17:53
  • 1
    @McAdam331 Really thanks for that fiddle. Also the difference between example3 and 5 is: in the example5 code2 5 does not exist in table.
    – stack
    Nov 18, 2015 at 22:40

0

Your Answer

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

Browse other questions tagged or ask your own question.