Magento 2 – Get Status of Last Order ID via SQL Query

collection;magento2sales-ordersql

i want to get last order id along with order status in magneto2, i use this query to extract it but the status is not extracting correct:

SELECT max(entity_id) , STATUS FROM sales_order

max entity id returns last order but the status of the last order is
returning wrong it's returning status of first order , please help me
to correct my sql query.

i am using this code in magento2 api:

$con = mysqli_connect("localhost","root","","development_v2");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else{

        $sql2 = "SELECT max(entity_id) and status FROM sales_order";
        $result_set2 = mysqli_query($con,$sql2);
        if ($result_set2) {
            $rowB = mysqli_fetch_array($result_set2);
            $hamza = $rowB['max(entity_id)'];
           // echo $hamza;
        } else {
            echo 'No latest order';
        }
        mysqli_close($con);
}

Best Answer

Change your query like this,

SELECT entity_id, status FROM sales_order ORDER BY entity_id DESC LIMIT 1;

Related Topic