Jump to content

[SOLVED] Me again!! Query Help Please.


d_barszczak

Recommended Posts

Hi all,

 

Just when im getting to grips with this mysql business i have another problem ???

 

The following query works:

SELECT MAX(`order`) as `MAXorder` FROM `nav_directory`;

 

which return MAXorder = 10.

 

i would like it to return the tag also

tag, MAXorder

page1, 10

 

My Solution to this was:

SELECT `tag`, MAX(`order`) as `MAXorder` FROM `nav_directory` GROUP BY order;

 

But that lists all the tags. Can anyone help???

Link to comment
https://forums.phpfreaks.com/topic/78399-solved-me-again-query-help-please/
Share on other sites

The robust way to do this is as follows (untested):

 

SELECT n.tag, n.order
FROM nav_directory AS n
JOIN 
( SELECT MAX(order) as MAXorder FROM nav_directory ) AS sub ON ( sub.MAXorder = n.order )

Of course, you could add as many conditions as you wanted.  Don't forget about the boundary case; you may want an order by, too.

I used this in the end.

 

SELECT `order`, `tag` FROM `nav_directory` ORDER BY `order` desc LIMIT 1;

 

This alway returns the highest value with the tag.

 

EDIT: Also gone and named `order` which is a reserved word. Need to be more careful with my naming stratergy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.