Solved

MySQL - Select Max Unique Order By

Posted on 2007-12-06
6
4,049 Views
Last Modified: 2013-12-12
I am trying to create a list of data where id is sorted highest to lowest, but where each name is unique.

For example:

name : id : text
aaaa : 11 :  stuff
bbbb : 12 :  more stuff
aaaa : 13 :  other stuff
bbbb : 14 :  more other stuff

Query should return only one name for it's highest id only so:

bbbb : 14 :  more other stuff
aaaa : 13 :  other stuff

Thanks for reading.
SELECT name, MAX(id) AS id, text, 
			option 
			FROM dataset1 
			LEFT OUTER JOIN dataset2 ON name_fk = name  
			WHERE something!=1
			GROUP BY name
			ORDER BY id DESC
			LIMIT 15;

Open in new window

0
Comment
Question by:ncoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20422759
SELECT name, id, text,
                        option
                        FROM dataset1
                        LEFT OUTER JOIN dataset2 ON name_fk = name  
                        WHERE something!=1
                           AND id = (SELECT MAX(ID) from dataset1 x where x.name = dataset1.name)
                        GROUP BY name
                        ORDER BY id DESC
                        LIMIT 15;
0
 
LVL 15

Author Comment

by:ncoo
ID: 20424190
Thanks, what about:

For example:

name : id : text : other :
aaaa : 11 :  stuff : 5 :
bbbb : 12 :  more stuff : 10 :
aaaa : 13 :  other stuff : 12 :
bbbb : 14 :  more other stuff : 1 :

Query should return only one name for it's highest other only so:

aaaa : 13 :  other stuff : 12 :
bbbb : 12 :  more stuff : 10 :

the field other is part of table dataset1
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20424525
SELECT * FROM
(
SELECT name, id, text, other
FROM dataset1
LEFT OUTER JOIN dataset2 ON name_fk = name  
WHERE something!=1
GROUP BY name
ORDER BY other desc
) AS Y
ORDER BY id DESC
LIMIT 15;
0
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 5

Assisted Solution

by:ursangel
ursangel earned 100 total points
ID: 20426617
i hope you always need only the highest id , what ever your field names are.
Most simple way seems like this.
Check this out...
select id, ename, text from Data a
where id in (select max(id) from Data group by ename)
order by id
0
 
LVL 15

Author Comment

by:ncoo
ID: 20427334
The problem with that imitchie is that it sorts the whole table, is there anyway to avoid sorting the whole table before selecting just the 15 records?
0
 
LVL 25

Accepted Solution

by:
imitchie earned 400 total points
ID: 20431117
The problem is that the way you have prescribed the problem domain requires that you do sort everything, group them in a Particular order, then Sort them by a secondary order, however the limit 15 is applied to the Secondary order!  You might just want to revisit what you can live with.  Because there is an inherent dependancy in there.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to dynamically set the form action using jQuery.
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 …

710 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