John Harman wrote:
I'm trying to do a MySQL Query using Mysql 3.23.58 something like that below
SELECT name FROM customers WHERE name LIKE "Fred" ORDER BY
difference(name,"Fred");
The difference piece doesn't seem to be working (Syntax Error returned). Is
this not possible without upgrading the server? or am I doing something
wrong, all I want to do is be able to return the results closest to that
entered first.
How about this?
SELECT C.name
FROM customers C
WHERE C.name LIKE 'Fred'
ORDER BY
ABS(ORD(UPPER(SUBSTRING(C.name,1,1)))
- ORD(UPPER(SUBSTRING('Fred',1,1)))),
ABS(ORD(UPPER(SUBSTRING(C.name,2,1)))
- ORD(UPPER(SUBSTRING('Fred',2,1)))),
ABS(ORD(UPPER(SUBSTRING(C.name,3,1)))
- ORD(UPPER(SUBSTRING('Fred',3,1)))),
ABS(ORD(UPPER(SUBSTRING(C.name,4,1)))
- ORD(UPPER(SUBSTRING('Fred',4,1))));
This is a hack -- it isn't scalable to an arbitrary depth of similarity
between strings, but after 4 or 5 characters, it might be adequate to
sort your dataset accurately.
Regards,
Bill K.