Mysql reverse like

Posted: July 11, 2012 in MySql
SELECT
*
FROM town_hint
WHERE
townName like '%M%' OR
townName like '%Mo%' OR
townName like '%Mor%' OR
townName like '%Morr%' OR
townName like '%Morri%' OR
townName like '%Morris%' OR
townName like '%Morrist%' OR
townName like '%Morristo%' OR
townName like '%Morristow%' OR
townName like '%Morristown%'
ORDER BY
CASE WHEN
townName like '%Morristown%' THEN 9
WHEN townName like '%Morristow%' THEN 8
WHEN townName like '%Morristo%' THEN 7
WHEN townName like '%Morrist%' THEN 6
WHEN townName like '%Morris%' THEN 5
WHEN townName like '%Morri%' THEN 4
WHEN townName like '%Morr%' THEN 3
WHEN townName like '%Mor%' THEN 2
WHEN townName like '%Mo%' THEN 1
WHEN townName like '%M%' THEN 0 ELSE 0 END desc
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s