MySQL 中的 Group By 查询无法正常工作

发布于 2025-01-04 21:38:02 字数 299 浏览 0 评论 0原文

我进行查询以查找三个不同表中的最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酒忠诚 2025-01-11 21:38:02

GROUP BY 不是问题(至少不是主要问题)。

真正的问题是您从 3 个表中选择数据而不加入它们。查询的这部分:

from   movie m
       , tvseries t
       , games g 

意味着第一个表的每一行与第二个表的每一行和第三个表的每一行的所有组合。这就是你想要的吗?

FROM   movie m
  CROSS JOIN  tvseries t
  CROSS JOIN  games g 

您很可能想要JOIN这 3 个表:

FROM   movie m
  INNER JOIN  tvseries t
    ON  t.someColumn = m.someColumn 
  INNER JOIN  games g 
    ON  g.someOtherColumn = m.someOtherColumn 

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:

from   movie m
       , tvseries t
       , games g 

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?

FROM   movie m
  CROSS JOIN  tvseries t
  CROSS JOIN  games g 

Most probably you want to JOIN the 3 tables:

FROM   movie m
  INNER JOIN  tvseries t
    ON  t.someColumn = m.someColumn 
  INNER JOIN  games g 
    ON  g.someOtherColumn = m.someOtherColumn 
百善笑为先 2025-01-11 21:38:02

SELECT 中出现的所有列都应该:

  1. 也出现在语句的 GROUP BY 部分中,或者
  2. 由某些聚合函数计算,例如 SUM、<代码>AVG等

All columns that appear in SELECT should either:

  1. be present also in GROUP BY part of statement OR
  2. be computed by some aggregate function like SUM, AVG etc.
泅渡 2025-01-11 21:38:02

没有任何关于您的查询出了什么问题或您期望的结果的更多信息。您收到的内容,我认为您应该在 SELECT 中包含 m.MovID 。最佳做法是对您SELECT 的内容进行GROUP BYSELECTGROUP 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 you SELECT . It is best practice to GROUP BY what you SELECT and to SELECT what you GROUP BY.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文