php - How to sort returned sql result by id that appears the most time in that result -
i have table offer_cpv
connecting 2 others "offer
" , "cpv
". can see fiddle here: http://sqlfiddle.com/#!9/02020/1
offer_cpv
has 2 columns important us: offer_id
, cpv_id
.
my starting query looks this: select cpv_id offer_cpv offer_id in (1,2,3,4) limit 10
as can see need return cpv_id
based on array of offer_id
( pass where in (offer_id)
.
i result:
cpv_id 1010 1020 1030 2010 4030 4060 1010 2010 1010 1020
as can see cpv_id repeating because several offers may have same cpv_id linked them. need list of "most popular" cpv_id, mean if id repeating time need on top of list. example, desired output:
cpv_id 1010 1020 2010 1030 4030 4060
as can see 1010 repeating 3 times want on top of returned result. comes 1020 , 2010 since repeating 2 times, come others until limit of 10 reached.
is possible somehow mysql , php ?
thanks
you need count(*)
, group by
select cpv_id,count(*) tot offer_cpv offer_id in (1,2,3,4) group cpv_id order tot desc limit 10
Comments
Post a Comment