Google Answers Logo
View Question
 
Q: Update field with incrementing number in some rows. (MySQL) ( No Answer,   0 Comments )
Question  
Subject: Update field with incrementing number in some rows. (MySQL)
Category: Computers > Programming
Asked by: qvtqht-ga
List Price: $10.00
Posted: 02 Apr 2004 17:50 PST
Expires: 02 May 2004 18:50 PDT
Question ID: 324313
I would like to reproduce the pseudo-code below in one MySQL query. (I am
using the ezSQL class for the $db object.)

The code selects all approved customers from the customers table and
then updates their priority_id to a consecutive incrementor. This is
performed only for approved customers, while other customers'
priority_id is set to NULL.

The query would look something like this:
UPDATE customers SET priority_id = ++i WHERE approved = 1;

Note, that I am not looking for an alternative solution to my
"problem". I am looking for an answer to the specific request stated
above.

--

Here is the pseudo-code:

   $db->query("UPDATE customers SET priority_id = NULL WHERE approved
!= 1 ORDER BY date_added");

   $customers = $db->get_results("SELECT customer_id, priority_id FROM customers
WHERE approved = 1");

   if (count($customers) > 0) {
      foreach($customers as $customer) {
         $i++;

         if ($customer->priority_id != $i)
            $db->query("UPDATE customers SET priority_id = $i WHERE
customer_id = $customer->customer_id");
      }
   }

Request for Question Clarification by googleexpert-ga on 07 Apr 2004 16:19 PDT
Hi qvtqht,
Just wanted to let you know that I have withdrawn from your question.
I will say that unless you're using MySQL v5.0+ which supports
Procedures, or MySQL v4.1+ which supports Subqueries, I don't think
the pseudo-code can be "reproduced" in one MySQL query.

Clarification of Question by qvtqht-ga on 07 Apr 2004 17:19 PDT
Thank you very much for your time and effort, googleexpert!
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy