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();

        $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';

2 Answers 2


Change your query like this,

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

  • SELECT entity_id , STATUS FROM sales_order ORDER BY entity_id DESC LIMIT 1; Mar 5, 2018 at 8:17

Load order collection by increment id desc, on collection


This may help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.