【问题标题】:query to retrieve last record from multiple rows with common column data查询以从具有公共列数据的多行中检索最后一条记录
【发布时间】:2016-12-25 18:48:52
【问题描述】:

我正在尝试从具有多行公共列数据的表中检索最后输入的数据。有一个名为 Tno 的 id 和 column2 作为三行通用的电子邮件,我想检索最后输入的行

   Tno  Email              pdesc             Activity
   A20  nancy@example.com      not working           Step1
   A20  nancy@example.com      Change the version    Step2
   A20  nancy@example.com      Still not working     step3          



SELECT * FROM `supactive2` WHERE Tno='A20' ORDER BY `Tno` DESC LIMIT 1;

目前我正在使用这个查询,但我只得到第一条记录(A20 -nancy@example.com-not working- Step1) 我需要最后一条记录(ie. A20 - nancy@example.com - still not working - step3)

【问题讨论】:

  • 尝试将主键列添加到您的表中并使用它进行排序。
  • 或者至少必须有一列可以区分最后输入的记录,例如日期时间。
  • 表格是按什么排序的?什么是表键?

标签: mysql sql


【解决方案1】:

向表中添加日期时间约束,以便您可以按时间戳排序。 或者,如果您的活动字段按顺序填充,则按活动排序,例如第一步,第二步,第三步

【讨论】:

    【解决方案2】:

    按活动排序,将为您提供结果。

    SELECT * FROM supactive2 WHERE Tno='A20' ORDER BY Tno, Activity DESC LIMIT 1;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多