Solved

MySQL: Select rows with highest value in a column only

Posted on 2009-03-29
2
1,234 Views
Last Modified: 2013-12-12
Hello,

I want to fetch rows with the highest value in a column.
With this table contents given:

ID --- VERSION --- text
1  --- 1              --- abc
1  --- 2              --- def
1  --- 3              --- ghj
2  --- 1              --- mno
3  --- 1              --- uvw
3  --- 2              --- xyz

I want a query that gives me

ID --- VERSION --- text
1  --- 3              --- ghj
2  --- 1              --- mno
3  --- 2              --- xyz

as result. I need a performant query, as this query will by executed quite often (possibly multiple times when visiting a page).

I startet experimenting with GROUP BY, but couldn't quite get to a working result.

I'd be glad if someone could help me on this one.
CREATE TABLE test(id INT UNSIGNED NOT NULL AUTO_INCREMENT, version SMALLINT NOT NULL DEFAULT 1, text TEXT, PRIMARY KEY(id, version));
 

INSERT INTO test(id, version, text) values(1, 1, 'abc'), (1, 2, 'def'), (1, 3, 'ghj'), (2, 1, 'mno'), (3, 1, 'uvw'), (3, 2, 'xyz');

Open in new window

0
Comment
Question by:Chakotay505
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24012595
Try this:
select test.* 

from test 

join (

  select id,max(version) as max_version 

  from test 

  group by id

) m on test.id=m.id and version=max_version;

Open in new window

0
 
LVL 3

Author Comment

by:Chakotay505
ID: 24012662
Thanks, works great.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now