Mysql选择每个类别解析函数的前10名

时间:2011-11-23 22:42:45

标签: mysql select multiple-select-query

我有一个表格,其中包含以下两列,其中包括:评级,价格和代码。评级类似于一个类别。我希望按代码='ABC'的价格上升为每个评级订单生成前10名。我无法弄清楚将最后两个条件放在下面的mysql语句中的哪个位置。请有人建议。非常感谢

SELECT x.*
  FROM (SELECT t.*,
               CASE 
                 WHEN @rating != t.rating THEN @rownum := 1
                 ELSE @rownum := @rownum + 1 
               END AS rank,
               @rating := t.rating AS var_rating
          FROM offers t
          JOIN (SELECT @rownum := NULL, @rating := '') r
      ORDER BY t.rating) x
 WHERE x.rank <= 10

另外,如果评级栏的条目如1,1,1k,1 *,1 +,2,2,2 +,3,3,3 *等,我怎么能考虑所有这些在同一个sql语句中,条目分别为'1',2和3?

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT x.*
  FROM (SELECT t.*,
               CASE 
                 WHEN @rating != t.rating THEN @rownum := 1
                 ELSE @rownum := @rownum + 1 
               END AS rank,
               @rating := t.rating AS var_rating
          FROM offers t
          JOIN (SELECT @rownum := NULL, @rating := '') r
         WHERE code = 'ABC'
      ORDER BY t.rating, price) x
 WHERE x.rank <= 10

变化是:

  • WHERE子句
  • 之后添加了JOIN子句
  • price条款中添加了ORDER BY