I want to add the additional column at the end from mysql query which show the distinct records(parent_id) with increment values(column "x" in image) as attached sample in image. Sample mysql query i tried :
select *, count(parent_id) as x
from businessDemands
where zip='123'
group by sub_Category_name
order by category_name desc
I want same result as in image as of now its comming same with incorrect values in "x" column
You can do this in MySQL using variables:
select t.*,
(@rn := if(@p = parent_id, @rn,
if(@p := parent_id, @rn + 1, @rn + 1)
)
) as x
from t cross join
(select @p := -1, @rn := 0) params
order by parent_id;
EDIT:
Variables and GROUP BY
don't work well together. Use a subquery:
select t.*,
(@rn := if(@p = parent_id, @rn,
if(@p := parent_id, @rn + 1, @rn + 1)
)
) as x
from (select . . .
from businessDemands bd
where zip = '123'
order by parent_id
) t cross join
(select @p := -1, @rn := 0) params
order by parent_id;
Fix the subquery so it is either a valid GROUP BY
or remove the aggregation entirely.