简体   繁体   English

我如何创建一个接受每个唯一记录的最后插入记录的sql?

[英]how do i create a sql which takes the last inserted record of each unique record?

I have a table (inspect) which gets filled every time an inspector visits the station. 我有一张桌子(检查),每次检查员访问车站时都会填满。

When an admin tries to view the inspect table, i would want to display all the records which satisfy these conditions: 当管理员尝试查看检查表时,我想显示所有满足以下条件的记录:

  1. Each record must hold unique station name 每条记录必须具有唯一的工作站名称

  2. Displayed record must be the last updated record for that particular station 显示的记录必须是该特定电台的最新更新记录

As of now, the sql I've used is 到目前为止,我使用的SQL是

$sql = "select `StationID`,`DOI`,`due`,`StationName` from inspect group by `StationID`";

This displays perfectly, except it shows the first record of each unique station. 除显示每个唯一电台的第一条记录外,这将完美显示。

Eg if the table has 例如,如果表有

StationID StationName      DOI         due
924         RMC      17-Mar-2014
924         RMC      20-Mar-2016    
926         IMD      23-Jan-2018                
926         IMD      18-Jan-2018                
926         IMD      18-Jan-2018                
926         IMD      18-Jan-2018     19-Jan-2019    

my sql must display 我的sql必须显示

924 RMC 20-Mar-2016
926 IMD 18-Jan-2018 19-Jan-2019

There are some big if's to this answer since the question really needs clarifying. 这个问题的答案是否定的,因为问题确实需要澄清。 However, it is possible if we make the following assumptions. 但是,如果我们做出以下假设,则是可能的。

  • StationName is what you want to group by StationName是您要分组的对象
  • StationID is a unique incrementing ID (the primary key) StationID是唯一的递增ID(主键)

The following query would work. 以下查询将起作用。

select a.StationID, a.DOI, a.due, a.StationName from inspect a
where a.StationID = (
  select max(b.StationID) from inspect b  
  where b.StationName = a.StationName
)
group by a.StationName;

As evidenced in this fiddle 正如这个小提琴所证明的

Back to those assumptions... I think to achieve what you want, you must have a primary key. 回到这些假设...我想实现您想要的目标,您必须具有主键。 So if StationID isn't the primary key then you need to add one and use that column instead. 因此,如果StationID不是主键,则需要添加一个并使用该列。

Edit 编辑
The question has now changed because as I suspected it had mistakes. 现在的问题已经改变,因为我怀疑它有错误。 You can group on StationID or StationName and get the same result so that bit doesn't matter. 您可以对StationID或StationName进行分组,并获得相同的结果,因此该位无关紧要。 But I don't think you can get the results you want with that table structure. 但是我认为您无法通过该表结构获得所需的结果。

First you need a way to get the "last updated record". 首先,您需要一种获取“最新更新记录”的方法。 You currently don't have a column that tells you this. 您目前没有专栏告诉您这一点。 The DOI column apparently gives you a clue but it is not unique and you have three rows there with the same date. DOI列显然为您提供了一个线索,但它并不是唯一的,并且在那里有三行具有相同的日期。 The due column can't be relied upon because it is mostly null. 由于该列通常为null,因此不能依赖该列。

Second, once you have that column you have no way of using it to select only that record within the group by. 其次,一旦有了该列,便无法使用它仅在分组依据中选择该记录。 You can only do this with a primary key. 您只能使用主键来执行此操作。 It's not a good table design if you don't have a means to refer to one row. 如果您没有办法引用一行,那么这不是一个好的表设计。 It completely rules out any type of join. 它完全排除了任何类型的联接。

$sql = "select * 
        from `inspect` 
        group by `StationID` 
        order by str_to_date(`DOI`, '%d-%b-%Y') desc";

You need to convert the DOI column to an actual date so that it can be ordered by the engine appropriately. 您需要将DOI列转换为实际日期,以便引擎可以对其进行适当排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM