I have this query:
SELECT `id` FROM `accounts`
WHERE AES_DECRYPT(`email`, :salt) = CONCAT_WS('@',:mailbox,:host)
OR AES_DECRYPT(`email`, :salt) LIKE CONCAT('%',:host)
I have 2 records in that table:
id email
1 test@test.com
2 other@test.com
When I'm running this query like this:
SELECT `id` FROM `accounts`
WHERE AES_DECRYPT(`email`, '123') = CONCAT_WS('@','test','test.com')
OR AES_DECRYPT(`email`, '123') LIKE CONCAT('%','test.com')
I get this as a result:
id email
2 other@test.com
1 test@test.com
Question: What I want is this: I want to have the best match as the first result, without using fulltext search. Is this possible, if so, how can I do this?
This code should suit you:
You can readily order the results by the number of matches:
This will work for your example.
To get records in a specific order, use an ORDER BY clause.
Here we are using a MySQL special. A boolean expression that evaluates to TRUE results in 1. A boolean expression that evaluates to FALSE results in 0. In another DBMS you could write this instead: