Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.