SELECT DISTINCT t.nid, f.* FROM (
SELECT
id,
parentId,
isMain,
@nid := CASE
WHEN @parentId <> parentId
THEN id
ELSE @nid
END AS nid,
@parentId := parentId AS t2
FROM hfeed
ORDER BY parentID, isMain DESC
) AS t
INNER JOIN hfeed AS f ON f.id = t.nid