Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/3dddf074bd26a916215057853c930848 to your computer and use it in GitHub Desktop.
Save code-boxx/3dddf074bd26a916215057853c930848 to your computer and use it in GitHub Desktop.
MYSQL Search Query Examples - Exact VS Like VS Fuzzy

MYSQL SEARCH QUERY EXAMPLES - EXACT, LIKE, FUZZY

https://code-boxx.com/mysql-search-exact-like-fuzzy/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) CAPTAIN OBVIOUS EXACT SEARCH
SELECT * FROM `articles` WHERE `article_id`=1;
-- (B) EXACT MATCH, CASE INSENSITIVE
SELECT * FROM `articles` WHERE `article_title`="the smoke flame";
-- (C) EXACT MATCH, CASE SENSITIVE
-- CREDIT: https://stackoverflow.com/questions/5629111/how-can-i-make-sql-case-sensitive-string-comparison-on-mysql
SELECT * FROM `articles` WHERE `article_title`= BINARY "The Smoke Flame";
-- (A) CONTAIN "SECRET" ANYWHERE
SELECT * FROM `articles` WHERE `article_title` LIKE "%secret%";
-- (B) MUST START WITH "SECRET"
SELECT * FROM `articles` WHERE `article_title` LIKE "secret%";
-- (C) MUST END WITH wITH "SECRET HAND"
SELECT * FROM `articles` WHERE `article_title` LIKE "%secret hand";
-- (D) CASE SENSITIVE
SELECT * FROM `articles` WHERE `article_title` LIKE BINARY "%Secret%";
-- (A) FUZZY SEARCH
SELECT * FROM `articles` WHERE MATCH(`article_text`)
AGAINST("difficulty ham" IN NATURAL LANGUAGE MODE);
-- (B) MATCH SCORE
SELECT `article_title`,
MATCH(`article_text`)
AGAINST("difficulty ham" IN NATURAL LANGUAGE MODE)
AS `score`
FROM `articles`
WHERE
MATCH(`article_text`)
AGAINST("difficulty ham" IN NATURAL LANGUAGE MODE);
-- (A) EITHER TITLE *OR* TEXT MUST CONTAIN "SECRET"
SELECT * FROM `articles` WHERE
`article_title` LIKE "%secret%"
OR `article_text` LIKE "%secret%"
-- (B) BOTH TITLE *AND* TEXT MUST CONTAIN "SECRET"
SELECT * FROM `articles` WHERE
`article_title` LIKE "%secret%"
AND `article_text` LIKE "%secret%"
-- (A) ARTICLES TABLE
CREATE TABLE `articles` (
`article_id` bigint(20) NOT NULL,
`article_title` varchar(255) NOT NULL,
`article_text` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `articles`
ADD PRIMARY KEY (`article_id`),
ADD KEY `article_title` (`article_title`),
ADD FULLTEXT KEY `article_text` (`article_text`);
ALTER TABLE `articles`
MODIFY `article_id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (B) DUMMY ENTRIES
INSERT INTO `articles` (`article_id`, `article_title`, `article_text`) VALUES
(1, 'Secret of the Voiceless Ringmaster', 'Unpacked reserved sir offering bed friends judgment may and quitting speaking. Is do be improved raptures offering required in replying raillery.'),
(2, 'The Smoke Flame', 'Stairs ladies friend by in mutual a no. Mr hence chief he causes. Whole no doors on hoped. Mile tells if help they ye full name. '),
(3, 'Sign of the Secret Hand', 'May indulgence difficulty ham can put especially. Bringing remember for supplied her why was confined.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment