mysql选择行号查询

时间:2015-03-18 06:54:43

标签: php mysql

我在尝试根据表格的某个子集中的(upvotes - downvotes)选择行号时遇到此查询的问题,但我还需要在同一查询中重置@rownum:< / p>

此查询有效,但我需要运行其中两个。无论如何将@rownum集合到一个查询中,我的尝试都是徒劳的。

SET @rownum = 0;
SELECT rank  
FROM (SELECT *, @rownum:=@rownum + 1 AS 
rank FROM Cars where Auto_SET = 'BMW'
order by (upvotes-downvotes) DESC) d
WHERE ID = 391802

这个引发错误:

SELECT rank 
FROM (SELECT *, @rownum:=@rownum + 1 AS 
rank FROM Cars  where Auto_SET = 'BMW' , 
(SELECT @rownum:=0) r order by 
(upvotes-downvotes) DESC) d
WHERE ID = 391913

1 个答案:

答案 0 :(得分:1)

cross-join应该与select from table_name类似

SELECT rank 
FROM (
  SELECT *, 
  @rownum:=@rownum + 1 AS rank 
  FROM Cars ,(SELECT @rownum:=0) r 
  where Auto_SET = 'BMW' 
  order by 
 (upvotes-downvotes) DESC
) d
WHERE ID = 391913