【问题标题】:Filter data based on last row mysql根据最后一行mysql过滤数据
【发布时间】:2014-01-11 05:53:37
【问题描述】:

我有这个简单的表格设置see fiddle

    CREATE TABLE mytable
    (`id` int, `itemid` int);

    INSERT INTO mytable
    (`id`, `itemid`)
    VALUES
    (1, 111),
    (2, 222),
    (3, 333),
    (4, 444),
    (5, 111),
    (6, 222),
    (7, 333),
    (8, 564),
    (9, 111),
    (10, 121),
    (11, 131),
    (12, 111),
    (13, 353),
    (14, 373);

我想显示当前行 itemid 旁边的行中最后一个 itemid 是什么。 我已经用下面的方法做到了

    SELECT 
    mt.id,
    mt.itemid,
    (
        select mt2.itemid
        from mytable mt2
        where mt2.id < mt.id
        ORDER BY mt2.id DESC
        LIMIT 1
    ) as lastitemid
     FROM mytable mt 
     ORDER BY id DESC
     LIMIT 5

这按预期返回

    ID  ITEMID  LASTITEMID
    14  373     353
    13  353     111
    12  111     131
    11  131     121
    10  121     111

但我只想显示 lastitemid = 111 的行。

我试过了

    SELECT 
    mt.id,
    mt.itemid,
    (
        select mt2.itemid
        from mytable mt2
        where mt2.id < mt.id
        ORDER BY mt2.id DESC
        LIMIT 1
    ) as lastitemid
    FROM mytable mt 
    WHERE lastitemid = 111
    ORDER BY id DESC
    LIMIT 5

在“where 子句”中获取未知列“lastitemid”

我也试过添加

    AND mt2.itemid = 111

到内部查询

这不会出现任何错误,但会为所有行返回 111,这不是我想要的,因为它是无效的,例如对于 id=12,lastitemid 是 131,但它显示的是 111

    ID  ITEMID  LASTITEMID
    14  373     111
    13  353     111
    12  111     111
    11  131     111
    10  121     111

使用我的示例数据集,如果查询正确,我应该得到以下结果

    ID  ITEMID  LASTITEMID
    13  353     111
    10  121     111
    6   222     111
    2   222     111

我怎样才能做到这一点?

【问题讨论】:

  • 在黑暗中刺伤,但我的第一个想法是不知何故使用 GROUP BY?

标签: mysql sql select sql-order-by having


【解决方案1】:

试试这个:

SELECT mt.id, mt.itemid,
      (SELECT mt2.itemid FROM mytable mt2 WHERE mt2.id < mt.id ORDER BY mt2.id DESC LIMIT 1) AS lastitemid
FROM mytable mt 
HAVING lastitemid = 111
ORDER BY id DESC
LIMIT 5

查看SQL FIDDLE DEMO

输出

| ID | ITEMID | LASTITEMID |
|----|--------|------------|
| 13 |    353 |        111 |
| 10 |    121 |        111 |
|  6 |    222 |        111 |
|  2 |    222 |        111 |

【讨论】:

  • 谢谢我以前没有遇到过HAVING,它会派上用场的!
  • 这实际上应该与 WHERE 代替 HAVING 一样有效。
【解决方案2】:

如果保证id是连续的,你可以这样做。

SELECT curr.id, curr.itemid, previous.itemid AS lastitemid
FROM   mytable curr JOIN mytable previous ON previous.id = curr.id - 1
WHERE  previous.itemid = 111

否则,你需要类似的东西

SELECT curr.id, curr.itemid, previous.itemid AS lastitemid
FROM   mytable curr, mytable previous
WHERE  previous.id < curr.id 
  AND  previous.itemid = 111
  AND NOT EXISTS ( 
      SELECT 1 FROM mytable interloper
      WHERE  interloper.id < curr.id
        AND  previous.id < interloper.id )

【讨论】:

    【解决方案3】:

    您只需将当前查询用作表,然后使用 where 子句从中选择。

    SELECT 
    l.id,
    l.itemid
    FROM
    (
        SELECT 
        mt.id,
        mt.itemid,
        (
            select mt2.itemid
            from mytable mt2
            where mt2.id < mt.id
            ORDER BY mt2.id DESC
            LIMIT 1
        ) as lastitemid
        FROM mytable mt 
    ) As l
    WHERE l.lastitemid = 111
    ORDER BY l.id DESC
    LIMIT 5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多