0

我有两张桌子:teamsformations

team_id | team_name
   1    | Barcelona
   2    | Real Madrid
   3    | PSG

表格编队

formation_id | team_id | module
    1        |    2    | 5-3-2
    2        |    1    | 4-4-2
    3        |    3    | 4-4-2
    4        |    2    | 4-4-3

实际上,我需要在 2 个表 GROUP BY team_id 之间“加入”,但最后一个“formation_id”

结果我是这样的:

 team_id | team_name  | formation_id  | module
    1    | Barcelona  |     2         |  4-4-2
    2    | Real Madrid|     4         |  4-4-3
    3    | PSG        |     3         |  4-4-2 

其实我的查询是:

SELECT *
  FROM formations f
 INNER JOIN teams t 
    ON (f.team_id = t.team_id)
 GROUP BY t.team_id

通过我的查询,我为每个团队选择了第一个插入阵型,而我必须为每个团队选择最后一个阵型。

4

7 回答 7

1

您可以使用稍后将其与原始表连接起来的子查询找到它们的最大编队 ID。试试这个,

SELECT  a.*, c.formation_ID, c.`module`
FROM    teams a
        INNER JOIN
        (
            SELECT team_id, MAX(formation_ID) maxID
            FROM formations
            GROUP BY team_ID
        ) b ON  a.team_id = b.team_id
        INNER JOIN formations c
            ON  c.team_id = b.team_id AND
                c.formation_ID = b.maxID
ORDER BY a.Team_id

SQLFiddle 演示

于 2012-09-20T00:36:05.950 回答
1

你可以写:

SELECT t.team_id,
       t.team_name,
       f.formation_id,
       f.module
  FROM teams t
  JOIN formations f
    ON f.team_id = t.team_id
       -- require f.formation_id to be MAX(formation_id) for some team:
  JOIN ( SELECT MAX(formation_id) AS id
           FROM formations
          GROUP
             BY team_id
       ) max_formation_ids
    ON max_formation_ids.id = f.formation_id
;

或者:

SELECT t.team_id,
       t.team_name,
       f.formation_id,
       f.module
  FROM teams t
  JOIN formations f
    ON f.team_id = t.team_id
       -- require f.formation_id to be MAX(formation_id) for this team:
 WHERE f.formation_id =
        ( SELECT MAX(formation_id)
            FROM formations
           WHERE team_id = t.team_id
        )
;

或者:

SELECT t.team_id,
       t.team_name,
       f.formation_id,
       f.module
  FROM teams t
  JOIN formations f
    ON f.team_id = t.team_id
       -- forbid f.formation_id to be less than another for the same team:
  LEFT
 OUTER
  JOIN formations f2
    ON f2.team_id = t.team_id
   AND f2.formation_id > f.formation_id
 WHERE f2.formation_id IS NULL
;
于 2012-09-19T18:52:44.583 回答
1

检查这个SQLFIDDLE

SELECT A.team_id,A.team_name,B.formation_id,B.module
FROM teams A,formations B
WHERE A.team_id=B.team_id
AND B.formation_id =
(
  SELECT max(formation_id)
  FROM formations C
  WHERE C.team_id =B.team_id
 )
ORDER BY A.team_id;


create table teams
(  
  team_id int
 ,team_name varchar(40)
);
create table formations 
(
  formation_id int
 ,team_id  int
 ,module int
);
insert into teams
values
(1,'Barcelona'),(2,'Real Madrid'),(3,'PSG');
insert into formations
values
(1,2,532),(2,1,442),(3,3,442),(4,2,443);
于 2012-09-19T19:17:23.890 回答
0

您可以执行以下操作:

select f.*, t.team_name from formations f
  join (select team_id, max(formation_id) as max_formation_id from formations f 
          group by team_id) as mrf on mrf.max_formation_id = f.formation_id
  join teams t on f.team_id = t.team_id
于 2012-09-19T18:56:36.677 回答
0
create table teams (team_id integer, team_name varchar(50));
create table formations (formation_id integer, team_id integer, module varchar(20));

insert into formations (formation_id, team_id, module) values
(1, 2, '5-3-2'),
(2, 1, '4-4-2'),
(3, 3, '4-4-2'),
(4, 2, '4-4-3')
;

insert into teams (team_id, team_name) values
(1, 'Barcelona'),
(2, 'Real Madrid'),
(3, 'PSG')
;

select t.team_id, team_name, f.formation_id, module
from
    formations f
    inner join
    teams t on f.team_id = t.team_id
    inner join (
        select team_id, max(formation_id) as formation_id
        from formations
        group by team_id
    ) s on s.team_id = t.team_id and s.formation_id = f.formation_id
order by t.team_id
;
+---------+-------------+--------------+--------+
| team_id | team_name   | formation_id | module |
+---------+-------------+--------------+--------+
|       1 | Barcelona   |            2 | 4-4-2  |
|       2 | Real Madrid |            4 | 4-4-3  |
|       3 | PSG         |            3 | 4-4-2  |
+---------+-------------+--------------+--------+
于 2012-09-19T18:57:48.223 回答
0

此版本在没有聚合的情况下产生相同的结果,这通常会导致内部排序。

SELECT t.team_id,
   t.team_name,
   f.formation_id,
   f.module
FROM teams t
INNER JOIN formations f
    ON f.team_id = t.team_id
LEFT OUTER JOIN formations f2
    ON f2.team_id = t.team_id
   AND f2.formation_id > f.formation_id
WHERE f2.formation_id IS NULL
于 2012-09-19T19:03:36.580 回答
0

SQL 规范指出,当您使用 group by 子句时,输出中的所有字段/表达式应该是聚合函数的结果(如 count、avg 等)或 group by 子句中列出的字段。否则行为未定义。因此,如果您需要准确选择此记录,则需要在查询中添加一些条件。此外,不能保证简单的“select * from some_table”总是以相同的顺序返回行。

于 2012-09-19T18:54:36.863 回答