MySQL Select语句DISTINCT用于多列

编程入门 行业动态 更新时间:2024-10-22 21:42:41
本文介绍了MySQL Select语句DISTINCT用于多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在尝试构建一个有点棘手的MySQL Select语句.这是我要完成的工作:

I am currently trying to construct a somewhat tricky MySQL Select Statement. Here is what I am trying to accomplish:

我有一个这样的表:

data_table uniqueID stringID subject 1 144 "My Subject" 2 144 "My Subject - New" 3 144 "My Subject - Newest" 4 211 "Some other column"

基本上,我想做的是能够通过stringID(表示stringID已穿线的图片)进行选择/组化,而不进行重复.此外,我想选择最近的stringID行(在上面的示例中为uniqueID 3).

Bascially, what I'd like to do is be able to SELECT/GROUP BY the stringID (picture that the stringID is threaded) and not have it duplicated. Furthermore, I'd like to SELECT the most recent stringID row, (which in the example above is uniqueID 3).

因此,如果我要查询数据库,它将返回以下内容(顶部是最新的uniqueID):

Therefore, if I were to query the database, it would return the following (with the most recent uniqueID at the top):

uniqueID stringID subject 4 211 "Some other column" 3 144 "My Subject - Newest" //Notice this is the most recent and distinct stringID row, with the proper subject column.

我希望这是有道理的.谢谢您的帮助.

I hope this makes sense. Thank you for you help.

推荐答案

请尝试以下操作.它可能不是最有效的查询,但可以使用:

Try the following. It might not be the most efficient query, but it will work:

SELECT uniqueID, stringID, subject FROM data_table WHERE uniqueID IN ( SELECT MAX(uniqueID) FROM data_table GROUP BY stringID ) ORDER BY uniqueID DESC

更多推荐

MySQL Select语句DISTINCT用于多列

本文发布于:2023-10-26 23:30:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   MySQL   Select   DISTINCT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!