gpt4 book ai didi

带最小计数的 MYSQL 数据库选择

转载 作者:行者123 更新时间:2023-11-29 02:42:23 26 4
gpt4 key购买 nike

<分区>

我有一个数据集作为表格,它看起来像这样:

###################################
UID |TID |MID |RESULT |
1 |1 |1 |1 |
2 |1 |1 |2 |
3 |1 |1 |3 |
4 |2 |1 |4 |
5 |2 |1 |5 |
6 |2 |1 |6 |
1 |1 |2 |6 |
2 |1 |2 |5 |
3 |1 |2 |4 |
4 |2 |2 |3 |
5 |2 |2 |2 |
6 |2 |2 |1 |
1 |1 |3 |2 |
2 |1 |3 |4 |
3 |1 |3 |6 |
4 |2 |3 |5 |
5 |2 |3 |3 |
6 |2 |3 |1 |

我现在想要每行的最小数量,按 TID 和 MID 分组

如果我这样做:

SELECT UID, TID, MID, RESULT  
FROM matches
WHERE matches.id = '$ID'
ORDER BY TID, MID, RESULT

我以正确的方式订购了东西。像这样:

###################################
UID |TID |MID |RESULT |
1 |1 |1 |1 |
2 |1 |1 |2 |
3 |1 |1 |3 |

3 |1 |2 |4 |
2 |1 |2 |5 |
1 |1 |2 |6 |

1 |1 |3 |2 |
2 |1 |3 |4 |
3 |1 |3 |6 |

4 |2 |1 |4 |
5 |2 |1 |5 |
6 |2 |1 |6 |

6 |2 |2 |1 |
5 |2 |2 |2 |
4 |2 |2 |3 |

6 |2 |3 |1 |
5 |2 |3 |3 |
4 |2 |3 |5 |

我现在只想拥有每个 block 的第一行。计算每个 UID 在每个 block 中的最佳频率。

如果不在 php 中进行多次循环,我不知道如何获得它。如果我按 TID、MID 和 MIN(RESULT) 进行 GROUP BY TID,它会有点随机地出现哪个 UID。

解决方案:

SELECT COUNT(*) AS AMOUNT, t1.UID AS UID, t1.TID AS TID, t1.MID AS MID, t1.RESULT AS result
FROM
(
SELECT um.user_id AS UID, um.team_id AS TID, um.match_id AS MID, um.q_result AS RESULT
FROM user_matches um JOIN league_matches lm
WHERE um.match_id = lm.id
AND lm.league_subseason_id = '135'
ORDER BY TID, MID
) t1
INNER JOIN
(
SELECT um.user_id AS UID, um.team_id AS TID, um.match_id AS MID, min(um.q_result) AS min_result
FROM user_matches um JOIN league_matches lm
WHERE um.match_id = lm.id
AND lm.league_subseason_id = '135'
GROUP BY TID, MID
) t2 ON t2.TID = t1.TID AND t2.MID = t1.MID AND t2.min_result = t1.result
GROUP BY t1.UID ORDER BY AMOUNT DESC

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com