从捐款表中选择用户位置

时间:2019-01-07 05:55:16

标签: php mysql sql

我有一个名为donations的表,该表存在每个用户的捐赠-像这样:

uid|month|year|what donate

我尝试在特定月份和年份在此表中获得一些uid职位。这是我的代码:

$table = $mysqli->prepare('SELECT count(*) AS counter FROM donations WHERE uid = ? AND month = ? AND year = ? GROUP BY uid LIMIT 1');
$table->bind_param('iii', $uid, $month, $year);
$table->execute();
$table->bind_result($count);
$table->store_result();
$rows = $table->num_rows;
$table->fetch();

count返回我在用户选择的日期中用户捐赠的次数,而不是他在donations表中的位置。有人看到问题了吗?

2 个答案:

答案 0 :(得分:0)

这应该有效。

SELECT count(*) AS counter 
FROM donations 
WHERE month = '01' AND year = '2019' 
GROUP BY uid
ORDER BY counter desc;

答案 1 :(得分:0)

我认为您想要这样的东西:

$file = 'file.txt'
$outFile = [IO.File]::CreateText("$PWD/new-file.txt")
$lineNo = 0
try {
  foreach ($line in [IO.File]::ReadLines("$PWD/$file")) {
    if (++$lineNo -eq 1005) { continue }
    $outFile.WriteLine($line)
  }
} finally {
  $outFile.Dispose()
}

这不识别纽带-它只是将值从1排列到n。您的问题不清楚职位的意思,所以这与您的要求相符。

在MySQL 8+中,您只需执行以下操作:

SELECT d.*
FROM (SELECT d.*, (@rn := @rn + 1) as position
      FROM (SELECT uid, COUNT(*) AS num_donations
            FROM donations
            WHERE month = ? AND year = ?
            GROUP BY uid
            ORDER BY num_donations DESC
           ) d CROSS JOIN
           (SELECT @rn := 0) params
     ) d
WHERE uid = ?;