简体   繁体   English

获取MYSQL中重复条目的最新数据

[英]Get the latest data of duplicated entries in MYSQL

I have a mysql table that contains all the subscribed plan by users. 我有一个mysql表,其中包含用户的所有预订计划。 I am trying to create a SELECT statement that will allow me to select for a particular user from the table. 我正在尝试创建一个SELECT语句,该语句将允许我从表中选择特定用户。 Also, if that user have duplicated entries, it will get the latest date of all. 同样,如果该用户有重复的条目,它将获得所有的最新日期。

For example, I want to look for John from the table and the date I should get is 2015-09-10. 例如,我想从表中查找John,我应该得到的日期是2015-09-10。

Subscribed table
ID   FirstName   Date
-------------------------------
1   John        2015-05-30
2   Mary        2014-01-10
3   John        2015-09-10
4   John        2015-03-15
5   Loen        2013-12-11

How should I go about creating the SELECT statement to above the result? 我应该如何在结果上方创建SELECT语句?

If you just need the first one, you can use LIMIT in conjunction with ORDER BY : 如果只需要第一个,则可以将LIMITORDER BY结合使用:

SELECT * FROM `Subscribed`
  WHERE `FirstName` = 'John' -- Get the name John.
  ORDER BY `Date` DESC       -- Sort results in descending order by Date.
  LIMIT 1                    -- Limit the results to one.

The result will be ordered by date in descending order, and the LIMIT 1 gives out the first row. 结果将按日期降序排列,并且LIMIT 1给出第一行。

Or to be precise, you can also use Grouping Functions if you need more than one row. 确切地说,如果需要多个行,也可以使用分组功能。

SELECT `FirstName`, MAX(`Date`) FROM `Subscribed`
  WHERE `FirstName` = 'John'
  GROUP BY `FirstName`

Fiddle: http://www.sqlfiddle.com/#!9/ebb90/1 小提琴: http ://www.sqlfiddle.com/#!9 / ebb90 /1

Try this 尝试这个

SELECT FirstName, max(Date) FROM yourTable
WHERE FirstName = 'John'

also you can use group by to take the result for all persons not just John 您也可以使用group by为所有人获取结果,而不仅仅是John

If you want to do this for all users: 如果要对所有用户执行此操作:

select s.*
from subscribed s join
     (select firstname, max(date) as maxd
      from subscribed
      group by firstname
     ) ss
     on ss.firstname = s.firstname and ss.maxd = s.date;

Query for required output: 查询所需的输出:

SELECT * FROM (SELECT * FROM `Subscribed`
       ORDER BY `DATE` DESC) t group by firstName

Alternatively, you can use MAX and GROUP BY 或者,您可以使用MAX和GROUP BY

SELECT firstName, MAX(date) FROM `Subscribed`

group by firstName 

Verify output here : http://www.sqlfiddle.com/#!9/ec5d1/1 在此处验证输出: http : //www.sqlfiddle.com/#!9/ec5d1/1

Output: 输出:

id  firstName   date
3   John        September, 10 2015 00:00:00
2   Mary        January, 10 2014 00:00:00

Gordeon Linoff has good answer, if your ID is a primary key and always grow on time as well, you can do this SQL for better speed: Gordeon Linoff有一个很好的答案,如果您的ID是主键并且总是按时增长,则可以执行以下SQL以获得更快的速度:

SELECT s.*
FROM subscribed s, 
    (SELECT FirstName, max(ID) maxID FROM subscribed GROUP BY 1) AS ss
WHERE s.ID = ss.maxID;

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

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