distinct - mysql结果

时间:2017-09-08 13:00:59

标签: mysql sql

我有一个不同的问题在每一行我都有100个结果。 我必须为player_id = 1显示听力结果,但它只显示大约250个结果。它必须是大约400

SELECT city.player_id, city.team_id, country.gender, account.first_name, 
account.last_name, 
city.position,player.status,GROUP_CONCAT(DISTINCT(playing))
FROM account
JOIN player
ON account.id = player.account_id JOIN city
ON player.id = city.player_id JOIN player_activity
ON player.id = player_activity.player_id JOIN team
ON city.team_id = country.id
GROUP BY player.id;

知道为什么这样做?

2 个答案:

答案 0 :(得分:0)

您只对1个未聚合的列进行分组.MySQL允许您执行此操作,但会产生不一致的结果

尝试:

select city.player_id, city.team_id, country.gender, account.first_name, 
account.last_name, 
city.position,player.status,GROUP_CONCAT(DISTINCT(playing))
FROM account
JOIN player
ON account.id = player.account_id JOIN city
ON player.id = city.player_id JOIN player_activity
ON player.id = player_activity.player_id JOIN team
ON city.team_id = country.id
GROUP BY city.player_id, city.team_id, country.gender, account.first_name, 
account.last_name, 
city.position,player.status

答案 1 :(得分:0)

从逻辑上讲,您要求SQL提供给您的信息没有任何意义。您的查询要求SQL为每个player.id返回单个记录,但您还要指定您希望看到显示此信息的其他几个列。

你期望SQL如何向你显示(teamId,gender等等)的行列表,这些行在数据库中都有多行,当它们的每个player.id被集中到一个结果中时(行)?

实际上,由于这个原因,您尝试执行的查询无效,并且SQL因此而表现出奇怪/抛出错误。假设您无法聚合teamId和gender之类的字段,我认为您对SQL希望为您做什么感到困惑。你真正想要的是SQL to ORDER BY,而不是GROUP BY,因为你希望看到你的结果被player.id分类而不是归结为它。如果您将“GROUP BY”更改为“ORDER BY”,我打赌您会看到您要求的结果。希望这会有所帮助。