Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mysql : Select query with like

I have this kind of data in my database "mydb" :

id | name
---------
1  | abcdef
2  | bcdefg
3  | cdefgh
4  | defghi
5  | efghij

I do a simple SELECT query like this one :

SELECT * FROM mydb WHERE name LIKE '%defgh%'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So i get those results :

id | name
---------
3  | cdefgh
4  | defghi

I would like to know if there is a way, with mysql, to inject html code which encompasses the string searched with "LIKE" statement directly in the result ?

So the results would be :

id | name   | html
----------------
3  | cdefgh | c<mark>defgh</mark>
4  | defghi | <mark>defgh</mark>i

>Solution :

Sure, something like this should do :

SELECT id, name, replace(name, 'defgh', '<mark>defgh</mark>') as html
  FROM mydb 
  WHERE name LIKE '%defgh%'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading