Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:54:35+00:00 2026-06-17T13:54:35+00:00

Here’s an SQL statement (actually two statements) that works — it’s taking a series

  • 0

Here’s an SQL statement (actually two statements) that works — it’s taking a series of matching rows and adding a delivery_number which increments for each row:

SELECT @i:=0;
UPDATE pipeline_deliveries AS d
SET d.delivery_number = @i:=@i+1
WHERE d.pipelineID = 11
ORDER BY d.setup_time;

But now, the client no longer wants them ordered by setup_time. They needed to be ordered according to departure time, which is a field in another table. I can’t figure out how to do this.

The MySQL docs, as well as this answer, suggest that in version 4.0 and up (we’re running MySQL 5.0) I should be able to do this:

SELECT @i:=0;
UPDATE pipeline_deliveries AS d RIGHT JOIN pipeline_routesXdeliveryID AS rXd
    ON d.pipeline_deliveryID = rXd.pipeline_deliveryID
LEFT JOIN pipeline_routes AS r
    ON rXd.pipeline_routeID = r.pipeline_routeID
SET d.delivery_number = @i:=@i+1
WHERE d.pipelineID = 11
ORDER BY r.departure_time,d.pipeline_deliveryID;

but I get the error #1221 - Incorrect usage of UPDATE and ORDER BY.

So what’s the correct usage?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:54:36+00:00Added an answer on June 17, 2026 at 1:54 pm

    You can’t mix UPDATE joining 2 (or more) tables and ORDER BY.

    You can bypass the limitation, with something like this:

    UPDATE 
        pipeline_deliveries AS upd
      JOIN
        ( SELECT t.pipeline_deliveryID, 
                 @i := @i+1 AS row_number 
          FROM 
              ( SELECT @i:=0 ) AS dummy
            CROSS JOIN 
              ( SELECT d.pipeline_deliveryID
                FROM 
                    pipeline_deliveries AS d 
                  JOIN 
                    pipeline_routesXdeliveryID AS rXd
                      ON d.pipeline_deliveryID = rXd.pipeline_deliveryID
                  LEFT JOIN 
                    pipeline_routes AS r
                      ON rXd.pipeline_routeID = r.pipeline_routeID
                WHERE 
                    d.pipelineID = 11
                ORDER BY 
                    r.departure_time, d.pipeline_deliveryID
              ) AS t
        ) AS tmp
          ON tmp.pipeline_deliveryID = upd.pipeline_deliveryID
    SET 
        upd.delivery_number = tmp.row_number ;
    

    The above uses two features of MySQL, user defined variables and ordering inside a derived table. Because the latter is not standard SQL, it may very well break in a feature release of MySQL (when the optimizer is clever enough to figure out that ordering inside a derived table is useless unless there is a LIMIT clause). In fact the query would do exactly that in the latest versions of MariaDB (5.3 and 5.5). It would run as if the ORDER BY was not there and the results would not be the expected. See a related question at MariaDB site: GROUP BY trick has been optimized away.

    The same may very well happen in any future release of main-strean MySQL (maybe in 5.6, anyone care to test this?) that will improve the optimizer code.

    So, it’s better to write this in standard SQL. The best would be window functions which haven’t been implemented yet. But you could also use a self-join, which will be not very bad regarding efficiency, as long as you are dealing with a small subset of rows to be affected by the update.

    UPDATE 
        pipeline_deliveries AS upd
      JOIN
        ( SELECT t1.pipeline_deliveryID
               , COUNT(*) AS row_number
          FROM
              ( SELECT d.pipeline_deliveryID
                     , r.departure_time
                FROM 
                    pipeline_deliveries AS d 
                  JOIN 
                    pipeline_routesXdeliveryID AS rXd
                      ON d.pipeline_deliveryID = rXd.pipeline_deliveryID
                  LEFT JOIN 
                    pipeline_routes AS r
                      ON rXd.pipeline_routeID = r.pipeline_routeID
                WHERE 
                    d.pipelineID = 11
              ) AS t1
            JOIN
              ( SELECT d.pipeline_deliveryID
                     , r.departure_time
                FROM 
                    pipeline_deliveries AS d 
                  JOIN 
                    pipeline_routesXdeliveryID AS rXd
                      ON d.pipeline_deliveryID = rXd.pipeline_deliveryID
                  LEFT JOIN 
                    pipeline_routes AS r
                      ON rXd.pipeline_routeID = r.pipeline_routeID
                WHERE 
                    d.pipelineID = 11
              ) AS t2
              ON t2.departure_time < t2.departure_time
              OR t2.departure_time = t2.departure_time 
                 AND t2.pipeline_deliveryID <= t1.pipeline_deliveryID
              OR t1.departure_time IS NULL
                 AND ( t2.departure_time IS NOT NULL
                    OR t2.departure_time IS NULL
                       AND t2.pipeline_deliveryID <= t1.pipeline_deliveryID
                     )
          GROUP BY
              t1.pipeline_deliveryID  
        ) AS tmp
          ON tmp.pipeline_deliveryID = upd.pipeline_deliveryID
    SET 
        upd.delivery_number = tmp.row_number ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my scenario. I have a website running under AppPool1 and that works
Here's the code I have. It works. The only problem is that the first
Here is the code in a function I'm trying to revise. This example works
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is the two scripts I have Script 1: <? include('config.php'); $json = $_POST['payload'];
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here's a sample of a SpinBox that writes its changes to underlying variables. The
Here is my situation: I am making a countdown app which works fine but
Here is my problem.. I have the option to create(add) two new input fields,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.