$sql ="SELECT topic_id FROM table
WHERE
(CASE WHEN {$sw_lat} < {$ne_lat} THEN topic_g_lat BETWEEN {$sw_lat} AND {$ne_lat}
ELSE topic_g_lat BETWEEN {$sw_lat} AND 180 OR topic_g_lat BETWEEN -180 AND {$ne_lat}
END)
AND
(CASE WHEN {$sw_lon} < {$ne_lon}
THEN topic_g_lng BETWEEN {$sw_lon} AND {$ne_lon}
ELSE topic_g_lng BETWEEN {$sw_lon} AND 180 OR topic_g_lng BETWEEN -180 AND {$ne_lon}
END)";