'mysql order by best match

Sorry for posting so many questions.

I was looking for the best match through google and stackoverflow.

I found a good way, but it's hard to apply, so I'm writing a question.

USER and CAT have a many-to-many relationship.

SELECT ...
FROM CAT
INNER JOIN CAT_USER ON CAT.ID = CAT_USER.CATID
INNER JOIN USER ON USER.ID = CAT_USER.USERID
WHERE (CAT.NAME1 LIKE '%{searchKeyword}%' 
       or CAT.NAME2 LIKE '%{searchKeyword}%' 
       or CAT.NAME3 LIKE '%{searchKeyword}%'
       or USER.NAME LIKE '%{searchKeyword}%')
GROUP BY CAT.ID

I'm trying to connect the query below to the query above.

ORDER BY CASE WHEN CAT.NAME1 = '{searchKeyword}' 
                   OR CAT.NAME2 = '{searchKeyword}' 
                   OR CAT.NAME3 = '{searchKeyword}' 
                   OR USER.NAME = '{searchKeyword}' 
              THEN 4
              WHEN CAT.NAME1 LIKE '{searchKeyword}%' 
                   OR CAT.NAME2 LIKE '{searchKeyword}%' 
                   OR CAT.NAME3 LIKE '{searchKeyword}%' 
                   OR USER.NAME LIKE '{searchKeyword}%' 
              THEN 3
              WHEN CAT.NAME1 LIKE '%{searchKeyword}' 
                   OR CAT.NAME2 LIKE '%{searchKeyword}' 
                   OR CAT.NAME3 LIKE '%{searchKeyword}' 
                   OR USER.NAME LIKE '%{searchKeyword}' 
              THEN 2
              WHEN CAT.NAME1 LIKE '%{searchKeyword}%' 
                   OR CAT.NAME2 LIKE '%{searchKeyword}%' 
                   OR CAT.NAME3 LIKE '%{searchKeyword}%' 
                   OR USER.NAME LIKE '%{searchKeyword}%' 
              THEN 1
              ELSE 0 END
DESC

However, I am getting an error on user.name because of group by. I know why the error occurs, but I don't know how to fix it.

If there is a better way, you can recommend it and help me solve this problem.

Thank you!

MATCH AGAINST decided to exclude it because it should be possible to search for one character. A lot of people have helped me in the past and I'm sorry.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source