SELECT tu.id,
tu.firstname,
tu.lastname,
role,
CONCAT('s3bucked.com/',profile_image) as profile_image,
CONCAT('s3bucked.com/',cover_image) as cover_image,
CONCAT('s3bucked.com/',profile_image, ?) as profile_image_imagekit,
CONCAT('s3bucked.com/}',cover_image, ?) as cover_image_imagekit,
((6372 * 2 * ASIN(
SQRT(POWER(SIN((? - tu.lat) * pi() / 180 / 2), 2) +
COS(? * pi() / 180) * COS(tu.lat * pi() / 180) *
POWER(SIN((? - tu.lng) * pi() / 180 / 2), 2))
)) * 1000) AS distance,
IF(
tu.id IN (225, 226, 227, 228),
TRUE,
FALSE
) as is_friends
FROM tbl_user tu
LEFT JOIN tbl_users_groups_relations tug ON tu.id = tug.user_id
LEFT JOIN tbl_groups tg ON tug.group_id = tg.id
WHERE tg.id = ?
AND IF(
tg.type != 'Gym',
tu.id IN (225, 226, 227, 228, 229, 230, 231),
tg.type = 'Gym'
)
AND tu.is_delete = '0'
AND tu.deleted_at IS NULL
HAVING (distance between 0 AND 200000)
ORDER BY
FIELD(role, 'A', 'M', 'U'),
profile_image = 'default_image',
tu.firstname,
tu.lastname
LIMIT ?
OFFSET ?