Solved

MySQL: Select rows with highest value in a column only

Posted on 2009-03-29
2
1,240 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Backup Strategy 15 46
mysql query for sum() 3 28
Restricting the scope of variables to private in OOP 4 22
PHP: Best way to scan folders and process files 10 42
These days socially coordinated efforts have turned into a critical requirement for enterprises.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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