从双重联接表中选择最大值

时间:2019-01-07 13:16:06

标签: mysql sql

在数据库中,我有三个表,如:

+-----------------+--------+
| ident (PrimKey) | ident2 |
+-----------------+--------+
|             123 |    333 |
|             321 |    334 |
|             213 |    335 |
|            1234 |    336 |
+-----------------+--------+

+---------+----------+-------+-------+
| PrimKey | group_id | value | ident |
+---------+----------+-------+-------+
|       1 |        1 |    10 |   213 |
|       2 |        1 |     5 |   321 |
|       3 |        1 |    15 |  1234 |
|       4 |        1 |    10 |  1234 |
|       5 |        2 |     7 |   213 |
|       6 |        2 |    15 |   321 |
+---------+----------+-------+-------+

+---------+----------+----------+
| PrimKey | ident2_1 | ident2_2 |
+---------+----------+----------+
|       1 |      333 |      334 |
|       2 |      333 |      335 |
|       3 |      333 |      336 |
+---------+----------+----------+

第三个表是第一个表的两行之间的连接。第二个包含来自该行不同组的数据。

我必须从第二张表中找到最大值,该第二张表由group_id分组,用于第三张表中特定的用户连接行。在示例333中。 正确答案应该是:

+----------+-------+-------+
| group_id | value | ident |
+----------+-------+-------+
|        1 |    15 |  1234 |
|        2 |    15 |   321 |
+----------+-------+-------+

但是现在我对所有行进行了排序:

+----+----------+-------+-------+
|    | group_id | value | ident |
+----+----------+-------+-------+
|  1 |        1 |    15 |  1234 |
|  2 |        1 |    10 |   213 |
|  3 |        1 |     5 |   321 |
|  4 |        2 |    15 |   321 |
|  5 |        2 |    10 |  1234 |
|  6 |        2 |     7 |   213 |
+----+----------+-------+-------+

或使用不正确的ident纠正行

+----+----------+-------+-------+
|    | group_id | value | ident |
+----+----------+-------+-------+
|  1 |        1 |    15 |   213 |
|  2 |        2 |    15 |  1234 |
+----+----------+-------+-------+

sql是:

DROP TABLE first;
DROP TABLE second;
DROP TABLE third;


CREATE TABLE first(group_id integer, value integer, ident integer);
CREATE TABLE second(ident integer, ident2 integer);
CREATE TABLE third(ident_1 integer, ident_2 integer);

INSERT INTO first VALUES(1, 10, 213);
INSERT INTO first VALUES(1, 5, 321);
INSERT INTO first VALUES(1, 15, 1234);

INSERT INTO first VALUES(2, 10, 1234);
INSERT INTO first VALUES(2, 7, 213);
INSERT INTO first VALUES(2, 15, 321);


INSERT INTO second VALUES(123, 333);
INSERT INTO second VALUES(321, 334);
INSERT INTO second VALUES(213, 335);
INSERT INTO second VALUES(1234, 336);

INSERT INTO third VALUES (333, 334);
INSERT INTO third VALUES (333, 335);
INSERT INTO third VALUES (333, 336);


SELECT f.group_id, max(f.value) as value, f.ident
FROM first as f
INNER JOIN second AS s ON f.ident = s.ident
INNER JOIN third AS t ON t.ident_2 = s.ident2
WHERE t.ident_1 = '333'
GROUP BY f.group_id
ORDER BY f.group_id ASC, f.value DESC;


SELECT f.group_id, f.value as value, f.ident
FROM first as f
INNER JOIN second AS s ON f.ident = s.ident
INNER JOIN third AS t ON t.ident_2 = s.ident2
WHERE t.ident_1 = '333'
ORDER BY f.group_id ASC, f.value DESC;

经过以下测试:https://rextester.com/l/mysql_online_compiler

问候

编辑:

第三个表就像是第二个表中的are与好友之间的联系。第一个表包含由group_id标识的不同任务的分数。我需要最好的朋友分数来完成不同的任务。因此,我从第三张桌子得到的朋友名单。和分数,我从第一个。该表之间的连接是第二个。

EDIT2:

在第一个表中,主键是ident(PrimKey)。

第二个和第三个作为主键的只有另一列。

在第二列中,ident列是从第一张表连接到ident(PrimKey)的索引。

在第三张表中,ident2_1和ident2_2列是从第一张表连接到indet2的索引。

2 个答案:

答案 0 :(得分:1)

肯定有一个更优雅的解决方案,但这将提供您所要求的结果。

select m.*
from(
    select f.group_id, f.value value, f.ident
    from   first f,
           second s,
           third t
    where  f.ident = s.ident
    and    t.ident_2 = s.ident2
    and    t.ident_1 = '333'
    ORDER BY f.group_id ASC, f.value DESC ) m,
(
    select max(f.value) value
    from   first f,
           second s,
           third t
    where  f.ident = s.ident
    and    t.ident_2 = s.ident2
    and    t.ident_1 = '333' ) n
where m.value = n.value

这是第二种(也是笨拙的)技术,可以达到预期的效果:

select f.group_id, f.value value, f.ident
from   first f,
       second s,
       third t
where  f.ident = s.ident
and    t.ident_2 = s.ident2
and    t.ident_1 = '333'
and    value = (   select max(f.value) value
                   from   first f,
                          second s,
                          third t
                   where  f.ident = s.ident
                   and    t.ident_2 = s.ident2
                   and    t.ident_1 = '333' )
ORDER BY f.group_id ASC, f.value DESC 

答案 1 :(得分:1)

我想出了这个SQL:

SELECT f.group_id, f.value as value, f.ident
FROM   first as f
INNER JOIN second AS s 
      ON f.ident = s.ident
INNER JOIN third AS t 
      ON t.ident_2 = s.ident2
WHERE t.ident_1 = '333'
and   f.value IN (  SELECT MAX(f1.value) 
                    FROM first as f1 
                    WHERE f1.group_id = f.group_id )
ORDER BY f.group_id ASC, f.value DESC;