[英]MySQL count in “complex” SQL query
I have the following SQL query: Let me describe it to you quickly. 我有以下SQL查询:让我快速向您描述。 I wanna develop a soccer tip game.
我想开发一个足球技巧游戏。 I have 4 tables: apps, tips, users, games.
我有4个表格:应用程序,提示,用户,游戏。
Relations: 关系:
apps 1 to N users
user 1 to N tips
game 1 to 1 tip
Instead of listing all games where the User had a successful tip, I just want to list the u.userId with the COUNT of successful tips. 我只想列出包含COUNT个成功提示的u.userId,而不是列出用户获得成功提示的所有游戏。 Check out my SQL:
查看我的SQL:
SELECT
t.*, u.*, g.*
FROM
tips AS t,
users AS u,
games AS g
WHERE
u.userAppId = 4 AND
t.tipUserId = u.userId AND
g.gameResult = t.tipGameResult
I guess I somehow have to implement a COUNT and u.userId instead of 我想我必须以某种方式实现COUNT和u.userId而不是
t.*, u.*, g.*
but I am not 100% sure how to do it. 但我不是100%知道该怎么做。 Do you have any suggestions?
你有什么建议吗?
SELECT
u.userid, count(*)
FROM
tips AS t,
users AS u,
games AS g
WHERE
u.userAppId = 4 AND
t.tipUserId = u.userId AND
g.gameResult = t.tipGameResult
group by u.userid
As mentioned in comment below, the group by on user id will give you 如以下评论中所述,按用户ID进行分组将为您提供
userid|count(*) userid | count(*)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.