MySQL在每个组中获取id的第二个最低行

时间:2016-09-08 09:51:02

标签: mysql sql join min

我试图从每个Grouped获得表格中第二低的行。我已经是最低的,但现在我需要第二低。 为我糟糕的英语而烦恼

有人知道解决方案吗?

SELECT bestlellangaben 
FROM bestellangaben
JOIN
(
    SELECT MIN(ID) AS newid 
    FROM bestellangaben
    WHERE SeitenID = 185
    GROUP BY grouping
) AS temp ON ID = temp.newid

JSFiddle

4 个答案:

答案 0 :(得分:2)

使用LIMIT和OFFSET

为每个组选择相关子查询中的第二个ID
SELECT b.* 
FROM bestellangaben b
WHERE b.ID = (
    SELECT b1.ID
    FROM bestellangaben b1
    WHERE b1.grouping = b.grouping
      AND SeitenID = 185
    ORDER BY ID ASC
    LIMIT 1 OFFSET 1
)

这是另一个,它计算较小的ID:

SELECT b.*
FROM bestellangaben b
JOIN bestellangaben b1
    ON  b1.grouping = b.grouping
    AND b1.ID < b.ID
WHERE b.SeitenID = 185
  AND b1.SeitenID = 185
GROUP BY b.ID
HAVING COUNT(b1.ID) = 1 -- one ID is lesser

将第一个用于少数大组。将第二个用于大量小组。

答案 1 :(得分:1)

这很复杂吗?我真的不知道 - 但它很快......

DROP TABLE IF EXISTS bestellangaben;

CREATE TABLE bestellangaben
(ID INT NOT NULL PRIMARY KEY
,SeitenId INT NOT NULL
,bestlellangaben VARCHAR(50) NOT NULL
,grouping TINYINT NOT NULL
);

INSERT INTO bestellangaben VALUES
(863 ,185, "CAN I/O & CAN PLC",0),
(864 ,185, "6 analog inputs. 8 I/Os",0),
(865 ,185, "G7" ,0),
(866 ,185, "10333000E" ,0),
(867 ,185, "CAN I/O & CAN PLC - RPM" ,1),
(868 ,185, "6 analog inputs. 8 I/Os (2x frequency input)" ,1),
(869 ,185, "G7" ,1),
(870 ,185, "103130100E" ,1),
(871 ,185, "CAN I/O & CAN PLC - Current In" ,2),
(872 ,185, "6 analog inputs. 8 I/Os (3x current input)" ,2),
(873 ,185, "G7",2),
(874 ,185, "103130100E", 2),
(875 ,185, "CAN I/O & CAN PLC - PWM Out",3),
(876 ,185, "6 analog inputs. 8 I/Os (6x PWM)", 3),
(877 ,185, "G7" ,3);

SELECT a.id
     , a.seitenId
     , a.bestlellangaben
     , a.grouping
  FROM 
     ( SELECT x.*
            , CASE WHEN @prev=grouping THEN @i:=@i+1 ELSE @i:=1 END rank
            , @prev:=grouping 
         FROM bestellangaben x
            , ( SELECT @prev:=null,@i:=0 ) vars 
        ORDER 
           BY grouping
            , id
     ) a
 WHERE rank = 2;

+-----+----------+----------------------------------------------+----------+
| id  | seitenId | bestlellangaben                              | grouping |
+-----+----------+----------------------------------------------+----------+
| 864 |      185 | 6 analog inputs. 8 I/Os                      |        0 |
| 868 |      185 | 6 analog inputs. 8 I/Os (2x frequency input) |        1 |
| 872 |      185 | 6 analog inputs. 8 I/Os (3x current input)   |        2 |
| 876 |      185 | 6 analog inputs. 8 I/Os (6x PWM)             |        3 |
+-----+----------+----------------------------------------------+----------+

答案 2 :(得分:0)

我不知道在MySQL中这样做的复杂方法,所以这里有两个强力解决方案......

SELECT
    b.*
FROM
    bestellangaben  b
INNER JOIN
(
    SELECT
        b.grouping,
        b.SeitenID,
        MIN(b.ID) AS MinID2
    FROM
        bestellangaben  b
    INNER JOIN
    (
        SELECT
            b.grouping,
            b.SeitenID,
            MIN(b.ID) AS MinID
        FROM
            bestellangaben  b
        GROUP BY
            b.grouping,
            b.SeitenID
    )
        b_min
            ON  b_min.grouping = b.grouping
            AND b_min.SeitenID = b.SeitenID
            AND b_min.MinID    < b.ID
    GROUP BY
        b.grouping,
        b.SeitenID
)
    b_2min
        ON  b_2min.grouping = b.grouping
        AND b_2min.SeitenID = b.SeitenID
        AND b_2min.MinID2   = b.ID
WHERE
    b.SeitenID = 185

或者,使用相关的子查询......

SELECT
    b.*
FROM
    bestellangaben  b
WHERE
    b.SeitenID = 185
    AND (
            SELECT
                COUNT(*)
            FROM
                bestellangaben   lookup
            WHERE
                    lookup.grouping = b.grouping
                AND lookup.SeitenID = b.SeitenID
                AND lookup.ID       < b.ID
        )
        = 1

答案 3 :(得分:-1)

SELECT ID AS newid 
FROM bestellangaben
WHERE SeitenID = 185
ORDER BY ID LIMIT 1,1

限制1,1可以在按ID升序排序时获取第二个元素。