SQL 查询时间戳

SQL Query for timestamp

我关注了 Table

CREATE TABLE Customer
    ( `Name` varchar(7), `Address` varchar(55), `City` varchar(15),`Contact` int,`timestamp` int)
;

INSERT INTO Customer
    (`Name`,`Address`, `City`, `Contact`,`timestamp`)
VALUES
    ('Jack','New City','LA',79878458,456125),
    ('Joseph','New Lane23','LA',87458458,794865),
   ('Rosy','Old City','Paris',79878458,215125),
   ('Maria','New City','LA',79878458,699125),
   ('Jack','New City','LA',79878458,456125),
   ('Rosy','Old City','Paris',79878458,845125),
   ('Jack','New Main Street','New York',79878458,555525),
   ('Joseph','Near Bank','SAn Francisco',79878458,984521)

;

我想获取所有具有最高时间戳的客户记录而不重复。

试试这个:

SELECT * FROM `customer` 
group by name,Address,City,Contact,timestamp
order by timestamp desc

I want to get all customer record with highest timestamp without duplication.

一样使用DISTINCT运算符和ORDER BY子句
select distinct `Name`,`Address`, `City`, `Contact`,`timestamp`
from customer
order by `timestamp` desc;

在这种情况下,您可以使用 JOIN 查询,例如

select t1.*
from customer t1 join
(select Name, max(`timestamp`) as maxstamp
 from customer
 group by Name) xx 
 on t1.Name = xx.Name
 and t1.`timestamp` = xx.maxstamp;

我正在加入客户 table 本身,加入子句上的条件 c1.timestamp<c2.timestampc2.timestamp IS NULL 将确保只返回每个人的最新记录。我放 DISTINCT 是因为在您的示例数据中有两条 Jack 的记录具有相同的时间戳:

SELECT DISTINCT
  c1.*
FROM
  Customer c1 LEFT JOIN Customer c2
  ON c1.Name=c2.Name
     AND c1.Contact=c2.Contact -- you might want to remove this
     AND c1.timestamp<c2.timestamp
WHERE
  c2.timestamp IS NULL
ORDER BY
  Name, Address

请看fiddle here.

试试下面的方法。

select name,max(timestamp),Address,City,Contact from Customer group by name