Wednesday, January 8, 2014

Complicate MySQl query





Complicate MySQl query
SELECT * FROM ubc_surgery_pub.publications WHERE (1 AND (CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) LIKE '%Bowen%' AND CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) REGEXP '[[:<:]]Bowen[[:>:]]' AND CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) LIKE '%V%' AND CONCAT_WS(' ', authorlist, secondaryauthorlist, seriesauthorlist) REGEXP '[[:<:]]V[[:>:]]' AND 1) AND (year!=9999) AND (deptlist <> ',' OR year=9999 OR (deptlist=',' AND pendingdepts=',')) AND (deptlist<>',' OR pendingdepts<>',') ) ORDER BY year DESC, authorlist ASC, secondaryauthorlist ASC, title ASC LIMIT 2000;

Explanation:
MySQL CONCAT_WS() function is used to join two or more strings with separator. - See more at: http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php#sthash.nfsRPqcX.dpuf
 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
<>: not equal  operator
[[:<:]] match beginning of words
[[:>:]] match ending of words - See more at: http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/#sthash.Jjvj1UWR.dpuf
MySQL CONCAT_WS() function is used to join two or more strings with separator. - See more at: http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php#sthash.nfsRPqcX.dpuf

No comments:

Post a Comment