我有一个表格,其中包含以下两列,其中包括:评级,价格和代码。评级类似于一个类别。我希望按代码='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?
答案 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
。