I am trying to order my search results based on the number of columns that are matched.
The variables :Name
, :Email
, and :Fullnumbers
get % before and after in PHP.
The query works fine when I leave out the ORDER BY
clause. It also works when I remove Fullnumbers from the ORDER BY
clause.
The rows where both Name and Email are partially matched are returned in the right order.
However, when Fullnumbers is included no rows are returned.
Why is this and how can I solve the problem?
SELECT SQL_CALC_FOUND_ROWS DISTINCT(User.UserId), Name, Email,
GROUP_CONCAT(CONCAT(Countrycode, Number)) AS Fullnumbers
FROM User LEFT JOIN UserPhonenumber ON User.UserId = UserPhonenumber.UserId
GROUP BY UserId HAVING Name LIKE :Name OR Email LIKE :Email OR Fullnumbers LIKE :Fullnumbers
ORDER BY ((Name LIKE :Name) + (Email LIKE :Email) + (Fullnumbers LIKE :Fullnumbers)) DESC