Skip to main content
added 1002 characters in body
Source Link
Hitesh Mundra
  • 1.6k
  • 1
  • 10
  • 13

There may be syntax error i haven't tested on sql prompt, my motto is to represent the approch. sorryYou can also use regualar expressions for that. For example input string the day boss get

Hitesh> select * from test;
+--------------------+
| name               |
+--------------------+
| i am the boss      |
| You will get soon  |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday    |
+--------------------+
5 rows in set (0.00 sec)

Hitesh> set @var=CONCAT('.*',REPLACE('the day boss get',' ','.*|.*'),'.*');
Query OK, 0 rows affected (0.00 sec)

Hitesh> select @var;
+----------------------------------+
| @var                             |
+----------------------------------+
| .*the.*|.*day.*|.*boss.*|.*get.* |
+----------------------------------+
1 row in set (0.00 sec)

Hitesh> select * from test where name  REGEXP @var;
+--------------------+
| name               |
+--------------------+
| i am the boss      |
| You will get soon  |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday    |
+--------------------+
5 rows in set (0.00 sec)

There may be syntax error i haven't tested on sql prompt, my motto is to represent the approch. sorry for that

You can also use regualar expressions for that. For example input string the day boss get

Hitesh> select * from test;
+--------------------+
| name               |
+--------------------+
| i am the boss      |
| You will get soon  |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday    |
+--------------------+
5 rows in set (0.00 sec)

Hitesh> set @var=CONCAT('.*',REPLACE('the day boss get',' ','.*|.*'),'.*');
Query OK, 0 rows affected (0.00 sec)

Hitesh> select @var;
+----------------------------------+
| @var                             |
+----------------------------------+
| .*the.*|.*day.*|.*boss.*|.*get.* |
+----------------------------------+
1 row in set (0.00 sec)

Hitesh> select * from test where name  REGEXP @var;
+--------------------+
| name               |
+--------------------+
| i am the boss      |
| You will get soon  |
| Happy birthday bro |
| the beautiful girl |
| oyee its sunday    |
+--------------------+
5 rows in set (0.00 sec)
Source Link
Hitesh Mundra
  • 1.6k
  • 1
  • 10
  • 13

There are few ways to do that- The very basic strategy is to match input with our table columns by the query like as you mentioned -

1. SELECT * FROM table WHERE (name='%name%' or zip='%name%' or city='%name%');

2. SELECT * FROM table WHERE LOCATE(name, GROUP_CONCAT(name,city,zip)) > 0;

3. 
SELECT * FROM table WHERE name like '%name%' 
          UNION 
SELECT * FROM table WHERE name like '%name%' 
          UNION
SELECT * FROM table WHERE name like '%name%';

but suppose the case where input box have the string- "varun bharti" but actual name in database is "varun bal bharti" So when you search you will missed the record. for that case you should break the string by space in to array elements and use these queries for elements or either you can replace the space in name column and match.

set @var=REPLACE ('varun bharti', ' ', '%');    
SELECT * FROM table WHERE name like concat('%',@var,'%') or 
         zip like concat('%',@var,'%') or 
         city like concat('%',@var,'%');

There may be syntax error i haven't tested on sql prompt, my motto is to represent the approch. sorry for that