0

What is the typical way to weigh results in MYSQL?

I have a table with a column called 'category_id' which goes from 1-10.

How do I weigh results so a higher frequency of results with one category_id will show up vs. another?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user1124535
  • 765
  • 3
  • 9
  • 15

2 Answers2

2

if you want to favor, say, category 5, you can do something in the lines of:

select id, description
from your table
where yourcondition='conditionvalue'
order by 
    case category
       when 5 then 0
       else 1
    end, 
    category
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1
select category_id, count(*) 
from table 
group by category_id order by count(*) desc

assuming you are using a table named table

To get the weights like you were saying, you can use two queries:

set @a := (select count(*) from table);

select category_id, count(*)/@a from table group by category_id order by count(*) desc;

maybe this is what you are looking for:

(select * from table where category_id = 1 order by rand() limit 50)
union all
(select * from table where category_id = 2 order by rand() limit 25)
union all
(select * from table where category_id = 3 order by rand() limit 25)

this will give you 100 rows, split up 50/25/25 by category ids 1,2,3.

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160