简体   繁体   English

MySql-加入删除列中的重复项

[英]MySql - Join that delete duplicity in column

i am newbie in MySql. 我是MySql的新手。 I don't know name of function (if its exist) so it's hard to find something. 我不知道函数的名称(如果存在),因此很难找到某些东西。 I have 2 tables. 我有2张桌子。

software
id | name
---------
1  | xxxx
2  | yyyy
software_version
id | software_id | date
----------------------------------
1  | 2           | 2013-10-9 10:20
2  | 1           | 2013-10-9 10:21
3  | 1           | 2013-10-9 10:22
4  | 2           | 2013-10-9 10:23

Im looking for SQL command, that join this tables, order it by date and show only unique record. 我正在寻找连接此表的SQL命令,按日期排序并仅显示唯一记录。 For these tables will looks like: 对于这些表将如下所示:

id | name | date
---------------------------
4  | yyyy | 2013-10-9 10:23
3  | xxxx | 2013-10-9 10:22

I found something about DISRINCT but it's unuseable for this i think (i found that it looks for duplicity of whole record). 我发现了有关DISRINCT的一些信息,但是我认为这是无用的(我发现它寻找整个记录的重复性)。 Maybe something with GROUP but I really don't know. 也许与GROUP有一些联系,但我真的不知道。 How I said in MySql I can do only basic selects, inserts etc. 我在MySql中怎么说,我只能做基本的选择,插入等操作。

Even I dont know every type of JOIN so it could be some INNER JOIN or LEFT JOIN. 即使我也不知道每种JOIN类型,也可能是一些INNER JOIN或LEFT JOIN。 There is so much types of JOIN but i dont see diference. JOIN有很多类型,但我看不到差异。

Thanks for every help! 感谢您的帮助!

You can use joins and for the most recent row for each software you can use selef join on the maxima of second table 您可以使用联接,对于每个软件的最新行,可以在第二个表的最大值上使用selef联接

select v.id,s.name,v.date from software s
join software_version v on (s.id=v.software_id)
join (select software_id ,max(date) date 
      from software_version group by software_id) v1
on(v.software_id =v1.software_id and v.date=v1.date)
order by v.date desc

Demo 演示版

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

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