SELECT u.ID as uid, u.display_name, cast(m4.meta_value as datetime) as last_activity, 99999 as distance,
CASE
WHEN u.display_name LIKE '% %' THEN right(u.display_name, length(u.display_name)-locate(' ', u.display_name))
ELSE u.display_name
END AS surname
FROM wp_users u
LEFT JOIN wp_usermeta m4 ON u.ID = m4.user_id
WHERE
m4.meta_key = 'symposium_last_activity' AND
(u.display_name IS NOT NULL) AND
(
(lower(u.display_name) LIKE 'm%')
)
ORDER BY cast(m4.meta_value as datetime) DESC
LIMIT 0,1000