gpt4 book ai didi

mysql - 获得最高分和胜利次数

转载 作者:行者123 更新时间:2023-11-29 21:09:16 24 4
gpt4 key购买 nike

Database

这是我的数据库。每张照片都有一些要点,并且是由特定用户拍摄的。每张照片也被分配给该事件。我想找到每个事件得分最高的图片。我还想计算您赢得了多少场比赛(例如,对于 id = 10 的用户)。请你帮助我好吗?我不知道如何解决这个问题。

最佳答案

这里有两个查询可以实现您的需求:

picture with the highest number of points for each event.

SELECT      e.id event_id,
e.name event_name,
ph.id photo_id,
ph.title photo_title,
u.id user_id,
u.login user_login,
COUNT(*) points
FROM events e
INNER JOIN photos ph
ON ph.event_id = e.id
AND ph.id = (
SELECT ph.id
FROM photos ph
INNER JOIN points p
ON p.photo_id = ph.id
WHERE ph.event_id = e.id
GROUP BY ph.id
ORDER BY COUNT(*) DESC
LIMIT 1
)
-- optional if you need to know the points
INNER JOIN points p
ON p.photo_id = ph.id
-- optional if you need to know the owner of the photo
INNER JOIN users u
ON u.id = ph.user_id
GROUP BY e.id,
e.name,
ph.id,
ph.title

参见SQL fiddle .

count how many events you won

SELECT      u.id user_id,
u.login user_login,
COUNT(distinct e.id) events_won
FROM events e
INNER JOIN photos ph
ON ph.event_id = e.id
AND ph.id = (
SELECT ph.id
FROM photos ph
INNER JOIN points p
ON p.photo_id = ph.id
WHERE ph.event_id = e.id
GROUP BY ph.id
ORDER BY COUNT(*) DESC
LIMIT 1
)
INNER JOIN users u
ON u.id = ph.user_id
GROUP BY u.id,
u.login

参见SQL fiddle

关于mysql - 获得最高分和胜利次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535639/

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