MySQL Query根据频率选择前50行的子集

时间:2012-12-02 00:07:28

标签: mysql frequency subset

我有一个名为data的表:

create table data
(
   ID int primary key,
   val varchar(50),
   forID int

   constraint fk_forID foreign key (forID) references otherTable(forID)
)

我有一个名为dataFrequencies的视图

create view dataFrequencies (val, freq)
as select val, COUNT(*)
     from data
 group by val
 order by freq desc

我想要的是表格data行的子集,其中val位于dataFrequencies的前五十行。

我目前的解决方案有点迂回。我创建了一个table topFifty,其中包含前50行dataFrequencies。然后我创建了一个视图topFiftyVals,它从数据中选择所有内容,但是在桌面上选择了内部连接:

create table topFifty
(
   val varchar(50) primary key
)

insert into topFifty select val from dataFrequencies order by frequency desc limit 50;

create view topFiftyVals (ID, val, forID)
as select *
     from data d
inner join topFifty tf on d.val = tf.val

我确信有某种直接查询方法可以做到这一点!谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

你不能这样做:

SELECT *
  FROM data
 WHERE val IN (SELECT val 
                 FROM dataFrequencies 
               ORDER BY frequency DESC 
               LIMIT 50);

答案 1 :(得分:1)

是的,有直接的方法。这是topFiftyVals视图中的代码,略有改动:

     select d.*, tf.freq
       from data d
 inner join ( select val, COUNT(*) AS freq
                from data
            group by val
            order by freq desc
               limit 50
            ) tf 
         on d.val = tf.val ;