我有一个数据库客户名称和日期是复合键。我试图在每个日期拉出'x'最大或最小的交易,并将其与排名一起插入另一个表格。
我在单独的运行中使用此代码,但是当从java运行JDBC以进行多次运行时它将无效。
set @N = 0;
SELECT @N := @N +1 AS rank, name, purchase FROM t1 ORDER BY close purchase
limit 15;
有什么建议吗?
答案 0 :(得分:5)
这非常有效:
SELECT t.*,
@n := @n + 1 AS rank
FROM WhateverTable t, (SELECT @n := 0) r
所以你的查询实际上会是这样的:
SELECT @N := @N +1 AS rank, name, purchase
FROM t1, (SELECT @N := 0) r
ORDER BY purchase desc
limit 15;