SELECT IF(@ids=w.contract_id,@num:=@num,@num:=@num+1) AS num,@ids:=w.contract_id,w.* FROM
(SELECT (SELECT GROUP_CONCAT(project_name)
FROM production_income_data WHERE contract_id = a.contract_id) AS `names`,
(SELECT GROUP_CONCAT(project_code) FROM production_income_data
WHERE contract_id = a.contract_id) AS `codes`, a.*FROM production_income_data a )w
,(SELECT @num:=0) u,(SELECT @ids:='') m ORDER BY w.contract_id