SQL - How to find the highest number in a column?

前端 未结 15 1089
借酒劲吻你
借酒劲吻你 2020-12-30 18:25

Let\'s say I have the following data in the Customers table: (nothing more)

ID   FirstName   LastName
-------------------------------
20   John        Macken         


        
15条回答
  •  忘掉有多难
    2020-12-30 19:24

    To find the next (still not used) auto-increment, I am using this function for somewhat years now.

    public function getNewAI($table)
        {
            $newAI = false;
    
            $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
            if(mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
    
            $sql = "SHOW TABLE STATUS LIKE '".$table."'";
    
            $result = $mysqli->query($sql);
    
            if($result) {
                $row = $result->fetch_assoc();
    
                $newAI = $row['Auto_increment'];
            }
            $mysqli->close();
    
            return $newAI;
        }
    

提交回复
热议问题