MySQL 中的 Group By 查询无法正常工作
我进行查询以查找三个不同表中的最后 24 个结果。
查询是:
SELECT m.MovID,m.Type,m.Image,m.Date,t.SerID,t.Type,t.Image,t.Date,g.GamID,g.Type,g.Image,g.Date
FROM movie m, tvseries t, games g
GROUP BY m.MovID, t.SerID, g.GamID
LIMIT 0,24
但在此查询的结果中 group by 不起作用。
I make a query to find the last 24 results from three different tables.
The query is :
SELECT m.MovID,m.Type,m.Image,m.Date,t.SerID,t.Type,t.Image,t.Date,g.GamID,g.Type,g.Image,g.Date
FROM movie m, tvseries t, games g
GROUP BY m.MovID, t.SerID, g.GamID
LIMIT 0,24
but in the result of this query group by is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GROUP BY 不是问题(至少不是主要问题)。
真正的问题是您从 3 个表中选择数据而不加入它们。查询的这部分:
意味着第一个表的每一行与第二个表的每一行和第三个表的每一行的所有组合。这就是你想要的吗?
您很可能想要
JOIN
这 3 个表:The
GROUP BY
is not the problem (at least not the major one).Th real problem is that you selecting data from 3 tables without joining them. This part of your query:
meanss all combinations of every row of the 1st table with every row of the 2nd with every row of the 3rd table. Is that what you want?
Most probably you want to
JOIN
the 3 tables:SELECT
中出现的所有列都应该:GROUP BY
部分中,或者SUM
、<代码>AVG等All columns that appear in
SELECT
should either:GROUP BY
part of statement ORSUM
,AVG
etc.没有任何关于您的查询出了什么问题或您期望的结果的更多信息。您收到的内容,我认为您应该在
SELECT
中包含m.MovID
。最佳做法是对您SELECT
的内容进行GROUP BY
并SELECT
您GROUP BY
的内容。Without any more information about what is wrong with your query or what results you are expecting Vs. what you are receiving, I think that you should include
m.MovID
in youSELECT
. It is best practice toGROUP BY
what youSELECT
and toSELECT
what youGROUP BY
.