douxun2018 2014-10-08 09:52
浏览 205
已采纳

PHP-从唯一ID的Mysql列获取最大值

My current projects consist of Registration of families of different areas in a City. i want to Generate a Unique ID to each families in which i need this ID for another part of this project.Each area in the city have already a unique number eg: 855, 856, 857,etc

So my plan is to generate unique Family ID by combining unique number already have + text "FM"+ number (1,2,3....) and store this uid to DB along with family details.

For eg (Area-855): 855FM1, 855FM2, 855FM3....

if the last registered family uid is 855FM40 , then next family uid must be 855FM41. so i want to fetch the largest value from uid field. in above eg:, largest is 855FM41. how i do this. ?

i have simple logic that fetch all uid, then split it after "FM". then find largest ,etc

How i solve this problem.? is there any simple way other than this.?

  • 写回答

2条回答 默认 最新

  • dongxia19772008 2014-10-08 09:56
    关注

    Using ORDER command you can sort your data by ordering of one column ascending or descending.

    so first we order FamilyID column descending (in sql we use DESC) and then we get the first row which has biggest FamilyID value using "LIMIT 0,1" command

    Try this:

    SELECT * FROM families ORDER BY FamilyID DESC LIMIT 0, 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?