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

Change your query like this,

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

| improve this answer | |
  • SELECT entity_id , STATUS FROM sales_order ORDER BY entity_id DESC LIMIT 1; – Hamza Mustafa Mar 5 '18 at 8:17

Load order collection by increment id desc, on collection


This may help.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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