从我的SQL数据库中获取最新的行

时间:2021-07-07 22:14:19

I have a table where there in rows there is a column called version. I have 2 same entries with 1 column say abc(unique) in all the same rows. I have 2 rows as follows

我有一个表,其中有一行称为版本。我有2个相同的条目,其中1列表示所有相同行中的abc(唯一)。我有两行如下

ID|Name|Version|Unique_Id
-------------------------
1 |abc |1      | 23
2 |abc1|2      |23
3 |xyz |1      |21
4 |tre |1      |20

I want the result as

我希望结果为

ID|Name|Version|Unique_Id
-------------------------
2 |abc1|2      |23
3 |xyz |1      |21
4 |tre |1      |20

I have tried grouping by Unique_Id, the result is as follows

我试过通过Unique_Id进行分组,结果如下

ID|Name|Version|Unique_Id
-------------------------
1 |abc |1      | 23
3 |xyz |1      |21
4 |tre |1      |20

Following is the query I am using

以下是我正在使用的查询

SELECT *  FROM test
group by Unique_Id
order by Version desc;

I want latest(top order by desc) of each each rows. Please help. How can i achieve that.

我想要每行的最新(desc的最高顺序)。请帮忙。我怎样才能做到这一点。

1 个解决方案

#1


How about something like

怎么样的

INSERT INTO tbllogs 
            (logorigin, 
             logaction, 
             loguser, 
             logdate, 
             logoutcome) 
VALUES      (:origin, 
             :action, 
             :user, 
             :dt, 
             :outcome) 

Use a sub select to determine the id and its max version number, then join back to the original table to retrieve the other values.

使用子选择来确定id及其最大版本号,然后连接回原始表以检索其他值。

SQL Fiddle DEMO

#1


How about something like

怎么样的

INSERT INTO tbllogs 
            (logorigin, 
             logaction, 
             loguser, 
             logdate, 
             logoutcome) 
VALUES      (:origin, 
             :action, 
             :user, 
             :dt, 
             :outcome) 

Use a sub select to determine the id and its max version number, then join back to the original table to retrieve the other values.

使用子选择来确定id及其最大版本号,然后连接回原始表以检索其他值。

SQL Fiddle DEMO