IF(e.email IS NOT NULL, e.email, IF(FIND_IN_SET('googleapps', u.mail_storage), 'googleapps', NULL)) AS email, e.last
FROM auth_user_md5 AS u
INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
INNER JOIN aliases AS b ON (b.id = u.user_id AND FIND_IN_SET('bestalias', b.flags))
LEFT JOIN emails AS e ON (e.flags = 'active' AND e.uid = u.user_id)
IF(e.email IS NOT NULL, e.email, IF(FIND_IN_SET('googleapps', u.mail_storage), 'googleapps', NULL)) AS email, e.last
FROM auth_user_md5 AS u
INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
INNER JOIN aliases AS b ON (b.id = u.user_id AND FIND_IN_SET('bestalias', b.flags))
LEFT JOIN emails AS e ON (e.flags = 'active' AND e.uid = u.user_id)