How to obtain the maximum value of a group of data

Source: Internet
Author: User



Problem
How to obtain the maximum value of a group of data
Solution
A set of data is not called by ID.
I want to obtain the data with the largest ID. What should I do.

Generally, all ID values are put in an array and then the size is compared.
But is there any other way, because this data operation takes the maximum value of ID only for assistance, if it is made into an array and then compare it feels more resource-consuming.

[]


Reference answer
Just compare the key!
Reference answer
Original Post published by liexusong at [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 814074 & ptid = 100300] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]
Just compare the key!
Isn't it in the array.
Reference answer
$ The_max = NULL;

Foreach ($ array as $ key => $ value ){

If ($ key> $ the_max) $ the_max = $ key;

}

Echo $ array [$ the_max]; // This is the answer.


Copy code
Reference answer
Original Post published by liexusong at [url = http://bbs.111cn.cn/redirect.php? Goto = findpost & pid = 814077 & ptid = 100300] link mark [img] http://bbs.111cn.cn/images/common/back.gif?/img=#/url]

$ The_max = NULL;
Foreach ($ array as $ key => $ value ){
If ($ key> $ the_max) $ the_max = $ key;
}
Echo $ array [$ the_max]; // This is the answer.
Thank you.
Reference answer
I didn't have FOREACH at last. I directly IF
In this way, the data is combined into an array. [Img] http://www.111cn.cn/bbs/images/smilies/default/lol.gif#/img]
Reference answer
Thank you for your ideas.
Reference answer
Max ??
Reference answer
$ SQL = 'select * FROM 'table' ORDER BY 'id' DESC LIMIT 1 ';


Copy code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.