MYSQL称重结果

时间:2012-04-02 19:24:24

标签: mysql frequency

在MYSQL中权衡结果的典型方法是什么?

我有一个名为'category_id'的列表,从1-10开始。

如何权衡结果,以便使用一个category_id显示更高的结果频率?

2 个答案:

答案 0 :(得分:2)

如果你想支持类别5,你可以做以下几行:

select id, description
from your table
where yourcondition='conditionvalue'
order by 
    case category
       when 5 then 0
       else 1
    end, 
    category

答案 1 :(得分:1)

select category_id, count(*) 
from table 
group by category_id order by count(*) desc

假设您使用的是名为table

的表

要获得您所说的权重,您可以使用两个查询:

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

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

也许这就是你要找的东西:

(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)

这将给你100行,按类别ID 1,2,3分开50/25/25。