Created
August 18, 2015 05:28
-
-
Save isogram/eb36e747899080001729 to your computer and use it in GitHub Desktop.
Query to get users by keyword order by match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
t1.user_id, | |
t1.user_uuid, | |
t1.first_name, | |
t1.last_name, | |
t1.user_name, | |
IF (t2.follow_status, 1, 0) AS is_following, | |
IF (t3.follow_status, 1, 0) AS is_followers | |
FROM | |
t_user t1 | |
-- Get following list | |
LEFT JOIN (SELECT user_uuid, user_uuid_follower, follow_status FROM t_friends WHERE user_uuid_follower = '166179196787279837486' AND follow_status = 1 GROUP BY user_uuid) t2 ON t2.user_uuid = t1.user_uuid | |
-- Get followers list | |
LEFT JOIN (SELECT user_uuid, user_uuid_follower, follow_status FROM t_friends WHERE user_uuid = '166179196787279837486' AND follow_status = 1 GROUP BY user_uuid_follower) t3 ON t3.user_uuid_follower = t1.user_uuid | |
WHERE | |
t1.first_name REGEXP "^rojok|rojok|rojok$" | |
OR t1.last_name REGEXP "^rojok|rojok|rojok$" | |
OR t1.user_name REGEXP "^rojok|rojok|rojok$" | |
GROUP BY | |
t1.user_uuid | |
ORDER BY | |
is_following DESC, | |
is_followers DESC, | |
CASE | |
WHEN t1.user_name = 'rojok' THEN 0 | |
WHEN t1.user_name LIKE 'rojok%' THEN 1 | |
WHEN t1.user_name LIKE '%rojok%' THEN 2 | |
WHEN t1.user_name LIKE '%rojok' THEN 3 | |
WHEN t1.first_name = 'rojok' THEN 0 | |
WHEN t1.first_name LIKE 'rojok%' THEN 1 | |
WHEN t1.first_name LIKE '%rojok%' THEN 2 | |
WHEN t1.first_name LIKE '%rojok' THEN 3 | |
WHEN t1.last_name = 'rojok' THEN 0 | |
WHEN t1.last_name LIKE 'rojok%' THEN 1 | |
WHEN t1.last_name LIKE '%rojok%' THEN 2 | |
WHEN t1.last_name LIKE '%rojok' THEN 3 | |
ELSE 4 | |
END, t1.user_name ASC | |
LIMIT 10 OFFSET 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment