2

Sample words:

iknkr st nm krt prk
iknkr vhgr j k pth  
iknkr hkm st
iknkr sr vhgr j k pth

Required Condition

Now, if a user types: ik s the query will return:

iknkr st nm krt prk 
iknkr sr vhgr j k pth

if a user types: i v the query will return:

iknkr vhgr j k pth

Currently Trying:

SELECT `walpha` AS word, pageID AS id 
FROM `SX01` 
WHERE `table`= 'S01' 
  AND `walpha` like '" . $this->db->escape_like_str($searchVal['keyword']) . "%' 
LIMIT 100"

But using this user have to type: iknkr s

to get the result

iknkr st nm krt prk 
iknkr sr vhgr j k pth

Can anyone suggest, what will be the right way of doing it.

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
Suraj Gupta
  • 148
  • 2
  • 11

3 Answers3

3

You could build a regular expression from the input and then use REGEXP to have MySQL use it. For example, you could turn ik s into ^ik[a-z]* s to match words which which start with ik followed by a word starting with s...

SELECT words FROM tbl WHERE t.words REGEXP '^ik[a-z]* s';
  • ^ means we must match from the start of the string
  • ik must then be the first two chars
  • [a-z] will match any lowercase alphabetic char
  • * allows us to match zero or more of the previous pattern - i.e. any sequence of chars can follow ik
  • then must have a space
  • s followed by an s - we don't care about what follows.

You can build this pattern from the user input by replacing all spaces with [a-z]* and prefixing with ^

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • regular expression are very hard for me. Why you dont have to include `s*` to indicate anything else? – Juan Carlos Oropeza Feb 23 '17 at 14:14
  • @JuanCarlosOropeza : the `*` signs indicates anything after ik and `[a-z]` means it can be any alphabetical chars . query is working fine – prakash tank Feb 23 '17 at 14:16
  • `s*` would mean 'zero or more s chars' which isn't what you want. You just want to be sure the next work starts with s, and not care about the rest. – Paul Dixon Feb 23 '17 at 14:16
  • @prakashtank I understand the first `*` my question is why `s` doesnt need `*`. – Juan Carlos Oropeza Feb 23 '17 at 14:18
  • OK, I got it now. Is different from DOS command like `dir myfile.*` – Juan Carlos Oropeza Feb 23 '17 at 14:18
  • The problem with this answer is how you use user input to update your query? – Juan Carlos Oropeza Feb 23 '17 at 14:21
  • break the input on spaces and build the regex yourself. Exercise for the reader :) You could build the regex with MySQL string replacement and concatenation operations too. – Paul Dixon Feb 23 '17 at 14:22
  • 1
    I am able to resolve the issue using your suggestion - `"SELECT \`walpha\` AS word, \`pageID\` AS id FROM \`SX01\` WHERE \`table\`= 'S01' AND \`walpha\` regexp concat('^', replace('" . $this->db->escape_like_str($searchVal['keyword']) . "', ' ', '[a-z]* ')) LIMIT 100"` +1 @PaulDixon – Suraj Gupta Feb 23 '17 at 14:53
1

If I understand your logic correctly, you could try with a query like this:

select `walpha` AS word, pageID AS id
from `SX01` 
where
  `table`= 'S01' 
  and `walpha` like concat(replace('" . $yoursearchval . "', ' ', '% '), '%') 
limit 100

for example:

concat(replace('i k', ' ', '% '), '%')

will become:

i% k%

but if you want the words to be consecutive, you'll have to use a regular expression:

where whalpa regexp concat('^', replace('i k', ' ', '[^[:space:]]+ '))

and i k will become:

^i[^[:space:]]+ k
  • the string has to start with i
  • after i there has to be at least + one non space character
  • then one space
  • then one k
  • then anything else
fthiella
  • 48,073
  • 15
  • 90
  • 106
0

select id,title,upper(left(title,1)) AS FirstLetter from store order by title

Ali Umair
  • 690
  • 7
  • 10