我有一个 PHP 脚本,显示按“虚拟货币”排序的玩家列表:
$sth = $db->prepare("
select u.id,
u.first_name,
u.city,
u.avatar,
m.money,
u.login > u.logout as online
from pref_users u, pref_money m where
m.yw=to_char(current_timestamp, 'IYYY-IW') and
u.id=m.id
order by m.money desc
limit 20 offset ?
");
$sth->execute(array($_GET['offset']));
为了显示玩家在列表中的位置,我使用了一个 PHP 变量 $pos,该变量在循环中递增,同时打印他们的姓名和更多数据。
由于各种原因,我想在 SQL 语句中而不是 PHP 中拥有该位置。所以我正在尝试以下操作:
$sth = $db->prepare("
select u.id,
row_number() + ? as pos,
u.first_name,
u.city,
u.avatar,
m.money,
u.login > u.logout as online
from pref_users u, pref_money m where
m.yw=to_char(current_timestamp, 'IYYY-IW') and
u.id=m.id
order by m.money desc
limit 20 offset ?
");
$sth->execute(array($_GET['offset'], $_GET['offset']));
但是得到错误:窗口函数调用需要一个 OVER 子句
我正在尝试添加over(m.money)但出现语法错误。
我可能误解了Window Functions文档。