0

I'm having trouble with this query im trying to select the row number based on (upvotes - downvotes) in a certain subset of a table , but i need to also reset the @rownum within the same query:

This query works but I need to run two of them. Anyway to combine the set @rownum into one query my attempts have been in vain.

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

This one throws an error:

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
  • 2
    Throws an error. What error and where? Commented Mar 18, 2015 at 7:05

2 Answers 2

1

The cross-join should be along with the select from table_name something like

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
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should also work:

SELECT @rn := @rn+1 AS RANK,t1.* FROM (

 # Your query here.
 SELECT * FROM Cars WHERE Auto_SET = 'BMW' 
 WHERE ID = 391913 ORDER BY (upvotes-downvotes) DESC

) t1, (SELECT @rn:=0) t2;

Good luck!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.