我无法让以下内容按预期工作,尝试了我在其他示例和解决方案中看到的内容,但仍然无法做到这一点。
Dim pos As String = Nothing
Try
OpenUserDB()
Dim cmd43 As MySqlCommand = New MySqlCommand("SELECT COUNT(id) AS position FROM users " & _
"WHERE user_id <= @UserID ORDER BY food_total DESC", connUser)
cmd43.Parameters.AddWithValue("@UserID", UserID)
Dim reader As MySqlDataReader = cmd43.ExecuteReader
If reader.Read() Then
pos = reader("position").ToString() & positionName(reader("position").ToString())
End If
Return pos
Catch ex As Exception
Dim err As New ErrorLog
err.logError(Format(Date.Now, "dd-MM-yyy hh:mm:ss ") & ex.ToString)
Return Nothing
Finally
CloseUserDB()
End Try
表用户
我用UserID = g02
查询,预期结果为2,但得到3。
用户被认为是第二个用户,但它返回错误的位置。将不胜感激。
答案 0 :(得分:1)
<asp:Label runat="server" ID="myLabel" Text=""></asp:Label>
答案 1 :(得分:0)
@TinTran解决方案不适用于vb.net,但以下工作正常;
SELECT Id,userId,rank
FROM
SELECT Id,userID, @rank := IFNULL(@rank,0)+1 as rank
FROM users ORDER BY food_total DESC) AS T, (SELECT @rank:=0) R
WHERE T.userID = 'g02'