Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHPWind-- A PHP forum script applcaition in China
- How to blend adsense inside your post?
- Formatting my post
- Notepad++ - Add C++ compiler
- Install PHPMailer 5.2.4 and use smtp gmail
- Set up a child account and set screen time limit in Windows 8
- Wayback Machine - see archived versions of web pages across time
- phpexcel toggle expand and hide column in EXCEL and summary
- Install PHP ibm_db2 extension in Linux (redHat)
- PHP: add a download as pdf file button in report page
Thursday, January 9, 2014
Search a word or a string in MySQL
We often perform searching a word or a string in MySQL. For example, we want to search Bowen from column authorlist, secondaryauthorlist and seriesauthorlist in table publications, we can use
SELECT * FROM publications WHERE (CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) LIKE '%Bowen%';
or
SELECT * FROM publications WHERE CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) REGEXP '[[:<:]]Bowen[[:>:]]';
There is a small difference between these two queries. The first one matches Bowen, CBowen, CBowenm ..., the second one matches only Bowen, more explanation:
MySQL function CONCAT_WS joins strings with separator.
CONCAT_WS(separator, string1, string2)
LIKE '%Bowen%': find string containing Bowen, which can be in the middle of a string
REGEXP: regular expression
[[:<:]] match beginning of the word
[[:>:]] match end of the word
[[:<:]]Bowen [[:>:]] match a word boundary at the beginning and end of a word,
i.e. BBowenn will not match, while LIKE '%Bowen%' match
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment