每组前N行仅获得1行

时间:2017-03-23 04:55:17

标签: mysql

我试图显示每个类别的前3个(或任何数字)外壳。顶部意味着访问量最大。所以,如果我有一个这样的表:

+------------+--------------------+--------+
| housing_id | category           | visits |
+------------+--------------------+--------+
|          7 | cat                |      2 |
|          8 | New Category       |      1 |
|         10 | bead and breakfast |      1 |
|         11 | bead and breakfast |      4 |
|         15 | 2                  |      3 |
|         16 | 2                  |      1 |
|         17 | New Category       |      1 |
|         18 | cat                |      1 |
+------------+--------------------+--------+

我和我想选择每个类别中访问次数最多的三个外壳,所以我这样做。

select housing_id, category, visits
from
    (select housing_id, category, visits,
        @category_rank := if(@current_category = category, @country_rank + 1, 1) as category_rank,
        @current_category := category
    from visit_counts
    order by category, visits desc
    ) ranked
where category_rank <= 3;

我明白了:

+------------+--------------------+--------+
| housing_id | category           | visits |
+------------+--------------------+--------+
|         15 | 2                  |      3 |
|         11 | bead and breakfast |      4 |
|          7 | cat                |      2 |
|          8 | New Category       |      1 |
+------------+--------------------+--------+

但我想:

+------------+--------------------+--------+
| housing_id | category           | visits |
+------------+--------------------+--------+
|         15 | 2                  |      3 |
|         16 | 2                  |      1 |
|         11 | bead and breakfast |      4 |
|         10 | bead and breakfast |      1 |
|          7 | cat                |      2 |
|         18 | cat                |      1 |
|          8 | New Category       |      1 |
|         17 | New Category       |      1 |
+------------+--------------------+--------+

1 个答案:

答案 0 :(得分:1)

您正在使用用户变量而未声明它们。此外,您应该在一个表达式中分配和读取用户变量,因为MySQL不保证列评估的顺序(因此可能在您阅读之前或之后进行分配)。

试试这个:

Dim rstT1 As Recordset
Dim rstUpdate As Recordset
Dim iMatching As String
Dim iT1, iT2 As Long
Dim iMatchNum As Long
Dim iMatchID As Long
Dim i As Long




CurrentDb.Execute ("UPDATE Table01 SET MatchNum = null")
CurrentDb.Execute ("UPDATE Table02 SET MatchNum = null")
Set rstT1 = CurrentDb.OpenRecordset("qryTable1Processing1")




Do While rstT1.EOF <> True
iMatching = rstT1!User_ID
iT1 = DCount("*", "Table01", "[User_ID] = '" & iMatching & "'")
iT2 = DCount("*", "Table02", "[Combine] = '" & iMatching & "'")
If iT1 > 0 And iT2 > 0 Then
If iT1 > iT2 Then
iMatchNum = iT2
Else
iMatchNum = iT1
End If

Debug.Print dDate & " " & iMatchNum
Set rstUpdate = CurrentDb.OpenRecordset("SELECT TOP " & iMatchNum & " MatchNum FROM Table01 WHERE [User_ID] = '" & iMatching & "'")
i = 0
Do While rstUpdate.EOF <> True
i = i + 1
rstUpdate.Edit
rstUpdate!matchnum = iMatchID + i
rstUpdate.Update
rstUpdate.MoveNext
Loop
rstUpdate.Close
Set rstUpdate = CurrentDb.OpenRecordset("SELECT TOP " & iMatchNum & " MatchNum FROM Table02 WHERE [Combine] = '" & iMatching & "'")
i = 0
Do While rstUpdate.EOF <> True
i = i + 1
rstUpdate.Edit
rstUpdate!matchnum = iMatchID + i
rstUpdate.Update
rstUpdate.MoveNext
Loop
rstUpdate.Close

Debug.Print "adding " & iMatchID & " to " & iMatchNum
iMatchID = iMatchID + iMatchNum
End If
rstT1.MoveNext
Loop
rstT1.Close
Set rstT1 = Nothing

End Sub

Demo