MySQL Select语句DISTINCT用于多列 [英] MySQL Select Statement DISTINCT for Multiple Columns

查看:793
本文介绍了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用于多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆