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?
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?
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
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.