Skip to content

Instantly share code, notes, and snippets.

@danieldram
Created February 16, 2023 04:36
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 danieldram/44ab8bddbb48ad21725c49e4bab727ce to your computer and use it in GitHub Desktop.
Save danieldram/44ab8bddbb48ad21725c49e4bab727ce to your computer and use it in GitHub Desktop.
Fuzzy Search with SQL
// Serach for SANDRA across multiple tables and columns
SELECT * FROM (
SELECT *,
(
CASE
WHEN first_name LIKE '%SANDRA%' THEN 1
ELSE 0
END
) AS match_strength
FROM actor
UNION ALL
SELECT *,
(
CASE
WHEN first_name like '%SANDRA%' THEN 1
ELSE 0
END
)as match_strength
FROM actor_tv
)
AS x
WHERE match_strength > 0
ORDER BY match_strength DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment