开发者

MySQL set a variable and run a query in the same statement/script

开发者 https://www.devze.com 2023-04-11 06:14 出处:网络
I\'m have a data开发者_开发百科base customer name and date are the composite keys. I\'m trying to pull out the \'x\' largest or smallest transactions on each date and insert it into another table alon

I'm have a data开发者_开发百科base customer name and date are the composite keys. I'm trying to pull out the 'x' largest or smallest transactions on each date and insert it into another table along with the ranking.

I'm using this code on individual runs, but it won't work when run thru JDBC from java for multiple runs.

set @N = 0;

SELECT @N := @N +1 AS rank, name, purchase FROM t1 ORDER BY close purchase
limit 15;

Any suggestions?


This works perfectly:

SELECT t.*, 
       @n := @n + 1 AS rank
  FROM WhateverTable t, (SELECT @n := 0) r

So your query will actually look like this:

SELECT @N := @N +1 AS rank, name, purchase 
FROM t1, (SELECT @N := 0) r 
ORDER BY purchase desc
limit 15;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号